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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
/*
Copyright (C) 2010-2012 Florian Jung
This file is part of flo's FM synth.
flo's FM synth is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
flo's FM synth is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with flo's FM synth. If not, see <http://www.gnu.org/licenses/>.
*/
#include "channel.h"
#include "math.h"
#include "globals.h"
#include "note.h"
#include "note_funcs.h"
Channel::Channel()
{
volume=ONE;
set_program(0);
curr_prg.controller[NO_CONT]=1;
quick_release=0;
portamento_frames2=portamento_frames=0;
do_portamento=false;
pitchbend=ONE;
n_voices=0;
max_pitchbend=1.0;
set_balance(64);
pressed_keys.clear();
held_keys.clear();
sostenuto_keys.clear();
hold_pedal_pressed=false;
legato_pedal_pressed=false;
curr_vol_factor=1.0;
}
Channel::~Channel()
{
panic(); //deletes all notes and empties notes-list
}
void Channel::cleanup()
{
list<NoteSkel*>::iterator it;
for (it=notes.begin(); it!=notes.end();)
if ((*it)->still_active()==false)
{
(*it)->destroy();
it=notes.erase(it);
}
else
++it;
}
fixed_t Channel::get_sample()
{
fixed_t sum=0;
for (list<NoteSkel*>::iterator it=notes.begin(), end=notes.end(); it!=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)
{
pressed_keys.erase(note);
if (hold_pedal_pressed)
held_keys.insert(note);
else if (sostenuto_keys.find(note)!=sostenuto_keys.end())
/* do nothing */;
else
really_do_note_off(note);
}
void Channel::really_do_note_off(int note)
{
for (list<NoteSkel*>::iterator it=notes.begin(); it!=notes.end(); ++it)
if ((*it)->get_note()==note)
(*it)->release();
}
void Channel::note_on(int note, int vel)
{
list<NoteSkel*>::iterator it;
if (vel>0) //note on
{
pressed_keys.insert(note);
if (program_lock[program]==false)
{
if ( (n_voices==1) && (!notes.empty()) ) //we're in monomode
{
//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
n->destroy();
notes.clear();
NoteSkel *newnote=NULL;
if (curr_prg.create_func==NULL)
newnote = new Note(note,(float)vel/128.0,
curr_prg,
portamento_frames, pitchbend,
program,
curr_vol_factor);
else
newnote = curr_prg.create_func(note,(float)vel/128.0,
curr_prg,
portamento_frames, pitchbend,
program,
curr_vol_factor);
notes.push_back(newnote);
}
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 ((legato_pedal_pressed==false) || !n->still_active()) n->reattack();
n->set_vol_factor(curr_vol_factor);
//no need to push back. would become #1 instead of #1
}
}
else //we're in polymode
{
bool neednewnote=true;
//if (always_reattack) always_reattack is always true when in polymode
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);
(*it)->set_vol_factor(curr_vol_factor);
notes.push_back(*it); //reorder notes
notes.erase(it);
break;
}
if (neednewnote)
{
NoteSkel *newnote=NULL;
if (curr_prg.create_func==NULL)
newnote = new Note(note,(float)vel/128.0,
curr_prg,
portamento_frames, pitchbend,
program,
curr_vol_factor);
else
newnote = curr_prg.create_func(note,(float)vel/128.0,
curr_prg,
portamento_frames, pitchbend,
program,
curr_vol_factor);
notes.push_back(newnote);
}
apply_voice_limit();
}
}
// else (if the program is locked) simply ignore the note-on
}
else //note off
{
note_off(note);
}
}
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)
{
(*it)->destroy();
it=notes.erase(it);
}
}
}
}
void Channel::set_controller(int con,int val)
{
switch (con)
{
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 64: set_hold_pedal(val>=64); break;
case 66: set_sostenuto_pedal(val>=64); break;
case 67: set_soft_pedal(val>=64); break;
case 68: set_legato_pedal(val>=64); 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)
{
set_portamento_time_sec(val*max_port_time_sec/128.0);
}
void Channel::set_portamento_time_sec(float val)
{
portamento_frames2=samp_rate*val;
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::set_hold_pedal(bool newstate)
{
if (hold_pedal_pressed!=newstate)
{
hold_pedal_pressed=newstate;
if (newstate==false)
{
//check for all held keys: is the key not pressed any more?
// is the key not in sostenuto_keys?
//if both conditions are fulfilled, release that note
for (set<int>::iterator it=held_keys.begin(); it!=held_keys.end(); ++it)
if ( (pressed_keys.find(*it)==pressed_keys.end()) &&
(sostenuto_keys.find(*it)==sostenuto_keys.end()) )
note_off(*it);
held_keys.clear();
}
}
}
void Channel::set_sostenuto_pedal(bool newstate)
{
// !sostenuto_keys.empty() equals pedal_pressed
if ( newstate != !sostenuto_keys.empty() )
{
if (newstate)
{
sostenuto_keys=pressed_keys;
}
else
{
if (hold_pedal_pressed==false)
for (set<int>::iterator it=sostenuto_keys.begin(); it!=sostenuto_keys.end(); ++it)
if (pressed_keys.find(*it)==pressed_keys.end())
really_do_note_off(*it);
sostenuto_keys.clear();
}
}
}
void Channel::set_legato_pedal(bool newstate)
{
legato_pedal_pressed=newstate;
}
void Channel::set_soft_pedal(bool newstate)
{
curr_vol_factor = (newstate==false) ? 1.0 : 0.5; //TODO richtigen wert!
}
void Channel::panic()
{
list<NoteSkel*>::iterator it;
for (it=notes.begin(); it!=notes.end();)
{
(*it)->destroy();
it=notes.erase(it);
}
}
void Channel::kill_program(int prog)
{
list<NoteSkel*>::iterator it;
for (it=notes.begin(); it!=notes.end();)
if ((*it)->get_program()==prog)
{
(*it)->destroy();
it=notes.erase(it);
}
else
++it;
}
void Channel::maybe_reload_program(int prog)
{
if (program==prog)
curr_prg=program_settings[prog];
}
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);
}
|