-
Notifications
You must be signed in to change notification settings - Fork 0
/
organ.c
710 lines (619 loc) · 15 KB
/
organ.c
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
/*
Simple pipe / drawbar organ synthesizer using additive synthesis
for the ALSA framework (MIDI in and PCM audio out)
(compile with c++ organ.c -lasound)
Blame RG 2014
*/
#define debug
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h> //Sound output and midi input
#include <math.h> //For sin(), pow() etc.
#include <stdio.h>
#include <sys/poll.h>
#define unitAmpl -2048.0 //Unity amplitude of an oscillator
// function declarations:
void errormessage(const char *format, ...);
float timbre[10][10]; //Storage for harmonic series of organ stops
float freq_table[96]; //Store frequencies for MIDI codes
void make_table()
{
//Create lookup table using root inversion
float tw_root_two = pow(2.0, (1.0/12.0));
//fprintf(stderr, "12:th root of 2: %f \n", tw_root_two);
for (int i=0; i< 96; i++)
{
freq_table[i] = 65.406 * pow(tw_root_two, i);
}
}
class Oscillator
//The basic unit, where sine waves are produced
{
private:
float val[3];
float a1;
public:
Oscillator();
void set(float f, float a);
float freq;
float ampl;
float next();
};
Oscillator::Oscillator()
{
freq = 0.02;
a1 = 2.0*cos(freq);
//val[0] = 0.0;
val[0] = 0.0; //y(-1)
val[1] = unitAmpl*sin(freq);
}
void Oscillator::set(float f, float a)
{
freq = f;
ampl = a;
a1 = 2.0*cos(freq);
//val[0] = 0.0;
val[0] = 0.0; //y(-1)
val[1] = -1.0*ampl*sin(freq);
}
float Oscillator::next()
{
val[2] = val[1]; //y(-2)
val[1] = val[0]; //y(-1)
val[0] = a1*val[1]-val[2]; //y(0)
return val[0];
}
class Voice
//A set of multiple sine oscillators to produce fundamental and harmonics
{
private:
Oscillator waves[7]; //Fundamental [0], harmonics [1,2, ...]
float volume;
float ampl;
float attack;
float release;
public:
Voice();
void play(int n, int k, float f);
void rel();
void reTrig();
float next();
int note; //Which MIDI note is playing?
int channel; //Which MIDI channel is this voice allocated to?
float mixer[7];
};
Voice::Voice()
{
volume = 0.0;
ampl = 0.0;
attack = 0.0;
note = -1;
for(int i=0;i<7;i++)
{
mixer[i]=0.0;
}
}
void Voice::play(int theChannel, int theNote, float theFreq)
{
note = theNote; //Save note code
channel = theChannel; //Dito with channel info
float f=theFreq/44100.0;
waves[0].set(1.0*f, 1024.0); //Fundamental
waves[1].set(2.0*f, 1024.0); //First (octave)
waves[2].set(3.0*f, 1024.0); //Second (octave + 5.)
waves[3].set(4.0*f, 1024.0); //Third (octave + )
waves[4].set(5.0*f, 1024.0); //Fourth (2. octave)
waves[5].set(6.0*f, 1024.0); //Fift
waves[6].set(7.0*f, 1024.0); //Sixth
volume = 1.0;
ampl = 0.0;
attack = 0.005;
release = 0;
}
void Voice::reTrig()
{
volume = 1.0;
ampl = 0.0;
attack = 0.005;
release = 0;
}
void Voice::rel()
{
volume = 0.0;
attack = 0;
release = 0.0008;
}
float Voice::next()
{
float sum
= waves[0].next()*mixer[0]
+ waves[1].next()*mixer[1]
+ waves[2].next()*mixer[2]
+ waves[3].next()*mixer[3]
+ waves[4].next()*mixer[4]
+ waves[5].next()*mixer[5]
+ waves[6].next()*mixer[6];
if(volume > 0 && ampl < volume)
{
ampl+=attack;
}
else
{
if(volume == 0 && ampl > 0)
{
ampl-=release;
if (ampl < 0.2 && ampl > -0.2)
{
ampl = 0.0;
}
}
}
return sum*ampl;
}
class Arbiter
//Handle the oscillator resourses
{
public:
Arbiter();
int resources[10];
void shift();
void reorder(int voice);
};
class Organ
{
private:
Arbiter theArbiter;
public:
Organ();
Voice voices[10];
void setReg(int r);
float next();
void noteOn(int theChannel, int theNote);
void noteOff(int theChannel, int theNote);
};
void Organ::setReg(int r)
{
for (int i=0;i<10;i++) //Iterate over voices
{
for (int h=0;h<7;h++) //Iterate over harmonic's mixer values
if(timbre[r][h]>0.0)
{
voices[i].mixer[h]=pow(2.0,(timbre[r][h]/400.0))/8;
}
else
{
voices[i].mixer[h]=0.0;
}
}
}
Organ::Organ()
{
setReg(3);
}
void Organ::noteOn(int theChannel, int theNote)
{
//Is that note already playing? (Don't play the same note on two oscillators)
int ok=0;
int i;
for(i=0; i<10; i++)
{
if (voices[i].note == theNote && voices[i].channel==theChannel)
{
//Only re-trigger ADSR gate
voices[i].reTrig();
ok=1;
theArbiter.reorder(i);
break;
}
}
if(!ok) //If not, then allocate least recently used oscillator
{
i=theArbiter.resources[0];
theArbiter.shift();
voices[i].play(theChannel, theNote, freq_table[theNote]);
#ifdef debug
fprintf(stderr, "use voice#: %d \n", i);
#endif
}
}
void Organ::noteOff(int theChannel, int theNote)
{
//Is that note (still) playing?
int ok=0;
int i;
for(i=0; i<10; i++)
{
if (voices[i].note == theNote && voices[i].channel == theChannel)
{
//Only re-trigger ADSR gate
voices[i].rel();
break;
}
}
voices[i].rel();
}
float Organ::next()
{
float val
= voices[0].next()
+ voices[1].next()
+ voices[2].next()
+ voices[3].next()
+ voices[4].next()
+ voices[5].next()
+ voices[6].next()
+ voices[7].next()
+ voices[8].next()
+ voices[9].next();
return val;
}
Arbiter::Arbiter()
{
for (int i=0;i<10;i++)
{
resources[i]=i;
}
}
void Arbiter::shift()
{
int temp = resources[0];
for (int i=0;i<10-1;i++)
{
resources[i]=resources[i+1];
}
resources[9]=temp;
}
void Arbiter::reorder(int voice)
{
int i;
int found=0;
for (i=0;i<9;i++)
{
if(resources[i]==voice)
{
found=1;
}
if(found)
{
resources[i]=resources[i+1];
}
}
resources[9]=voice;
}
class Reverb
{
private:
float *delayPipe;
int length;
float feedback;
int pointerIn;
int pointerOut;
public:
Reverb();
Reverb(int l, float f);
float next(float input);
};
Reverb::Reverb()
{
for (int i=0;i<32768;i++)
{
delayPipe[i]=0;
}
pointerIn=0;
pointerOut=2;
}
Reverb::Reverb(int l, float f)
{
delayPipe = new float[32768];
for (int i=0;i<32768;i++)
{
delayPipe[i]=0;
}
length = l;
feedback = f;
pointerIn = 0;
pointerOut = l;
}
float Reverb::next(float input)
{
float outValue = delayPipe[pointerOut];
if(pointerIn > length)
fprintf(stderr, "pointerIn Out of bounds\n");
if(pointerOut > length)
fprintf(stderr, "pointerOut Out of bounds\n");
delayPipe[pointerIn] = input+outValue*feedback;
++pointerIn %= length;
++pointerOut %= length;
return outValue;
}
void set_timbres()
{
//Principal 8"
timbre[0][0] = 500.0;
timbre[0][1] = 700.0;
timbre[0][2] = 500.0;
timbre[0][3] = 400.0;
timbre[0][4] = 200.0;
timbre[0][5] = 100.0;
timbre[0][6] = 0.0;
//Diapson 8"
timbre[1][0] = 500.0;
timbre[1][1] = 600.0;
timbre[1][2] = 400.0;
timbre[1][3] = 200.0;
timbre[1][4] = 100.0;
timbre[1][5] = 100.0;
timbre[1][6] = 0.0;
//Clarinet 8"
timbre[2][0] = 800.0;
timbre[2][1] = 0.0;
timbre[2][2] = 800.0;
timbre[2][3] = 0.0;
timbre[2][4] = 800.0;
timbre[2][5] = 400.0;
timbre[2][6] = 0.0;
//Trumpet 8"
timbre[3][0] = 600.0;
timbre[3][1] = 700.0;
timbre[3][2] = 800.0;
timbre[3][3] = 600.0;
timbre[3][4] = 500.0;
timbre[3][5] = 300.0;
timbre[3][6] = 0.0;
//Cello 8"
timbre[4][0] = 400.0;
timbre[4][1] = 500.0;
timbre[4][2] = 400.0;
timbre[4][3] = 500.0;
timbre[4][4] = 400.0;
timbre[4][5] = 400.0;
timbre[4][6] = 200.0;
}
//int main() {
int main(int argc, char *argv[]) {
//initscr(); //Initialize ncurses
//noecho();
set_timbres();
make_table(); //Set up note frequencies int the table freq_table[]
Organ theOrgan;
Reverb* rev1 = new Reverb(2500, 0.8);
//Reverb* rev2 = new Reverb(4000, 0.8);
//Reverb* rev3 = new Reverb(5500, 0.8);
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
char *buffer;
/* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default",
SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
fprintf(stderr,
"unable to open pcm device: %s\n",
snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Set period size to 32 frames. */
frames = 16;
snd_pcm_hw_params_set_period_size_near(handle,
params, &frames, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr,
"unable to set hw parameters: %s\n",
snd_strerror(rc));
exit(1);
}
snd_pcm_uframes_t bufferSize;
snd_pcm_hw_params_get_buffer_size( params, &bufferSize );
fprintf(stderr,
"alsa_buffer_size: %lu frames\n", bufferSize);
rc = snd_pcm_set_params(handle,
SND_PCM_FORMAT_S16_LE,
SND_PCM_ACCESS_RW_INTERLEAVED,
2,
44100,
32,
50000); //Latency
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames,
&dir);
size = frames * 4; /* 2 bytes/sample, 2 channels */
buffer = (char *) malloc(size);
int16_t* buffer_16 = (int16_t *) buffer; //Cast buffer for 16-bit values
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params,
&val, &dir);
/* 5 seconds in microseconds divided by
* period time */
//loops = 5000000 / val;
//rc = snd_pcm_nonblock(handle, 0);
// Make init for MIDI INPUT
int status;
int mode = SND_RAWMIDI_SYNC | SND_RAWMIDI_NONBLOCK;
snd_rawmidi_t* midiin = NULL;
const char* portname = "hw:1,0,0"; // see alsarawportlist.c example program
if ((argc > 1) && (strncmp("hw:", argv[1], 3) == 0)) {
portname = argv[1];
}
if ((status = snd_rawmidi_open(&midiin, NULL, portname, mode)) < 0) {
errormessage("Problem opening MIDI input: %s", snd_strerror(status));
exit(1);
}
int count = 0; // Current count of bytes received.
char mid_buffer[1]; // Storage for input buffer received
int framesleft=0;
//Storage for MIDI-parsing:
int midi_cmd;
int midi_count=0;
int midi_channel=0;
int midi_note;
int midi_offset=1; //Transpose value
//
while (true) {
for(int i=0;i<frames;i++)
{
float organOut = theOrgan.next(); //Read output from organ
float outValue = organOut*3.0
+ rev1->next(organOut)*0.5
//+ rev2->next(organOut)*0.25
//+ rev3->next(organOut)*0.12
;
buffer_16[i*2] = (int16_t)outValue;
buffer_16[i*2+1] = (int16_t)outValue;
}
rc = snd_pcm_writei(handle, buffer, frames);
if (rc == -EPIPE) {
/* EPIPE means underrun */
fprintf(stderr, "underrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc < 0) {
fprintf(stderr,
"error from writei: %s\n",
snd_strerror(rc));
} else if (rc != (int)frames) {
fprintf(stderr,
"short write, write %d frames\n", rc);
}
else
{
/*
*/
}
//MIDI code
status = snd_rawmidi_read(midiin, mid_buffer, 1);
if(status > 0)
{
unsigned char theByte = mid_buffer[0];
//Simple MIDI parser //Blame RG 2014
if((theByte & 0x80) == 0x80) //Is it a command?
{
if((theByte & 0x90) == 0x90) //Note on
{
midi_channel = (theByte & 0x0f);
#ifdef debug
fprintf(stderr, "midi_channel: %d \n", midi_channel);
#endif
midi_count=1;
midi_cmd=0x90;
}
else
{
if((theByte & 0x90) == 0x80) //Note off
{
midi_channel = (theByte & 0x0f);
#ifdef debug
fprintf(stderr, "midi_channel: %d \n", midi_channel);
#endif
midi_count=1;
midi_cmd=0x80;
}
}
} //End of command processing
else
{
if (midi_count==1)
{
midi_note = (theByte+midi_offset);
midi_count++;
}
else
{
if (midi_count==2)
{
//Velocity info here
midi_count=1;
if(midi_cmd == 0x90)
{
if(theByte!=00) //21.11.2014 (Check if note on with vel 0 is used as note off!)
{
theOrgan.noteOn(midi_channel, midi_note);
#ifdef debug
fprintf(stderr, "play note: %d \n", midi_note);
#endif
}
else
{
theOrgan.noteOff(midi_channel, midi_note);
#ifdef debug
fprintf(stderr, "note on, volume 0: %d \n", midi_note);
#endif
}
}
if(midi_cmd == 0x80)
{
theOrgan.noteOff(midi_channel, midi_note);
#ifdef debug
fprintf(stderr, "note off: %d \n", midi_note);
#endif
}
}
}
}
//count++;
}
//END MIDI CODE
char command[80];
struct pollfd fds;
int ret;
fds.fd = 0; /* this is STDIN */
fds.events = POLLIN;
ret = poll(&fds, 1, 0);
if(ret == 1)
{
read(STDIN_FILENO, command, 80);
switch(command[0])
{
case '0':
printf("Command 0\n");
theOrgan.setReg(0);
break;
case '1':
printf("Command 1\n");
theOrgan.setReg(1);
break;
case '2':
printf("Command 2\n");
theOrgan.setReg(2);
break;
case '3':
printf("Command 3\n");
theOrgan.setReg(3);
break;
case '4':
printf("Command 4\n");
theOrgan.setReg(4);
break;
}
}
else if(ret == 0)
{
//printf("No\n");
}
else
printf("Error\n");
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
snd_rawmidi_close(midiin);
midiin = NULL; // snd_rawmidi_close() does not clear invalid pointer,
return 0;
}
// error -- print error message
//
void errormessage(const char *format, ...) {
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
putc('\n', stderr);
}