1
2 /*
3 * Diverse Bristol audio routines.
4 * Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 /*#define DEBUG */
23
24 #include <math.h>
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <pthread.h>
30
31 #include "bristol.h"
32 #include "bristolmidi.h"
33 #include "palette.h"
34
35 #ifdef BRISTOL_PA
36 extern int bristolPulseInterface();
37 #endif
38
39 #ifdef _BRISTOL_JACK
40 extern int bristolJackInterface();
41 #endif
42
43 /*int atStatus = 0; */
44
45 extern int dupfd;
46 extern char *outputfile;
47 extern int buildCurrentTable(Baudio *, float);
48 extern void initMicrotonalTable(fTab []);
49
50 static void initPalette();
51 static void freePalette();
52 static void resetAudioThread();
53 static void initMidiVoices();
54
55 void mapVelocityCurve(int, float []);
56
57 void llgain(float *, int, float);
58
59 /*Baudio *baudio; */
60
61 void
audioThread(audioMain * audiomain)62 audioThread(audioMain *audiomain)
63 {
64 int i, rr, pthreadstatus;
65 float *outbuf, *startbuf;
66 char *device;
67
68 audiomain->atStatus = BRISTOL_WAIT;
69
70 audiomain->debuglevel = 0;
71 #ifdef DEBUG
72 audiomain->debuglevel = -1;
73 #endif
74
75 if (outputfile != NULL)
76 {
77 if ((dupfd = open(outputfile, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0)
78 printf("failed to open duplicate output file %s\n", outputfile);
79 }
80
81 if (audiomain->audiodev != (char *) NULL)
82 device = audiomain->audiodev;
83 else if ((audiomain->flags & BRISTOL_AUDIOMASK) == BRISTOL_OSS)
84 device = bOAD;
85 #if (BRISTOL_HAS_ALSA == 1)
86 else
87 device = bAAD;
88 #endif
89
90 audiomain->opCount = BRISTOL_SYNTHCOUNT;
91
92 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
93 printf("starting audio thread\n");
94
95 /*
96 * Due to the nature of the jack interface, we need to have this file be
97 * minimally jack aware. The rest of the code takes an audio device, does
98 * its own read and write calls, then dispatches to doAudioOps(). This is
99 * not the same with jack, we register a callback and let it do the work
100 * for us.
101 */
102
103 if (audiomain->flags & BRISTOL_PULSE)
104 {
105 #ifdef BRISTOL_PA
106 while (audiomain->atReq != BRISTOL_REQSTOP)
107 {
108 /*
109 * This will not return except when problem arise with the jack
110 * interface. The returning code will decide whether to flag a
111 * reqstop to exit
112 */
113 bristolPulseInterface(audiomain);
114
115 audiomain->atStatus = BRISTOL_EXIT;
116
117 printf("pulse audio interface returned\n");
118 #ifdef MONOLITHIC
119 pthread_exit(&pthreadstatus);
120 #else
121 _exit(0);
122 #endif
123 }
124
125 return;
126 #else
127 printf("pulse requested but not compiled with engine\n");
128
129 audiomain->atStatus = BRISTOL_EXIT;
130
131 _exit(0);
132 #endif /* BRISTOL_PA */
133 }
134
135 if (audiomain->flags & BRISTOL_JACK)
136 {
137 #ifdef _BRISTOL_JACK
138 while (audiomain->atReq != BRISTOL_REQSTOP)
139 {
140 /*
141 * This will not return except when problem arise with the jack
142 * interface. The returning code will decide whether to flag a
143 * reqstop to exit
144 */
145 bristolJackInterface(audiomain);
146
147 audiomain->atStatus = BRISTOL_EXIT;
148
149 printf("jack audio interface returned\n");
150
151 _exit(0);
152 }
153
154 return;
155 #else
156 printf("jack requested but not compiled with engine\n");
157
158 audiomain->atStatus = BRISTOL_EXIT;
159
160 _exit(0);
161 #endif /* _BRISTOL_JACK */
162 }
163
164 /*
165 * open the audio device with our desired buffer size, and take its
166 * returned fragment size.
167 */
168 if ((audiomain->iosize = bristolAudioOpen(device, audiomain->samplerate,
169 audiomain->samplecount, audiomain->flags|audiomain->preload))
170 < 0)
171 {
172 /*
173 * If we have Jack support but have not request jack audio then this
174 * failure is typically due to not giving the -jack option. Chekc for
175 * it and report.
176 */
177 #ifdef _BRISTOL_JACK
178 if (~audiomain->flags & BRISTOL_JACK)
179 {
180 printf("Failed to open audio device %s\n", device);
181 printf("If jack is running then use 'startBristol -jack'\n");
182 } else
183 #endif
184 printf("Problem opening audio device %s, exiting audio thread\n",
185 device);
186 audiomain->atStatus = BRISTOL_FAIL;
187 pthread_exit(&pthreadstatus);
188 }
189
190 /*
191 * This fragment size is in bytes. The audio will be stereo shorts, so
192 * find out how many samples this is:
193 * samplecount/2
194 * Our samples are going to be floats, and Mono until the later stages?
195 */
196 audiomain->segmentsize = audiomain->samplecount * sizeof(float);
197
198 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
199 printf("segmentsize is %i\n", audiomain->segmentsize);
200
201 /*
202 * A segment is enough for the given number of mono samples in float res.
203 * Our IO buffers deal with stereo, so we need twice as many.
204 *
205 * This should be samplecount * periodsize bytes.
206 */
207 outbuf = (float *) bristolmalloc(audiomain->segmentsize * 2);
208
209 /* This will give 2 deinterleaved sample streams. */
210 startbuf = (float *) bristolmalloc(audiomain->segmentsize * 2);
211
212 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
213 printf("IO %i, samples %i, segsize %i\n",
214 audiomain->iosize, audiomain->samplecount, audiomain->segmentsize);
215
216 initAudioThread(audiomain);
217
218 bristolbzero(outbuf, audiomain->segmentsize * 2);
219
220 /*
221 * Preload the output
222 */
223 for (i = 0; i < audiomain->preload; i++)
224 bristolAudioWrite(outbuf, audiomain->samplecount);
225
226 bristolAudioStart();
227
228 audiomain->atStatus = BRISTOL_OK;
229
230 while (audiomain->atReq != BRISTOL_REQSTOP)
231 {
232 /*
233 * We should call here to operate the voices. Suggest we pass a single
234 * destination buffer, which we will write, on return, to the audio
235 * device.
236 */
237 if (bristolAudioRead(startbuf, audiomain->samplecount) < 0)
238 {
239 printf("Audio device read issue\n");
240
241 #ifdef _BRISTOL_DRAIN
242 /*
243 * This code avoids reopening of the audio interface which was
244 * pretty ugly.
245 */
246 bristolAudioStart();
247 // for (i = 1; i < audiomain->preload; i++)
248 // bristolAudioWrite(outbuf, audiomain->samplecount);
249 bristolAudioRead(startbuf, audiomain->samplecount);
250 #else
251 bristolAudioClose();
252 if (bristolAudioOpen(device, audiomain->samplerate,
253 audiomain->samplecount,
254 audiomain->flags|audiomain->preload) < 0)
255 /*
256 * Then we have an issue.
257 */
258 audiomain->atReq = BRISTOL_REQSTOP;
259 else
260 /*
261 * Preload the output
262 */
263 for (i = 0; i < audiomain->preload; i++)
264 bristolAudioWrite(outbuf, audiomain->samplecount);
265
266 bristolAudioStart();
267 bristolAudioRead(startbuf, audiomain->samplecount);
268 #endif
269 }
270
271 /*
272 * The startbuf is actually interleaved at the moment, something which
273 * needs to be changed.
274 */
275 if (audiomain->ingain > 1)
276 llgain(startbuf, audiomain->samplecount, audiomain->ingain);
277
278 /*
279 * doAudioOps should not use baudio, these will become a linked list
280 */
281 doAudioOps(audiomain, outbuf, startbuf);
282
283 if (audiomain->outgain > 1)
284 llgain(outbuf, audiomain->samplecount, audiomain->outgain);
285
286 if ((rr = bristolAudioWrite(outbuf, audiomain->samplecount)) < 0)
287 {
288 if (rr != -4)
289 {
290 printf("Audio device write issue: restart pl %i\n",
291 audiomain->preload);
292 #ifdef _BRISTOL_DRAIN
293 /*
294 * This code avoids reopening of the audio interface which was
295 * pretty ugly.
296 */
297 bristolAudioStart();
298 // for (i = 1; i < audiomain->preload; i++)
299 // bristolAudioWrite(outbuf, audiomain->samplecount);
300 #else
301 bristolAudioClose();
302 if (bristolAudioOpen(device, audiomain->samplerate,
303 audiomain->samplecount,
304 audiomain->flags|audiomain->preload) < 0)
305 /*
306 * Then we have an issue.
307 */
308 audiomain->atReq = BRISTOL_REQSTOP;
309 else
310 /*
311 * Preload the output
312 */
313 for (i = 0; i < audiomain->preload; i++)
314 bristolAudioWrite(outbuf, audiomain->samplecount);
315
316 bristolAudioStart();
317 #endif
318 }
319 }
320 }
321
322 bristolfree(outbuf);
323 bristolfree(startbuf);
324
325 resetAudioThread(audiomain);
326
327 bristolAudioClose();
328
329 printf("audioThread exiting\n");
330
331 audiomain->atStatus = BRISTOL_EXIT;
332 pthread_exit(&pthreadstatus);
333 }
334
335 /*
336 * This assumes a stereo channel.....
337 */
338 #warning llgain() assumes a stereo channel.
339 void
llgain(register float * buf,register int count,register float gain)340 llgain(register float *buf, register int count, register float gain)
341 {
342 for (;count > 0; count -= 8)
343 {
344 *buf++ *= gain;
345 *buf++ *= gain;
346 *buf++ *= gain;
347 *buf++ *= gain;
348 *buf++ *= gain;
349 *buf++ *= gain;
350 *buf++ *= gain;
351 *buf++ *= gain;
352 *buf++ *= gain;
353 *buf++ *= gain;
354 *buf++ *= gain;
355 *buf++ *= gain;
356 *buf++ *= gain;
357 *buf++ *= gain;
358 *buf++ *= gain;
359 *buf++ *= gain;
360 }
361 }
362
363 extern void doPitchWheel(Baudio *);
364
365 int
bristolMidiController(Baudio * baudio,int NRP,float value)366 bristolMidiController(Baudio *baudio, int NRP, float value)
367 {
368 if (baudio->midiflags & BRISTOL_MIDI_DEBUG2)
369 printf("bristolMidiController(%i, %f)\n", NRP, value);
370
371 /*
372 * We are concerned with the GM RP values for
373 *
374 * PW Range (0)
375 * Fine Tune (1)
376 * Coarse Tune (2)
377 *
378 * Due to the emulation methods where each emulation is actually a different
379 * synth I am not certain what abstraction to use here, it could be per
380 * emulation or globally? Since the messages are associated with a MIDI
381 * channel then I think we should maintain. These messages are sent by the
382 * library to each emulation on a given channel, implying that it should
383 * not be global.
384 *
385 * There should also be an RP for Mod Wheel depth.
386 * Pitch wheel depth is only implemented in notes, not cents.
387 */
388 switch (NRP) {
389 case MIDI_RP_PW:
390 baudio->midi_pitch = value * 16384.0f;
391 doPitchWheel(baudio);
392 alterAllNotes(baudio);
393 return(0);
394 case MIDI_RP_FINETUNE:
395 baudio->gtune = 1.0
396 + (baudio->note_diff - 1)
397 * (value * 2 - 1);
398 buildCurrentTable(baudio, baudio->gtune);
399 alterAllNotes(baudio);
400 return(0);
401 case MIDI_RP_COARSETUNE:
402 baudio->finetune = value * 16383.0f;
403 return(0);
404 case BRISTOL_NRP_VELOCITY:
405 mapVelocityCurve(value * CONTROLLER_RANGE, &baudio->velocitymap[0]);
406 return(0);
407 case BRISTOL_NRP_NULL:
408 return(0);
409 case BRISTOL_NRP_DETUNE:
410 baudio->detune = value;
411 /* We should apply it to the list of voices that has this baudio */
412 return(0);
413 case BRISTOL_NRP_GAIN:
414 baudio->gain = value * 64;
415 return(0);
416 case BRISTOL_NRP_GLIDE:
417 baudio->glidemax = value * 30;
418 return(0);
419 case BRISTOL_NRP_LWF:
420 if (baudio->midiflags & BRISTOL_MIDI_DEBUG2)
421 printf("LWF requested as %i\n", (int) (value * 16384.0f));
422 baudio->mixflags &= ~(BRISTOL_LW_FILTER|BRISTOL_LW_FILTER2);
423 switch ((int) (value * 16384.0f)) {
424 case 0: /* was heavyweight, now medium */
425 break;
426 case 1:
427 /* Chamberlains - not changed */
428 baudio->mixflags |= BRISTOL_LW_FILTER;
429 break;
430 case 2:
431 /* -lwf2 */
432 baudio->mixflags |= BRISTOL_LW_FILTER2;
433 break;
434 case 3:
435 /* Now -hwf heavyweight */
436 baudio->mixflags |= (BRISTOL_LW_FILTER|BRISTOL_LW_FILTER2);
437 break;
438 }
439 return(0);
440 case BRISTOL_NRP_MNL_PREF:
441 baudio->notemap.flags &= ~(BRISTOL_MNL_LNP|BRISTOL_MNL_HNP);
442 if ((int) (value * CONTROLLER_RANGE) == 1)
443 baudio->notemap.flags |= BRISTOL_MNL_LNP;
444 else if ((int) (value * CONTROLLER_RANGE) == 2)
445 baudio->notemap.flags |= BRISTOL_MNL_HNP;
446 baudio->notemap.extreme = -1;
447 return(0);
448 case BRISTOL_NRP_MNL_TRIG:
449 if (value != 0)
450 baudio->notemap.flags |= BRISTOL_MNL_TRIG;
451 else
452 baudio->notemap.flags &= ~BRISTOL_MNL_TRIG;
453 return(0);
454 case BRISTOL_NRP_MNL_VELOC:
455 if (value != 0)
456 baudio->notemap.flags |= BRISTOL_MNL_VELOC;
457 else
458 baudio->notemap.flags &= ~BRISTOL_MNL_VELOC;
459 return(0);
460 case BRISTOL_NRP_MIDI_GO:
461 bristolMidiOption(0, BRISTOL_NRP_MIDI_GO,
462 (value > 0)? (int) 1: (int) 0);
463 return(0);
464 case BRISTOL_NRP_FORWARD:
465 bristolMidiOption(0, BRISTOL_NRP_FORWARD,
466 value > 0? (int) 1: (int) 0);
467 return(0);
468 case BRISTOL_NRP_DEBUG:
469 /*
470 * This is a little unfortunte, we do not have direct access to
471 * the midi library structures here so can only enable MIDI debug
472 * for the emulator. This does not have to be an issue, it allows
473 * us to view what each emulation is delivered by midi through the
474 * bristol API.
475 *
476 * 052009 - integrate code to request the MIDI library include byte
477 * debuging of interfaces.
478 */
479 if ((value * C_RANGE_MIN_1) == 0)
480 baudio->midiflags &= ~(BRISTOL_MIDI_DEBUG1|BRISTOL_MIDI_DEBUG2);
481 else {
482 if ((value * C_RANGE_MIN_1) >= 1.99)
483 baudio->midiflags |= BRISTOL_MIDI_DEBUG2;
484 else
485 baudio->midiflags |= BRISTOL_MIDI_DEBUG1;
486 }
487
488 bristolMidiOption(0, BRISTOL_NRP_DEBUG,
489 ((int) (value * C_RANGE_MIN_1)));
490
491 return(0);
492 case BRISTOL_NRP_ENABLE_NRP:
493 if (value != 0)
494 baudio->midiflags |= BRISTOL_MIDI_NRP_ENABLE;
495 else
496 baudio->midiflags &= ~BRISTOL_MIDI_NRP_ENABLE;
497 return(0);
498 case BRISTOL_NRP_REQ_SYSEX:
499 printf(" REQ SYSEX %f\n", value);
500 return(0);
501 }
502
503 /*
504 * This should also consider indexing this NRP value into an arbitrary
505 * engine control? This is non-trivial since there is not a table to
506 * link them together to would have to parse the complete parameter tables
507 * that go to each operator.
508 *
509 * It would make more sense (ie, be easier) to take the two GM2 integer
510 * parameters and break the Data Entry into MSB/LSB and map them to
511 * operator/controller and then pass them the value.
512 */
513
514 return(0);
515 }
516
517 /*
518 * This could also go into the library so the engine and GUI use the same code?
519 * It might have been possible other than that the GUI code works with integer
520 * values (mappings the actual controller indeces) and this code is for the
521 * float curves in the engine, will leave that FFS.
522 *
523 * We want to go through the midi controller mapping file for this synth and
524 * search for directives for value maps. The names are taken from the midi
525 * header file and we want to add a few others for preconfigured value tables.
526 */
527 static void
invertMap(float map[128])528 invertMap(float map[128])
529 {
530 int j;
531
532 for (j = 0; j < 128; j++)
533 map[j] = (1.0 - map[j]);
534 }
535
536 /*
537 static void
538 linearMap(float map[128])
539 {
540 int j;
541
542 for (j = 0; j < 128; j++)
543 map[j] = ((float) j) / 127.0;
544 }
545 */
546
547 /*
548 * Takes a parameter for a start point, plus a ramp rate that is greater
549 * than linear to build tables that map to different playing styles. These
550 * should be extended to include curves as well.
551 * Each parameter takes 8 value.
552 */
553 static void
nonSensitiveMap(float map[128],int start,int end)554 nonSensitiveMap(float map[128], int start, int end)
555 {
556 int j;
557
558 for (j = 0; j < 128; j++)
559 map[j] = 0.71f;
560 }
561
562 static void
linearScaledMap(float map[128],int start,int end)563 linearScaledMap(float map[128], int start, int end)
564 {
565 int j;
566 float inc;
567
568 /* Start can be up to halfway through existing scale */
569 start = start * 64 / 9;
570 end = end * 64 / 9 + 64;
571
572 inc = 1.0 / ((float) (end - start));
573
574 for (j = 0; j < 128; j++)
575 {
576 if (j > start) {
577 if ((map[j] = ((float) (j - start)) * inc) > 1.0)
578 map[j] = 1.0;
579 } else
580 map[j] = 0.0;
581 }
582 }
583
584 static void
logMap(float map[128])585 logMap(float map[128])
586 {
587 int i;
588 float scaler = 127 / logf(128.0);
589
590 /*
591 * We are going to take a logarithmic scale such that values go from 0 to
592 * 127
593 */
594 for (i = 1; i < 129; i++)
595 map[i - 1] = (logf((float) i) * scaler) / 127.0;
596 }
597
598 static void
exponentialMap(float map[128],float power)599 exponentialMap(float map[128], float power)
600 {
601 int i;
602 float scaler = 1 / powf(127, power);
603
604 /*
605 * We are going to take exponential scale
606 */
607 for (i = 0; i < 128; i++)
608 map[i] = powf((float) i, power) * scaler;
609 }
610
611 static void
parabolaMap(float map[128])612 parabolaMap(float map[128])
613 {
614 int i;
615 float scaler = 1.0 / (64.0 * 64.0);
616
617 /*
618 * We are going to take a parabolic scale. Not sure if it will be use....
619 */
620 for (i = 0; i < 128; i++)
621 map[i] = (float) (((float) ((i - 64) * (i - 64))) * scaler);
622 }
623
624 #define V_COUNT 500
625 #define R_COUNT 1000
626 void
mapVelocityCurve(int velocity,float map[128])627 mapVelocityCurve(int velocity, float map[128])
628 {
629 int v = velocity, r = 0;
630
631 if (velocity > R_COUNT) {
632 v = velocity - R_COUNT;
633 r = 1;
634 }
635
636 /*
637 * The first 100 are linear scaled maps. The second 100 should be
638 * exponentially scaled ones?
639 */
640 if (v == 0) {
641 nonSensitiveMap(map, 0, 127);
642 } else if (v < 100) {
643 linearScaledMap(map, v / 10, v % 10);
644 } else if (v < 200) {
645 /*
646 * This are exponential maps from powers of 0.01 up to 1.0 (linear) in
647 * 100 steps.
648 */
649 exponentialMap(map, ((float) (v - 100)) / 100.0);
650 } else if (v < V_COUNT) {
651 /*
652 * This are exponentials from ~linear up to quadratic in steps of
653 * 100th's.
654 */
655 exponentialMap(map, ((float) (v - 100)) / 100.0);
656 } else {
657 switch (v) {
658 case V_COUNT + 0: exponentialMap(map, 0.05); break; /* Soft touch */
659 case V_COUNT + 1: exponentialMap(map, 0.1); break;
660 case V_COUNT + 2: exponentialMap(map, 0.2); break;
661 case V_COUNT + 3: exponentialMap(map, 0.3); break;
662 case V_COUNT + 4: exponentialMap(map, 0.4); break;
663 case V_COUNT + 5: exponentialMap(map, 0.5); break;
664 case V_COUNT + 6: exponentialMap(map, 0.6); break;
665 case V_COUNT + 7: exponentialMap(map, 0.7); break;
666 case V_COUNT + 8: exponentialMap(map, 0.8); break;
667 case V_COUNT + 9: exponentialMap(map, 0.9); break;
668 default:
669 case V_COUNT + 10: exponentialMap(map, 1.0); break; /* Linear */
670 case V_COUNT + 11: exponentialMap(map, 1.1); break;
671 case V_COUNT + 12: exponentialMap(map, 1.2); break;
672 case V_COUNT + 13: exponentialMap(map, 1.3); break;
673 case V_COUNT + 14: exponentialMap(map, 1.4); break;
674 case V_COUNT + 15: exponentialMap(map, 1.5); break;
675 case V_COUNT + 16: exponentialMap(map, 1.6); break;
676 case V_COUNT + 17: exponentialMap(map, 1.7); break;
677 case V_COUNT + 18: exponentialMap(map, 1.8); break;
678 case V_COUNT + 19: exponentialMap(map, 1.9); break;
679 case V_COUNT + 20: exponentialMap(map, 2.0); break;
680 case V_COUNT + 21: exponentialMap(map, 2.2); break;
681 case V_COUNT + 22: exponentialMap(map, 2.4); break;
682 case V_COUNT + 23: exponentialMap(map, 2.6); break;
683 case V_COUNT + 24: exponentialMap(map, 2.8); break;
684 case V_COUNT + 25: exponentialMap(map, 3.0); break; /* Hard touch */
685
686 case V_COUNT + 50: logMap(map); break;
687 case V_COUNT + 60: parabolaMap(map); break;
688 }
689 }
690
691 if (r) invertMap(map);
692 }
693
694 /*
695 * This was initiallly a call made in the MIDI thread. That naturally led to
696 * problems with timing and was eventually moved as a request from the MIDI
697 * thread to the audio thread.
698 */
699 void
initBristolAudio(audioMain * audiomain,Baudio * baudio)700 initBristolAudio(audioMain *audiomain, Baudio *baudio)
701 {
702 int i, j;
703 bristolOP *op;
704 char name[256];
705 float tmap[128];
706
707 if (baudio == NULL)
708 return;
709
710 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
711 printf("initBristolAudio()\n");
712
713 if (audiomain->atStatus != BRISTOL_OK)
714 return;
715
716 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG3)
717 printf("using baudio: %p->%p\n", audiomain->audiolist,
718 baudio->next);
719
720 baudio->leftbuf = (float *) bristolmalloc0(audiomain->segmentsize);
721 baudio->rightbuf = (float *) bristolmalloc0(audiomain->segmentsize);
722
723 baudio->glidemax = 5;
724
725 /*
726 * These will come from some configuration file, eventually.
727 */
728 baudio->note_diff = pow((double) 2, (double) 1/12);
729 baudio->gain_diff = pow((double) 13, ((double) 1)/DEF_TAB_SIZE);
730 /* Pitchwheel defaults to 2 semitones although some synths override it. */
731 baudio->midi_pitch = 2;
732 baudio->midi_pitchfreq = 0.0;
733 baudio->gtune = 1.0;
734 baudio->glide = 0.0;
735 buildCurrentTable(baudio, 1.0);
736
737 baudio->contcontroller[1] = 0.5;
738 baudio->pitchwheel = 0.0;
739
740 /*
741 * This should call an init routine for any given baudio. If it is not
742 * initialised then we have an issue and should really return a negative
743 * response.
744 */
745 if (bristolAlgos[(audiomain->midiflags & BRISTOL_PARAMMASK)].initialise)
746 bristolAlgos[(audiomain->midiflags & BRISTOL_PARAMMASK)].initialise
747 (audiomain, baudio);
748
749 /*
750 * All of this could go into the midithread?
751 */
752 if (bristolAlgos[(audiomain->midiflags & BRISTOL_PARAMMASK)].name != NULL)
753 {
754 char file[256];
755 float velocity;
756 int vint;
757
758 /*
759 * Get hold of any local configuration, microtunings, etc.
760 * I would like these to come from the profile for that synth. We
761 * should first try a local file, then a general one?
762 */
763 sprintf(file, "%s.mcm", bristolAlgos[
764 (audiomain->midiflags & BRISTOL_PARAMMASK)].name);
765
766 bristolGetFreqMap(file, "microTonalMap",
767 baudio->microtonalmap, 128, 0, audiomain->samplerate);
768
769 /* initMicrotonalTable(&baudio->microtonalmap[0]); */
770
771 if (baudio->microtonalmap[0].step == baudio->microtonalmap[127].step)
772 {
773 /*
774 * We could consider promoting the audiomain global mapping at
775 * this point? The main issue is that we will probably eventually
776 * have to support at least per key remapping so having a private
777 * map will be a requirement.
778 */
779 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG4)
780 printf("no private microtonal mapping for %s\n",
781 bristolAlgos[(audiomain->midiflags & BRISTOL_PARAMMASK)].name);
782 initMicrotonalTable(&baudio->microtonalmap[0]);
783 } else
784 printf("micro first %f last %f (%s)\n",
785 baudio->microtonalmap[0].step, baudio->microtonalmap[127].step,
786 bristolAlgos[(audiomain->midiflags & BRISTOL_PARAMMASK)].name);
787
788 /*
789 * See if we have been requested a specific velocity curve. This
790 * could be moved into the library.....
791 */
792 if (bristolGetMap(file, "velocityTable", &velocity, 1, 0) != 0)
793 {
794 vint = (int) velocity;
795 mapVelocityCurve(vint, &baudio->velocitymap[0]);
796 } else if (bristolGetMap(file, "velocityMap",
797 &baudio->velocitymap[0], 128, 0) == 0)
798 /*
799 * If we do not have some mapping we should default to a linear
800 * map
801 */
802 mapVelocityCurve(510, &baudio->velocitymap[0]);
803
804 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG4)
805 printf("veloc first %1.2f last %1.2f\n",
806 baudio->velocitymap[0], baudio->velocitymap[127]);
807 }
808
809 /*
810 * Put in a locals array pointer. This is two dimensional, since the locals
811 * are in the audio structure, but we need a copy for each individual voice.
812 *
813 * The net result is that locals at the baudio level is a pointer to an
814 * array of pointers to arrays pointing to chars.
815 */
816 baudio->locals = (char ***)
817 bristolmalloc0(sizeof(void *) * BRISTOL_MAXVOICECOUNT);
818 baudio->FXlocals = (char ***)
819 bristolmalloc0(sizeof(void *) * BRISTOL_MAXVOICECOUNT);
820
821 for (i = 0; i < audiomain->voiceCount; i++)
822 {
823 baudio->locals[i] =
824 (void *) bristolmalloc0(baudio->soundCount * sizeof(void *));
825
826 baudio->FXlocals[i] =
827 (void *) bristolmalloc0(baudio->soundCount * sizeof(void *));
828
829 if (audiomain->effects == NULL) {
830 int i = 5;
831
832 printf("Null audio FX list\n");
833
834 while (audiomain->effects == NULL) {
835 sleep(1);
836 if (--i == 0)
837 {
838 printf("Null audio FX list unrecovered audio thread\n");
839 exit(0);
840 }
841 }
842
843 printf("recovered\n");
844 }
845
846 if ((baudio->effect != NULL) && (baudio->effect[0] != NULL)
847 && (baudio->soundCount != 0))
848 {
849 op = (audiomain->effects)[(*baudio->effect[0]).index];
850 if (op && op->specs)
851 baudio->FXlocals[i][0]
852 = (char *) bristolmalloc0(op->specs->localsize);
853 else
854 baudio->FXlocals[i][0] = (char *) bristolmalloc0(256);
855 }
856
857 if ((baudio->soundCount > 1)
858 && (baudio->effect != NULL) && (baudio->effect[1] != NULL))
859 {
860 op = (audiomain->effects)[(*baudio->effect[1]).index];
861 baudio->FXlocals[i][1]
862 = (char *) bristolmalloc0(op->specs->localsize);
863 }
864
865 /*
866 * Put in the locals.
867 */
868 for (j = 0; j < baudio->soundCount; j++)
869 {
870 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
871 printf("Adding new audio operator: %i\n", j);
872
873 op = (audiomain->palette)[(*baudio->sound[j]).index];
874 baudio->locals[i][j]
875 = (char *) bristolmalloc0(op->specs->localsize);
876
877 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG4)
878 printf("malloc locals %p %i\n", baudio->locals[i][j],
879 op->specs->localsize);
880 }
881 }
882
883 /*
884 * We give a NULL name in the engine: these are all GUI mappings but the
885 * call will init the tables to defaults (linear).
886 */
887 bristolMidiValueMappingTable(baudio->valuemap, baudio->midimap, "null");
888 // bristolAlgos[(audiomain->midiflags & BRISTOL_PARAMMASK)].name);
889 /* We should do a getmap for the modwheel */
890 sprintf(name, "%s.mcm",
891 bristolAlgos[(audiomain->midiflags & BRISTOL_PARAMMASK)].name);
892
893 if (bristolGetMap(name, "modWheel", tmap, 128, 0) > 0)
894 {
895 for (i = 0; i < 128; i++)
896 baudio->valuemap[1][i] = (int) tmap[i];
897 }
898
899 baudio->midi = bristolMidiController;
900 baudio->mixflags &= ~BRISTOL_HOLDDOWN;
901 baudio->lowkey = 0;
902 baudio->highkey = 127;
903 audiomain->flags &= ~BRISTOL_AUDIOWAIT;
904 }
905
906 void
initAudioThread(audioMain * audiomain)907 initAudioThread(audioMain *audiomain)
908 {
909 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
910 printf("initAudioThread(%i)\n", audiomain->opCount);
911
912 /*
913 * Assign an array of operator pointers.
914 */
915 audiomain->palette = (bristolOP **)
916 bristolmalloc0(sizeof(bristolOP *) * audiomain->opCount);
917
918 /*
919 * Now that we have some basic stuctures we should start initiating the
920 * operators. This is done one by one, for the
921 *
922 * DCO/DCF/DCA/ADSR/NOISE/LFO/FX/OTHERS
923 *
924 * For extensibility there should be some nice way to configure the list
925 * of operators that will be placed in our palette.
926 */
927 initPalette(audiomain, audiomain->palette);
928
929 /*
930 * The effect chain is identical to the palette - they can be used for the
931 * same purpose. The difference is that when they are used as effects, they
932 * will appear on a different list in a bristolAudio structure.
933 */
934 audiomain->effects = audiomain->palette;
935
936 /*
937 * Assign an array of voice pointers. Need to call "initMidiVoices()"
938 */
939 initMidiVoices(audiomain);
940 }
941
942 void
freeAudioMain(audioMain * audiomain)943 freeAudioMain(audioMain *audiomain)
944 {
945 printf("freeAudioMain()\n");
946
947 freePalette(audiomain, audiomain->palette);
948 /* freePalette(audiomain, audiomain->effects); */
949
950 if (audiomain->palette != (bristolOP **) NULL)
951 bristolfree(audiomain->palette);
952 /* if (audiomain->effects != (bristolOP **) NULL) */
953 /* bristolfree(audiomain->effects); */
954 }
955
956 void
freeBristolAudio(audioMain * audiomain,Baudio * baudio)957 freeBristolAudio(audioMain *audiomain, Baudio *baudio)
958 {
959 bristolVoice *voice = audiomain->freelist;
960
961 if (baudio == (Baudio *) NULL)
962 return;
963
964 baudio->mixflags |= BRISTOL_HOLDDOWN;
965
966 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
967 printf("freeBristolAudio(%p, %p)\n", audiomain, baudio);
968
969 freeSoundAlgo(baudio, 0, baudio->sound);
970 bristolfree(baudio->sound);
971
972 if (baudio->effect != NULL)
973 {
974 freeSoundAlgo(baudio, 0, baudio->effect);
975 bristolfree(baudio->effect);
976 baudio->effect = NULL;
977 }
978
979 if (baudio->locals != NULL)
980 {
981 int i, j = 0;
982
983 for (i = 0; i < audiomain->voiceCount; i++)
984 {
985 /*
986 * Free the inividual locals.
987 */
988 if (baudio->FXlocals[i][0] == NULL)
989 continue;
990
991 for (j = 0; j < baudio->soundCount; j++) {
992 bristolfree(baudio->locals[i][j]);
993 bristolfree(baudio->FXlocals[i][j]);
994 }
995
996 /*
997 * Free the pointer for this voice.
998 */
999 bristolfree(baudio->locals[i]);
1000 bristolfree(baudio->FXlocals[i]);
1001 }
1002 }
1003
1004 bristolfree(baudio->leftbuf);
1005 bristolfree(baudio->rightbuf);
1006
1007 /*
1008 * Free the locals pointer itself.
1009 */
1010 bristolfree(baudio->locals);
1011 bristolfree(baudio->FXlocals);
1012
1013 if (baudio->next != NULL)
1014 baudio->next->last = baudio->last;
1015
1016 if (baudio->last != NULL)
1017 baudio->last->next = baudio->next;
1018 else
1019 audiomain->audiolist = baudio->next;
1020
1021 while (voice != NULL)
1022 {
1023 if (voice->baudio != NULL)
1024 {
1025 if (voice->baudio->controlid == baudio->controlid)
1026 {
1027 voice->flags |= BRISTOL_KEYDONE;
1028 voice->baudio = NULL;
1029 }
1030 } else
1031 voice->flags |= BRISTOL_KEYDONE;
1032
1033 voice = voice->next;
1034 }
1035
1036 if (baudio->destroy != NULL)
1037 baudio->destroy(audiomain, baudio);
1038
1039 if (baudio->mixlocals != NULL)
1040 bristolfree(baudio->mixlocals);
1041
1042 /*
1043 * This can only be freed once exit handling has been done by the audio
1044 * engine and MIDI event management code, refer to audioEngine.c and
1045 * bristolsystem.c
1046 bristolfree(baudio);
1047 */
1048 }
1049
1050 void
freeMidiVoices(audioMain * audiomain,bristolVoice * voice)1051 freeMidiVoices(audioMain *audiomain, bristolVoice *voice)
1052 {
1053 /* if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1) */
1054 /* printf("freeMidiVoices(%x, %x)\n", audiomain, voice); */
1055
1056 if (voice == NULL)
1057 return;
1058
1059 freeMidiVoices(audiomain, voice->next);
1060
1061 audiomain->playlist = NULL;
1062 audiomain->playlast = NULL;
1063 audiomain->freelist = NULL;
1064 audiomain->freelast = NULL;
1065 audiomain->newlist = NULL;
1066 }
1067
1068 static void
resetAudioThread(audioMain * audiomain)1069 resetAudioThread(audioMain *audiomain)
1070 {
1071 Baudio *holder;
1072
1073 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
1074 printf("resetAudioThread()\n");
1075
1076 while (audiomain->audiolist != NULL) {
1077 holder = audiomain->audiolist;
1078 freeBristolAudio(audiomain, audiomain->audiolist);
1079 bristolfree(holder);
1080 }
1081
1082 /*
1083 * Free the array of voice pointers.
1084 */
1085 freeMidiVoices(audiomain, audiomain->playlist);
1086 }
1087
1088 static void
initPalette(audioMain * audiomain,bristolOP * palette[])1089 initPalette(audioMain *audiomain, bristolOP *palette[])
1090 {
1091 int i;
1092
1093 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
1094 printf("initPalette()\n");
1095
1096 /*
1097 * Go through the palette, and initialise all our operators.
1098 */
1099 for (i = 0; i < BRISTOL_SYNTHCOUNT; i++)
1100 {
1101 if (bristolPalette[i].initialise == NULL)
1102 continue;
1103
1104 palette[i] = bristolPalette[i].initialise
1105 (&palette[i], i, audiomain->samplerate,
1106 audiomain->samplecount);
1107
1108 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG3)
1109 printf("Assigned operator %s to %i at %p\n",
1110 palette[i]->specs->opname, i, palette[i]);
1111 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG4)
1112 bristolOPprint(palette[i]);
1113 }
1114 audiomain->opCount = i - 1;
1115 }
1116
1117 static void
freePalette(audioMain * audiomain,bristolOP * palette[])1118 freePalette(audioMain *audiomain, bristolOP *palette[])
1119 {
1120 int i;
1121
1122 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
1123 printf("freePalette()\n");
1124
1125 /*
1126 * This needs to be sorted out when we move on to operators other than the
1127 * DCO!
1128 */
1129 for (i = 0; i < audiomain->opCount; i++)
1130 {
1131 if (palette[i] != (bristolOP *) NULL)
1132 {
1133 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG3)
1134 printf("Removed operator %s from %i at %p\n",
1135 palette[i]->specs->opname, i, palette[i]);
1136 //printf("Removed operator %s from %i at %x\n",
1137 //palette[i]->specs->opname, i, (size_t) palette[i]);
1138
1139 palette[i]->destroy(palette[i]);
1140
1141 palette[i] = (bristolOP *) NULL;
1142 }
1143 }
1144 }
1145
1146 static void
initMidiVoices(audioMain * audiomain)1147 initMidiVoices(audioMain *audiomain)
1148 {
1149 int i;
1150 bristolVoice *voice;
1151
1152 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
1153 printf("initMidiVoices(%p, %i)\n", audiomain, audiomain->voiceCount);
1154
1155 audiomain->playlist = NULL;
1156 audiomain->playlast = NULL;
1157 audiomain->freelist = NULL;
1158 audiomain->freelast = NULL;
1159 audiomain->newlist = NULL;
1160
1161 /*
1162 * Create the voice structures, put them on the playlist with some default
1163 * flags, etc.
1164 */
1165 for (i = 0; i < audiomain->voiceCount; i++)
1166 {
1167 if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG1)
1168 printf("Adding new MIDI Voice: %i\n", i);
1169
1170 voice = (bristolVoice *) bristolmalloc0(sizeof(bristolVoice));
1171
1172 voice->flags = BRISTOL_KEYDONE;
1173 voice->last = NULL;
1174 voice->next = audiomain->freelist;
1175
1176 /*
1177 * Chain it into the voice list.
1178 */
1179 voice->next = audiomain->freelist;
1180 voice->last = NULL;
1181 if (audiomain->freelist == NULL)
1182 audiomain->freelast = voice;
1183 else
1184 audiomain->freelist->last = voice;
1185 audiomain->freelist = voice;
1186
1187 voice->index = i;
1188 }
1189 }
1190
1191 /*
1192 * Change the frequencies of all active notes. Called when global tuning is
1193 * changed.
1194 */
1195 extern bristolMidiHandler bristolMidiRoutines;
1196
1197 void
doNoteChanges(bristolVoice * voice)1198 doNoteChanges(bristolVoice *voice)
1199 {
1200 if (~voice->flags & BRISTOL_KEYDONE)
1201 {
1202 /*
1203 * First make sure we have no microtonal map.
1204 */
1205 if (voice->baudio->microtonalmap[voice->key.key].step > 0.0) {
1206 voice->dFreq = voice->baudio->microtonalmap[voice->key.key].step;
1207 voice->dfreq = voice->baudio->microtonalmap[voice->key.key].freq;
1208 } else if (bristolMidiRoutines.bmr[7].floatmap[voice->key.key] > 0.0) {
1209 /*
1210 * This is the synth global microtonal map
1211 */
1212 voice->dFreq = bristolMidiRoutines.freq[voice->key.key].step;
1213 voice->dfreq = bristolMidiRoutines.freq[voice->key.key].freq;
1214 } else {
1215 voice->dFreq = voice->baudio->ctab[voice->key.key].step;
1216 voice->dfreq = voice->baudio->ctab[voice->key.key].freq;
1217 }
1218
1219 if (voice->detune != 0.0) {
1220 voice->dFreq *= voice->detune;
1221 voice->dfreq *= voice->detune;
1222 }
1223 }
1224 }
1225
1226