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 #include <math.h>
23 #include <stdlib.h>
24
25 #include "bristol.h"
26 #include "bristolarpeggiation.h"
27
28 extern bristolMidiHandler bristolMidiRoutines;
29
30 /* This is required for 2602222, voices left hanging when terminating */
desequence(audioMain * am,Baudio * ba)31 static void desequence(audioMain *am, Baudio *ba)
32 {
33 bristolVoice *voice = am->playlist;
34
35 while (voice != NULL) {
36 if (voice->baudio == ba)
37 voice->flags |= BRISTOL_KEYDONE;
38
39 voice = voice->next;
40 }
41 }
42
43 /*
44 * This is sent arpeggiator messages via controller BRISTOL_ARPEGGIATOR (125)
45 * and does the configuration of the arpeggiator.
46 *
47 * The code supports sequencing where steps can be learnt from the keyboard
48 * and then stepped through them sequencially, chording where a similar learn
49 * process is executed and then each note can play the chord singularly, and
50 * arpeggiation where we scan through the active (pressed) notes.
51 */
52 void
bristolArpeggiator(audioMain * audiomain,bristolMidiMsg * msg)53 bristolArpeggiator(audioMain *audiomain, bristolMidiMsg *msg)
54 {
55 float value;
56 int tval;
57 Baudio *baudio = findBristolAudio(audiomain->audiolist,
58 msg->params.bristol.channel, 0);
59
60 if (baudio == NULL)
61 return;
62
63 tval = (msg->params.bristol.valueMSB << 7) + msg->params.bristol.valueLSB;
64 value = ((float) tval) / CONTROLLER_RANGE;
65
66 /*
67 * Arpeggio parameters. There are 4, on/off, direction, range, rate. I am
68 * going to use a 5th for envelope retriggering. These could probably be
69 * moved into the baudio structure for general use?
70 */
71 switch (msg->params.bristol.controller) {
72 case BRISTOL_ARPEG_ENABLE:
73 /* on/off */
74 if (value != 0)
75 {
76 int i;
77
78 if (baudio->arpeggio.a.span == 0)
79 baudio->arpeggio.a.span = 1;
80
81 /*
82 * Clear out the known key list
83 */
84 if ((baudio->mixflags & BRISTOL_ARPEGGIATE) == 0)
85 for (i = 0; i < 127; i++)
86 baudio->arpeggio.a.notes[i].k
87 = baudio->arpeggio.a.notes[i].v = -1;
88
89 baudio->arpeggio.a.current = 0;
90
91 /* Put in some exclusion */
92 baudio->mixflags &= ~BRISTOL_SEQUENCE;
93 baudio->mixflags |= BRISTOL_ARPEGGIATE;
94 } else {
95 /*
96 * This is a little bit abrupt: arpeggiation is stopped but
97 * since the arpeggiated/sequenced keys are still active they
98 * all sound at once.
99 * MARK: 2602222
100 */
101 desequence(audiomain, baudio);
102 baudio->mixflags &= ~BRISTOL_ARPEGGIATE;
103 }
104 break;
105 case BRISTOL_ARPEG_DIR:
106 /* direction up/down/up+down/rnd */
107 switch (tval) {
108 case BRISTOL_ARPEG_UP:
109 default:
110 baudio->arpeggio.a.dir = BRISTOL_ARPEG_UP;
111 baudio->arpeggio.a.step = 1;
112 baudio->arpeggio.a.count = baudio->arpeggio.a.rate;
113 baudio->arpeggio.a.current = 0;
114 break;
115 case BRISTOL_ARPEG_DOWN:
116 baudio->arpeggio.a.dir = BRISTOL_ARPEG_DOWN;
117 baudio->arpeggio.a.step = -1;
118 baudio->arpeggio.a.count = baudio->arpeggio.a.rate;
119 baudio->arpeggio.a.current = 0;
120 break;
121 case BRISTOL_ARPEG_UD:
122 baudio->arpeggio.a.dir = BRISTOL_ARPEG_UD;
123 baudio->arpeggio.a.step = 1;
124 baudio->arpeggio.a.count = baudio->arpeggio.a.rate;
125 baudio->arpeggio.a.current = 0;
126 break;
127 case BRISTOL_ARPEG_RND:
128 baudio->arpeggio.a.dir = BRISTOL_ARPEG_RND;
129 baudio->arpeggio.a.step = 1;
130 baudio->arpeggio.a.count = baudio->arpeggio.a.rate;
131 baudio->arpeggio.a.current = 0;
132 break;
133 }
134 break;
135 case BRISTOL_ARPEG_RANGE:
136 /* range in octaves */
137 baudio->arpeggio.a.span = tval + 1;
138 break;
139 case BRISTOL_ARPEG_CLOCK:
140 if (value != 0)
141 baudio->arpeggio.flags |= BRISTOL_A_CLOCK;
142 else
143 baudio->arpeggio.flags &= ~BRISTOL_A_CLOCK;
144 break;
145 case BRISTOL_ARPEG_RATE:
146 /* rate */
147 baudio->arpeggio.a.rate = 2000 + (1.0 - value) *
148 audiomain->samplerate;
149 if (baudio->arpeggio.a.rate < baudio->arpeggio.a.count)
150 baudio->arpeggio.a.count = baudio->arpeggio.a.rate;
151 break;
152 case BRISTOL_ARPEG_TRIGGER:
153 /* retrigger */
154 if (value != 0)
155 baudio->arpeggio.flags |= BRISTOL_A_TRIGGER;
156 else
157 baudio->arpeggio.flags &= ~BRISTOL_A_TRIGGER;
158 break;
159 case BRISTOL_ARPEG_POLY_2:
160 if (value != 0) {
161 baudio->arpeggio.flags |= BRISTOL_POLY_2;
162 baudio->mixflags &= ~BRISTOL_SEQUENCE;
163 baudio->mixflags |= BRISTOL_V_UNISON|BRISTOL_ARPEGGIATE;
164 } else {
165 baudio->arpeggio.flags &= ~BRISTOL_POLY_2;
166 baudio->mixflags &= ~(BRISTOL_V_UNISON|BRISTOL_ARPEGGIATE);
167 }
168 break;
169
170 case BRISTOL_SEQ_DIR:
171 /* direction up/down/up+down/rnd */
172 switch (tval) {
173 case BRISTOL_ARPEG_UP:
174 default:
175 baudio->arpeggio.s.dir = BRISTOL_ARPEG_UP;
176 baudio->arpeggio.s.step = 1;
177 baudio->arpeggio.s.count = baudio->arpeggio.s.rate;
178 baudio->arpeggio.s.current = 0;
179 break;
180 case BRISTOL_ARPEG_DOWN:
181 baudio->arpeggio.s.dir = BRISTOL_ARPEG_DOWN;
182 baudio->arpeggio.s.step = -1;
183 baudio->arpeggio.s.count = baudio->arpeggio.s.rate;
184 baudio->arpeggio.s.current = 0;
185 break;
186 case BRISTOL_ARPEG_UD:
187 baudio->arpeggio.s.dir = BRISTOL_ARPEG_UD;
188 baudio->arpeggio.s.step = 1;
189 baudio->arpeggio.s.count = baudio->arpeggio.s.rate;
190 baudio->arpeggio.s.current = 0;
191 break;
192 case BRISTOL_ARPEG_RND:
193 baudio->arpeggio.s.dir = BRISTOL_ARPEG_RND;
194 baudio->arpeggio.s.step = 1;
195 baudio->arpeggio.s.count = baudio->arpeggio.s.rate;
196 baudio->arpeggio.s.current = 0;
197 break;
198 }
199 break;
200 case BRISTOL_SEQ_RANGE:
201 /* range in octaves */
202 baudio->arpeggio.s.span = tval + 1;
203 break;
204 case BRISTOL_SEQ_CLOCK:
205 if (value != 0)
206 baudio->arpeggio.flags |= BRISTOL_S_CLOCK;
207 else
208 baudio->arpeggio.flags &= ~BRISTOL_S_CLOCK;
209 break;
210 case BRISTOL_SEQ_RATE:
211 /* rate */
212 // tval = gainTable[C_RANGE_MIN_1 - tval].gain * CONTROLLER_RANGE;
213 // baudio->arpeggio.s.rate = baudio->arpeggio.s.count = tval * 5;
214 baudio->arpeggio.s.rate = 2000 + (1.0 - value) *
215 audiomain->samplerate;
216 if (baudio->arpeggio.s.rate < baudio->arpeggio.s.count)
217 baudio->arpeggio.s.count = baudio->arpeggio.s.rate;
218 break;
219 case BRISTOL_SEQ_TRIGGER:
220 /* retrigger */
221 if (value != 0)
222 baudio->arpeggio.flags |= BRISTOL_S_TRIGGER;
223 else
224 baudio->arpeggio.flags &= ~BRISTOL_S_TRIGGER;
225 break;
226
227 case BRISTOL_SEQ_ENABLE:
228 /* on/off */
229 if (value != 0) {
230 baudio->mixflags &= ~BRISTOL_ARPEGGIATE;
231 baudio->arpeggio.flags &= ~BRISTOL_SEQ_LEARN;
232 if (baudio->arpeggio.s.span == 0)
233 baudio->arpeggio.s.span = 1;
234
235 baudio->mixflags |= BRISTOL_SEQUENCE;
236 } else {
237 /*
238 * This is a little bit abrupt: arpeggiation is stopped but
239 * since the arpeggiated/sequenced keys are still active they
240 * all sound at once.
241 * MARK: 2602222
242 */
243 desequence(audiomain, baudio);
244 baudio->mixflags &= ~BRISTOL_SEQUENCE;
245 }
246 break;
247 case BRISTOL_SEQ_RESEQ:
248 /*
249 * We should reset the MAX count and start to learn new sequence.
250 *
251 * To prevent any damage we should consider either stopping the
252 * arpeggiator or putting in some silly long step time?
253 *
254 * When we have requested a resequence then we start accepting
255 * midi note events in our hook and also key requests from the GUI.
256 * At the moment key requests do not carry velocity, that is for
257 * future study since we don't even use it here, we just maintain
258 * it in the engine. Velocity support in the engine would require
259 * some changes in key event dispatching in the GUI too since it
260 * does the non-volatile memory and it only receives key id at the
261 * moment.
262 */
263 if (tval == 0)
264 {
265 if (baudio->arpeggio.s.max == 0)
266 baudio->arpeggio.s.max = BRISTOL_SEQ_MIN;
267 baudio->arpeggio.flags &= ~BRISTOL_SEQ_LEARN;
268 return;
269 } else {
270 baudio->arpeggio.flags |= BRISTOL_SEQ_LEARN;
271 baudio->mixflags &= ~BRISTOL_SEQUENCE;
272 baudio->arpeggio.s.current = 0;
273 baudio->arpeggio.s.max = 0;
274 }
275 break;
276 case BRISTOL_SEQ_KEY:
277 /*
278 * This should contain another key to put into the next location
279 * in our step list. There is a hook from midinote.c to get this
280 * from a master keyboard - NoteEvent() below.
281 *
282 * So, find the key and put it into the current MAX position then
283 * move MAX forward. This will be used to reprogram the arpeggiator
284 * from the GUI memories.
285 */
286 if (baudio->arpeggio.flags & BRISTOL_SEQ_LEARN) {
287 if (baudio->arpeggio.s.max == 0)
288 baudio->arpeggio.s.dif = tval;
289 baudio->arpeggio.s.notes[baudio->arpeggio.s.max].k
290 = tval - baudio->arpeggio.s.dif;
291 baudio->arpeggio.s.notes[baudio->arpeggio.s.max++].v = 120;
292 if (baudio->arpeggio.s.max > BRISTOL_SEQ_MAX)
293 baudio->arpeggio.s.max = BRISTOL_SEQ_MAX;
294 //printf("insert %i\n", tval - baudio->arpeggio.s.dif);
295 }
296 break;
297 case BRISTOL_CHORD_ENABLE:
298 /* on/off */
299 if (value != 0)
300 baudio->mixflags |= BRISTOL_CHORD;
301 else
302 baudio->mixflags &= ~BRISTOL_CHORD;
303 break;
304 case BRISTOL_CHORD_RESEQ:
305 /*
306 * We should reset the MAX count and start to learn new sequence.
307 *
308 * To prevent any damage we should consider either stopping the
309 * arpeggiator or putting in some silly long step time?
310 */
311 if (tval == 0)
312 {
313 baudio->arpeggio.flags &= ~BRISTOL_CHORD_LEARN;
314 return;
315 } else {
316 baudio->arpeggio.flags |= BRISTOL_CHORD_LEARN;
317 baudio->mixflags &= ~BRISTOL_CHORD;
318 baudio->arpeggio.c.max = 0;
319 }
320 break;
321 case BRISTOL_CHORD_KEY:
322 /*
323 * This should contain another key to put into the next location
324 * in our step list. There is a hook from midinote.c to get this
325 * from a master keyboard - NoteEvent() below.
326 *
327 * So, find the key and put it into the current MAX position then
328 * move MAX forward. This will be used to reprogram the chord
329 * from the GUI memories.
330 */
331 if (baudio->arpeggio.flags & BRISTOL_CHORD_LEARN) {
332 baudio->arpeggio.c.notes[baudio->arpeggio.c.max].k = tval;
333 baudio->arpeggio.c.notes[baudio->arpeggio.c.max].v = 120;
334 if (++baudio->arpeggio.c.max > BRISTOL_CHORD_MAX)
335 baudio->arpeggio.c.max = BRISTOL_CHORD_MAX;
336 }
337 break;
338 }
339 }
340
341 static void
arpeggiatorRePoly2(arpSeq * seq,int voicecount)342 arpeggiatorRePoly2(arpSeq *seq, int voicecount)
343 {
344 int i, max = seq->max, j = 0, vc = voicecount;
345
346 /*
347 * We minimally want to get one voice per key in our table. If we have
348 * more keys than voices we will flag an error, it is only going to be
349 * done with note on/off but the table should only contain entries up to
350 * voicecount. It does not break anything, the keys at the start of the
351 * list will not get voices.
352 *
353 * We need to make sure that:
354 *
355 * All voices get allocated.
356 * Voice do not get overallocated.
357 * If we have more voices than keys then they are piled at the end.
358 *
359 * Also, we use an upper part of the table to allocate a full list of
360 * notes. We could better stuff the '.v' with this information though.
361 */
362 for (i = 0; i < max; i++) {
363 vc -= (seq->notes[i].v = vc / (max - i));
364 for (;j < (voicecount - vc); j++)
365 seq->notes[j + BRISTOL_ARPEG_MAX].k = seq->notes[i].k;
366 }
367 }
368
369 /*
370 * This is used to initialise the arpeggiator settings when a new note is
371 * assigned. We will hijack the operation as well if we are recording a new
372 * sequence.
373 *
374 * The key has not yet been transposed.
375 *
376 * This is always called for NOTE ON, irrespective of SEQUENCER status.
377 */
378 void
bristolArpeggiatorNoteEvent(Baudio * baudio,bristolMidiMsg * msg)379 bristolArpeggiatorNoteEvent(Baudio *baudio, bristolMidiMsg *msg)
380 {
381 /*
382 * The code could be speeded up by putting the sequencer pointer into a
383 * buffer register.
384 */
385 register arpeggiator *arpeggio = &baudio->arpeggio;
386
387 if ((msg->command & MIDI_COMMAND_MASK) == MIDI_NOTE_OFF)
388 {
389 /*
390 * This will be used by the arpeggiator to maintain its note table.
391 *
392 * All arpeggiated note on events will go into the table, we scan
393 * through that up or down once per configured octave. We should keep
394 * a count of the number of arpeggiated notes so that we can make the
395 * random work correctly.
396 *
397 * Typically we would only want to keep a known keylist if we are
398 * actively arpeggiating however it could be used in other cases. It
399 * can later be considered to run continuously.
400 */
401 if (baudio->mixflags & BRISTOL_ARPEGGIATE) {
402 int i;
403
404 /*
405 * Key off are a little painful. We assume we lost a key from
406 * the list, we need to search for it and then move the rest of
407 * the list up.
408 */
409 for (i = 0; i < 128; i++)
410 if ((arpeggio->a.notes[i].k == msg->params.key.key)
411 || (arpeggio->a.notes[i].k == -1))
412 {
413 for (; i < 127; i++)
414 {
415 arpeggio->a.notes[i].k = arpeggio->a.notes[i + 1].k;
416 arpeggio->a.notes[i].v = arpeggio->a.notes[i + 1].v;
417 if (arpeggio->a.notes[i].k == -1)
418 break;
419 }
420 arpeggio->a.max = i;
421 break;
422 }
423
424 if (arpeggio->a.current >= i)
425 arpeggio->a.current = 0;
426
427 if (baudio->arpeggio.flags & BRISTOL_POLY_2)
428 arpeggiatorRePoly2(&arpeggio->a, baudio->voicecount);
429
430 /* Print the list for debugging * */
431 printf("Note %i, total %i:", msg->params.key.key, arpeggio->a.max);
432 for (i = 0; arpeggio->a.notes[i].k > 0; i++)
433 printf(" %i/%i ",arpeggio->a.notes[i].k,arpeggio->a.notes[i].v);
434 printf("\n");
435 }
436 return;
437 }
438
439 if (baudio->mixflags & BRISTOL_ARPEGGIATE) {
440 int i;
441
442 /*
443 * This next section of code produces an unordered list of which keys
444 * were pressed, or correctly stated they are ordered by time not by
445 * key id. For an arpeggiator this is arguably incorrect however it can
446 * be used in the same way as a key ordered list if so played and has
447 * more flexibility.
448 *
449 * We should make sure this list is a reasonable reflection of the
450 * baudio capabilities, ie, if we only have 4 voices assigned then we
451 * should not be letting this list grow beyond that, so if the max
452 * is already the same as our voice count then shift all the voices
453 * up and put the new one at the end.
454 */
455 if (arpeggio->a.max >= baudio->voicecount) {
456 for (i = 0; i < baudio->voicecount; i++)
457 arpeggio->a.notes[i].k = arpeggio->a.notes[i + 1].k;
458 arpeggio->a.notes[i].k = -1;
459 arpeggio->a.notes[arpeggio->a.max - 1].k = msg->params.key.key;
460 arpeggio->a.max = baudio->voicecount - 1;
461 } else
462 arpeggio->a.notes[arpeggio->a.max].k = msg->params.key.key;
463
464 /* Terminate the list */
465 arpeggio->a.notes[++arpeggio->a.max].k = -1;
466
467 if (baudio->arpeggio.flags & BRISTOL_POLY_2)
468 arpeggiatorRePoly2(&arpeggio->a, baudio->voicecount);
469
470 /* Print the list for debugging */
471 printf("Note On total: %i:", arpeggio->a.max);
472 for (i = 0; arpeggio->a.notes[i].k > 0; i++)
473 printf(" %i/%i ", arpeggio->a.notes[i].k, arpeggio->a.notes[i].v);
474 printf("\n");
475 }
476
477 if (baudio->arpeggio.flags & BRISTOL_CHORD_LEARN) {
478 if (arpeggio->c.max == 0) {
479 arpeggio->c.dif = msg->params.key.key;
480 arpeggio->c.vdif = msg->params.key.velocity;
481 }
482 arpeggio->c.notes[arpeggio->c.max].k
483 = msg->params.key.key - arpeggio->c.dif;
484 arpeggio->c.notes[arpeggio->c.max].v
485 = msg->params.key.velocity - arpeggio->c.vdif;
486 if (++arpeggio->c.max > BRISTOL_CHORD_MAX)
487 arpeggio->c.max = BRISTOL_CHORD_MAX;
488 }
489
490 /*
491 * We should not really be doing both of these at the same time however
492 * from the perspective of the recording it does not have to be an issue.
493 */
494 if (arpeggio->flags & BRISTOL_SEQ_LEARN) {
495 if (arpeggio->s.max == 0) {
496 arpeggio->s.dif = msg->params.key.key;
497 arpeggio->s.vdif = msg->params.key.velocity;
498 }
499 arpeggio->s.notes[arpeggio->s.max].k
500 = msg->params.key.key - arpeggio->s.dif;
501 //printf("live insert %i\n", arpeggio->s.notes[arpeggio->s.max].k);
502 arpeggio->s.notes[arpeggio->s.max].v
503 = msg->params.key.velocity - arpeggio->s.vdif;
504 if (++arpeggio->s.max >= BRISTOL_SEQ_MAX)
505 arpeggio->s.max = BRISTOL_SEQ_MAX;
506 } else if (baudio->mixflags & BRISTOL_SEQUENCE) {
507 if (baudio->lvoices <= 0) {
508 arpeggio->s.current = 0;
509
510 if (arpeggio->s.dir == BRISTOL_ARPEG_DOWN)
511 {
512 arpeggio->s.current = arpeggio->s.max;
513 arpeggio->s.octave = arpeggio->s.span;
514 arpeggio->s.step = -1;
515 } else {
516 arpeggio->s.step = 1;
517 arpeggio->s.octave = 1;
518 }
519
520 arpeggio->s.count = arpeggio->s.rate;
521 }
522 }
523 }
524
525 /*
526 * Take a sequence structure, roll its counters, then see what we have to
527 * do with the index selections for key and octave.
528 */
529 static int
arpeggioCounterCheck(arpSeq * seq,int samplecount)530 arpeggioCounterCheck(arpSeq *seq, int samplecount)
531 {
532 if ((seq->count -= samplecount) <= 0)
533 {
534 seq->count += seq->rate;
535
536 switch (seq->dir) {
537 case BRISTOL_ARPEG_UP:
538 default:
539 if (++seq->current >= seq->max)
540 {
541 seq->current = 0;
542 /*
543 * See if we need to move up an octave
544 */
545 if (++seq->octave > seq->span)
546 seq->octave = 1;
547 }
548 break;
549 case BRISTOL_ARPEG_DOWN:
550 if (--seq->current < 0)
551 {
552 seq->current = seq->max - 1;
553 /*
554 * See if we need to move down an octave
555 */
556 if (--seq->octave <= 0)
557 seq->octave = seq->span;
558 }
559 break;
560 case BRISTOL_ARPEG_UD:
561 if ((seq->current += seq->step) < 0)
562 {
563 /*
564 * We have gone through all the notes on the way
565 * down, see if we have got to bottom octave
566 */
567 if (--seq->octave <= 0)
568 {
569 /*
570 * Start going back up. The s.current is moved to
571 * 1 since we have already played zero and I do not
572 * want two strikes on the first and last notes.
573 * We might tweak that idea though, it fails if we
574 * only have a single note.
575 */
576 seq->current = 0;
577 seq->step = 1;
578 seq->octave = 1;
579 } else
580 /* Continue down through another octave */
581 seq->current = seq->max - 1;
582 } else if (seq->current >= seq->max) {
583 if (++seq->octave > seq->span)
584 {
585 /*
586 * Start going back down
587 */
588 seq->current = seq->max - 1;
589 seq->step = -1;
590 seq->octave = seq->span;
591 } else
592 /* Continue up through this octave */
593 seq->current = 0;
594 }
595 break;
596 case BRISTOL_ARPEG_RND:
597 /*
598 * We need to take a random note from a random octave
599 */
600 seq->current = (rand() & 0x00ff) % seq->max;
601 seq->octave = ((rand() & 0x00003) % seq->span) + 1;
602 break;
603 }
604 /*
605 * have wound the counters, tell parent to retrigger
606 */
607 return(1);
608 }
609 return(-1);
610 }
611
612 /*
613 * Called by the audio engine to roll through any emulations and see if they
614 * need some updates to the arpeggiation parameters.
615 *
616 * This should be redefined. We have a step sequencer and a chording system
617 * however for performance it would be better to have a true arpeggiator:
618 * Out of the available pressed noes scan through them up/down/ud/rnd.
619 *
620 * This is called once per baudio per frame. Another call, ArpegReFreq() is
621 * called per voice.
622 */
623 int
bristolArpegReAudio(audioMain * audiomain,Baudio * baudio)624 bristolArpegReAudio(audioMain *audiomain, Baudio *baudio)
625 {
626 baudio->arpeggio.flags &= ~BRISTOL_REQ_TRIGGER;
627
628 if (baudio->mixflags & BRISTOL_ARPEGGIATE)
629 {
630 if (baudio->arpeggio.flags & BRISTOL_POLY_2)
631 baudio->arpeggio.a.count = 8;
632 else if (arpeggioCounterCheck(&baudio->arpeggio.a,
633 audiomain->samplecount) >= 0)
634 baudio->arpeggio.flags |= BRISTOL_REQ_TRIGGER;
635 baudio->arpeggio.flags &= ~BRISTOL_DONE_FIRST;
636 }
637
638 /*
639 * If we are sequencing or arpeggiating then we need to look at their
640 * counters here. The counters are moved just once for however many voices
641 * we have but the counters are separate so we could anticipate sequencing
642 * arpeggios? Hm, perhaps not, but we can garantee exclusion elsewhere.
643 */
644 if (baudio->mixflags & BRISTOL_SEQUENCE)
645 {
646 if (baudio->arpeggio.flags & BRISTOL_SEQ_LEARN)
647 return(0);
648
649 if (arpeggioCounterCheck(&baudio->arpeggio.s, audiomain->samplecount)
650 >= 0)
651 baudio->arpeggio.flags |= BRISTOL_REQ_TRIGGER;
652 }
653
654 return(0);
655 }
656
657 int
bristolArpegReVoice(Baudio * baudio,bristolVoice * voice,float sr)658 bristolArpegReVoice(Baudio *baudio, bristolVoice *voice, float sr)
659 {
660 float dFreq = voice->dFreq;
661 int key = voice->key.key;
662
663 /*
664 * See if we should move on to the next sequence in the chain.
665 */
666 if (baudio->mixflags & BRISTOL_ARPEGGIATE)
667 {
668 if (baudio->arpeggio.flags & BRISTOL_POLY_2)
669 {
670 /*
671 * Spread notes across all voices. This is all done with ARPEG,
672 * POLY_2 and UNISON together. The first round of code may be a
673 * bit poppy as voices will not have much consistency in the keys
674 * they are assigned, will have to see.
675 */
676 if (--baudio->arpeggio.a.count >= 0)
677 key = baudio->arpeggio.a.notes[BRISTOL_ARPEG_MAX
678 + baudio->arpeggio.a.count].k;
679 else
680 return(-1);
681 } else {
682 /*
683 * We are only interested in working on the first voice. This may
684 * give us an occasional pop if the first note is released, we then
685 * have to select the next available voice.
686 */
687 if (baudio->arpeggio.flags & BRISTOL_DONE_FIRST)
688 return(-1);
689
690 baudio->arpeggio.flags |= BRISTOL_DONE_FIRST;
691 /*
692 * We are going to use just the first voice in our list and cycle
693 * it through the frequencies of the keys held.
694 */
695 if ((key = baudio->arpeggio.a.notes[baudio->arpeggio.a.current].k)
696 < 0)
697 return(-1);
698 key += baudio->arpeggio.a.octave * 12;
699 }
700 } else if (baudio->mixflags & BRISTOL_SEQUENCE) {
701 /*
702 * We have to track two sequence variables, the current note and
703 * then the current octave. I want the process to be similar to the
704 * arpeggiation - play the sequence once in each octave. If the octave
705 * is '1' then it is just the sequence.
706 */
707 key += baudio->arpeggio.s.notes[baudio->arpeggio.s.current].k
708 + baudio->arpeggio.s.octave * 12;
709 }
710
711 while (key < 0)
712 key += 12;
713 while (key >= 128)
714 key -= 12;
715
716 /*
717 * This is used by arpeggiation to redefine the operating frequency of the
718 * voice. It uses the same code as in the NoteOn request other than that
719 * we have dropped the temperature detune code here.
720 */
721 if (baudio->microtonalmap[key].step > 0.0f) {
722 /*
723 * This is the synth private microtonal map. This should really
724 * contain frequencies rather than the internal adjusted values?
725 */
726 voice->dFreq = baudio->microtonalmap[key].step;
727 voice->dfreq = baudio->microtonalmap[key].freq;
728 } else if (bristolMidiRoutines.bmr[7].floatmap[key] > 0.0f) {
729 /*
730 * This is the synth global microtonal map
731 */
732 voice->dFreq = bristolMidiRoutines.freq[key].step;
733 voice->dfreq = bristolMidiRoutines.freq[key].freq;
734 } else {
735 /*
736 * Otherwise this is the default ET internal map 'currentTable'.
737 */
738 voice->dFreq = baudio->ctab[key].step;
739 voice->dfreq = baudio->ctab[key].freq;
740 }
741
742 /*
743 * This is FFS. We need to alter the way a voice looks for glide. It
744 * is currently based on key numbers however here it needs to be based
745 * on frequencies.
746 *
747 * Now, if the destination frequency changed then we may need to request
748 * a retrigger and reorganise the glissando. The glissando algorithm should
749 * also be based on powers.....
750 */
751 if (dFreq != voice->dFreq)
752 {
753 if (baudio->glide != 0) {
754 voice->cFreqstep = (voice->dFreq - voice->cFreq)
755 / (baudio->glide * sr);
756 voice->cFreqmult = powf(M_E, logf(voice->dFreq/voice->cFreq)
757 / (baudio->glide * sr));
758
759 voice->cfreqmult = powf(M_E, logf(voice->dfreq/voice->cfreq)
760 / (baudio->glide * sr));
761 } else {
762 voice->cFreq = voice->dFreq;
763 voice->cfreq = voice->dfreq;
764 }
765 }
766
767 /*
768 * Trigger should be an arpeggiator flag
769 */
770 if ((baudio->arpeggio.flags & BRISTOL_A_TRIGGER)
771 && (((voice->flags & BRISTOL_KEYOFF) == 0)
772 || (baudio->arpeggio.a.max >= 1))
773 && (baudio->arpeggio.flags & BRISTOL_REQ_TRIGGER))
774 voice->flags |= BRISTOL_KEYREON;
775 if ((baudio->arpeggio.flags & BRISTOL_S_TRIGGER)
776 && ((voice->flags & BRISTOL_KEYOFF) == 0)
777 && (baudio->arpeggio.flags & BRISTOL_REQ_TRIGGER))
778 voice->flags |= BRISTOL_KEYREON;
779
780 return(0);
781 }
782
783 /*
784 * Called from bristolsystem.c when a client sends a HELLO message to start
785 * an emulation using the baudio given here.
786 */
787 void
bristolArpeggiatorInit(Baudio * baudio)788 bristolArpeggiatorInit(Baudio *baudio)
789 {
790 int i;
791
792 /*
793 * Clear out the arpeggiator known key list
794 */
795 for (i = 0; i < 127; i++)
796 baudio->arpeggio.a.notes[i].k = baudio->arpeggio.a.notes[i].v = -1;
797
798 /*
799 * Put in some default arpeggio sequence
800 */
801 baudio->arpeggio.s.notes[0].k = 0;
802 baudio->arpeggio.s.notes[1].k = 4;
803 baudio->arpeggio.s.notes[2].k = 7;
804 baudio->arpeggio.s.notes[3].k = 12;
805 baudio->arpeggio.s.notes[4].k = 16;
806 baudio->arpeggio.s.notes[0].v = 120;
807 baudio->arpeggio.s.notes[1].v = 120;
808 baudio->arpeggio.s.notes[2].v = 120;
809 baudio->arpeggio.s.notes[3].v = 120;
810 baudio->arpeggio.s.notes[4].v = 120;
811
812 baudio->arpeggio.s.dir = BRISTOL_ARPEG_UD;
813 baudio->arpeggio.s.max = 4;
814 baudio->arpeggio.s.span = 1;
815 baudio->arpeggio.s.step = 1;
816 baudio->arpeggio.s.rate = 400000;
817 baudio->arpeggio.s.current = 0;
818
819 baudio->arpeggio.flags = 0;
820
821 baudio->arpeggio.c.notes[0].k = 0;
822 baudio->arpeggio.c.notes[1].k = 4;
823 baudio->arpeggio.c.notes[2].k = 7;
824 baudio->arpeggio.c.notes[3].k = 12;
825 baudio->arpeggio.c.notes[0].v = 120;
826 baudio->arpeggio.c.notes[1].v = 120;
827 baudio->arpeggio.c.notes[2].v = 120;
828 baudio->arpeggio.c.notes[3].v = 120;
829 baudio->arpeggio.c.max = 4;
830 }
831
832