summaryrefslogtreecommitdiff
path: root/synth/channel.cpp
blob: ddad9fe628f5e9f6016b20ba2aa1928d656663e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "channel.h"

#include "math.h"
#include "globals.h"

#include "note.h"


Channel::Channel()
{
	volume=ONE;
	set_program(0);
	curr_prg.controller[NO_CONT]=1;
	quick_release=0;
	always_reattack=false;
	portamento_frames2=portamento_frames=0;
	do_portamento=false;
	pitchbend=ONE;
	n_voices=0;
	
	max_pitchbend=1.0;
	
	set_balance(64);
}

Channel::~Channel()
{
	panic(); //deletes all notes and empties notes-list
}

void Channel::cleanup()
{
	list<NoteSkel*>::iterator it;
	for (it=notes.begin(); it!=notes.end(); it++)
		if ((*it)->still_active()==false)
		{
			delete *it;
			it=notes.erase(it);
		}
}

fixed_t Channel::get_sample()
{
	fixed_t sum=0;
	
	for (list<NoteSkel*>::iterator it=notes.begin(); it!=notes.end(); it++)
		sum+=(*it)->get_sample();

	return sum*volume >>SCALE;
}

void Channel::event(uint8_t a, uint8_t b, uint8_t c)
{
	switch(a & 0xF0)
	{
		case 0x80: note_off(b); break;
		case 0x90: note_on(b,c); break;
		case 0xA0: break; //IMPLEMENTME: polyphonic aftertouch (note, dynamic)
		case 0xB0: set_controller(b,c); break;
		case 0xC0: set_program(b); break;
		case 0xD0: break; //IMPLEMENTME: monotonic aftertouch (dynamic)
		case 0xE0: set_pitch_bend( ( (((b&0x7F) + ((c&0x7F)<<7)) - 8192) / 8192.0 ) * max_pitchbend ); break;
		case 0xF0: break; //own controls/sysex (to be implemented) IMPLEMENTME
		default: output_verbose("NOTE: got unknown command "+ IntToStrHex(a&0xF0) +", ignoring it\n");
		;
	}
}

void Channel::note_off(int note)
{
	note_on(note,0);
}

void Channel::note_on(int note, int vel)
{
	list<NoteSkel*>::iterator it;
	if (vel>0) //note on
	{
		if ( (n_voices==1) && (!notes.empty()) )
		{
			//no need to create a new note; reuse the existing
			NoteSkel *n; //i'm lazy
			n= *(notes.begin());
			
			if (n->get_program() != program)
			{
				//if the program has changed, kill the previous note and
				//create a new one
				delete n;
				notes.clear();
				
				notes.push_back(  new Note(note,(float)vel/128.0,
																	 curr_prg,
																	 portamento_frames,
																	 pitchbend, 
																	 program)  );
				
			}
			else //program did not change
			{
				//if not still active, don't do portamento
				n->set_note(note,n->still_active());
				n->set_vel((float)vel/128.0);
				if (always_reattack || !n->still_active()) n->reattack();
				//no need to push back. would become #1 instead of #1
			}
		}
		else
		{
			bool neednewnote=true;
			if (always_reattack)
			{
				for (it=notes.begin(); it!=notes.end(); it++)
					if ( ((*it)->get_note()==note) && ((*it)->get_program()==program) )
					{
						neednewnote=false;
						(*it)->reattack();
						(*it)->set_vel((float)vel/128.0);
						notes.push_back(*it); //reorder notes
						notes.erase(it);
						break;
					}
			}
			if (neednewnote)
				notes.push_back(  new Note(note,(float)vel/128.0,
																	 curr_prg,
																	 portamento_frames,
																	 pitchbend, 
																	 program)  );
			apply_voice_limit();
		}
	}                                                  
	else //note off
	{
		for (it=notes.begin(); it!=notes.end(); it++)
			if ((*it)->get_note()==note)
				(*it)->release();
	}
		
}

void Channel::set_n_voices(int val)
{
	n_voices=val;
	
	if ((n_voices<=0) || (n_voices>=128))
		n_voices=0; //unlimited
	
	apply_voice_limit();
}

void Channel::apply_voice_limit()
{
	if (n_voices) //is a limit defined?
	{
		int diff=notes.size()-n_voices;
		if (diff>0)
		{
			list<NoteSkel*>::iterator it=notes.begin();

			if (quick_release)
				for (int i=0;i<diff;i++)
				{
					(*it)->release_quickly(quick_release);
					it++;
				}
			else
				for (int i=0;i<diff;i++)
				{
					delete (*it);
					it=notes.erase(it);
				}
		}
	}
}


