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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
//=========================================================
// MusE
// Linux Music Editor
// $Id: organ.cpp,v 1.15.2.8 2009/12/06 10:05:00 terminator356 Exp $
//
// Parts of this file taken from:
// Organ - Additive Organ Synthesizer Voice
// Copyright (c) 1999, 2000 David A. Bartold
//
// (C) Copyright 2001-2004 Werner Schweer (ws@seh.de)
//=========================================================
#include <cmath>
#include <stdio.h>
#include "muse/midi.h"
#include "libsynti/mpevent.h"
#include "organ.h"
#include "organgui.h"
//#define ORGAN_DEBUG
SynthCtrl Organ::synthCtrl[] = {
{ "harm0", HARM0, 0 },
{ "harm1", HARM1, 0 },
{ "harm2", HARM2, 0 },
{ "harm3", HARM3, 0 },
{ "harm4", HARM4, 0 },
{ "harm5", HARM5, 0 },
{ "attackLo", ATTACK_LO, 20 },
{ "decayLo", DECAY_LO, 20 },
{ "sustainLo", SUSTAIN_LO, 0 },
{ "releaseLo", RELEASE_LO, 20 },
{ "attackHi", ATTACK_HI, 10 },
{ "decayHi", DECAY_HI, 10 },
{ "sustainHi", SUSTAIN_HI, 0 },
{ "releaseHi", RELEASE_HI, 10 },
{ "brass", BRASS, 1 },
{ "flute", FLUTE, 1 },
{ "reed", REED, 1 },
{ "velocity", VELO, 0 },
// next controller not send as init data
{ "volume", CTRL_VOLUME, 100 },
};
static int NUM_CONTROLLER = sizeof(Organ::synthCtrl)/sizeof(*(Organ::synthCtrl));
static int NUM_INIT_CONTROLLER = NUM_CONTROLLER - 1;
float* Organ::sine_table;
float* Organ::g_triangle_table;
float* Organ::g_pulse_table;
int Organ::useCount = 0;
double Organ::cb2amp_tab[MAX_ATTENUATION];
unsigned Organ::freq256[128];
//---------------------------------------------------------
// cb2amp
// convert centibel to amplification (0 - 96dB)
//---------------------------------------------------------
double Organ::cb2amp(int cb)
{
if (cb < 0)
return 1.0;
if (cb >= MAX_ATTENUATION)
return 0.0;
return cb2amp_tab[cb];
}
//---------------------------------------------------------
// Organ
//---------------------------------------------------------
Organ::Organ(int sr)
: Mess(1)
{
idata = new int[NUM_CONTROLLER];
setSampleRate(sr);
gui = 0;
++useCount;
if (useCount > 1)
return;
// centibels to amplitude conversion
for (int i = 0; i < MAX_ATTENUATION; i++)
cb2amp_tab[i] = pow(10.0, double(i) / -200.0);
for (int i = 0; i < 128; ++i) {
double freq = 8.176 * exp(double(i)*log(2.0)/12.0);
freq256[i] = (int) (freq * ((double) RESOLUTION) / sr * 256.0);
}
int size = RESOLUTION;
int half = size / 2;
int slope = size / 10;
int i;
// Initialize sine table.
sine_table = new float[size];
for (i = 0; i < size; i++)
sine_table[i] = sin ((i * 2.0 * M_PI) / size) / 6.0;
// Initialize triangle table.
g_triangle_table = new float[size];
for (i = 0; i < half; i++)
g_triangle_table[i] = (4.0 / size * i - 1.0) / 6.0;
for (; i < size; i++)
g_triangle_table[i] = (4.0 / size * (size - i) - 1.0) / 6.0;
// Initialize pulse table.
g_pulse_table = new float[size];
for (i = 0; i < slope; i++)
g_pulse_table[i] = (((double) -i) / slope) / 6.0;
for (; i < half - slope; i++)
g_pulse_table[i] = -1.0 / 6.0;
for (; i < half + slope; i++)
g_pulse_table[i] = (((double) i - half) / slope) / 6.0;
for (; i < size - slope; i++)
g_pulse_table[i] = 1.0 / 6.0;
for (; i < size; i++)
g_pulse_table[i] = (((double) size - i) / slope) / 6.0;
}
//---------------------------------------------------------
// ~Organ
//---------------------------------------------------------
Organ::~Organ()
{
if (gui)
delete gui;
delete idata;
--useCount;
if (useCount == 0) {
delete[] g_pulse_table;
delete[] g_triangle_table;
delete[] sine_table;
}
}
//---------------------------------------------------------
// table_pos
//---------------------------------------------------------
static inline float table_pos (float* table, unsigned long freq_256, unsigned *accum)
{
*accum += freq_256;
while (*accum >= RESOLUTION * 256)
*accum -= RESOLUTION * 256;
return table[*accum >> 8];
}
//---------------------------------------------------------
// init
//---------------------------------------------------------
bool Organ::init(const char* name)
{
gui = new OrganGui;
gui->setCaption(QString(name));
gui->show();
for (int i = 0; i < NUM_CONTROLLER; ++i)
setController(0, synthCtrl[i].num, synthCtrl[i].val);
for (int i = 0; i < VOICES; ++i)
voices[i].isOn = false;
return false;
}
//---------------------------------------------------------
// processMessages
// Called from host always, even if output path is unconnected.
//---------------------------------------------------------
void Organ::processMessages()
{
//Process messages from the gui
//
// get and process all pending events from the
// synthesizer GUI
//
while (gui->fifoSize())
{
MidiPlayEvent ev = gui->readEvent();
if (ev.type() == ME_CONTROLLER)
{
// process local?
setController(ev.dataA(), ev.dataB());
sendEvent(ev);
}
else
printf("Organ::process(): unknown event\n");
}
}
//---------------------------------------------------------
// process
// Called from host, ONLY if output path is connected.
//---------------------------------------------------------
void Organ::process(float** ports, int offset, int sampleCount)
{
/*
//
// get and process all pending events from the
// synthesizer GUI
//
while (gui->fifoSize()) {
MidiPlayEvent ev = gui->readEvent();
if (ev.type() == ME_CONTROLLER) {
// process local?
setController(ev.dataA(), ev.dataB());
sendEvent(ev);
}
else
printf("Organ::process(): unknown event\n");
}
*/
float* buffer = *ports + offset;
for (int i = 0; i < VOICES; ++i) {
Voice* v = &voices[i];
if (!v->isOn)
continue;
double vol = velo ? v->velocity : 1.0;
vol *= volume;
unsigned freq_256 = freq256[v->pitch];
unsigned* harm0_accum = &(v->harm0_accum);
unsigned* harm1_accum = &(v->harm1_accum);
unsigned* harm2_accum = &(v->harm2_accum);
unsigned* harm3_accum = &(v->harm3_accum);
unsigned* harm4_accum = &(v->harm4_accum);
unsigned* harm5_accum = &(v->harm5_accum);
unsigned long freq_256_harm2, freq_256_harm3;
unsigned long freq_256_harm4, freq_256_harm5;
float* reed_table = reed ? g_pulse_table : sine_table;
float* flute_table = flute ? g_triangle_table : sine_table;
unsigned freq_256_harm0 = freq_256 / 2;
unsigned freq_256_harm1 = freq_256;
if (brass) {
freq_256_harm2 = freq_256 * 2;
freq_256_harm3 = freq_256_harm2 * 2;
freq_256_harm4 = freq_256_harm3 * 2;
freq_256_harm5 = freq_256_harm4 * 2;
for (int i = 0; i < sampleCount; i++) {
int a1=0, a2=0; //prevent compiler warning: unitialized usage of vars a1 & a2
switch(v->state1) {
case ATTACK:
if (v->envL1.step(&a1))
break;
v->state1 = DECAY;
case DECAY:
if (v->envL2.step(&a1))
break;
v->state1 = SUSTAIN;
case SUSTAIN:
a1 = sustain0;
break;
case RELEASE:
if (v->envL3.step(&a1))
break;
v->state1 = OFF;
a1 = MAX_ATTENUATION;
break;
}
switch(v->state2) {
case ATTACK:
if (v->envH1.step(&a2))
break;
v->state2 = DECAY;
case DECAY:
if (v->envH2.step(&a2))
break;
v->state2 = SUSTAIN;
case SUSTAIN:
a2 = sustain1;
break;
case RELEASE:
if (v->envH3.step(&a2))
break;
v->state2 = OFF;
a1 = MAX_ATTENUATION;
break;
}
if (v->state1 == OFF && v->state2 == OFF) {
v->isOn = false;
break;
}
buffer[i] +=
(table_pos (sine_table, freq_256_harm0, harm0_accum) * harm0
+ table_pos (sine_table, freq_256_harm1, harm1_accum) * harm1
+ table_pos (reed_table, freq_256_harm2, harm2_accum) * harm2)
* cb2amp(a1) * vol
+ (table_pos (sine_table, freq_256_harm3, harm3_accum) * harm3
+ table_pos (flute_table, freq_256_harm4, harm4_accum) * harm4
+ table_pos (flute_table, freq_256_harm5, harm5_accum) * harm5)
* cb2amp(a2) * vol;
}
}
else {
freq_256_harm2 = freq_256 * 3 / 2;
freq_256_harm3 = freq_256 * 2;
freq_256_harm4 = freq_256 * 3;
freq_256_harm5 = freq_256_harm3 * 2;
for (int i = 0; i < sampleCount; i++) {
int a1=0, a2=0;//prevent compiler warning: unitialized usage of vars a1 & a2
switch(v->state1) {
case ATTACK:
if (v->envL1.step(&a1))
break;
v->state1 = DECAY;
case DECAY:
if (v->envL2.step(&a1))
break;
v->state1 = SUSTAIN;
case SUSTAIN:
a1 = sustain0;
break;
case RELEASE:
if (v->envL3.step(&a1))
break;
v->state1 = OFF;
a1 = MAX_ATTENUATION;
break;
}
switch(v->state2) {
case ATTACK:
if (v->envH1.step(&a2))
break;
v->state2 = DECAY;
case DECAY:
if (v->envH2.step(&a2))
break;
v->state2 = SUSTAIN;
case SUSTAIN:
a2 = sustain1;
break;
case RELEASE:
if (v->envH3.step(&a2))
break;
v->state2 = OFF;
a1 = MAX_ATTENUATION;
break;
}
if (v->state1 == OFF && v->state2 == OFF) {
v->isOn = false;
break;
}
buffer[i] +=
(table_pos (sine_table, freq_256_harm0, harm0_accum) * harm0
+ table_pos (sine_table, freq_256_harm1, harm1_accum) * harm1
+ table_pos (sine_table, freq_256_harm2, harm2_accum) * harm2)
* cb2amp(a1) * vol
+ (table_pos (reed_table, freq_256_harm3, harm3_accum) * harm3
+ table_pos (sine_table, freq_256_harm4, harm4_accum) * harm4
+ table_pos (flute_table, freq_256_harm5, harm5_accum) * harm5)
* cb2amp(a2) * vol;
}
}
}
}
//---------------------------------------------------------
// playNote
//---------------------------------------------------------
bool Organ::playNote(int channel, int pitch, int velo)
{
if (velo == 0) {
noteoff(channel, pitch);
return false;
}
for (int i = 0; i < VOICES; ++i) {
if (voices[i].isOn)
continue;
voices[i].isOn = true;
voices[i].pitch = pitch;
voices[i].channel = channel;
// velo is never 0
voices[i].velocity = cb2amp(int(200 * log10((127.0 * 127)/(velo*velo))));
voices[i].state1 = ATTACK;
voices[i].state2 = ATTACK;
voices[i].envL1.set(attack0, MAX_ATTENUATION, 0);
voices[i].envL2.set(decay0, MAX_ATTENUATION, sustain0);
voices[i].envL3.set(release0, sustain0, MAX_ATTENUATION);
voices[i].envH1.set(attack1, MAX_ATTENUATION, 0);
voices[i].envH2.set(decay1, MAX_ATTENUATION, sustain1);
voices[i].envH3.set(release1, sustain1, MAX_ATTENUATION);
voices[i].harm0_accum = 0;
voices[i].harm1_accum = 0;
voices[i].harm2_accum = 0;
voices[i].harm3_accum = 0;
voices[i].harm4_accum = 0;
voices[i].harm5_accum = 0;
return false;
}
printf("organ: voices overflow!\n");
return false;
}
//---------------------------------------------------------
// noteoff
//---------------------------------------------------------
void Organ::noteoff(int channel, int pitch)
{
bool found = false;
for (int i = 0; i < VOICES; ++i) {
if (voices[i].isOn && (voices[i].pitch == pitch)
&& (voices[i].channel == channel)) {
found = true;
voices[i].state1 = RELEASE;
voices[i].state2 = RELEASE;
}
}
if (!found)
printf("Organ: noteoff %d:%d not found\n", channel, pitch);
}
//---------------------------------------------------------
// setController
//---------------------------------------------------------
void Organ::setController(int ctrl, int data)
{
int sr = sampleRate();
// Changed By T356.
// Because of muse's auto-bias controllers, some of these negative-range
// controls need to apply the auto-bias correction.
switch (ctrl) {
case HARM0:
//harm0 = cb2amp(-data);
harm0 = cb2amp(-data + 8192);
break;
case HARM1:
//harm1 = cb2amp(-data);
harm1 = cb2amp(-data + 8192);
break;
case HARM2:
//harm2 = cb2amp(-data);
harm2 = cb2amp(-data + 8192);
break;
case HARM3:
//harm3 = cb2amp(-data);
harm3 = cb2amp(-data + 8192);
break;
case HARM4:
//harm4 = cb2amp(-data);
harm4 = cb2amp(-data + 8192);
break;
case HARM5:
//harm5 = cb2amp(-data);
harm5 = cb2amp(-data + 8192);
break;
case ATTACK_LO: // maxval -> 500msec
attack0 = (data * sr) / 1000;
break;
case DECAY_LO: // maxval -> 5000msec
decay0 = (data * sr) / 1000;
break;
case SUSTAIN_LO:
//sustain0 = -data;
sustain0 = -data + 8192;
break;
case RELEASE_LO:
release0 = (data * sr) / 1000;
break;
case ATTACK_HI:
attack1 = (data * sr) / 1000;
break;
case DECAY_HI:
decay1 = (data * sr) / 1000;
break;
case SUSTAIN_HI:
//sustain1 = -data;
sustain1 = -data + 8192;
break;
case RELEASE_HI:
release1 = (data * sr) / 1000;
break;
case BRASS:
brass = data;
break;
case FLUTE:
flute = data;
break;
case REED:
reed = data;
break;
case VELO:
velo = data;
break;
case CTRL_VOLUME:
data &= 0x7f;
volume = data == 0 ? 0.0 : cb2amp(int(200 * log10((127.0 * 127)/(data*data))));
break;
case CTRL_ALL_SOUNDS_OFF:
for (int i = 0; i < VOICES; ++i)
voices[i].isOn = false;
break;
case CTRL_RESET_ALL_CTRL:
for (int i = 0; i < NUM_CONTROLLER; ++i)
setController(0, synthCtrl[i].num, synthCtrl[i].val);
break;
default:
fprintf(stderr, "Organ:set unknown Ctrl 0x%x to 0x%x\n", ctrl, data);
return;
}
for (int i = 0; i < NUM_CONTROLLER; ++i) {
if (synthCtrl[i].num == ctrl) {
synthCtrl[i].val = data;
break;
}
}
}
//---------------------------------------------------------
// setController
//---------------------------------------------------------
bool Organ::setController(int channel, int ctrl, int data)
{
setController(ctrl, data);
switch (ctrl) {
case HARM0:
case HARM1:
case HARM2:
case HARM3:
case HARM4:
case HARM5:
case ATTACK_LO:
case DECAY_LO:
case SUSTAIN_LO:
case RELEASE_LO:
case ATTACK_HI:
case DECAY_HI:
case SUSTAIN_HI:
case RELEASE_HI:
case BRASS:
case FLUTE:
case REED:
case VELO:
{
MidiPlayEvent ev(0, 0, channel, ME_CONTROLLER, ctrl, data);
#ifdef ORGAN_DEBUG
fprintf(stderr, "OrganGui:setController before gui->writeEvent ctrl:%d data:%d\n", ctrl, data);
#endif
gui->writeEvent(ev);
}
break;
default:
break;
}
return false;
}
//---------------------------------------------------------
// sysex
//---------------------------------------------------------
bool Organ::sysex(int n, const unsigned char* data)
{
#ifdef ORGAN_DEBUG
printf("Organ: sysex\n");
#endif
if (unsigned(n) != (NUM_INIT_CONTROLLER * sizeof(int))) {
printf("Organ: unknown sysex\n");
return false;
}
int* s = (int*) data;
for (int i = 0; i < NUM_INIT_CONTROLLER; ++i) {
int val = *s++;
#ifdef ORGAN_DEBUG
printf("Organ: sysex before setController num:%d val:%d\n", synthCtrl[i].num, val);
#endif
setController(0, synthCtrl[i].num, val);
}
return false;
}
//---------------------------------------------------------
// getInitData
//---------------------------------------------------------
void Organ::getInitData(int* n, const unsigned char**p) const
{
int* d = idata;
for (int i = 0; i < NUM_INIT_CONTROLLER; ++i)
*d++ = synthCtrl[i].val;
*n = NUM_INIT_CONTROLLER * sizeof(int); // sizeof(idata);
*p = (unsigned char*)idata;
}
//---------------------------------------------------------
// MESS
//---------------------------------------------------------
//---------------------------------------------------------
// getControllerInfo
//---------------------------------------------------------
int Organ::getControllerInfo(int id, const char** name, int* controller,
int* min, int* max, int* initval) const
{
if (id >= NUM_CONTROLLER)
return 0;
*controller = synthCtrl[id].num;
*name = synthCtrl[id].name;
*initval = synthCtrl[id].val;
if(synthCtrl[id].num == CTRL_VOLUME)
{
*min = 0;
*max = 127;
}
else
gui->getControllerMinMax(id,min,max);
//*min = 0;
//*max = 128*128-1;
return ++id;
}
//---------------------------------------------------------
// guiVisible
//---------------------------------------------------------
bool Organ::guiVisible() const
{
return gui->isVisible();
}
//---------------------------------------------------------
// showGui
//---------------------------------------------------------
void Organ::showGui(bool val)
{
gui->setShown(val);
}
//---------------------------------------------------------
// getGeometry
//---------------------------------------------------------
void Organ::getGeometry(int* x, int* y, int* w, int* h) const
{
QPoint pos(gui->pos());
QSize size(gui->size());
*x = pos.x();
*y = pos.y();
*w = size.width();
*h = size.height();
}
//---------------------------------------------------------
// setGeometry
//---------------------------------------------------------
void Organ::setGeometry(int x, int y, int w, int h)
{
gui->resize(QSize(w, h));
gui->move(QPoint(x, y));
}
//---------------------------------------------------------
// instantiate
// construct a new synthesizer instance
//---------------------------------------------------------
static Mess* instantiate(int sr, QWidget*, QString* projectPathPtr, const char* name)
{
Organ* synth = new Organ(sr);
if (synth->init(name)) {
delete synth;
synth = 0;
}
return synth;
}
//---------------------------------------------------------
// msynth_descriptor
// Return a descriptor of the requested plugin type.
//---------------------------------------------------------
extern "C" {
static MESS descriptor = {
"Organ",
"Organ based on David A. Bartold's LADSPA plugin",
"0.1", // version string
MESS_MAJOR_VERSION, MESS_MINOR_VERSION,
instantiate,
};
// We must compile with -fvisibility=hidden to avoid namespace
// conflicts with global variables.
// Only visible symbol is "mess_descriptor".
// (TODO: all plugins should be compiled this way)
__attribute__ ((visibility("default")))
const MESS* mess_descriptor() { return &descriptor; }
}
|