File: bristolsystem.c

package info (click to toggle)
bristol 0.60.10-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,652 kB
  • sloc: ansic: 124,457; sh: 10,579; makefile: 111
file content (506 lines) | stat: -rw-r--r-- 12,776 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506

/*
 *  Diverse Bristol audio routines.
 *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <unistd.h>

#include <bristol.h>
#include <bristolmidiapi.h>
#include <bristolmidiapidata.h>
#include <bristolmidi.h>

extern void inthandler();
extern void initBristolAudio();
#ifdef BRISTOL_PA
extern int bristolPulseInterface();
#endif
#ifdef _BRISTOL_JACK
extern int bristolJackInterface();
#endif
extern void allNotesOff();

static int initCount = 0;

int
bristolgetsid(Baudio *baudio, int sid)
{
	int tsid;

	if (baudio)
		tsid = bristolgetsid(baudio->next, sid);
	else
		return(sid);

/*	printf("bristolgetsid(%x, %i): %i\n", baudio, sid, tsid); */

	if (baudio->sid == tsid)
		return(++tsid);

/*	printf("nomatch: %i\n", tsid); */

	return(tsid);
}

int
bristolSystem(audioMain *audiomain, bristolMidiMsg *msg)
{
	int result = 0, hclose = -1;

	/*
	*/
	if (audiomain->debuglevel > 1)
		printf("bristolSystem(%i, %i, %i) %x\n",
			msg->params.bristol.operator,
			msg->params.bristol.controller,
			msg->params.bristol.from,
			(msg->params.bristol.valueMSB << 7) + msg->params.bristol.valueLSB);

	/*
	 * We are looking for a "hello" message from the GUI, which should include
	 * an algorithm index that we shall be running. We also need to look out 
	 * for exit requests.
	 */
	if (msg->params.bristol.operator != BRISTOL_SYSTEM)
		return(0);

	if (msg->params.bristol.controller == 0)
	{
		/*
		 * These are system global messages - start/stop/hello.
		 */
		int flags = (msg->params.bristol.valueMSB << 7) +
			msg->params.bristol.valueLSB;

		/*
		 * See if we can activate message connections
		 */
		if (flags & BRISTOL_HELLO)
		{
			int x, y;
			/*
			 * When we get a hello we need to suppress midi processing for a
			 * period of time whilst the new emulator is activated. It will be
			 * reactivated when the algorithm has initted later.
			 */
			bristolMidiOption(0, BRISTOL_NRP_MIDI_GO, 0);

			Baudio *baudio = (Baudio *) bristolmalloc0(sizeof(Baudio));

			baudio->mixflags = (BRISTOL_HOLDDOWN|BRISTOL_SENSE);
			/* Turn on sensing with a long timer, GUI can disable it later */
			baudio->sensecount = 15 * audiomain->samplerate;

			if (audiomain->atStatus == BRISTOL_EXIT)
				audiomain->midiflags |= BRISTOL_HELLO;

			/*
			 * We assume that anyone attempting a hello is going to want access
			 * to a bristolAudio channel. We should allocate this here, and give
			 * it an ID for the connection. This ID is returned in an ack back
			 * to the source.
			 */
			baudio->midichannel = msg->params.bristol.channel;
			baudio->samplerate = audiomain->samplerate;
			baudio->samplecount = audiomain->samplecount;

			/*
			 * Put in some default arpeggio values
			 */
			bristolArpeggiatorInit(baudio);

			baudio->sid = result = bristolgetsid(audiomain->audiolist, 0);

			if (baudio->sid == -1)
				printf("SID failure....\n");
			else if (audiomain->debuglevel > 2)
				printf("	alloc SID %i\n", baudio->sid);

			baudio->voicecount = (BRISTOL_PARAMMASK & flags);
			if (audiomain->voiceCount <= 0)
			{
				if ((audiomain->voiceCount = (BRISTOL_PARAMMASK & flags))
					< BRISTOL_VOICECOUNT)
					audiomain->voiceCount = BRISTOL_VOICECOUNT;
			}
			if (audiomain->voiceCount > BRISTOL_MAXVOICECOUNT)
				audiomain->voiceCount = BRISTOL_MAXVOICECOUNT;
			if (baudio->voicecount > BRISTOL_MAXVOICECOUNT)
				baudio->voicecount = BRISTOL_MAXVOICECOUNT;

			if (audiomain->debuglevel)
				printf("created %i voices: allocated %i to synth\n",
					audiomain->voiceCount, baudio->voicecount);

			if (audiomain->voiceCount <= 1)
				audiomain->voiceCount = 1;

			baudio->controlid = msg->params.bristol.from;

			baudio->next = audiomain->audiolist;
			if (audiomain->audiolist != NULL)
				audiomain->audiolist->last = baudio;
			audiomain->audiolist = baudio;

#ifndef BRISTOL_SEMAPHORE
			jack_ringbuffer_go(audiomain->rb);
#endif
		}

		if ((audiomain->midiflags & BRISTOL_HELLO) == 0)
		{
			printf("no hello, returning\n");
			return(0);
		}

		if (audiomain->debuglevel > 2)
			printf("commask %x\n", flags & BRISTOL_COMMASK);

		/*
		 * See what command we have, if any.
		 */
		switch (flags & BRISTOL_COMMASK)
		{
			case BRISTOL_REQSTART:
				if (audiomain->atStatus == BRISTOL_EXIT) {
					audiomain->atReq |= BRISTOL_REQSTART;

					while (audiomain->atStatus != BRISTOL_OK)
						usleep(100000);
				}
				break;
			case BRISTOL_REQSTOP:
				audiomain->atReq = BRISTOL_REQSTOP;
				break;
			case BRISTOL_DEBUGON:
				printf("Debugging enabled\n");
				audiomain->debuglevel = -1;
				break;
			case BRISTOL_DEBUGOF:
				printf("Debugging disabled\n");
				audiomain->debuglevel = 0;
				break;
			case BRISTOL_VOICES:
			{
				Baudio *baudio;

				if (audiomain->voiceCount <= 0)
					audiomain->voiceCount = (BRISTOL_PARAMMASK & flags);
				if (audiomain->voiceCount <= 1)
					audiomain->voiceCount = 1;

				if (audiomain->debuglevel >= 0)
					printf("configured %i voices\n", audiomain->voiceCount);

				baudio = audiomain->audiolist;
				while (baudio != NULL)
				{
					if ((baudio->sid == msg->params.bristol.channel)
							|| (msg->params.bristol.channel == SYSTEM_CHANNEL))
						break;

					baudio = baudio->next;
				}

				/*
				 * If we found an associated bristolAudio then configure its
				 * voices as well, since it may be fewer than the total number
				 * of configured voices.
				 */
				if (baudio != NULL)
				{
					baudio->voicecount = (BRISTOL_PARAMMASK & flags);
					if (audiomain->debuglevel)
						printf("configured %i channel voices\n",
							baudio->voicecount);
				}

				break;
			}
			case BRISTOL_INIT_ALGO:
			{
				Baudio *baudio;

				if (audiomain->atStatus != BRISTOL_OK)
					break;

				if (audiomain->debuglevel)
					printf("init request nr %i\n", initCount++);

				baudio = audiomain->audiolist;
				allNotesOff(audiomain, msg->params.bristol.channel);

				while (baudio != NULL)
				{
					if (baudio->sid == msg->params.bristol.channel)
						break;

					baudio = baudio->next;
				}

				audiomain->midiflags &= ~BRISTOL_PARAMMASK;
				audiomain->midiflags |= (BRISTOL_PARAMMASK & flags);

				initBristolAudio(audiomain, baudio);
				//bristolMidiOption(0, BRISTOL_NRP_MIDI_STOP, 0);

				break;
			}
			case BRISTOL_EXIT_ALGO:
			{
				Baudio *baudio = findBristolAudio(audiomain->audiolist,
					msg->params.bristol.channel, 0);

				if ((baudio == NULL) || (baudio->mixflags == 0))
					return(0);

				bristolMidiOption(0, BRISTOL_NRP_MIDI_GO, 0);

				/*
				printf("exit algo: %i, %x, %x\n",
					msg->params.bristol.channel,
						(size_t) baudio, (size_t) baudio->mixflags);
				*/

				allNotesOff(audiomain, -1);

				/*
				 * Flag that the audio thread remove the algorithm
				 */
				if (baudio != NULL)
					baudio->mixflags = (BRISTOL_HOLDDOWN | BRISTOL_REMOVE);

				/*
				 * Check if this was the last emulator. If it was then do some
				 * shutdown operations here.
				 */
				if (((audiomain->audiolist == NULL)
					|| ((audiomain->audiolist == baudio)
						&& (baudio->next == NULL)))
					&& (audiomain->flags & BRISTOL_MIDI_WAIT))
				{
					int i = 10;

					printf("empty audio list, cleaning up\n");

					while (audiomain->audiolist != NULL)
					{
						usleep(100000);

						if (audiomain->audiolist != NULL)
							printf("waiting for audiothread cleanup\n");

						if (--i == 0)
						{
							printf("timeout on audio thread cleanup\n");
							break;
						}
					}

					bristolMidiSendMsg(msg->params.bristol.from, 0, 127, 0, 0);

					/*
					 * It looks like this should be called first however we do
					 * still need the audio thread to be called by jack for the
					 * time being: the audio thread has do clean its stuff up
					 * before exit. When it has done its cleanup it returns to
					 * jack immediately if the audiolist is empty so now we can
					 * unregister the interface and clean up the rest of the
					 * app
					 */
					if (audiomain->flags & BRISTOL_JACK)
					{
						audiomain->atReq = BRISTOL_REQSTOP;
#ifdef BRISTOL_JACK_MULTI_CLOSE
#ifdef _BRISTOL_JACK // The bristol idle thread should do this.
						bristolJackInterface(NULL);
#endif
#endif
					}
					if (audiomain->flags & BRISTOL_PULSE)
					{
						audiomain->atReq = BRISTOL_REQSTOP;
#ifdef BRISTOL_PA // The bristol idle thread should do this.
						bristolPulseInterface(NULL);
#endif
					}

					bristolMidiSendMsg(msg->params.bristol.from, 0, 127, 0, 1);
					audiomain->atStatus = BRISTOL_TERM;

					inthandler();

					return(0);
				} else {
					int i = 20;

					while ((baudio->mixflags & BRISTOL_REMOVE) && (--i > 0))
						usleep(100000);
				}

				hclose = msg->params.bristol.from;

				bristolfree(baudio);

				bristolMidiOption(0, BRISTOL_NRP_MIDI_GO, 1);

				break;
			}
			/*
			 * This is broken, or we need to find every synth using this
			 * channel?
			 */
			case (BRISTOL_TRANSPOSE & BRISTOL_COMMASK):
			{
				Baudio *baudio = findBristolAudio(audiomain->audiolist,
					msg->params.bristol.channel, 0);

				if (baudio == NULL)
					break;

				/*
				 * We should also consider restating existing frequencies but
				 * that is for later. We should support negative transposes.
				 *
				 * Negative transposes can be done for 2 octaves only - we only
				 * support +ve numbers in messaging so we added in 24 to the
				 * flag that we remove now.
				 */
				baudio->transpose = ((int) (BRISTOL_PARAMMASK & flags)) - 24;

				if (baudio->midiflags & BRISTOL_MIDI_DEBUG1)
					printf("tranpose now %i\n", baudio->transpose);

				break;
			}
			case BRISTOL_HOLD:
			{
				Baudio *baudio = findBristolAudio(audiomain->audiolist,
					msg->params.bristol.channel, 0);

				if (baudio == NULL)
					break;

				if (BRISTOL_PARAMMASK & flags)
					baudio->mixflags |= BRISTOL_KEYHOLD;
				else {
					baudio->mixflags &= ~BRISTOL_KEYHOLD;
					allNotesOff(audiomain, baudio->midichannel);
				}

				break;
			}
			case BRISTOL_LOWKEY:
			{
				Baudio *baudio = findBristolAudio(audiomain->audiolist,
					msg->params.bristol.channel, 0);

				if (baudio == NULL)
					break;

				baudio->lowkey = (BRISTOL_PARAMMASK & flags);

				break;
			}
			case BRISTOL_HIGHKEY:
			{
				Baudio *baudio = findBristolAudio(audiomain->audiolist,
					msg->params.bristol.channel, 0);

				if (baudio == NULL)
					break;

				baudio->highkey = (BRISTOL_PARAMMASK & flags);
				break;
			}
			case BRISTOL_UNISON:
			{
				Baudio *baudio = findBristolAudio(audiomain->audiolist,
					msg->params.bristol.channel, 0);

				if (msg->params.bristol.valueLSB > 0)
					baudio->mixflags |= BRISTOL_V_UNISON;
				else
					baudio->mixflags &= ~BRISTOL_V_UNISON;
				break;
			}
			case BRISTOL_ALL_NOTES_OFF:
				allNotesOff(audiomain, -1);
				break;
			case BRISTOL_MIDICHANNEL:
			{
				Baudio *baudio;

				allNotesOff(audiomain, -1);

				baudio = audiomain->audiolist;

				while (baudio != NULL)
				{
					if (baudio->sid == msg->params.bristol.channel)
						break;

					baudio = baudio->next;
				}

				/*
				 * We may need to put an all notes off in here.
				 */
				if (baudio != NULL)
					baudio->midichannel = flags & BRISTOL_PARAMMASK;

				break;
			}
			case BRISTOL_REQ_FORWARD:
				bristolMidiOption(msg->params.bristol.from,
					BRISTOL_NRP_REQ_FORWARD, BRISTOL_PARAMMASK & flags);
				break;
			case BRISTOL_REQ_DEBUG:
				//printf("SETUP DEBUG LEVELS: %i\n", BRISTOL_PARAMMASK & flags);
				audiomain->debuglevel = BRISTOL_PARAMMASK & flags;
				if (audiomain->debuglevel >= 2)
					bristolMidiOption(0, BRISTOL_NRP_DEBUG,
						audiomain->debuglevel - 2);
				break;
			default:
				break;
		}

		if (audiomain->debuglevel > 2)
			printf("cont 2: %i: res %i\n", msg->params.bristol.from, result);

		/*
		 * return a message, with the controlid assumably at the head
		 * of the playlist - we have just created it.
		msg->params.bristol.valueLSB = result;
		bristolMidiPrint(msg);
		 */
		bristolMidiSendMsg(msg->params.bristol.from, 0, 127, 0, result);

/*			audiomain->audiolist->controlid); */
		if (hclose > 0)
		{
			usleep(100000);
			bristolMidiClose(hclose);
		}
	}

	return(0);
}