1 /*
2 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE // needed for M_PI
21 #endif
22
23 #include <math.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #ifndef COMPILER_MSVC
29 #include <stdbool.h>
30 #endif
31
32 #include <assert.h>
33
34 #ifndef BUFFER_SIZE_SAMPLES
35 #define BUFFER_SIZE_SAMPLES 64
36 #endif
37
38 #ifndef MIN
39 #define MIN(A, B) ((A) < (B) ? (A) : (B))
40 #endif
41
42 /* internal MIDI event abstraction */
43 enum RMIDI_EV_TYPE {
44 INVALID = 0,
45 NOTE_ON,
46 NOTE_OFF,
47 PROGRAM_CHANGE,
48 CONTROL_CHANGE,
49 };
50
51 struct rmidi_event_t {
52 enum RMIDI_EV_TYPE type;
53 uint8_t channel; /**< the MIDI channel number 0-15 */
54 union {
55 struct {
56 uint8_t note;
57 uint8_t velocity;
58 } tone;
59 struct {
60 uint8_t param;
61 uint8_t value;
62 } control;
63 } d;
64 };
65
66 typedef struct {
67 uint32_t tme[3]; // attack, decay, release times [settings:ms || internal:samples]
68 float vol[2]; // attack, sustain volume [0..1]
69 uint32_t off[3]; // internal use (added attack,decay,release times)
70 } ADSRcfg;
71
72 typedef struct _RSSynthChannel {
73 uint32_t keycomp;
74 uint32_t adsr_cnt[128];
75 float adsr_amp[128];
76 float phase[128]; // various use, zero'ed on note-on
77 int8_t miditable[128]; // internal, note-on/off velocity
78 int8_t midimsgs[128]; // internal, note-off + on in same cycle, sustained-off
79 int8_t sustain; // sustain pedal pressed
80 ADSRcfg adsr;
81 void (*synthesize) (struct _RSSynthChannel* sc,
82 const uint8_t note, const float vol, const float pc,
83 const size_t n_samples, float* left, float* right);
84 } RSSynthChannel;
85
86 typedef void (*SynthFunction) (RSSynthChannel* sc,
87 const uint8_t note,
88 const float vol,
89 const float pc,
90 const size_t n_samples,
91 float* left, float* right);
92
93 typedef struct {
94 uint32_t boffset;
95 float buf[2][BUFFER_SIZE_SAMPLES];
96 RSSynthChannel sc[16];
97 float freqs[128];
98 float kcgain;
99 float kcfilt;
100 double rate;
101 uint32_t xmas_on;
102 uint32_t xmas_off;
103 } RSSynthesizer;
104
105 /* initialize ADSR values
106 *
107 * @param rate sample-rate
108 * @param a attack time in seconds
109 * @param d decay time in seconds
110 * @param r release time in seconds
111 * @param avol attack gain [0..1]
112 * @param svol sustain volume level [0..1]
113 */
114 static void
init_adsr(ADSRcfg * adsr,const double rate,const uint32_t a,const uint32_t d,const uint32_t r,const float avol,const float svol)115 init_adsr (ADSRcfg* adsr, const double rate,
116 const uint32_t a, const uint32_t d, const uint32_t r,
117 const float avol, const float svol)
118 {
119 adsr->vol[0] = avol;
120 adsr->vol[1] = svol;
121 adsr->tme[0] = a * rate / 1000.0;
122 adsr->tme[1] = d * rate / 1000.0;
123 adsr->tme[2] = r * rate / 1000.0;
124
125 assert (adsr->tme[0] > 32);
126 assert (adsr->tme[1] > 32);
127 assert (adsr->tme[2] > 32);
128 assert (adsr->vol[0] >= 0 && adsr->vol[1] <= 1.0);
129 assert (adsr->vol[1] >= 0 && adsr->vol[1] <= 1.0);
130
131 adsr->off[0] = adsr->tme[0];
132 adsr->off[1] = adsr->tme[1] + adsr->off[0];
133 adsr->off[2] = adsr->tme[2] + adsr->off[1];
134 }
135
136 /* calculate per-sample, per-key envelope */
137 static inline float
adsr_env(RSSynthChannel * sc,const uint8_t note)138 adsr_env (RSSynthChannel* sc, const uint8_t note)
139 {
140 if (sc->adsr_cnt[note] < sc->adsr.off[0]) {
141 // attack
142 const uint32_t p = ++sc->adsr_cnt[note];
143 if (p == sc->adsr.tme[0]) {
144 sc->adsr_amp[note] = sc->adsr.vol[0];
145 return sc->adsr.vol[0];
146 } else {
147 const float d = sc->adsr.vol[0] - sc->adsr_amp[note];
148 return sc->adsr_amp[note] + (p / (float)sc->adsr.tme[0]) * d;
149 }
150 } else if (sc->adsr_cnt[note] < sc->adsr.off[1]) {
151 // decay
152 const uint32_t p = ++sc->adsr_cnt[note] - sc->adsr.off[0];
153 if (p == sc->adsr.tme[1]) {
154 sc->adsr_amp[note] = sc->adsr.vol[1];
155 return sc->adsr.vol[1];
156 } else {
157 const float d = sc->adsr.vol[1] - sc->adsr_amp[note];
158 return sc->adsr_amp[note] + (p / (float)sc->adsr.tme[1]) * d;
159 }
160 } else if (sc->adsr_cnt[note] == sc->adsr.off[1]) {
161 // sustain
162 return sc->adsr.vol[1];
163 } else if (sc->adsr_cnt[note] < sc->adsr.off[2]) {
164 // release
165 const uint32_t p = ++sc->adsr_cnt[note] - sc->adsr.off[1];
166 if (p == sc->adsr.tme[2]) {
167 sc->adsr_amp[note] = 0;
168 return 0;
169 } else {
170 const float d = 0 - sc->adsr_amp[note];
171 return sc->adsr_amp[note] + (p / (float)sc->adsr.tme[2]) * d;
172 }
173 } else {
174 sc->adsr_cnt[note] = 0;
175 return 0;
176 }
177 }
178
179 /*****************************************************************************/
180 /* piano like sound w/slight stereo phase */
181 static void
synthesize_sineP(RSSynthChannel * sc,const uint8_t note,const float vol,const float fq,const size_t n_samples,float * left,float * right)182 synthesize_sineP (RSSynthChannel* sc,
183 const uint8_t note, const float vol, const float fq,
184 const size_t n_samples, float* left, float* right)
185 {
186 size_t i;
187 float phase = sc->phase[note];
188
189 for (i = 0; i < n_samples; ++i) {
190 float env = adsr_env (sc, note);
191 if (sc->adsr_cnt[note] == 0) {
192 break;
193 }
194 const float amp = vol * env;
195 if (amp > 1e-10) {
196 left[i] += amp * sinf (2.0 * M_PI * phase);
197 left[i] += .300 * amp * sinf (2.0 * M_PI * phase * 2.0);
198 left[i] += .150 * amp * sinf (2.0 * M_PI * phase * 3.0);
199 left[i] += .080 * amp * sinf (2.0 * M_PI * phase * 4.0);
200 //left[i] -= .007 * amp * sinf(2.0 * M_PI * phase * 5.0);
201 //left[i] += .010 * amp * sinf(2.0 * M_PI * phase * 6.0);
202 left[i] += .020 * amp * sinf (2.0 * M_PI * phase * 7.0);
203 phase += fq;
204 right[i] += amp * sinf (2.0 * M_PI * phase);
205 right[i] += .300 * amp * sinf (2.0 * M_PI * phase * 2.0);
206 right[i] += .150 * amp * sinf (2.0 * M_PI * phase * 3.0);
207 right[i] -= .080 * amp * sinf (2.0 * M_PI * phase * 4.0);
208 //right[i] += .007 * amp * sinf(2.0 * M_PI * phase * 5.0);
209 //right[i] += .010 * amp * sinf(2.0 * M_PI * phase * 6.0);
210 right[i] -= .020 * amp * sinf (2.0 * M_PI * phase * 7.0);
211 } else {
212 phase += fq;
213 }
214 if (phase > 1.0)
215 phase -= 2.0;
216 }
217 sc->phase[note] = phase;
218 }
219
220 static const ADSRcfg piano_adsr = { { 5, 800, 100 }, { 1.0, 0.0 }, { 0, 0, 0 } };
221
222 /*****************************************************************************/
223
224 /* process note - move through ADSR states, count active keys,.. */
225 static void
process_key(void * synth,const uint8_t chn,const uint8_t note,const size_t n_samples,float * left,float * right)226 process_key (void* synth,
227 const uint8_t chn, const uint8_t note,
228 const size_t n_samples,
229 float* left, float* right)
230 {
231 RSSynthesizer* rs = (RSSynthesizer*)synth;
232 RSSynthChannel* sc = &rs->sc[chn];
233 const int8_t vel = sc->miditable[note];
234 const int8_t msg = sc->midimsgs[note];
235 const float vol = /* master_volume */ 0.1f * abs (vel) / 127.f;
236 const float phase = sc->phase[note];
237 const int8_t sus = sc->sustain;
238 sc->midimsgs[note] &= ~3;
239
240 if (phase == -10 && vel > 0) {
241 // new note on
242 sc->midimsgs[note] &= ~4;
243 assert (sc->adsr_cnt[note] == 0);
244 sc->adsr_amp[note] = 0;
245 sc->adsr_cnt[note] = 0;
246 sc->phase[note] = 0;
247 sc->keycomp++;
248 //printf("[On] Now %d keys active on chn %d\n", sc->keycomp, chn);
249 } else if (phase >= -1.0 && phase <= 1.0 && vel > 0) {
250 // sustain note or re-start note while adsr in progress:
251 if (sc->adsr_cnt[note] > sc->adsr.off[1] || msg == 3 || msg == 5 || msg == 7) {
252 sc->midimsgs[note] &= ~4;
253 // x-fade to attack
254 sc->adsr_amp[note] = adsr_env (sc, note);
255 sc->adsr_cnt[note] = 0;
256 }
257 } else if (phase >= -1.0 && phase <= 1.0 && vel < 0) {
258 sc->midimsgs[note] |= 4;
259 // note off
260 if (sc->adsr_cnt[note] <= sc->adsr.off[1] && !sus) {
261 if (sc->adsr_cnt[note] != sc->adsr.off[1]) {
262 // x-fade to release
263 sc->adsr_amp[note] = adsr_env (sc, note);
264 }
265 sc->adsr_cnt[note] = sc->adsr.off[1] + 1;
266 } else if (sus && sc->adsr_cnt[note] == sc->adsr.off[1]) {
267 sc->adsr_cnt[note] = sc->adsr.off[1] + 1;
268 }
269 } else {
270 //printf("FORCE NOTE OFF: %d %d\n", vel, sus);
271 /* note-on + off in same cycle */
272 sc->miditable[note] = 0;
273 sc->adsr_cnt[note] = 0;
274 sc->phase[note] = -10;
275 return;
276 }
277 //printf("NOTE: %d (%d %d %d)\n", sc->adsr_cnt[note], sc->adsr.off[0], sc->adsr.off[1], sc->adsr.off[2]);
278
279 // synthesize actual sound
280 sc->synthesize (sc, note, vol, rs->freqs[note], n_samples, left, right);
281
282 if (sc->adsr_cnt[note] == 0) {
283 //printf("Note %d,%d released\n", chn, note);
284 sc->midimsgs[note] = 0;
285 sc->miditable[note] = 0;
286 sc->adsr_amp[note] = 0;
287 sc->phase[note] = -10;
288 sc->keycomp--;
289 //printf("[off] Now %d keys active on chn %d\n", sc->keycomp, chn);
290 }
291 }
292
293 /* synthesize a BUFFER_SIZE_SAMPLES's of audio-data */
294 static void
synth_fragment(void * synth,const size_t n_samples,float * left,float * right)295 synth_fragment (void* synth, const size_t n_samples, float* left, float* right)
296 {
297 RSSynthesizer* rs = (RSSynthesizer*)synth;
298 memset (left, 0, n_samples * sizeof (float));
299 memset (right, 0, n_samples * sizeof (float));
300 uint8_t keycomp = 0;
301 int c, k;
302 size_t i;
303
304 for (c = 0; c < 16; ++c) {
305 for (k = 0; k < 128; ++k) {
306 if (rs->sc[c].miditable[k] == 0) {
307 continue;
308 }
309 process_key (synth, c, k, n_samples, left, right);
310 }
311 keycomp += rs->sc[c].keycomp;
312 }
313
314 #if 1 // key-compression
315 float kctgt = 8.0 / (float)(keycomp + 7.0);
316 if (kctgt < .5) kctgt = .5;
317 if (kctgt > 1.0) kctgt = 1.0;
318
319 const float _w = rs->kcfilt;
320 for (i = 0; i < n_samples; ++i) {
321 rs->kcgain += _w * (kctgt - rs->kcgain);
322 left[i] *= rs->kcgain;
323 right[i] *= rs->kcgain;
324 }
325 rs->kcgain += 1e-12;
326 #endif
327 }
328
329 static void
synth_reset_channel(RSSynthChannel * sc)330 synth_reset_channel (RSSynthChannel* sc)
331 {
332 int k;
333 for (k = 0; k < 128; ++k) {
334 sc->adsr_cnt[k] = 0;
335 sc->adsr_amp[k] = 0;
336 sc->phase[k] = -10;
337 sc->miditable[k] = 0;
338 sc->midimsgs[k] = 0;
339 }
340 sc->keycomp = 0;
341 }
342
343 static void
synth_reset(void * synth)344 synth_reset (void* synth)
345 {
346 RSSynthesizer* rs = (RSSynthesizer*)synth;
347 int c;
348 for (c = 0; c < 16; ++c) {
349 synth_reset_channel (&(rs->sc[c]));
350 }
351 rs->kcgain = 0;
352 }
353
354 static void
synth_load(RSSynthChannel * sc,const double rate,SynthFunction synthesize,ADSRcfg const * const adsr)355 synth_load (RSSynthChannel* sc, const double rate,
356 SynthFunction synthesize,
357 ADSRcfg const* const adsr)
358 {
359 synth_reset_channel (sc);
360 init_adsr (&sc->adsr, rate,
361 adsr->tme[0], adsr->tme[1], adsr->tme[2],
362 adsr->vol[0], adsr->vol[1]);
363 sc->synthesize = synthesize;
364 }
365
366 /**
367 * internal abstraction of MIDI data handling
368 */
369 static void
synth_process_midi_event(void * synth,struct rmidi_event_t * ev)370 synth_process_midi_event (void* synth, struct rmidi_event_t* ev)
371 {
372 RSSynthesizer* rs = (RSSynthesizer*)synth;
373 switch (ev->type) {
374 case NOTE_ON:
375 rs->sc[ev->channel].midimsgs[ev->d.tone.note] |= 1;
376 if (rs->sc[ev->channel].miditable[ev->d.tone.note] <= 0)
377 rs->sc[ev->channel].miditable[ev->d.tone.note] = ev->d.tone.velocity;
378 break;
379 case NOTE_OFF:
380 rs->sc[ev->channel].midimsgs[ev->d.tone.note] |= 2;
381 if (rs->sc[ev->channel].miditable[ev->d.tone.note] > 0)
382 rs->sc[ev->channel].miditable[ev->d.tone.note] *= -1.0;
383 break;
384 case PROGRAM_CHANGE:
385 break;
386 case CONTROL_CHANGE:
387 if (ev->d.control.param == 0x00 || ev->d.control.param == 0x20) {
388 /* 0x00 and 0x20 are used for BANK select */
389 } else if (ev->d.control.param == 64) {
390 /* damper pedal*/
391 rs->sc[ev->channel].sustain = ev->d.control.value < 64 ? 0 : 1;
392 } else if (ev->d.control.param == 121) {
393 /* reset all controllers */
394 } else if (ev->d.control.param == 120 || ev->d.control.param == 123) {
395 /* Midi panic: 120: all sound off, 123: all notes off*/
396 synth_reset_channel (&(rs->sc[ev->channel]));
397 } else if (ev->d.control.param >= 120) {
398 /* params 122-127 are reserved - skip them. */
399 }
400 break;
401 default:
402 break;
403 }
404 }
405
406 /******************************************************************************
407 * PUBLIC API (used by lv2.c)
408 */
409
410 /**
411 * align LV2 and internal synth buffers
412 * call synth_fragment as often as needed for the given LV2 buffer size
413 *
414 * @param synth synth-handle
415 * @param written samples written so far (offset in \ref out)
416 * @param nframes total samples to synthesize and write to the \out buffer
417 * @param out pointer to stereo output buffers
418 * @return end of buffer (written + nframes)
419 */
420 static uint32_t
synth_sound(void * synth,uint32_t written,const uint32_t nframes,float ** out)421 synth_sound (void* synth, uint32_t written, const uint32_t nframes, float** out)
422 {
423 RSSynthesizer* rs = (RSSynthesizer*)synth;
424
425 while (written < nframes) {
426 uint32_t nremain = nframes - written;
427
428 if (rs->boffset >= BUFFER_SIZE_SAMPLES) {
429 const uint32_t tosynth = MIN (BUFFER_SIZE_SAMPLES, nremain);
430 rs->boffset = BUFFER_SIZE_SAMPLES - tosynth;
431 synth_fragment (rs, tosynth, &(rs->buf[0][rs->boffset]), &(rs->buf[1][rs->boffset]));
432 }
433
434 uint32_t nread = MIN (nremain, (BUFFER_SIZE_SAMPLES - rs->boffset));
435
436 memcpy (&out[0][written], &rs->buf[0][rs->boffset], nread * sizeof (float));
437 memcpy (&out[1][written], &rs->buf[1][rs->boffset], nread * sizeof (float));
438
439 written += nread;
440 rs->boffset += nread;
441 }
442 return written;
443 }
444
445 /**
446 * parse raw midi-data.
447 *
448 * @param synth synth-handle
449 * @param data 8bit midi message
450 * @param size number of bytes in the midi-message
451 */
452 static void
synth_parse_midi(void * synth,const uint8_t * data,const size_t size)453 synth_parse_midi (void* synth, const uint8_t* data, const size_t size)
454 {
455 if (size < 2 || size > 3)
456 return;
457 // All messages need to be 3 bytes; except program-changes: 2bytes.
458 if (size == 2 && (data[0] & 0xf0) != 0xC0)
459 return;
460
461 struct rmidi_event_t ev;
462
463 ev.channel = data[0] & 0x0f;
464 switch (data[0] & 0xf0) {
465 case 0x80:
466 ev.type = NOTE_OFF;
467 ev.d.tone.note = data[1] & 0x7f;
468 ev.d.tone.velocity = data[2] & 0x7f;
469 break;
470 case 0x90:
471 ev.type = NOTE_ON;
472 ev.d.tone.note = data[1] & 0x7f;
473 ev.d.tone.velocity = data[2] & 0x7f;
474 if (ev.d.tone.velocity == 0) {
475 ev.type = NOTE_OFF;
476 }
477 break;
478 case 0xB0:
479 ev.type = CONTROL_CHANGE;
480 ev.d.control.param = data[1] & 0x7f;
481 ev.d.control.value = data[2] & 0x7f;
482 break;
483 case 0xC0:
484 ev.type = PROGRAM_CHANGE;
485 ev.d.control.value = data[1] & 0x7f;
486 break;
487 default:
488 return;
489 }
490 synth_process_midi_event (synth, &ev);
491 }
492
493 static const uint8_t jingle[] = { 71, 71, 71, 71, 71, 71, 71, 74, 67, 69, 71, 72, 72, 72, 72, 72, 71, 71, 71, 71, 71, 69, 69, 71, 69, 74, 71, 71, 71, 71, 71, 71, 71, 74, 67, 69, 71, 72, 72, 72, 72, 72, 71, 71, 71, 71, 74, 74, 72, 69, 67, 62, 62, 71, 69, 67, 62, 62, 62, 62, 71, 69, 67, 64, 64, 64, 72, 71, 69, 66, 74, 76, 74, 72, 69, 71, 62, 62, 71, 69, 67, 62, 62, 62, 62, 71, 69, 67, 64, 64, 64, 72, 71, 69, 74, 74, 74, 74, 76, 74, 72, 69, 67, 74, 71, 71, 71, 71, 71, 71, 71, 74, 67, 69, 71, 72, 72, 72, 72, 72, 71, 71, 71, 71, 71, 69, 69, 71, 69, 74, 71, 71, 71, 71, 71, 71, 71, 74, 67, 69, 71, 72, 72, 72, 72, 72, 71, 71, 71, 71, 74, 74, 72, 69, 67 };
494
495 static void
synth_parse_xmas(void * synth,const uint8_t * data,const size_t size)496 synth_parse_xmas (void* synth, const uint8_t* data, const size_t size)
497 {
498 RSSynthesizer* rs = (RSSynthesizer*)synth;
499 if (size < 2 || size > 3)
500 return;
501 // All messages need to be 3 bytes; except program-changes: 2bytes.
502 if (size == 2 && (data[0] & 0xf0) != 0xC0)
503 return;
504
505 struct rmidi_event_t ev;
506
507 ev.channel = data[0] & 0x0f;
508 switch (data[0] & 0xf0) {
509 case 0x80:
510 ev.type = NOTE_OFF;
511 ev.d.tone.note = jingle[rs->xmas_off++];
512 ev.d.tone.velocity = data[2] & 0x7f;
513 if (rs->xmas_off >= sizeof (jingle))
514 rs->xmas_off = 0;
515 break;
516 case 0x90:
517 ev.type = NOTE_ON;
518 ev.d.tone.note = jingle[rs->xmas_on++];
519 ev.d.tone.velocity = data[2] & 0x7f;
520 if (rs->xmas_on >= sizeof (jingle))
521 rs->xmas_on = 0;
522 break;
523 case 0xB0:
524 ev.type = CONTROL_CHANGE;
525 ev.d.control.param = data[1] & 0x7f;
526 ev.d.control.value = data[2] & 0x7f;
527 break;
528 case 0xC0:
529 ev.type = PROGRAM_CHANGE;
530 ev.d.control.value = data[1] & 0x7f;
531 break;
532 default:
533 return;
534 }
535 synth_process_midi_event (synth, &ev);
536 }
537 /**
538 * initialize the synth
539 * This should be called after synth_alloc()
540 * as soon as the sample-rate is known
541 *
542 * @param synth synth-handle
543 * @param rate sample-rate
544 */
545 static void
synth_init(void * synth,double rate)546 synth_init (void* synth, double rate)
547 {
548 RSSynthesizer* rs = (RSSynthesizer*)synth;
549 rs->rate = rate;
550 rs->boffset = BUFFER_SIZE_SAMPLES;
551 const float tuning = 440;
552 int c, k;
553 for (k = 0; k < 128; k++) {
554 rs->freqs[k] = (tuning / 32.0f) * powf (2, (k - 9.0) / 12.0) / rate;
555 assert (rs->freqs[k] < M_PI / 2); // otherwise spatialization may phase out..
556 }
557 rs->kcfilt = 12.0 / rate;
558 synth_reset (synth);
559
560 for (c = 0; c < 16; c++) {
561 synth_load (&rs->sc[c], rate, &synthesize_sineP, &piano_adsr);
562 }
563 rs->xmas_on = 0;
564 rs->xmas_off = 0;
565 }
566
567 /**
568 * Allocate data-structure, create a handle for all other synth_* functions.
569 *
570 * This data should be freeded with \ref synth_free when the synth is no
571 * longer needed.
572 *
573 * The synth can only be used after calling \rev synth_init as well.
574 *
575 * @return synth-handle
576 */
577 static void*
synth_alloc(void)578 synth_alloc (void)
579 {
580 return calloc (1, sizeof (RSSynthesizer));
581 }
582
583 /**
584 * release synth data structure
585 * @param synth synth-handle
586 */
587 static void
synth_free(void * synth)588 synth_free (void* synth)
589 {
590 free (synth);
591 }
592