void Channel::set_controller(int con,int val)
{
	switch (con)
	{
		case 3:   always_reattack=(val>=64);
		case 5:   set_portamento_time(val); break;
		case 7:   set_volume(val); break;
		case 8:   set_balance(val); break;
		case 65:  set_portamento(val); break;
		case 119: set_quick_release(val);
		case 120: panic(); break;
		case 121: reset_controllers(); break;
		case 123: release_all(); break;
		case 126: set_n_voices(val); break;
		case 127: set_n_voices(999); break;
		default:  set_user_controller(con,val); break;
	}
}

void Channel::set_user_controller(int con, int val)
{
	curr_prg.controller[con]=val;
	for (set<parameter_t>::iterator it=curr_prg.controller_affects[con].begin(); it!=curr_prg.controller_affects[con].end(); it++)
		recalc_param(*it,curr_prg);
}

void Channel::recalc_param(const parameter_t &par, program_t &prg)
{
	fixed_t val=0;
	
	list<term_t> *l;
	l=&(prg.formula[par]);
	
	for (list<term_t>::iterator it=l->begin(); it!=l->end(); it++)
		val+=curr_prg.controller[it->c]*it->f;
	
	if (val<0) val=0;

	// now we have the final value of the formula in units of fixed_t
	// in the range 0..+infinity
	
	switch(par.par)
	{
		case SUSTAIN: 
		case FILTER_SUSTAIN: if (val>ONE) val=ONE;  break;
				
		case TREM_LFO: 
		case VIB_LFO:  
		case FILTER_TREM_LFO: val=val>>SCALE; if (val>=N_LFOS+1) val=N_LFOS+1 -1; break;
		
		case TREMOLO:
		case VIBRATO:  
		case FILTER_TREMOLO: val=val>>SCALE; if (val>=N_LFO_LEVELS) val=N_LFO_LEVELS-1; break;
		
		case WAVEFORM:  val=val>>SCALE; if (val>=N_WAVEFORMS) val=N_WAVEFORMS-1; break;
					
		case FILTER_RESONANCE: if (val>ONE) val=ONE; break;

		default: break;
	}

	// now we have the value clipped to the valid range. for stuff
	// expecting real numbers, it's in units of fixed_t. for booleans
	// it's zero or nonzero. for stuff expecting integers, like lfo,
	// waveform etc it's in int (i.e., val/ONE is very small, while
	// val is what we want)

	for (list<NoteSkel*>::iterator it=notes.begin(); it!=notes.end(); it++)
		(*it)->set_param(par, val);
	
	curr_prg.set_param(par, val);
}

void Channel::reset_controllers()
{
	program_t *orig=&program_settings[program];
	
	for (int i=0;i<128;i++)
		set_user_controller(i,orig->controller[i]);
}

void Channel::set_quick_release(int val)
//ranges from zero to one second.
{
	quick_release=samp_rate*val/128;
}

void Channel::set_volume(int val)
{
	volume=val*ONE/128;
}

void Channel::set_balance(int val)
{
#ifdef STEREO
	balR=val/64.0;
	balL=(128-val)/64.0;
#endif
}

void Channel::set_portamento(int val)
{
	if (val>=64)
	{
		do_portamento=true;
		set_real_portamento_frames();
	}
	else
	{
		do_portamento=false;
		set_real_portamento_frames();
	}
}

void Channel::set_portamento_time(int val)
{
	portamento_frames2=samp_rate*val*max_port_time_sec/128;
	if (do_portamento)
		set_real_portamento_frames();
}

void Channel::set_real_portamento_frames()
{
	if (do_portamento)
		portamento_frames=portamento_frames2;
	else
		portamento_frames=0;
		
	list<NoteSkel*>::iterator it;
	for (it=notes.begin(); it!=notes.end(); it++)
		(*it)->set_portamento_frames(portamento_frames);
}

void Channel::panic()
{
	list<NoteSkel*>::iterator it;
	for (it=notes.begin(); it!=notes.end();)
	{
		delete *it;
		it=notes.erase(it);
	}
}

void Channel::release_all()
{
	list<NoteSkel*>::iterator it;
	for (it=notes.begin(); it!=notes.end(); it++)
		(*it)->release();
}

void Channel::set_program(int prog)
{
	program=prog;
	curr_prg=program_settings[program];
}

void Channel::set_pitch_bend(float val)
{
	pitchbend=pow(2.0,val/12.0)*ONE;
	
	list<NoteSkel*>::iterator it;
	for (it=notes.begin(); it!=notes.end(); it++)
		(*it)->set_pitchbend(pitchbend);
}