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 /* Korg mono poly agorithm */
22 /*#define DEBUG */
23 
24 #include "bristol.h"
25 #include "bristolmm.h"
26 #include "bristolpoly.h"
27 
28 #define CONTROL_MG1 1
29 /*
30  * This should be zero, we might change it.
31  */
32 #define CONTROL_BEND 2
33 
34 #define DEST_FLT 0
35 #define DEST_PITCH 1
36 #define DEST_VCO 2
37 
38 /*
39  * Buffer requirements.
40  */
41 static float *freqbuf = (float *) NULL;
42 static float *scratchbuf = (float *) NULL;
43 static float *osc0buf = (float *) NULL;
44 static float *osc1buf = (float *) NULL;
45 static float *osc2buf = (float *) NULL;
46 static float *osc3buf = (float *) NULL;
47 static float *adsrbuf = (float *) NULL;
48 static float *mg1buf = (float *) NULL;
49 static float *mg2buf = (float *) NULL;
50 
51 int
polyController(Baudio * baudio,u_char operator,u_char controller,float value)52 polyController(Baudio *baudio, u_char operator, u_char controller, float value)
53 {
54 	int ivalue = value * CONTROLLER_RANGE;
55 	pmods *mods = (pmods *) baudio->mixlocals;
56 
57 #ifdef DEBUG
58 	printf("bristolPolyControl(%i, %i, %f)\n", operator, controller, value);
59 #endif
60 
61 	if (operator != 126)
62 		return(0);
63 
64 	switch (controller) {
65 		case 0:
66 			baudio->glide = value * value * baudio->glidemax;
67 			break;
68 		case 1:
69 			break;
70 		case 2: /* Global tune */
71 			baudio->gtune = 1.0
72 				+ (baudio->note_diff - 1)
73 				* (value * 2 - 1);
74 
75 			buildCurrentTable(baudio, baudio->gtune);
76 			/*
77 			 * We need to do this as a part of the synth, the usual call to
78 			 * alterAllNotes(baudio) would not work here.
79 			 */
80 			mods->keydata[0].oFreq
81 				= mods->keydata[0].cFreq
82 				= mods->keydata[0].dFreq
83 				= baudio->ctab[mods->keydata[0].key].step - mods->detune;
84 			mods->keydata[1].oFreq
85 				= mods->keydata[1].cFreq
86 				= mods->keydata[1].dFreq
87 				= baudio->ctab[mods->keydata[1].key].step + mods->detune;
88 			mods->keydata[2].oFreq
89 				= mods->keydata[2].cFreq
90 				= mods->keydata[2].dFreq
91 				= baudio->ctab[mods->keydata[2].key].step - mods->detune * 2;
92 			mods->keydata[3].oFreq
93 				= mods->keydata[3].cFreq
94 				= mods->keydata[3].dFreq
95 				= baudio->ctab[mods->keydata[3].key].step + mods->detune * 2;
96 			break;
97 		case 3:
98 			baudio->mixflags &= ~0x03;
99 			baudio->mixflags |= ivalue;
100 			break;
101 		case 4: /* PWM source */
102 			baudio->mixflags &= ~PWM_MASK;
103 			switch (ivalue)
104 			{
105 				case 0:
106 					baudio->mixflags |= PWM_MG2;
107 					break;
108 				case 1:
109 					baudio->mixflags |= PWM_MG1;
110 					break;
111 				case 2:
112 					baudio->mixflags |= PWM_ENV;
113 					break;
114 			}
115 			break;
116 		case 5: /* PWM depth */
117 			((pmods *) baudio->mixlocals)->pwmdepth = value;
118 			break;
119 		case 6: /* General mods */
120 			mods->xmod = value;
121 			break;
122 		case 7: /* General mods */
123 			mods->mfreq = value;
124 			break;
125 		case 8: /* General mods */
126 			if (ivalue == 0)
127 				baudio->mixflags |= P_MOD_ENV;
128 			else
129 				baudio->mixflags &= ~P_MOD_ENV;
130 			break;
131 		case 9: /* General mods */
132 			baudio->mixflags &= ~PWM_MASK;
133 			switch (ivalue)
134 			{
135 				case 0:
136 					baudio->mixflags |= P_MOD_XMOD;
137 					baudio->mixflags &= ~P_MOD_SYNC;
138 					break;
139 				case 1:
140 					baudio->mixflags |= P_MOD_XMOD|P_MOD_SYNC;
141 					break;
142 				case 2:
143 					baudio->mixflags &= ~P_MOD_XMOD;
144 					baudio->mixflags |= P_MOD_SYNC;
145 					break;
146 			}
147 			break;
148 		case 10: /* General mods */
149 			if (ivalue == 0)
150 				baudio->mixflags |= P_MOD_SING;
151 			else
152 				baudio->mixflags &= ~P_MOD_SING;
153 			break;
154 		case 11: /* Detune */
155 			mods->detune = value / 4;
156 
157 			mods->keydata[0].dFreq = mods->keydata[0].oFreq - mods->detune;
158 			mods->keydata[1].dFreq = mods->keydata[1].oFreq + mods->detune;
159 			mods->keydata[2].dFreq = mods->keydata[2].oFreq - mods->detune * 2;
160 			mods->keydata[3].dFreq = mods->keydata[3].oFreq + mods->detune * 2;
161 			break;
162 		case 12: /* Trigger */
163 			if (ivalue == 0)
164 				baudio->mixflags &= ~P_MULTI;
165 			else
166 				baudio->mixflags |= P_MULTI;
167 			break;
168 		case 13: /* Damp */
169 			if (ivalue == 0)
170 				baudio->mixflags |= P_DAMP;
171 			else
172 				baudio->mixflags &= ~P_DAMP;
173 			break;
174 		case 14: /* Effects */
175 			if (ivalue == 0)
176 				baudio->mixflags &= ~P_EFFECTS;
177 			else
178 				baudio->mixflags |= P_EFFECTS;
179 			break;
180 		case 15: /* MG1/2 */
181 			((pmods *) baudio->mixlocals)->bendintensity = value;
182 			break;
183 		case 16: /* MG1/2 */
184 			((pmods *) baudio->mixlocals)->benddst = ivalue;
185 			break;
186 		case 17: /* MG1/2 */
187 			((pmods *) baudio->mixlocals)->mg1intensity = value;
188 			break;
189 		case 18: /* MG1/2 */
190 			((pmods *) baudio->mixlocals)->mg1dst = ivalue;
191 			break;
192 		case 19:
193 			/*
194 			 * MONO = Unison request
195 			 */
196 			if (ivalue == 0)
197 				baudio->mixflags &= ~P_UNISON;
198 			else
199 				baudio->mixflags |= P_UNISON;
200 			baudio->mixflags &= ~P_SHARE;
201 			break;
202 		case 20:
203 			/*
204 			 * Poly
205 			 */
206 			baudio->mixflags &= ~P_UNISON;
207 			baudio->mixflags &= ~P_SHARE;
208 			break;
209 		case 21:
210 			/*
211 			 * Share mode
212 			 */
213 			baudio->mixflags &= ~P_UNISON;
214 			baudio->mixflags |= P_SHARE;
215 			break;
216 		case 22:
217 		case 23:
218 		case 24:
219 		case 25:
220 			/*
221 			 * PWM application
222 			 */
223 			if (ivalue == 0)
224 				baudio->mixflags &= ~(P_PWM_1 << (controller - 22));
225 			else
226 				baudio->mixflags |= (P_PWM_1 << (controller - 22));
227 			break;
228 		case 26:
229 			((pmods *) baudio->mixlocals)->arpegtotal =
230 				10 + (1.0 - value) * 300;
231 			break;
232 		case 27:
233 			/*
234 			 * Chord will operate in different modes. There will be MONO chord
235 			 * which will accept 4 notes and hold them, and transpose them
236 			 * for each keypress. In Poly mode will arpeggiate between the
237 			 * notes for cheesey 80's style arpeg and transpose the arpeggiation
238 			 * with new notes. In share mode will just rotate through the notes
239 			 * accepting each onto the next in the sequence.
240 			 */
241 			if (ivalue == 0) {
242 				baudio->mixflags &= ~P_CHORD;
243 				baudio->mixflags |= P_CHORD_OFF;
244 			} else {
245 				baudio->mixflags |= P_CHORD;
246 			}
247 			((pmods *) baudio->mixlocals)->firstchord = -1;
248 			break;
249 		case 28:
250 			/*
251 			 * 2 = up
252 			 * 1 = updown
253 			 * 0 = down
254 			 */
255 			mods->arpegdir = ivalue;
256 			break;
257 		case 30:
258 			mods->osc0gain = value;
259 			break;
260 		case 31:
261 			mods->osc1gain = value;
262 			break;
263 		case 32:
264 			mods->osc2gain = value;
265 			break;
266 		case 33:
267 			mods->osc3gain = value;
268 			break;
269 	}
270 	return(0);
271 }
272 
273 /*
274  * Diverse cases to cater for. We nee to watch out to clear noteoff flags
275  * for restrikes.
276  */
277 static void
polyNoteOn(audioMain * audiomain,Baudio * baudio,bristolVoice * voice)278 polyNoteOn(audioMain *audiomain, Baudio *baudio, bristolVoice *voice)
279 {
280 	pmods *mods = ((pmods *) baudio->mixlocals);
281 	int i = 0;
282 /*printf("key %i on, %x\n", voice->key.key, baudio->mixflags); */
283 
284 	if (baudio->mixflags & P_CHORD)
285 	{
286 		/*
287 		 * Chord will fill the 4 Oscillators, and then transpose all of
288 		 * them according to the keys when further notes are pressed,
289 		 * arpeggiate between them all with/without transpose.
290 		 *
291 		 * Find out how many keys are on
292 		 */
293 		if (mods->keydata[0].key != -1) i++;
294 		if (mods->keydata[1].key != -1) i++;
295 		if (mods->keydata[2].key != -1) i++;
296 		if (mods->keydata[3].key != -1) i++;
297 
298 		/*
299 		 * If this is the first note, keep it for eventual transpose.
300 		 */
301 		if (i == 0) {
302 			mods->firstchord = voice->key.key;
303 		} else if (i == 4) {
304 			int delta;
305 
306 			mods->lastchord = voice->key.key;
307 /*printf("all on %i %i\n", mods->lastchord, voice->key.key); */
308 			/*
309 			 * Do transpose stuff, etc. Three modes of operation:
310 			 *
311 			 * MONO: accept 4 notes and hold them, and transpose them.
312 			 * SHARE: arpeggiate between the notes for cheesey 80's style arpeg.
313 			 * POLY: rotate through the notes accepting each.
314 			 */
315 			if (baudio->mixflags & (P_UNISON|P_SHARE)) {
316 				/*
317 				 * MONO:
318 				 */
319 				delta = voice->key.key - mods->keydata[0].key;
320 				mods->firstchord = voice->key.key;
321 
322 				/*
323 				 * Apply this delta to each osc, this is easy for the first
324 				 * key:
325 				 */
326 				mods->keydata[0].key = voice->key.key;
327 				mods->keydata[0].oFreq = voice->dFreq;
328 				mods->keydata[0].dFreq = voice->dFreq - mods->detune;
329 				mods->keydata[0].cFreq = voice->cFreq;
330 				mods->keydata[0].cFreqstep = voice->cFreqstep;
331 				mods->keydata[0].cFreqmult = voice->cFreqmult;
332 				mods->keydata[0].lastkey = voice->lastkey;
333 
334 				mods->keydata[1].key += delta;
335 				mods->keydata[1].oFreq = voice->dFreq;
336 				mods->keydata[1].dFreq = mods->keydata[1].cFreq
337 					= voice->baudio->ctab[mods->keydata[1].key].step + mods->detune;
338 				mods->keydata[2].key += delta;
339 				mods->keydata[2].oFreq = voice->dFreq;
340 				mods->keydata[2].dFreq = mods->keydata[2].cFreq
341 					= voice->baudio->ctab[mods->keydata[2].key].step
342 						- mods->detune * 2;
343 				mods->keydata[3].key += delta;
344 				mods->keydata[3].oFreq = voice->dFreq;
345 				mods->keydata[3].dFreq = mods->keydata[3].cFreq
346 					= voice->baudio->ctab[mods->keydata[3].key].step
347 						+ mods->detune * 2;
348 
349 				/*
350 				 * KeyOn is managed now by the arpeggiator
351 				 */
352 				if (baudio->mixflags & P_SHARE)
353 				{
354 					if (baudio->mixflags & P_REON)
355 					{
356 						baudio->mixflags &= ~P_REON;
357 						voice->flags &= ~BRISTOL_KEYREON;
358 						voice->flags |= BRISTOL_KEYON;
359 						mods->arpegcount = 0;
360 						mods->arpegstep = 0;
361 					} else
362 						voice->flags &= ~(BRISTOL_KEYON|BRISTOL_KEYREON);
363 				}
364 
365 				return;
366 			} else {
367 				/*
368 				 * POLY:
369 				 */
370 				if ((delta = ++mods->nextosc) > 3)
371 					delta = mods->nextosc = 0;
372 
373 				mods->keydata[delta].oFreq = voice->cFreq;
374 				mods->keydata[delta].cFreq = voice->cFreq;
375 				mods->keydata[delta].dFreq = voice->dFreq
376 				 + (delta - 2) * mods->detune;
377 				mods->keydata[delta].cFreqstep = voice->cFreqstep;
378 				mods->keydata[delta].cFreqmult = voice->cFreqmult;
379 				mods->keydata[delta].key = voice->key.key;
380 				mods->keydata[delta].lastkey = voice->lastkey;
381 
382 				/*
383 				 * KeyOn is managed now by the arpeggiator
384 				 */
385 				if (baudio->mixflags & P_REON)
386 				{
387 					baudio->mixflags &= ~P_REON;
388 					voice->flags &= ~BRISTOL_KEYREON;
389 					voice->flags |= BRISTOL_KEYON;
390 					mods->arpegcount = 0;
391 					mods->arpegstep = 0;
392 				} else
393 					voice->flags &= ~(BRISTOL_KEYON|BRISTOL_KEYREON);
394 
395 				return;
396 			}
397 
398 			/*
399 			 * If this is after the first voice then clear the trigger if nec.
400 			 */
401 			if (((baudio->mixflags & P_MULTI) == 0) && (i > 0))
402 				voice->flags &= ~(BRISTOL_KEYON|BRISTOL_KEYREON);
403 
404 			return;
405 		}
406 
407 		mods->keydata[i].oFreq = voice->cFreq;
408 		mods->keydata[i].cFreq = voice->cFreq;
409 		mods->keydata[i].dFreq = voice->dFreq
410 			+ (i - 2) * mods->detune;
411 		mods->keydata[i].cFreqstep = voice->cFreqstep;
412 		mods->keydata[i].cFreqmult = voice->cFreqmult;
413 		mods->keydata[i].key = voice->key.key;
414 		mods->keydata[i].lastkey = voice->lastkey;
415 
416 		/*
417 		 * If this is after the first voice then clear the trigger if nec.
418 		 */
419 		if (((baudio->mixflags & P_MULTI) == 0) && (i > 1))
420 			voice->flags &= ~(BRISTOL_KEYON|BRISTOL_KEYREON);
421 
422 		return;
423 	}
424 
425 	if (baudio->mixflags & P_UNISON)
426 	{
427 		/*
428 		 * All osc to the desired note.
429 		 */
430 		mods->keydata[0].cFreq = mods->keydata[1].cFreq
431 			= mods->keydata[2].cFreq = mods->keydata[3].cFreq
432 			= voice->cFreq;
433 		mods->keydata[0].oFreq = mods->keydata[1].oFreq =
434 		mods->keydata[2].oFreq = mods->keydata[3].oFreq =
435 		mods->keydata[0].dFreq = mods->keydata[1].dFreq
436 			= mods->keydata[2].dFreq = mods->keydata[3].dFreq
437 			= voice->dFreq;
438 		mods->keydata[0].cFreqstep = mods->keydata[1].cFreqstep
439 			= mods->keydata[2].cFreqstep = mods->keydata[3].cFreqstep
440 			= voice->cFreqstep;
441 		mods->keydata[0].cFreqmult = mods->keydata[1].cFreqmult
442 			= mods->keydata[2].cFreqmult = mods->keydata[3].cFreqmult
443 			= voice->cFreqmult;
444 		mods->keydata[0].key = mods->keydata[1].key
445 			= mods->keydata[2].key = mods->keydata[3].key
446 			= voice->key.key;
447 		mods->keydata[0].lastkey = mods->keydata[1].lastkey
448 			= mods->keydata[2].lastkey = mods->keydata[3].lastkey
449 			= voice->lastkey;
450 
451 		mods->keydata[0].dFreq += mods->detune;
452 		mods->keydata[1].dFreq -= mods->detune;
453 		mods->keydata[2].dFreq += mods->detune * 2;
454 		mods->keydata[3].dFreq -= mods->detune * 2;
455 	} else if (baudio->mixflags & P_SHARE) {
456 		/*
457 		 * Find out how many keys are on
458 		 */
459 		if (mods->keydata[0].key != -1) i++;
460 		if (mods->keydata[1].key != -1) i++;
461 		if (mods->keydata[2].key != -1) i++;
462 		if (mods->keydata[3].key != -1) i++;
463 /*printf("keys: %i\n", i); */
464 
465 		/*
466 		 * Sharing will monitor the number of notes:
467 		 *	1 = UNISON
468 		 * 	2 = OSC1/2 and OSC3/4
469 		 *	else poly.
470 		 */
471 		if (i == 0)
472 		{
473 /*printf("unison\n"); */
474 			/*
475 			 * Do unison mode
476 			 */
477 			mods->keydata[0].cFreq = mods->keydata[1].cFreq
478 				= mods->keydata[2].cFreq = mods->keydata[3].cFreq
479 				= voice->cFreq;
480 			mods->keydata[0].oFreq = mods->keydata[1].oFreq =
481 			mods->keydata[2].oFreq = mods->keydata[3].oFreq =
482 			mods->keydata[0].dFreq = mods->keydata[1].dFreq
483 				= mods->keydata[2].dFreq = mods->keydata[3].dFreq
484 				= voice->dFreq;
485 			mods->keydata[0].cFreqstep = mods->keydata[1].cFreqstep
486 				= mods->keydata[2].cFreqstep = mods->keydata[3].cFreqstep
487 				= voice->cFreqstep;
488 			mods->keydata[0].cFreqmult = mods->keydata[1].cFreqmult
489 				= mods->keydata[2].cFreqmult = mods->keydata[3].cFreqmult
490 				= voice->cFreqmult;
491 			mods->keydata[0].key = mods->keydata[1].key
492 				= mods->keydata[2].key = mods->keydata[3].key
493 				= voice->key.key;
494 			mods->keydata[0].lastkey = mods->keydata[1].lastkey
495 				= mods->keydata[2].lastkey = mods->keydata[3].lastkey
496 				= voice->lastkey;
497 			voice->flags |= BRISTOL_VCO_MASK;
498 
499 			mods->keydata[0].dFreq += mods->detune;
500 			mods->keydata[1].dFreq -= mods->detune;
501 			mods->keydata[2].dFreq += mods->detune * 2;
502 			mods->keydata[3].dFreq -= mods->detune * 2;
503 
504 			return;
505 		} else if (i == 4) {
506 			/*
507 			 * There are 3 cases where we have four notes.
508 			 *
509 			 *	They are all the same - this is second key
510 			 *		assign this note to 1 and 3.
511 			 *	0 = 1 and 2 = 3, we have two notes keyed.
512 			 *		assign this to 1, clear 3.
513 			 *  all are different.
514 			 *		New key, rotate through them.
515 			 */
516 			if (mods->keydata[0].key == mods->keydata[2].key)
517 			{
518 /*printf("was unison, now dual\n"); */
519 				/*
520 				 * Was unison, steal 2 and 3
521 				 */
522 				mods->keydata[2].cFreq = mods->keydata[3].cFreq
523 					= voice->cFreq;
524 				mods->keydata[2].oFreq =
525 				mods->keydata[2].dFreq = mods->keydata[3].dFreq
526 					= voice->dFreq;
527 				mods->keydata[2].dFreq -= mods->detune * 2;
528 				mods->keydata[3].dFreq += mods->detune * 2;
529 
530 				mods->keydata[2].cFreqstep = mods->keydata[3].cFreqstep
531 					= voice->cFreqstep;
532 				mods->keydata[2].cFreqmult = mods->keydata[3].cFreqmult
533 					= voice->cFreqmult;
534 				mods->keydata[2].key = mods->keydata[3].key
535 					= voice->key.key;
536 				mods->keydata[2].lastkey = mods->keydata[3].lastkey
537 					= voice->lastkey;
538 			} else if (mods->keydata[0].key == mods->keydata[1].key) {
539 /*printf("was dual, now three\n"); */
540 				/*
541 				 * Was dual, steal 1 free 3
542 				 */
543 				mods->keydata[1].cFreq = voice->cFreq;
544 				mods->keydata[1].oFreq = voice->dFreq;
545 				mods->keydata[1].dFreq = voice->dFreq + mods->detune;
546 				mods->keydata[1].cFreqstep = voice->cFreqstep;
547 				mods->keydata[1].cFreqmult = voice->cFreqmult;
548 				mods->keydata[1].key = voice->key.key;
549 				mods->keydata[1].lastkey = voice->lastkey;
550 
551 				mods->keydata[3].key = -1;
552 			} else {
553 				/*
554 				 * Was all, rotate the steal.
555 				 */
556 				i = mods->nextosc;
557 /*printf("was loaded, now %i\n", i); */
558 
559 				mods->keydata[i].cFreq = voice->cFreq;
560 				mods->keydata[i].oFreq = voice->dFreq;
561 				mods->keydata[i].dFreq = voice->dFreq
562 				 	+ (i - 2) * mods->detune;
563 				mods->keydata[i].cFreqstep = voice->cFreqstep;
564 				mods->keydata[i].cFreqmult = voice->cFreqmult;
565 				mods->keydata[i].key = voice->key.key;
566 				mods->keydata[i].lastkey = voice->lastkey;
567 
568 				if (++mods->nextosc > 3)
569 					mods->nextosc = 0;
570 			}
571 		} else {
572 			i = 3;
573 			/*
574 			 * In this case we had one free key. Use it.
575 			 */
576 			if (mods->keydata[0].key == -1)
577 				i = 0;
578 			else if (mods->keydata[1].key == -1)
579 				i = 1;
580 			else if (mods->keydata[2].key == -1)
581 				i = 2;
582 
583 			mods->keydata[i].cFreq = voice->cFreq;
584 			mods->keydata[i].oFreq = voice->dFreq;
585 			mods->keydata[i].dFreq = voice->dFreq
586 				 	+ (i - 2) * mods->detune;
587 			mods->keydata[i].cFreqstep = voice->cFreqstep;
588 			mods->keydata[i].cFreqmult = voice->cFreqmult;
589 			mods->keydata[i].key = voice->key.key;
590 			mods->keydata[i].lastkey = voice->lastkey;
591 		}
592 	} else {
593 		int osc = 0;
594 		/*
595 		 * Poly mode. Find a free osc and assign it. Else take the next
596 		 * in rotation.
597 		 */
598 		if ((mods->keydata[0].key != -1)
599 			&& (mods->keydata[0].key != voice->key.key))
600 		{
601 			if ((mods->keydata[1].key != -1)
602 				&& (mods->keydata[0].key != voice->key.key))
603 			{
604 				if ((mods->keydata[2].key != -1)
605 					&& (mods->keydata[0].key != voice->key.key))
606 				{
607 					if ((mods->keydata[3].key != -1)
608 						&& (mods->keydata[0].key != voice->key.key))
609 					{
610 						/*
611 						 * Bummer, all taken. Go for the next free.
612 						 */
613 						if ((osc = mods->nextosc++) >= 3)
614 							mods->nextosc = 0;
615 					} else osc = 3;
616 				} else osc = 2;
617 			} else osc = 1;
618 		}
619 		mods->keydata[osc].cFreq = voice->cFreq;
620 		mods->keydata[osc].oFreq = voice->dFreq;
621 		mods->keydata[osc].dFreq = voice->dFreq
622 		 	+ (osc - 2) * mods->detune;
623 		mods->keydata[osc].cFreqstep = voice->cFreqstep;
624 		mods->keydata[osc].cFreqmult = voice->cFreqmult;
625 		mods->keydata[osc].key = voice->key.key;
626 		mods->keydata[osc].lastkey = voice->lastkey;
627 
628 		voice->flags |= (BRISTOL_K_VCO1 >> osc);
629 	}
630 
631 	/*
632 	 * If we are not multi trigger then clear this flag
633 	 */
634 	if (((baudio->mixflags & P_MULTI) == 0)
635 		&& (baudio->lvoices > 0))
636 		voice->flags &= ~(BRISTOL_KEYON|BRISTOL_KEYREON);
637 }
638 
639 static void
polyNoteOff(audioMain * audiomain,Baudio * baudio,bristolVoice * voice)640 polyNoteOff(audioMain *audiomain, Baudio *baudio, bristolVoice *voice)
641 {
642 	pmods *mods = ((pmods *) baudio->mixlocals);
643 	int i = 0;
644 
645 #warning issues here if we have two midi events between frames
646 	/*
647 	 * Find out how many keys are on
648 	 */
649 	if (mods->keydata[0].key != -1) i++;
650 	if (mods->keydata[1].key != -1) i++;
651 	if (mods->keydata[2].key != -1) i++;
652 	if (mods->keydata[3].key != -1) i++;
653 
654 	/*
655 	 * In chord we never turn off if we were in share mode. We only turn off
656 	 * in other modes after we have loaded all the oscillators.
657 	 */
658 	if (baudio->mixflags & P_CHORD)
659 	{
660 		if ((baudio->mixflags & (P_SHARE|P_MULTI)) == 0)
661 		{
662 			voice->flags &= ~BRISTOL_KEYOFF;
663 			return;
664 		}
665 
666 		/*
667 		 * So now we have 4 keys loaded. We should clear the keyoff if this
668 		 * was not the most recent key.
669 		 */
670 		if (i < 4)
671 			voice->flags &= ~BRISTOL_KEYOFF;
672 		else if (mods->lastchord != voice->key.key)
673 		{
674 			voice->flags &= ~BRISTOL_KEYOFF;
675 /*printf("note off, key: %i, last %i, chord %i, %i\n", */
676 /*voice->key.key, voice->lastkey, mods->lastchord, i); */
677 		}
678 
679 		return;
680 	}
681 
682 	if (baudio->mixflags & P_SHARE)
683 	{
684 		int a = 2, b = 3;
685 		/*
686 		 * We need to free this key if we find it, see how many are left,
687 		 * redistributing notes if required.
688 		 */
689 		if (i == 4) {
690 			/*
691 			 * There are 3 cases of having 4 notes on
692 			 *	They are the same - all off.
693 			 *	1 = 2 then go unison.
694 			 *	else turn this note off, make it the next available
695 			 */
696 			if (mods->keydata[0].key == mods->keydata[2].key) {
697 /*printf("was unison, all off\n"); */
698 				/*
699 				 * All off, do nothing.
700 				 */
701 				return;
702 			}
703 
704 			if (mods->keydata[0].key == mods->keydata[1].key) {
705 /*printf("was dual, go unison\n"); */
706 				/*
707 				 * Dual, go unison. Find out which key went off, it will not
708 				 * be the unison note.
709 				 */
710 				if (mods->keydata[0].key == voice->key.key)
711 					i = 2;
712 				else if (mods->keydata[2].key == voice->key.key)
713 					i = 0;
714 				else
715 					/*
716 					 * Was an already dropped note
717 					 */
718 					return;
719 
720 				mods->keydata[0].cFreq = mods->keydata[1].cFreq
721 					= mods->keydata[2].cFreq = mods->keydata[3].cFreq
722 						= mods->keydata[i].cFreq;
723 				mods->keydata[0].dFreq = mods->keydata[1].dFreq
724 					= mods->keydata[2].dFreq = mods->keydata[3].dFreq
725 						= mods->keydata[i].dFreq;
726 				mods->keydata[0].cFreqstep = mods->keydata[1].cFreqstep
727 					= mods->keydata[2].cFreqstep = mods->keydata[3].cFreqstep
728 						= mods->keydata[i].cFreqstep;
729 				mods->keydata[0].cFreqmult = mods->keydata[1].cFreqmult
730 					= mods->keydata[2].cFreqmult = mods->keydata[3].cFreqmult
731 						= mods->keydata[i].cFreqmult;
732 				mods->keydata[0].key = mods->keydata[1].key
733 					= mods->keydata[2].key = mods->keydata[3].key
734 						= mods->keydata[i].key;
735 				mods->keydata[0].lastkey = mods->keydata[1].lastkey
736 					= mods->keydata[2].lastkey = mods->keydata[3].lastkey
737 						= mods->keydata[i].lastkey;
738 				voice->flags &= ~BRISTOL_KEYOFF;
739 				return;
740 			}
741 
742 /*printf("free key\n"); */
743 			/*
744 			 * Find the key, turn it off, becomes the next.
745 			 */
746 			if (mods->keydata[0].key == voice->key.key) {
747 				mods->keydata[0].key = -1;
748 				mods->nextosc = 0;
749 			} else if (mods->keydata[1].key == voice->key.key) {
750 				mods->keydata[1].key = -1;
751 				mods->nextosc = 1;
752 			} else if (mods->keydata[2].key == voice->key.key) {
753 				mods->keydata[2].key = -1;
754 				mods->nextosc = 2;
755 			} else if (mods->keydata[3].key == voice->key.key) {
756 				mods->keydata[3].key = -1;
757 				mods->nextosc = 3;
758 			}
759 
760 			voice->flags &= ~BRISTOL_KEYOFF;
761 			return;
762 		}
763 
764 /*printf("three keys\n"); */
765 		/*
766 		 * We have three keys on, need to find out which one went off and
767 		 * assign the dual to the others.
768 		 */
769 		if (mods->keydata[0].key == voice->key.key) {
770 
771 			if (mods->keydata[1].key == -1) {
772 				a = 2;
773 				b = 3;
774 			} else if (mods->keydata[2].key == -1) {
775 				a = 1;
776 				b = 3;
777 			} else {
778 				a = 1;
779 				b = 2;
780 			}
781 
782 		} else if (mods->keydata[1].key == voice->key.key) {
783 
784 			if (mods->keydata[0].key == -1) {
785 				a = 2;
786 				b = 3;
787 			} else if (mods->keydata[2].key == -1) {
788 				a = 0;
789 				b = 3;
790 			} else {
791 				a = 0;
792 				b = 2;
793 			}
794 
795 		} else if (mods->keydata[2].key == voice->key.key) {
796 
797 			if (mods->keydata[0].key == -1) {
798 				a = 1;
799 				b = 3;
800 			} else if (mods->keydata[1].key == -1) {
801 				a = 0;
802 				b = 3;
803 			} else {
804 				a = 0;
805 				b = 1;
806 			}
807 
808 		} else if (mods->keydata[3].key == voice->key.key) {
809 
810 			if (mods->keydata[0].key == -1) {
811 				a = 1;
812 				b = 2;
813 			} else if (mods->keydata[1].key == -1) {
814 				a = 0;
815 				b = 2;
816 			} else {
817 				a = 0;
818 				b = 1;
819 			}
820 
821 		}
822 
823 		mods->keydata[0].cFreq = mods->keydata[1].cFreq
824 			= mods->keydata[a].cFreq;
825 		mods->keydata[0].dFreq = mods->keydata[1].dFreq
826 			= mods->keydata[a].dFreq;
827 		mods->keydata[0].cFreqstep = mods->keydata[1].cFreqstep
828 			= mods->keydata[a].cFreqstep;
829 		mods->keydata[0].cFreqmult = mods->keydata[1].cFreqmult
830 			= mods->keydata[a].cFreqmult;
831 		mods->keydata[0].key = mods->keydata[1].key
832 			= mods->keydata[a].key;
833 		mods->keydata[0].lastkey = mods->keydata[0].lastkey
834 			= mods->keydata[1].lastkey;
835 
836 		mods->keydata[2].cFreq = mods->keydata[3].cFreq
837 			= mods->keydata[b].cFreq;
838 		mods->keydata[2].dFreq = mods->keydata[3].dFreq
839 			= mods->keydata[b].dFreq;
840 		mods->keydata[2].cFreqstep = mods->keydata[3].cFreqstep
841 			= mods->keydata[b].cFreqstep;
842 		mods->keydata[2].cFreqmult = mods->keydata[3].cFreqmult
843 			= mods->keydata[b].cFreqmult;
844 		mods->keydata[2].key = mods->keydata[3].key
845 			= mods->keydata[b].key;
846 		mods->keydata[2].lastkey = mods->keydata[3].lastkey
847 			= mods->keydata[b].lastkey;
848 
849 		voice->flags &= ~BRISTOL_KEYOFF;
850 		return;
851 	} else if (baudio->mixflags & P_UNISON) {
852 		/*
853 		 * This is the Unison mode. If this is not the current key then
854 		 * clear the keyoff flag.
855 		 */
856 		if (mods->keydata[0].key != voice->key.key)
857 			voice->flags &= ~BRISTOL_KEYOFF;
858 	} else {
859 		/*
860 		 * If this is the last note let is continue with the key off active.
861 		 */
862 		if (i != 1) {
863 			/*
864 			 * This is the poly mode. The operation does not work well with
865 			 * damping off, the last note should hold.....
866 			 *
867 			 * If we are DAMPED then skip the note off:
868 			 */
869 			if (mods->keydata[0].key == voice->key.key) {
870 				if ((baudio->mixflags & P_DAMP) == 0)
871 					mods->keydata[0].key = -1;
872 				voice->flags &= ~BRISTOL_K_VCO1;
873 			}
874 			if (mods->keydata[1].key == voice->key.key) {
875 				if ((baudio->mixflags & P_DAMP) == 0)
876 					mods->keydata[1].key = -1;
877 				voice->flags &= ~BRISTOL_K_VCO2;
878 			}
879 			if (mods->keydata[2].key == voice->key.key) {
880 				if ((baudio->mixflags & P_DAMP) == 0)
881 					mods->keydata[2].key = -1;
882 				voice->flags &= ~BRISTOL_K_VCO3;
883 			}
884 			if (mods->keydata[3].key == voice->key.key) {
885 				if ((baudio->mixflags & P_DAMP) == 0)
886 					mods->keydata[3].key = -1;
887 				voice->flags &= ~BRISTOL_K_VCO4;
888 			}
889 
890 			/*
891 			 * If that was not the last key then clear the KEYOFF event
892 			 */
893 			if (voice->flags & BRISTOL_VCO_MASK)
894 				voice->flags &= ~BRISTOL_KEYOFF;
895 		}
896 	}
897 }
898 
899 /*
900  * Preops will do noise, and one oscillator - the LFO.
901  */
902 int
operatePolyPreops(audioMain * audiomain,Baudio * baudio,bristolVoice * voice,register float * startbuf)903 operatePolyPreops(audioMain *audiomain, Baudio *baudio,
904 bristolVoice *voice, register float *startbuf)
905 {
906 #ifdef DEBUG
907 	if ((audiomain->debuglevel & BRISTOL_DEBUG_MASK) > BRISTOL_DEBUG5)
908 		printf("operatePolyPreops(%x, %x, %x) %i\n",
909 			baudio, voice, startbuf, baudio->cvoices);
910 #endif
911 
912 	if (((pmods *) baudio->mixlocals)->mg1locals == 0)
913 	{
914 		/*
915 		 * We don't really need to do this, the synth is mono
916 		 */
917 		((pmods *) baudio->mixlocals)->mg1locals =
918 			baudio->locals[voice->index][9];
919 		((pmods *) baudio->mixlocals)->mg2locals =
920 			baudio->locals[voice->index][10];
921 	}
922 
923 	if (voice->flags & (BRISTOL_KEYON|BRISTOL_KEYREON))
924 		polyNoteOn(audiomain, baudio, voice);
925 
926 	if (voice->flags & BRISTOL_KEYOFF)
927 		polyNoteOff(audiomain, baudio, voice);
928 
929 	bristolbzero(osc0buf, audiomain->segmentsize);
930 	bristolbzero(osc1buf, audiomain->segmentsize);
931 	bristolbzero(osc2buf, audiomain->segmentsize);
932 	bristolbzero(osc3buf, audiomain->segmentsize);
933 	bristolbzero(scratchbuf, audiomain->segmentsize);
934 
935 /* LFO MG1 */
936 	/*
937 	 * We have to watch for the waveform selector:
938 	 */
939 	audiomain->palette[(*baudio->sound[9]).index]->specs->io[0].buf
940 		= baudio->leftbuf;
941 	audiomain->palette[(*baudio->sound[9]).index]->specs->io[1].buf
942 		= 0;
943 	audiomain->palette[(*baudio->sound[9]).index]->specs->io[2].buf
944 		= 0;
945 	audiomain->palette[(*baudio->sound[9]).index]->specs->io[3].buf
946 		= 0;
947 	audiomain->palette[(*baudio->sound[9]).index]->specs->io[4].buf
948 		= 0;
949 	audiomain->palette[(*baudio->sound[9]).index]->specs->io[5].buf
950 		= 0;
951 	audiomain->palette[(*baudio->sound[9]).index]->specs->io[6].buf
952 		= 0;
953 
954 	switch (baudio->mixflags & 0x03)
955 	{
956 		case 0:
957 			audiomain->palette[(*baudio->sound[9]).index]->specs->io[1].buf
958 				= mg1buf;
959 			break;
960 		case 1:
961 			audiomain->palette[(*baudio->sound[9]).index]->specs->io[5].buf
962 				= mg1buf;
963 			break;
964 		case 2:
965 			audiomain->palette[(*baudio->sound[9]).index]->specs->io[6].buf
966 				= mg1buf;
967 			break;
968 		case 3:
969 			audiomain->palette[(*baudio->sound[9]).index]->specs->io[2].buf
970 				= mg1buf;
971 			break;
972 	}
973 
974 	(*baudio->sound[9]).operate(
975 		(audiomain->palette)[16],
976 		voice,
977 		(*baudio->sound[9]).param,
978 		/*baudio->locals[voice->index][9]); */
979 		((pmods *) baudio->mixlocals)->mg1locals);
980 /* MG1 OVER */
981 
982 /* LFO MG2 */
983 	audiomain->palette[(*baudio->sound[10]).index]->specs->io[0].buf
984 		= baudio->leftbuf;
985 	audiomain->palette[(*baudio->sound[10]).index]->specs->io[1].buf
986 		= mg2buf;
987 	audiomain->palette[(*baudio->sound[10]).index]->specs->io[2].buf = 0;
988 	audiomain->palette[(*baudio->sound[10]).index]->specs->io[3].buf = 0;
989 	audiomain->palette[(*baudio->sound[10]).index]->specs->io[4].buf = 0;
990 	audiomain->palette[(*baudio->sound[10]).index]->specs->io[5].buf = 0;
991 	audiomain->palette[(*baudio->sound[10]).index]->specs->io[6].buf = 0;
992 
993 	(*baudio->sound[10]).operate(
994 		(audiomain->palette)[16],
995 		voice,
996 		(*baudio->sound[10]).param,
997 		/*baudio->locals[voice->index][10]); */
998 		((pmods *) baudio->mixlocals)->mg2locals);
999 /* MG2 OVER */
1000 	return(0);
1001 }
1002 
1003 int
operateOnePoly(audioMain * audiomain,Baudio * baudio,bristolVoice * voice,register float * startbuf)1004 operateOnePoly(audioMain *audiomain, Baudio *baudio,
1005 bristolVoice *voice, register float *startbuf)
1006 {
1007 	register int sc = audiomain->samplecount, o_act = 0, oper, kh;
1008 	pmods *mods = ((pmods *) baudio->mixlocals);
1009 
1010 /* Arpeggiate */
1011 	/*
1012 	 * We do arpeggiator operations here as they may affect the envelope
1013 	 * trigger. The mixing of the selected oscillators that depend on the
1014 	 * arpeggiator is done after the oscillator generation has completed.
1015 	 */
1016 	if (baudio->mixflags & P_CHORD)
1017 	{
1018 		if (baudio->mixflags & P_UNISON) {
1019 		} else {
1020 			if (mods->arpegcount++ > mods->arpegtotal)
1021 			{
1022 				mods->arpegcount = 0;
1023 
1024 				switch (mods->arpegdir) {
1025 					case P_ARPEG_UP:
1026 						if (++mods->arpegstep > 3)
1027 							mods->arpegstep = 0;
1028 						break;
1029 					case P_ARPEG_DOWN:
1030 						if (--mods->arpegstep < 0)
1031 							mods->arpegstep = 3;
1032 						break;
1033 					case P_ARPEG_UPDOWN:
1034 						if (++mods->arpegstep > 3)
1035 						{
1036 							mods->arpegstep = 3;
1037 							mods->arpegdir = P_ARPEG_DOWNUP;
1038 						}
1039 						break;
1040 					case P_ARPEG_DOWNUP:
1041 						if (--mods->arpegstep < 0)
1042 						{
1043 							mods->arpegstep = 0;
1044 							mods->arpegdir = P_ARPEG_UPDOWN;
1045 						}
1046 						break;
1047 				}
1048 				if ((baudio->mixflags & P_MULTI)
1049 					&& ((voice->flags & BRISTOL_KEYOFF) == 0))
1050 					voice->flags |= BRISTOL_KEYREON;
1051 			}
1052 		}
1053 	}
1054 /* */
1055 
1056 /* ADSR - FILTER MOD */
1057 	/*
1058 	 * Run the ADSR for the filter.
1059 	 */
1060 	audiomain->palette[(*baudio->sound[3]).index]->specs->io[0].buf = adsrbuf;
1061 	(*baudio->sound[3]).operate(
1062 		(audiomain->palette)[1],
1063 		voice,
1064 		(*baudio->sound[3]).param,
1065 		voice->locals[voice->index][3]);
1066 /* ADSR - FILTER - OVER */
1067 
1068 /* PWM Scratchbuf */
1069 	/*
1070 	 * If we have any mods on the oscillators, we need to put them in here.
1071 	 * This should be under the control of polypressure and/or
1072 	 * channelpressure?
1073 	 */
1074 	bzero(scratchbuf, audiomain->segmentsize);
1075 
1076 	/*
1077 	 * We know the intensity and mod source. Build the buffer now and apply
1078 	 * it depending on the waveform. PWM can have 3 sources, mg1, mg2 or
1079 	 * filter env.
1080 	 */
1081 	if (baudio->mixflags & PWM_MG1)
1082 		bufmerge(mg1buf, mods->pwmdepth * 128,
1083 			scratchbuf, 0.0, sc);
1084 	else if (baudio->mixflags & PWM_MG2)
1085 		bufmerge(mg2buf, mods->pwmdepth * 128,
1086 			scratchbuf, 0.0, sc);
1087 	else
1088 		bufmerge(adsrbuf, mods->pwmdepth * 128,
1089 			scratchbuf, 0.0, sc);
1090 /* PWM Scratchbuf - DONE */
1091 
1092 	kh = voice->key.key;
1093 /* OSCILLATORS */
1094 	/*
1095 	 * Due to the mod routings we have to organise how the oscillators are
1096 	 * generated. They are going to have independent bufferings to allow the
1097 	 * mod routings depending on switches, and have a separate final mix
1098 	 * section. The 'cheaper' option of having the osc gain parameter do the
1099 	 * mixing does not work well with mod routing. Since this is a mono synth
1100 	 * the overhead is not excessive.
1101 	 */
1102 	for (o_act = 0; o_act < 4; o_act++)
1103 	{
1104 		if (mods->keydata[o_act].key == -1)
1105 			continue;
1106 
1107 		oper = o_act;
1108 
1109 		/*
1110 		 * Last oscillator is actually the 8th sound.
1111 		 * Depending on our oscillator need to select the output buffer.
1112 		 */
1113 		switch (o_act)
1114 		{
1115 			case 0:
1116 				audiomain->palette[
1117 					(*baudio->sound[oper]).index]->specs->io[2].buf = osc0buf;
1118 				break;
1119 			case 1:
1120 				audiomain->palette[
1121 					(*baudio->sound[oper]).index]->specs->io[2].buf = osc1buf;
1122 				break;
1123 			case 2:
1124 				audiomain->palette[
1125 					(*baudio->sound[oper]).index]->specs->io[2].buf = osc2buf;
1126 				break;
1127 			case 3:
1128 				/*
1129 				 * Looks odd, but the 4th osc is operator 9
1130 				 */
1131 				oper = 8;
1132 				audiomain->palette[
1133 					(*baudio->sound[oper]).index]->specs->io[2].buf = osc3buf;
1134 				break;
1135 		}
1136 
1137 		/*
1138 		 * Insert the rest of the desired buffers.
1139 		 */
1140 		audiomain->palette[(*baudio->sound[oper]).index]->specs->io[0].buf
1141 			= freqbuf;
1142 		if (baudio->mixflags & (P_PWM_1 << o_act))
1143 			audiomain->palette[(*baudio->sound[oper]).index]->specs->io[1].buf
1144 				= scratchbuf;
1145 		else
1146 			audiomain->palette[(*baudio->sound[oper]).index]->specs->io[1].buf
1147 				= baudio->rightbuf;
1148 		/*
1149 		 * Default is no sync, may be changed by mod routing later.
1150 		 */
1151 		audiomain->palette[(*baudio->sound[oper]).index]->specs->io[3].buf = 0;
1152 
1153 		/*
1154 		 * Stuff the voice parameters with our key information. This is called
1155 		 * cheating, but as this is a synth within a synth.....
1156 		 */
1157 		voice->cFreq = mods->keydata[o_act].cFreq;
1158 		voice->dFreq = mods->keydata[o_act].dFreq;
1159 		voice->cFreqstep = mods->keydata[o_act].cFreqstep;
1160 		voice->cFreqmult = mods->keydata[o_act].cFreqmult;
1161 		voice->key.key = mods->keydata[o_act].key;
1162 		voice->lastkey = mods->keydata[o_act].lastkey;
1163 
1164 		/*
1165 		 * Fill our frequency information
1166 		 */
1167 		fillFreqTable(baudio, voice, freqbuf, sc, 1);
1168 
1169 		mods->keydata[o_act].cFreq = voice->cFreq;
1170 
1171 		/*
1172 		 * If wheels are set for VCO then apply it to all of them.
1173 		 */
1174 		if (mods->mg1dst == DEST_PITCH)
1175 		{
1176 			bufmerge(mg1buf,
1177 				mods->mg1intensity * baudio->contcontroller[CONTROL_MG1],
1178 					freqbuf, 1.0, sc);
1179 		} else if (mods->mg1dst == DEST_VCO) {
1180 			if (baudio->mixflags & P_EFFECTS)
1181 			{
1182 				if (baudio->mixflags & P_MOD_SING)
1183 				{
1184 					if (o_act != 0)
1185 						bufmerge(mg1buf, mods->mg1intensity * mods->mfreq
1186 							* baudio->contcontroller[CONTROL_MG1],
1187 							freqbuf, 1.0, sc);
1188 				} else {
1189 					/*
1190 					 * Dual, apply to 1 and 3.
1191 					 */
1192 					if ((o_act == 1) || (o_act == 3))
1193 						bufmerge(mg1buf, mods->mg1intensity * mods->mfreq
1194 							* baudio->contcontroller[CONTROL_MG1],
1195 							freqbuf, 1.0, sc);
1196 				}
1197 			} else {
1198 				/*
1199 				 * Only apply to VCO-1
1200 				 */
1201 				if (o_act == 0)
1202 					bufmerge(mg1buf, mods->mg1intensity
1203 						* baudio->contcontroller[CONTROL_MG1],
1204 						freqbuf, 1.0, sc);
1205 			}
1206 		}
1207 
1208 		/*
1209 		 * If DEST_PITCH apply mods. This is rather an ugly bit of logic.
1210 		 */
1211 		if ((mods->benddst == DEST_PITCH)
1212 		|| ((mods->benddst == DEST_VCO)
1213 			&& (
1214 				/* VCO and no effects and osc-0 */
1215 				(((baudio->mixflags & P_EFFECTS) == 0) && (o_act == 0))
1216 				||
1217 					((baudio->mixflags & P_EFFECTS)
1218 						/* VCO and effects and SINGLE and not osc-0 */
1219 						&& (((baudio->mixflags & P_MOD_SING) && (o_act != 0))
1220 							||
1221 							/* VCO and effects and not SINGLE and osc-1/3 */
1222 							(((baudio->mixflags & P_MOD_SING) == 0)
1223 								&& ((o_act == 1) && (o_act == 3)))
1224 						)
1225 					)
1226 				)
1227 			)
1228 		)
1229 		{
1230 			register int c = sc;
1231 			/*
1232 			 * value of 0.5 is no change.
1233 			 * value of 0.0 is one octave down
1234 			 * value of 1.0 is one octave up
1235 			 */
1236 			register float *f = freqbuf,
1237 				a = baudio->contcontroller[CONTROL_BEND] - 0.5;
1238 
1239 			a = a * baudio->note_diff * 12 *
1240 				mods->bendintensity;
1241 
1242 			for (; c > 0; c-=16)
1243 			{
1244 				*f++ += a;
1245 				*f++ += a;
1246 				*f++ += a;
1247 				*f++ += a;
1248 				*f++ += a;
1249 				*f++ += a;
1250 				*f++ += a;
1251 				*f++ += a;
1252 				*f++ += a;
1253 				*f++ += a;
1254 				*f++ += a;
1255 				*f++ += a;
1256 				*f++ += a;
1257 				*f++ += a;
1258 				*f++ += a;
1259 				*f++ += a;
1260 			}
1261 		}
1262 
1263 /* MODS */
1264 		/*
1265 		 * Some of the FX appear to conflict with the wheel mods, but it is
1266 		 * just a case of applying them except as per VCO selection of wheel.
1267 		 */
1268 		if (baudio->mixflags & P_EFFECTS)
1269 		{
1270 			/*
1271 			 * We have diverse mod routings to apply, they are not really
1272 			 * effects in a true sense, but the MonoPoly "Effects" enabled or
1273 			 * disabled the routing.
1274 			 *
1275 			 * There are two main modes, single and double
1276 			 *	Single takes Osc0 to mod 1/2/3.
1277 			 *	Double takes Osc 0->1 and 2->3 (original had 0->2/1->3)
1278 			 */
1279 			if (baudio->mixflags & P_MOD_SING)
1280 			{
1281 				/*
1282 				 * In single, apply mods to other oscillators
1283 				 */
1284 				if (o_act != 0)
1285 				{
1286 					/*
1287 					 * Apply mods. We can have sync, an amount of XMOD and
1288 					 * a modulated frequency.
1289 					 */
1290 					if (baudio->mixflags & P_MOD_SYNC)
1291 						audiomain->palette[
1292 							(*baudio->sound[oper]).index]->specs->io[3].buf
1293 								= osc0buf;
1294 
1295 					/*
1296 					 * Put the fxbuf into the frequency buf for this osc.
1297 					 */
1298 					if (baudio->mixflags & P_MOD_XMOD)
1299 						bufmerge(osc0buf, mods->xmod, freqbuf, 1.0, sc);
1300 
1301 					if (baudio->mixflags & P_MOD_ENV)
1302 						bufmerge(adsrbuf, mods->mfreq * 2, freqbuf, 1.0, sc);
1303 				}
1304 			} else {
1305 				if (o_act == 1)
1306 				{
1307 					/*
1308 					 * Apply mods. We can have sync, an amount of XMOD and
1309 					 * a modulated frequency.
1310 					 */
1311 					if (baudio->mixflags & P_MOD_SYNC)
1312 						audiomain->palette[
1313 							(*baudio->sound[oper]).index]->specs->io[3].buf
1314 								= osc0buf;
1315 
1316 					/*
1317 					 * Put the fxbuf into the frequency buf for this osc.
1318 					 */
1319 					if (baudio->mixflags & P_MOD_XMOD)
1320 						bufmerge(osc0buf, mods->xmod, freqbuf, 1.0, sc);
1321 					if (baudio->mixflags & P_MOD_ENV)
1322 						bufmerge(adsrbuf, mods->mfreq * 2, freqbuf, 1.0, sc);
1323 				} else if (o_act == 3) {
1324 					/*
1325 					 * Apply mods. We can have sync, an amount of XMOD and
1326 					 * a modulated frequency.
1327 					 */
1328 					if (baudio->mixflags & P_MOD_SYNC)
1329 						audiomain->palette[
1330 							(*baudio->sound[oper]).index]->specs->io[3].buf
1331 								= osc2buf;
1332 
1333 					/*
1334 					 * Put the fxbuf into the frequency buf for this osc.
1335 					 */
1336 					if (baudio->mixflags & P_MOD_XMOD)
1337 						bufmerge(osc2buf, mods->xmod, freqbuf, 1.0, sc);
1338 					if (baudio->mixflags & P_MOD_ENV)
1339 						bufmerge(adsrbuf, mods->mfreq * 2, freqbuf, 1.0, sc);
1340 				}
1341 			}
1342 		}
1343 /* Mods done */
1344 
1345 		(*baudio->sound[oper]).operate(
1346 			(audiomain->palette)[8],
1347 			voice,
1348 			(*baudio->sound[oper]).param,
1349 			voice->locals[voice->index][oper]);
1350 	}
1351 /* OSCILLATORS - DONE */
1352 	 voice->key.key = kh;
1353 
1354 /* Arpeggiated mixer. */
1355 	/*
1356 	 * Take each osc buf and mix into leftbuf ready for filter. We need to
1357 	 * consider what do to with argep at this point, since if we are chorded
1358 	 * the only selected oscillators may actually voice.
1359 	 */
1360 	if (baudio->mixflags & P_CHORD)
1361 	{
1362 		if (baudio->mixflags & P_UNISON) {
1363 			bufmerge(osc0buf, mods->osc0gain, baudio->leftbuf, 0.0, sc);
1364 			bufmerge(osc1buf, mods->osc1gain, baudio->leftbuf, 1.0, sc);
1365 			bufmerge(osc2buf, mods->osc2gain, baudio->leftbuf, 1.0, sc);
1366 			bufmerge(osc3buf, mods->osc3gain, baudio->leftbuf, 1.0, sc);
1367 		} else {
1368 			if (mods->arpegstep == 0)
1369 				bufmerge(osc0buf, mods->osc0gain, baudio->leftbuf, 0.0, sc);
1370 			else if (mods->arpegstep == 1)
1371 				bufmerge(osc1buf, mods->osc1gain, baudio->leftbuf, 0.0, sc);
1372 			else if (mods->arpegstep == 2)
1373 				bufmerge(osc2buf, mods->osc2gain, baudio->leftbuf, 0.0, sc);
1374 			else if (mods->arpegstep == 3)
1375 				bufmerge(osc3buf, mods->osc3gain, baudio->leftbuf, 0.0, sc);
1376 		}
1377 	} else {
1378 		/*
1379 		 * No chording, mix them all.
1380 		 */
1381 		bufmerge(osc0buf, mods->osc0gain, baudio->leftbuf, 0.0, sc);
1382 		bufmerge(osc1buf, mods->osc1gain, baudio->leftbuf, 1.0, sc);
1383 		bufmerge(osc2buf, mods->osc2gain, baudio->leftbuf, 1.0, sc);
1384 		bufmerge(osc3buf, mods->osc3gain, baudio->leftbuf, 1.0, sc);
1385 	}
1386 /* Arpeggiated mixer - done */
1387 
1388 /* NOISE */
1389 	audiomain->palette[(*baudio->sound[7]).index]->specs->io[0].buf =
1390 		baudio->leftbuf;
1391 	(*baudio->sound[7]).operate(
1392 		(audiomain->palette)[4],
1393 		voice,
1394 		(*baudio->sound[7]).param,
1395 		voice->locals[voice->index][7]);
1396 /* NOISE OVER */
1397 
1398 	/* Filter input driving */
1399 	bufmerge(baudio->leftbuf, 192.0, baudio->leftbuf, 0.0, sc);
1400 
1401 /* FILTER */
1402 	audiomain->palette[(*baudio->sound[4]).index]->specs->io[0].buf
1403 		= baudio->leftbuf;
1404 	audiomain->palette[(*baudio->sound[4]).index]->specs->io[1].buf
1405 		= adsrbuf;
1406 	audiomain->palette[(*baudio->sound[4]).index]->specs->io[2].buf
1407 		= baudio->rightbuf;
1408 
1409 	if (mods->mg1dst == DEST_FLT)
1410 		bufmerge(mg1buf, mods->mg1intensity
1411 			* baudio->contcontroller[CONTROL_MG1] * 2,
1412 			adsrbuf, 1.0, sc);
1413 
1414 	if (mods->benddst == DEST_FLT)
1415 	{
1416 		register int c = sc;
1417 		register float *f = adsrbuf, g = baudio->contcontroller[CONTROL_BEND];
1418 
1419 		g *= mods->bendintensity * 8;
1420 
1421 		for (; c > 0; c-=16)
1422 		{
1423 			*f++ += g;
1424 			*f++ += g;
1425 			*f++ += g;
1426 			*f++ += g;
1427 			*f++ += g;
1428 			*f++ += g;
1429 			*f++ += g;
1430 			*f++ += g;
1431 			*f++ += g;
1432 			*f++ += g;
1433 			*f++ += g;
1434 			*f++ += g;
1435 			*f++ += g;
1436 			*f++ += g;
1437 			*f++ += g;
1438 			*f++ += g;
1439 		}
1440 	}
1441 
1442 	(*baudio->sound[4]).operate(
1443 		(audiomain->palette)[3],
1444 		voice,
1445 		(*baudio->sound[4]).param,
1446 		voice->locals[voice->index][4]);
1447 /* FILTER OVER */
1448 
1449 /* ADSR - ENV MOD */
1450 	/*
1451 	 * Run the ADSR for the amp.
1452 	 */
1453 	audiomain->palette[(*baudio->sound[5]).index]->specs->io[0].buf = adsrbuf;
1454 	(*baudio->sound[5]).operate(
1455 		(audiomain->palette)[1],
1456 		voice,
1457 		(*baudio->sound[5]).param,
1458 		voice->locals[voice->index][5]);
1459 /* ADSR - ENV - OVER */
1460 
1461 /* AMP */
1462 	bristolbzero(baudio->leftbuf, audiomain->segmentsize);
1463 
1464 	audiomain->palette[(*baudio->sound[6]).index]->specs->io[0].buf
1465 		= baudio->rightbuf;
1466 	audiomain->palette[(*baudio->sound[6]).index]->specs->io[1].buf
1467 		= adsrbuf;
1468 	audiomain->palette[(*baudio->sound[6]).index]->specs->io[2].buf =
1469 		baudio->leftbuf;
1470 	(*baudio->sound[6]).operate(
1471 		(audiomain->palette)[2],
1472 		voice,
1473 		(*baudio->sound[6]).param,
1474 		voice->locals[voice->index][6]);
1475 /* AMP OVER */
1476 	return(0);
1477 }
1478 
1479 int
operatePolyPostops(audioMain * audiomain,Baudio * baudio,bristolVoice * voice,register float * startbuf)1480 operatePolyPostops(audioMain *audiomain, Baudio *baudio,
1481 bristolVoice *voice, register float *startbuf)
1482 {
1483 	if ((voice->flags & BRISTOL_KEYDONE) || (baudio->mixflags & P_CHORD_OFF))
1484 	{
1485 /*printf("done: %x\n", baudio->mixflags); */
1486 		if ((baudio->mixflags & P_CHORD_OFF)
1487 			|| ((baudio->mixflags & P_CHORD) == 0))
1488 		{
1489 			((pmods *) baudio->mixlocals)->keydata[0].key = -1;
1490 			((pmods *) baudio->mixlocals)->keydata[1].key = -1;
1491 			((pmods *) baudio->mixlocals)->keydata[2].key = -1;
1492 			((pmods *) baudio->mixlocals)->keydata[3].key = -1;
1493 
1494 			voice->flags &= ~BRISTOL_VCO_MASK;
1495 			baudio->mixflags &= ~P_CHORD_OFF;
1496 
1497 			return(0);
1498 		}
1499 		if ((baudio->mixflags & P_CHORD) && ~(baudio->mixflags & P_UNISON))
1500 			baudio->mixflags |= P_REON;
1501 	}
1502 	return(0);
1503 }
1504 
1505 int
bristolPolyDestroy(audioMain * audiomain,Baudio * baudio)1506 bristolPolyDestroy(audioMain *audiomain, Baudio *baudio)
1507 {
1508 	printf("removing one poly\n");
1509 	return(0);
1510 }
1511 
1512 int
bristolPolyInit(audioMain * audiomain,Baudio * baudio)1513 bristolPolyInit(audioMain *audiomain, Baudio *baudio)
1514 {
1515 printf("initialising one mono/poly\n");
1516 	/*
1517 	 * The Poly is relatively straightforward in terms of operators
1518 	 *
1519 	 * We will need the following and will use those from the prophet:
1520 	 *
1521 	 *	Two LFO for Mod groups
1522 	 *	Four DCO - Each voice will only sound one of them though.
1523 	 *  Noise
1524 	 *	Filter
1525 	 *	Two ENV
1526 	 *	Amplifier
1527 	 */
1528 	baudio->soundCount = 11; /* Number of operators in this voice (MM) */
1529 
1530 	/*
1531 	 * Assign an array of sound pointers.
1532 	 */
1533 	baudio->sound = (bristolSound **)
1534 		bristolmalloc0(sizeof(bristolOP *) * baudio->soundCount);
1535 	baudio->effect = (bristolSound **)
1536 		bristolmalloc0(sizeof(bristolOP *) * baudio->soundCount);
1537 
1538 	/*
1539 	 * The fourth Osc and two LFO are tagged on the end. Ordering is not
1540 	 * important, but this will allow us to import much of the existing
1541 	 * code from the other synths that already use for first 8 operators:
1542 	 * So many of these classic synths used the same sound routing and
1543 	 * we use that here.
1544 	 */
1545 	initSoundAlgo(8, 0, baudio, audiomain, baudio->sound);
1546 	initSoundAlgo(8, 1, baudio, audiomain, baudio->sound);
1547 	initSoundAlgo(8, 2, baudio, audiomain, baudio->sound);
1548 	/* An ADSR */
1549 	initSoundAlgo(1, 3, baudio, audiomain, baudio->sound);
1550 	/* A filter */
1551 	initSoundAlgo(3, 4, baudio, audiomain, baudio->sound);
1552 	/* Another ADSR */
1553 	initSoundAlgo(1, 5, baudio, audiomain, baudio->sound);
1554 	/* An amplifier */
1555 	initSoundAlgo(2, 6, baudio, audiomain, baudio->sound);
1556 	/* An noise source */
1557 	initSoundAlgo(4, 7, baudio, audiomain, baudio->sound);
1558 	/* Fourth Osc */
1559 	initSoundAlgo(8, 8, baudio, audiomain, baudio->sound);
1560 	/* Two LFO */
1561 	initSoundAlgo(16, 9, baudio, audiomain, baudio->sound);
1562 	initSoundAlgo(16, 10, baudio, audiomain, baudio->sound);
1563 
1564 	baudio->param = polyController;
1565 	baudio->destroy = bristolPolyDestroy;
1566 	baudio->preops = operatePolyPreops; /* Mod Group LFO */
1567 	baudio->operate = operateOnePoly; /* Mod application to single OSC */
1568 	baudio->postops = operatePolyPostops; /* Env and Filter */
1569 
1570 	if (mg1buf == 0)
1571 		mg1buf = (float *) bristolmalloc0(audiomain->segmentsize);
1572 	if (mg2buf == 0)
1573 		mg2buf = (float *) bristolmalloc0(audiomain->segmentsize);
1574 	if (freqbuf == 0)
1575 		freqbuf = (float *) bristolmalloc0(audiomain->segmentsize);
1576 	if (osc0buf == 0)
1577 		osc0buf = (float *) bristolmalloc0(audiomain->segmentsize);
1578 	if (osc1buf == 0)
1579 		osc1buf = (float *) bristolmalloc0(audiomain->segmentsize);
1580 	if (osc2buf == 0)
1581 		osc2buf = (float *) bristolmalloc0(audiomain->segmentsize);
1582 	if (osc3buf == 0)
1583 		osc3buf = (float *) bristolmalloc0(audiomain->segmentsize);
1584 	if (adsrbuf == 0)
1585 		adsrbuf = (float *) bristolmalloc0(audiomain->segmentsize);
1586 	if (scratchbuf == 0)
1587 		scratchbuf = (float *) bristolmalloc0(audiomain->segmentsize);
1588 
1589 	baudio->mixlocals = (float *) bristolmalloc0(sizeof(pmods));
1590 
1591 	((pmods *) baudio->mixlocals)->keydata[0].key = -1;
1592 	((pmods *) baudio->mixlocals)->keydata[1].key = -1;
1593 	((pmods *) baudio->mixlocals)->keydata[2].key = -1;
1594 	((pmods *) baudio->mixlocals)->keydata[3].key = -1;
1595 
1596 	((pmods *) baudio->mixlocals)->nextosc = 0;
1597 
1598 	baudio->mixflags |= BRISTOL_MULTITRIG;
1599 	return(0);
1600 }
1601 
1602