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 
23 #include <fcntl.h>
24 
25 #include "brighton.h"
26 #include "brightonMini.h"
27 #include "brightoninternals.h"
28 
29 static int masterInit();
30 static int masterConfigure();
31 static int masterCallback(brightonWindow * , int, int, float);
32 static int masterMidiCallback(brightonWindow * , int, int, float);
33 
34 extern guimain global;
35 
36 #include "brightonKeys.h"
37 
38 #define BLUEPRINT "bitmaps/blueprints/master7.xpm"
39 
40 #define OP_COUNT 6
41 #define FIRST_DEV 0
42 #define PARAM_COUNT 20
43 #define ALGOS_COUNT 29
44 #define MEM_COUNT 17
45 #define ACTIVE_DEVS (PARAM_COUNT * OP_COUNT + ALGOS_COUNT + FIRST_DEV)
46 
47 #define DISPLAY_DEV (MEM_COUNT - 3)
48 #define DISPLAY_PANEL 7
49 
50 #define OP_START FIRST_DEV
51 #define OP1_START FIRST_DEV
52 #define OP2_START (OP1_START + PARAM_COUNT)
53 #define OP3_START (OP2_START + PARAM_COUNT)
54 #define OP4_START (OP3_START + PARAM_COUNT)
55 #define OP5_START (OP4_START + PARAM_COUNT)
56 #define OP6_START (OP5_START + PARAM_COUNT)
57 #define ALGO_START (PARAM_COUNT * OP_COUNT + FIRST_DEV)
58 #define MEM_START ACTIVE_DEVS
59 
60 #define OP_PANEL 0
61 #define ALGOS_PANEL OP_COUNT
62 #define MEM_PANEL (ALGOS_PANEL + 1)
63 
64 #define KEY_PANEL 1
65 
66 #define DEVICE_COUNT (ACTIVE_DEVS + MEM_COUNT)
67 
68 /*
69  * This structure is for device definition. The structure is defined in
70  * include/brighton.h, further definitions in brighton/brightonDevtable.h and
71  * include/brightoninternals.h
72  *
73  *	typedef int (*brightonCallback)(int, float);
74  *	typedef struct BrightonLocations {
75  *		int device; 0=rotary, 1=scale, etc.
76  *		float relx, rely; relative position with regards to 1000 by 1000 window
77  *		float relw, relh; relative height.
78  *		int from, to;
79  *		brightonCallback callback; specific to this dev
80  *		char *image; bitmap. If zero take a device default.
81  *		int flags;
82  *	} brightonLocations;
83  *
84  * This example is for a masterBristol type synth interface.
85  */
86 
87 #define R1 150
88 #define R2 665
89 
90 #define S1 300
91 #define S2 40
92 #define S3 250
93 
94 #define D2 50
95 
96 #define S4 45
97 #define S5 250
98 
99 #define AR1 150
100 #define AR2 425
101 #define AR3 700
102 
103 #define AC0 50
104 #define AC1 (AC0 + D2)
105 #define AC2 (AC1 + D2)
106 #define AC3 (AC2 + D2)
107 #define AC4 (AC3 + D2)
108 #define AC5 (AC4 + D2)
109 #define AC6 (AC5 + D2)
110 #define AC7 (AC6 + D2)
111 #define AC8 (AC7 + D2 + 253)
112 #define AC9 (AC8 + D2 + 142)
113 #define AC10 (AC9 + D2 + 10)
114 
115 static brightonLocations algopanel[ALGOS_COUNT] = {
116 	{"", 2, AC0, AR1, S4, S5, 0, 1, 0,
117 		"bitmaps/buttons/pressoff.xpm",
118 		"bitmaps/buttons/presson.xpm", 0},
119 	{"", 2, AC1, AR1, S4, S5, 0, 1, 0,
120 		"bitmaps/buttons/pressoff.xpm",
121 		"bitmaps/buttons/presson.xpm", 0},
122 	{"", 2, AC2, AR1, S4, S5, 0, 1, 0,
123 		"bitmaps/buttons/pressoff.xpm",
124 		"bitmaps/buttons/presson.xpm", 0},
125 	{"", 2, AC3, AR1, S4, S5, 0, 1, 0,
126 		"bitmaps/buttons/pressoff.xpm",
127 		"bitmaps/buttons/presson.xpm", 0},
128 	{"", 2, AC4, AR1, S4, S5, 0, 1, 0,
129 		"bitmaps/buttons/pressoff.xpm",
130 		"bitmaps/buttons/presson.xpm", 0},
131 	{"", 2, AC5, AR1, S4, S5, 0, 1, 0,
132 		"bitmaps/buttons/pressoff.xpm",
133 		"bitmaps/buttons/presson.xpm", 0},
134 	{"", 2, AC6, AR1, S4, S5, 0, 1, 0,
135 		"bitmaps/buttons/pressoff.xpm",
136 		"bitmaps/buttons/presson.xpm", 0},
137 	{"", 2, AC7, AR1, S4, S5, 0, 1, 0,
138 		"bitmaps/buttons/pressoff.xpm",
139 		"bitmaps/buttons/presson.xpm", 0},
140 	{"", 2, AC0, AR2, S4, S5, 0, 1, 0,
141 		"bitmaps/buttons/pressoff.xpm",
142 		"bitmaps/buttons/presson.xpm", 0},
143 	{"", 2, AC1, AR2, S4, S5, 0, 1, 0,
144 		"bitmaps/buttons/pressoff.xpm",
145 		"bitmaps/buttons/presson.xpm", 0},
146 	{"", 2, AC2, AR2, S4, S5, 0, 1, 0,
147 		"bitmaps/buttons/pressoff.xpm",
148 		"bitmaps/buttons/presson.xpm", 0},
149 	{"", 2, AC3, AR2, S4, S5, 0, 1, 0,
150 		"bitmaps/buttons/pressoff.xpm",
151 		"bitmaps/buttons/presson.xpm", 0},
152 	{"", 2, AC4, AR2, S4, S5, 0, 1, 0,
153 		"bitmaps/buttons/pressoff.xpm",
154 		"bitmaps/buttons/presson.xpm", 0},
155 	{"", 2, AC5, AR2, S4, S5, 0, 1, 0,
156 		"bitmaps/buttons/pressoff.xpm",
157 		"bitmaps/buttons/presson.xpm", 0},
158 	{"", 2, AC6, AR2, S4, S5, 0, 1, 0,
159 		"bitmaps/buttons/pressoff.xpm",
160 		"bitmaps/buttons/presson.xpm", 0},
161 	{"", 2, AC7, AR2, S4, S5, 0, 1, 0,
162 		"bitmaps/buttons/pressoff.xpm",
163 		"bitmaps/buttons/presson.xpm", 0},
164 	{"", 2, AC0, AR3, S4, S5, 0, 1, 0,
165 		"bitmaps/buttons/pressoff.xpm",
166 		"bitmaps/buttons/presson.xpm", 0},
167 	{"", 2, AC1, AR3, S4, S5, 0, 1, 0,
168 		"bitmaps/buttons/pressoff.xpm",
169 		"bitmaps/buttons/presson.xpm", 0},
170 	{"", 2, AC2, AR3, S4, S5, 0, 1, 0,
171 		"bitmaps/buttons/pressoff.xpm",
172 		"bitmaps/buttons/presson.xpm", 0},
173 	{"", 2, AC3, AR3, S4, S5, 0, 1, 0,
174 		"bitmaps/buttons/pressoff.xpm",
175 		"bitmaps/buttons/presson.xpm", 0},
176 	{"", 2, AC4, AR3, S4, S5, 0, 1, 0,
177 		"bitmaps/buttons/pressoff.xpm",
178 		"bitmaps/buttons/presson.xpm", 0},
179 	{"", 2, AC5, AR3, S4, S5, 0, 1, 0,
180 		"bitmaps/buttons/pressoff.xpm",
181 		"bitmaps/buttons/presson.xpm", 0},
182 	{"", 2, AC6, AR3, S4, S5, 0, 1, 0,
183 		"bitmaps/buttons/pressoff.xpm",
184 		"bitmaps/buttons/presson.xpm", 0},
185 	{"", 2, AC7, AR3, S4, S5, 0, 1, 0,
186 		"bitmaps/buttons/pressoff.xpm",
187 		"bitmaps/buttons/presson.xpm", 0},
188 	{"", 2, AC9, R1 + 40, S4, S5, 0, 1, 0, "bitmaps/buttons/pressoffg.xpm",
189 		"bitmaps/buttons/pressong.xpm", BRIGHTON_CHECKBUTTON},
190 	{"", 4, AC7 + 90, AR1, 190, 800, 0, 1, 0, 0, 0, 0},
191 };
192 
193 /*
194  * This is a set of globals for the main window rendering. Again taken from
195  * include/brighton.h
196  */
197 brightonApp masterApp = {
198 	"master",
199 	0, /* no blueprint on wood background. */
200 	"bitmaps/textures/metal6.xpm",
201 	BRIGHTON_STRETCH,
202 	masterInit,
203 	masterConfigure, /* 3 callbacks, unused? */
204 	masterMidiCallback,
205 	destroySynth,
206 	{8, 0, 2, 2, 5, 520, 0, 0},
207 	1000, 300, 0, 0,
208 	8,
209 	{
210 		{
211 			"MASTER",
212 			"bitmaps/blueprints/masteralgo.xpm",
213 			"bitmaps/textures/metal5.xpm",
214 			0, /* flags */
215 			0,
216 			0,
217 			masterCallback,
218 			25, 40, 310, 530,
219 			ALGOS_COUNT,
220 			algopanel
221 		},
222 		{
223 			"Keyboard",
224 			0,
225 			"bitmaps/newkeys/dkbg.xpm", /* flags */
226 			0x020|BRIGHTON_STRETCH,
227 			0,
228 			0,
229 			keyCallback,
230 			110, 615, 870, 380,
231 			KEY_COUNT_6_OCTAVE,
232 			keys6octave
233 		},
234 		{
235 			"MASTER",
236 			"bitmaps/blueprints/masteralgo.xpm",
237 			"bitmaps/textures/metal5.xpm",
238 			0, /* flags */
239 			0,
240 			0,
241 			masterCallback,
242 			350, 40, 310, 530,
243 			ALGOS_COUNT,
244 			algopanel
245 		},
246 		{
247 			"Mods",
248 			"bitmaps/blueprints/mods.xpm",
249 			"bitmaps/textures/metal5.xpm", // flags */
250 			0,
251 			0,
252 			0,
253 			modCallback,
254 			15, 615, 95, 380,
255 			2,
256 			mods
257 		},
258 		/* Wood rim */
259 		{
260 			"Wood",
261 			0,
262 			"bitmaps/textures/wood2.xpm",
263 			0,
264 			0,
265 			0,
266 			0,
267 			0, 0, 15, 1000,
268 			0,
269 			0
270 		},
271 		{
272 			"Wood",
273 			0,
274 			"bitmaps/textures/wood2.xpm",
275 			0,
276 			0,
277 			0,
278 			0,
279 			985, 0, 15, 1000,
280 			0,
281 			0
282 		},
283 		{
284 			"Wood",
285 			0,
286 			"bitmaps/textures/wood.xpm",
287 			0,
288 			0,
289 			0,
290 			0,
291 			1, 5, 13, 980,
292 			0,
293 			0
294 		},
295 		{
296 			"Wood",
297 			0,
298 			"bitmaps/textures/wood.xpm",
299 			0,
300 			0,
301 			0,
302 			0,
303 			986, 5, 13, 980,
304 			0,
305 			0
306 		},
307 	}
308 };
309 
310 /*static dispatcher dispatch[DEVICE_COUNT]; */
311 
312 static int
masterMidiSendMsg(void * synth,int fd,int chan,int c,int o,int v)313 masterMidiSendMsg(void *synth, int fd, int chan, int c, int o, int v)
314 {
315 /*printf("%i, %i, %i\n", c, o, v); */
316 	bristolMidiSendMsg(fd, chan, c, o, v);
317 	return(0);
318 }
319 
320 int
masterLoadMem(guiSynth * synth,char * algo,char * name,int location,int active,int skip,int flags)321 masterLoadMem(guiSynth *synth, char *algo, char *name, int location, int active,
322 int skip, int flags)
323 {
324 	brightonEvent event;
325 	int op;
326 	float mo, fmop[OP_COUNT][2];
327 
328 	/*
329 	 * See if the memory actually exists. This is a bit of file system overhead
330 	 * but prevents attempting to load non-existant memories
331 	 */
332 	op = loadMemory(synth, algo, name, location, active, skip, BRISTOL_STAT);
333 
334 	if (flags == 2)
335 		return(op);
336 
337 	if (op < 0)
338 		return(op);
339 
340 	event.type = BRIGHTON_FLOAT;
341 	event.value = 0.0;
342 
343 	/*
344 	 * Zero out diverse gain functions to prevent a noisy transition.
345 	 * What happens is that as the parameters change and the algorithm alters
346 	 * we get a LOT of FM noise. We can dump this by zeroing the output
347 	 * parameters. We have to be careful, since if the memory does not load,
348 	 * for example it does not exist, then we have to reset the parameters
349 	 * afterwards.
350 	 *
351 	 * Start with main out.
352 	 */
353 	mo = synth->mem.param[OP_COUNT * PARAM_COUNT + 26];
354 	brightonParamChange(synth->win, ALGOS_PANEL, 26, &event);
355 
356 	for (op = 0; op < 6; op++)
357 	{
358 		/*
359 		 * Output gain
360 		 */
361 		fmop[op][0] = synth->mem.param[op * PARAM_COUNT];
362 		brightonParamChange(synth->win, op, 0, &event);
363 		/*
364 		 * Input gain
365 		 */
366 		fmop[op][1] = synth->mem.param[op * PARAM_COUNT + 3];
367 		brightonParamChange(synth->win, op, 3, &event);
368 	}
369 
370 	if (loadMemory(synth, algo, name, location, active, skip, flags) == 0)
371 		return(0);
372 
373 	/*
374 	 * Load failed, return the gain parameters to their previous values.
375 	 */
376 	event.value = mo;
377 	brightonParamChange(synth->win, ALGOS_PANEL, 26, &event);
378 
379 	for (op = 0; op < 6; op++)
380 	{
381 		/*
382 		 * Output gain
383 		 */
384 		event.value = fmop[op][0];
385 		brightonParamChange(synth->win, op, 0, &event);
386 		/*
387 		 * Input gain
388 		 */
389 		event.value = fmop[op][1];
390 		brightonParamChange(synth->win, op, 3, &event);
391 	}
392 	return(-1);
393 }
394 
395 static int
masterMidiCallback(brightonWindow * win,int command,int value,float v)396 masterMidiCallback(brightonWindow *win, int command, int value, float v)
397 {
398 	guiSynth *synth = findSynth(global.synths, win);
399 
400 	printf("midi callback: %x, %i\n", command, value);
401 
402 	switch(command)
403 	{
404 		case MIDI_PROGRAM:
405 			printf("midi program: %x, %i\n", command, value);
406 			synth->location = value;
407 			masterLoadMem(synth, "master", 0, synth->location,
408 				synth->mem.active, FIRST_DEV, 0);
409 			break;
410 		case MIDI_BANK_SELECT:
411 			printf("midi banksel: %x, %i\n", command, value);
412 			synth->bank = value;
413 			break;
414 	}
415 	return(0);
416 }
417 
418 static void
masterMemory(guiSynth * synth,int fd,int chan,int c,int o,int v)419 masterMemory(guiSynth *synth, int fd, int chan, int c, int o, int v)
420 {
421 	switch (c) {
422 		default:
423 		case 0:
424 			synth->location = synth->location * 10 + o;
425 
426 			if (synth->location >= 1000)
427 				synth->location = o;
428 			if (masterLoadMem(synth, "master", 0, synth->location,
429 				synth->mem.active, FIRST_DEV, BRISTOL_STAT) < 0)
430 				displayPanelText(synth, "FRE", synth->location,
431 					DISPLAY_PANEL, DISPLAY_DEV);
432 			else
433 				displayPanelText(synth, "PRG", synth->location,
434 					DISPLAY_PANEL, DISPLAY_DEV);
435 			break;
436 		case 1:
437 			if (masterLoadMem(synth, "master", 0, synth->location,
438 				synth->mem.active, FIRST_DEV, 0) < 0)
439 				displayPanelText(synth, "FRE", synth->location,
440 					DISPLAY_PANEL, DISPLAY_DEV);
441 			else
442 				displayPanelText(synth, "PRG", synth->location,
443 					DISPLAY_PANEL, DISPLAY_DEV);
444 			break;
445 		case 2:
446 			saveMemory(synth, "master", 0, synth->location, FIRST_DEV);
447 			displayPanelText(synth, "PRG", synth->location,
448 				DISPLAY_PANEL, DISPLAY_DEV);
449 			break;
450 		case 3:
451 			while (masterLoadMem(synth, "master", 0, --synth->location,
452 				synth->mem.active, FIRST_DEV, 0) < 0)
453 			{
454 				if (synth->location < 0)
455 					synth->location = 999;
456 			}
457 			displayPanelText(synth, "PRG", synth->location,
458 				DISPLAY_PANEL, DISPLAY_DEV);
459 			break;
460 		case 4:
461 			while (masterLoadMem(synth, "master", 0, ++synth->location,
462 				synth->mem.active, FIRST_DEV, 0) < 0)
463 			{
464 				if (synth->location > 999)
465 					synth->location = -1;
466 			}
467 			displayPanelText(synth, "PRG", synth->location,
468 				DISPLAY_PANEL, DISPLAY_DEV);
469 			break;
470 	}
471 }
472 
473 static int
masterMidi(guiSynth * synth,int fd,int chan,int c,int o,int v)474 masterMidi(guiSynth *synth, int fd, int chan, int c, int o, int v)
475 {
476 	int newchan;
477 
478 	if ((synth->flags & OPERATIONAL) == 0)
479 		return(0);
480 
481 	if (c == 1) {
482 		if ((newchan = synth->midichannel - 1) < 0)
483 		{
484 			synth->midichannel = 0;
485 			return(0);
486 		}
487 	} else {
488 		if ((newchan = synth->midichannel + 1) >= 16)
489 		{
490 			synth->midichannel = 15;
491 			return(0);
492 		}
493 	}
494 
495 	if (global.libtest == 0)
496 	{
497 		bristolMidiSendMsg(global.controlfd, synth->sid,
498 			127, 0, BRISTOL_MIDICHANNEL|newchan);
499 	}
500 
501 	synth->midichannel = newchan;
502 
503 	displayPanelText(synth, "MIDI", synth->midichannel + 1,
504 		DISPLAY_PANEL, DISPLAY_DEV);
505 
506 	return(0);
507 }
508 
509 /*
510  * For the sake of ease of use, links have been placed here to be called
511  * by any of the devices created. They would be better in some other file,
512  * perhaps with this as a dispatch.
513  *
514  * Param refers to the device index in the locations table given below.
515  */
516 static int
masterCallback(brightonWindow * win,int panel,int index,float value)517 masterCallback(brightonWindow * win, int panel, int index, float value)
518 {
519 	guiSynth *synth = findSynth(global.synths, win);
520 	int sendvalue;
521 
522 	if (synth == 0)
523 		return(0);
524 
525 	if (masterApp.resources[panel].devlocn[index].to == 1)
526 		sendvalue = value * C_RANGE_MIN_1;
527 	else
528 		sendvalue = value;
529 
530 	switch (panel) {
531 		case OP_PANEL:
532 		case OP_PANEL + 1:
533 		case OP_PANEL + 2:
534 		case OP_PANEL + 3:
535 		case OP_PANEL + 4:
536 		case OP_PANEL + 5:
537 			index += panel * PARAM_COUNT;
538 			break;
539 		case ALGOS_PANEL:
540 			index += ALGO_START;
541 			break;
542 		case MEM_PANEL:
543 			index += MEM_START;
544 			break;
545 		default:
546 			printf("unknown panel\n");
547 			break;
548 	}
549 
550 	if ((synth->flags & OPERATIONAL) == 0)
551 		return(0);
552 
553 /*	printf("masterCallback(%i, %f): %x\n", index, value, synth); */
554 
555 	synth->mem.param[index] = value;
556 
557 	synth->dispatch[index].routine(synth,
558 		global.controlfd, synth->sid,
559 		synth->dispatch[index].controller,
560 		synth->dispatch[index].operator,
561 		sendvalue);
562 #ifdef DEBUG
563 	else
564 		printf("dispatch[%p,%i](%i, %i, %i, %i, %i)\n", synth, index,
565 			global.controlfd, synth->sid,
566 			synth->dispatch[index].controller,
567 			synth->dispatch[index].operator,
568 			sendvalue);
569 #endif
570 
571 	return(0);
572 }
573 
574 static void
masterAlgo(guiSynth * synth,int fd,int chan,int c,int o,int v)575 masterAlgo(guiSynth *synth, int fd, int chan, int c, int o, int v)
576 {
577 	brightonEvent event;
578 
579 /*	printf("masterAlgo(%x, %i, %i, %i, %i, %i): %i\n", */
580 /*		synth, fd, chan, c, o, v, synth->mem.param[v - 1]); */
581 
582 	/*
583 	 * These will be radio buttons
584 	 */
585 	if (synth->dispatch[ALGO_START].other2)
586 	{
587 		synth->dispatch[ALGO_START].other2 = 0;
588 		return;
589 	}
590 
591 	if (v == 0)
592 		return;
593 
594 	if (synth->dispatch[ALGO_START].other1 >= 0)
595 	{
596 		synth->dispatch[ALGO_START].other2 = 1;
597 
598 		if (synth->dispatch[ALGO_START].other1 != o)
599 			event.value = 0;
600 		else
601 			event.value = 1;
602 
603 /*printf("other one is %i\n", synth->dispatch[ALGO_START].other1); */
604 		brightonParamChange(synth->win, ALGOS_PANEL,
605 			synth->dispatch[ALGO_START].other1, &event);
606 	}
607 
608 	if (v != 0)
609 	{
610 		char bitmap[128];
611 
612 		event.type = BRIGHTON_MEM;
613 
614 		sprintf(bitmap, "bitmaps/images/algo%i.xpm", o);
615 		event.m = bitmap;
616 
617 /*printf("%i %i: RECONFIGURING BITMAP\n", v, synth->mem.param[ALGO_START + o]); */
618 		brightonParamChange(synth->win,
619 			ALGOS_PANEL, ALGOS_COUNT - 1, &event);
620 	}
621 
622 	bristolMidiSendMsg(global.controlfd, synth->sid,
623 		126, 101, o);
624 
625 	synth->dispatch[ALGO_START].other1 = o;
626 }
627 
628 static void
masterTune(guiSynth * synth)629 masterTune(guiSynth *synth)
630 {
631 	brightonEvent event;
632 
633 	printf("masterTune(%p, %i, %i)\n", synth->win, OP_PANEL, OP1_START);
634 
635 	event.value = 0.5;
636 	brightonParamChange(synth->win, 0, 1, &event);
637 	brightonParamChange(synth->win, 1, 1, &event);
638 	brightonParamChange(synth->win, 2, 1, &event);
639 	brightonParamChange(synth->win, 3, 1, &event);
640 	brightonParamChange(synth->win, 4, 1, &event);
641 	brightonParamChange(synth->win, 5, 1, &event);
642 }
643 
644 /*
645  * Any location initialisation required to run the callbacks. For bristol, this
646  * will connect to the engine, and give it some base parameters.
647  * May need to generate some application specific menus.
648  * Will also then make specific requests to some of the devices to alter their
649  * rendering.
650  */
651 static int
masterInit(brightonWindow * win)652 masterInit(brightonWindow *win)
653 {
654 	guiSynth *synth = findSynth(global.synths, win);
655 	dispatcher *dispatch;
656 	int i;
657 /*
658 	brightonEvent event;
659 	char bitmap[128];
660 
661 	event.type = BRIGHTON_MEM;
662 	event.m = OP2XPM;
663 	brightonParamChange(synth->win,
664 		ALGOS_PANEL, ALGOS_COUNT - 1, &event);
665 */
666 
667 	if (synth == 0)
668 	{
669 		synth = findSynth(global.synths, 0);
670 		if (synth == 0)
671 		{
672 			printf("cannot init\n");
673 			return(0);
674 		}
675 	}
676 
677 	synth->win = win;
678 
679 	printf("Initialise the master link to bristol: %p\n", synth->win);
680 
681 	synth->mem.param = (float *) brightonmalloc(DEVICE_COUNT * sizeof(float));
682 	synth->mem.count = DEVICE_COUNT;
683 	synth->mem.active = ACTIVE_DEVS;
684 	synth->dispatch = (dispatcher *)
685 		brightonmalloc(DEVICE_COUNT * sizeof(dispatcher));
686 	dispatch = synth->dispatch;
687 
688 	/*
689 	 * We really want to have three connection mechanisms. These should be
690 	 *	1. Unix named sockets.
691 	 *	2. UDP sockets.
692 	 *	3. MIDI pipe.
693 	 */
694 	if (!global.libtest)
695 		if ((synth->sid = initConnection(&global, synth)) < 0)
696 			return(-1);
697 
698 	for (i = 0; i < DEVICE_COUNT; i++)
699 		synth->dispatch[i].routine = masterMidiSendMsg;
700 
701 	/* algo panel */
702 	dispatch[ALGO_START].operator = 0;
703 	dispatch[ALGO_START + 1].operator = 1;
704 	dispatch[ALGO_START + 2].operator = 2;
705 	dispatch[ALGO_START + 3].operator = 3;
706 	dispatch[ALGO_START + 4].operator = 4;
707 	dispatch[ALGO_START + 5].operator = 5;
708 	dispatch[ALGO_START + 6].operator = 6;
709 	dispatch[ALGO_START + 7].operator = 7;
710 	dispatch[ALGO_START + 8].operator = 8;
711 	dispatch[ALGO_START + 9].operator = 9;
712 	dispatch[ALGO_START + 10].operator = 10;
713 	dispatch[ALGO_START + 11].operator = 11;
714 	dispatch[ALGO_START + 12].operator = 12;
715 	dispatch[ALGO_START + 13].operator = 13;
716 	dispatch[ALGO_START + 14].operator = 14;
717 	dispatch[ALGO_START + 15].operator = 15;
718 	dispatch[ALGO_START + 16].operator = 16;
719 	dispatch[ALGO_START + 17].operator = 17;
720 	dispatch[ALGO_START + 18].operator = 18;
721 	dispatch[ALGO_START + 19].operator = 19;
722 	dispatch[ALGO_START + 20].operator = 20;
723 	dispatch[ALGO_START + 21].operator = 21;
724 	dispatch[ALGO_START + 22].operator = 22;
725 	dispatch[ALGO_START + 23].operator = 23;
726 	dispatch[ALGO_START].routine = dispatch[ALGO_START + 1].routine =
727 		dispatch[ALGO_START + 2].routine = dispatch[ALGO_START + 3].routine =
728 		dispatch[ALGO_START + 4].routine = dispatch[ALGO_START + 5].routine =
729 		dispatch[ALGO_START + 6].routine = dispatch[ALGO_START + 7].routine =
730 		dispatch[ALGO_START + 8].routine = dispatch[ALGO_START + 9].routine =
731 		dispatch[ALGO_START + 10].routine = dispatch[ALGO_START + 11].routine =
732 		dispatch[ALGO_START + 12].routine = dispatch[ALGO_START + 13].routine =
733 		dispatch[ALGO_START + 14].routine = dispatch[ALGO_START + 15].routine =
734 		dispatch[ALGO_START + 16].routine = dispatch[ALGO_START + 17].routine =
735 		dispatch[ALGO_START + 18].routine = dispatch[ALGO_START + 19].routine =
736 		dispatch[ALGO_START + 20].routine = dispatch[ALGO_START + 21].routine =
737 		dispatch[ALGO_START + 22].routine = dispatch[ALGO_START + 23].routine =
738 		(synthRoutine) masterAlgo;
739 
740 	dispatch[ALGO_START + 24].controller = 126;
741 	dispatch[ALGO_START + 24].operator = 99;
742 	dispatch[ALGO_START + 25].controller = 126;
743 	dispatch[ALGO_START + 25].operator = 100;
744 	dispatch[ALGO_START + 27].routine = (synthRoutine) masterTune;
745 
746 	/* Main volume */
747 	dispatch[ALGO_START + 26].controller = 126;
748 	dispatch[ALGO_START + 26].operator = 102;
749 
750 	/* operator = 1 */
751 	dispatch[OP1_START].controller = 126;
752 	dispatch[OP1_START].operator = 0;
753 	dispatch[OP1_START + 1].controller = 0;
754 	dispatch[OP1_START + 1].operator = 1;
755 	dispatch[OP1_START + 2].controller = 0;
756 	dispatch[OP1_START + 2].operator = 0;
757 	/* envelope */
758 	dispatch[OP1_START + 5].controller = 0;
759 	dispatch[OP1_START + 5].operator = 2;
760 	dispatch[OP1_START + 6].controller = 0;
761 	dispatch[OP1_START + 6].operator = 3;
762 	dispatch[OP1_START + 7].controller = 0;
763 	dispatch[OP1_START + 7].operator = 4;
764 	dispatch[OP1_START + 8].controller = 0;
765 	dispatch[OP1_START + 8].operator = 5;
766 	dispatch[OP1_START + 3].controller = 0;
767 	dispatch[OP1_START + 3].operator = 6;
768 	dispatch[OP1_START + 9].controller = 0;
769 	dispatch[OP1_START + 9].operator = 7;
770 	dispatch[OP1_START + 4].controller = 126;
771 	dispatch[OP1_START + 4].operator = 1;
772 	dispatch[OP1_START + 10].controller = 126;
773 	dispatch[OP1_START + 10].operator = 2;
774 	dispatch[OP1_START + 11].controller = 126;
775 	dispatch[OP1_START + 11].operator = 3;
776 	dispatch[OP1_START + 12].controller = 0;
777 	dispatch[OP1_START + 12].operator = 9;
778 	/* L1-A-L2 */
779 	dispatch[OP1_START + 13].controller = 0;
780 	dispatch[OP1_START + 13].operator = 10;
781 	dispatch[OP1_START + 14].controller = 0;
782 	dispatch[OP1_START + 14].operator = 11;
783 	dispatch[OP1_START + 15].controller = 0;
784 	dispatch[OP1_START + 15].operator = 12;
785 
786 	/* operator = 2 */
787 	dispatch[OP2_START].controller = 126;
788 	dispatch[OP2_START].operator = 10;
789 	dispatch[OP2_START + 1].controller = 1;
790 	dispatch[OP2_START + 1].operator = 1;
791 	dispatch[OP2_START + 2].controller = 1;
792 	dispatch[OP2_START + 2].operator = 0;
793 	/* envelope */
794 	dispatch[OP2_START + 5].controller = 1;
795 	dispatch[OP2_START + 5].operator = 2;
796 	dispatch[OP2_START + 6].controller = 1;
797 	dispatch[OP2_START + 6].operator = 3;
798 	dispatch[OP2_START + 7].controller = 1;
799 	dispatch[OP2_START + 7].operator = 4;
800 	dispatch[OP2_START + 8].controller = 1;
801 	dispatch[OP2_START + 8].operator = 5;
802 	dispatch[OP2_START + 3].controller = 1;
803 	dispatch[OP2_START + 3].operator = 6;
804 	dispatch[OP2_START + 9].controller = 1;
805 	dispatch[OP2_START + 9].operator = 7;
806 	dispatch[OP2_START + 4].controller = 126;
807 	dispatch[OP2_START + 4].operator = 11;
808 	dispatch[OP2_START + 10].controller = 126;
809 	dispatch[OP2_START + 10].operator = 12;
810 	dispatch[OP2_START + 11].controller = 126;
811 	dispatch[OP2_START + 11].operator = 13;
812 	dispatch[OP2_START + 12].controller = 1;
813 	dispatch[OP2_START + 12].operator = 9;
814 	/* L1-A-L2 */
815 	dispatch[OP2_START + 13].controller = 1;
816 	dispatch[OP2_START + 13].operator = 10;
817 	dispatch[OP2_START + 14].controller = 1;
818 	dispatch[OP2_START + 14].operator = 11;
819 	dispatch[OP2_START + 15].controller = 1;
820 	dispatch[OP2_START + 15].operator = 12;
821 
822 	/* operator = 3 */
823 	dispatch[OP3_START].controller = 126;
824 	dispatch[OP3_START].operator = 20;
825 	dispatch[OP3_START + 1].controller = 2;
826 	dispatch[OP3_START + 1].operator = 1;
827 	dispatch[OP3_START + 2].controller = 2;
828 	dispatch[OP3_START + 2].operator = 0;
829 	/* envelope */
830 	dispatch[OP3_START + 5].controller = 2;
831 	dispatch[OP3_START + 5].operator = 2;
832 	dispatch[OP3_START + 6].controller = 2;
833 	dispatch[OP3_START + 6].operator = 3;
834 	dispatch[OP3_START + 7].controller = 2;
835 	dispatch[OP3_START + 7].operator = 4;
836 	dispatch[OP3_START + 8].controller = 2;
837 	dispatch[OP3_START + 8].operator = 5;
838 	dispatch[OP3_START + 3].controller = 2;
839 	dispatch[OP3_START + 3].operator = 6;
840 	dispatch[OP3_START + 9].controller = 2;
841 	dispatch[OP3_START + 9].operator = 7;
842 	dispatch[OP3_START + 4].controller = 126;
843 	dispatch[OP3_START + 4].operator = 21;
844 	dispatch[OP3_START + 10].controller = 126;
845 	dispatch[OP3_START + 10].operator = 22;
846 	dispatch[OP3_START + 11].controller = 126;
847 	dispatch[OP3_START + 11].operator = 23;
848 	dispatch[OP3_START + 12].controller = 2;
849 	dispatch[OP3_START + 12].operator = 9;
850 	/* L1-A-L2 */
851 	dispatch[OP3_START + 13].controller = 2;
852 	dispatch[OP3_START + 13].operator = 10;
853 	dispatch[OP3_START + 14].controller = 2;
854 	dispatch[OP3_START + 14].operator = 11;
855 	dispatch[OP3_START + 15].controller = 2;
856 	dispatch[OP3_START + 15].operator = 12;
857 
858 	/* operator = 4 */
859 	dispatch[OP4_START].controller = 126;
860 	dispatch[OP4_START].operator = 30;
861 	dispatch[OP4_START + 1].controller = 3;
862 	dispatch[OP4_START + 1].operator = 1;
863 	dispatch[OP4_START + 2].controller = 3;
864 	dispatch[OP4_START + 2].operator = 0;
865 	/* envelope */
866 	dispatch[OP4_START + 5].controller = 3;
867 	dispatch[OP4_START + 5].operator = 2;
868 	dispatch[OP4_START + 6].controller = 3;
869 	dispatch[OP4_START + 6].operator = 3;
870 	dispatch[OP4_START + 7].controller = 3;
871 	dispatch[OP4_START + 7].operator = 4;
872 	dispatch[OP4_START + 8].controller = 3;
873 	dispatch[OP4_START + 8].operator = 5;
874 	dispatch[OP4_START + 3].controller = 3;
875 	dispatch[OP4_START + 3].operator = 6;
876 	dispatch[OP4_START + 9].controller = 3;
877 	dispatch[OP4_START + 9].operator = 7;
878 	dispatch[OP4_START + 4].controller = 126;
879 	dispatch[OP4_START + 4].operator = 31;
880 	dispatch[OP4_START + 10].controller = 126;
881 	dispatch[OP4_START + 10].operator = 32;
882 	dispatch[OP4_START + 11].controller = 126;
883 	dispatch[OP4_START + 11].operator = 33;
884 	dispatch[OP4_START + 12].controller = 3;
885 	dispatch[OP4_START + 12].operator = 9;
886 	/* L1-A-L2 */
887 	dispatch[OP4_START + 13].controller = 3;
888 	dispatch[OP4_START + 13].operator = 10;
889 	dispatch[OP4_START + 14].controller = 3;
890 	dispatch[OP4_START + 14].operator = 11;
891 	dispatch[OP4_START + 15].controller = 3;
892 	dispatch[OP4_START + 15].operator = 12;
893 
894 	/* operator = 5 */
895 	dispatch[OP5_START].controller = 126;
896 	dispatch[OP5_START].operator = 40;
897 	dispatch[OP5_START + 1].controller = 4;
898 	dispatch[OP5_START + 1].operator = 1;
899 	dispatch[OP5_START + 2].controller = 4;
900 	dispatch[OP5_START + 2].operator = 0;
901 	/* envelope */
902 	dispatch[OP5_START + 5].controller = 4;
903 	dispatch[OP5_START + 5].operator = 2;
904 	dispatch[OP5_START + 6].controller = 4;
905 	dispatch[OP5_START + 6].operator = 3;
906 	dispatch[OP5_START + 7].controller = 4;
907 	dispatch[OP5_START + 7].operator = 4;
908 	dispatch[OP5_START + 8].controller = 4;
909 	dispatch[OP5_START + 8].operator = 5;
910 	dispatch[OP5_START + 3].controller = 4;
911 	dispatch[OP5_START + 3].operator = 6;
912 	dispatch[OP5_START + 9].controller = 4;
913 	dispatch[OP5_START + 9].operator = 7;
914 	dispatch[OP5_START + 4].controller = 126;
915 	dispatch[OP5_START + 4].operator = 41;
916 	dispatch[OP5_START + 10].controller = 126;
917 	dispatch[OP5_START + 10].operator = 42;
918 	dispatch[OP5_START + 11].controller = 126;
919 	dispatch[OP5_START + 11].operator = 43;
920 	dispatch[OP5_START + 12].controller = 4;
921 	dispatch[OP5_START + 12].operator = 9;
922 	/* L1-A-L2 */
923 	dispatch[OP5_START + 13].controller = 4;
924 	dispatch[OP5_START + 13].operator = 10;
925 	dispatch[OP5_START + 14].controller = 4;
926 	dispatch[OP5_START + 14].operator = 11;
927 	dispatch[OP5_START + 15].controller = 4;
928 	dispatch[OP5_START + 15].operator = 12;
929 
930 	/* operator = 6 */
931 	dispatch[OP6_START].controller = 126;
932 	dispatch[OP6_START].operator = 50;
933 	dispatch[OP6_START + 1].controller = 5;
934 	dispatch[OP6_START + 1].operator = 1;
935 	dispatch[OP6_START + 2].controller = 5;
936 	dispatch[OP6_START + 2].operator = 0;
937 	/* envelope */
938 	dispatch[OP6_START + 5].controller = 5;
939 	dispatch[OP6_START + 5].operator = 2;
940 	dispatch[OP6_START + 6].controller = 5;
941 	dispatch[OP6_START + 6].operator = 3;
942 	dispatch[OP6_START + 7].controller = 5;
943 	dispatch[OP6_START + 7].operator = 4;
944 	dispatch[OP6_START + 8].controller = 5;
945 	dispatch[OP6_START + 8].operator = 5;
946 	dispatch[OP6_START + 3].controller = 5;
947 	dispatch[OP6_START + 3].operator = 6;
948 	dispatch[OP6_START + 9].controller = 5;
949 	dispatch[OP6_START + 9].operator = 7;
950 	dispatch[OP6_START + 4].controller = 126;
951 	dispatch[OP6_START + 4].operator = 51;
952 	dispatch[OP6_START + 10].controller = 126;
953 	dispatch[OP6_START + 10].operator = 52;
954 	dispatch[OP6_START + 11].controller = 126;
955 	dispatch[OP6_START + 11].operator = 53;
956 	dispatch[OP6_START + 12].controller = 5;
957 	dispatch[OP6_START + 12].operator = 9;
958 	/* L1-A-L2 */
959 	dispatch[OP6_START + 13].controller = 5;
960 	dispatch[OP6_START + 13].operator = 10;
961 	dispatch[OP6_START + 14].controller = 5;
962 	dispatch[OP6_START + 14].operator = 11;
963 	dispatch[OP6_START + 15].controller = 5;
964 	dispatch[OP6_START + 15].operator = 12;
965 
966 	/* memories */
967 	dispatch[MEM_START].operator = 0;
968 	dispatch[MEM_START + 1].operator = 1;
969 	dispatch[MEM_START + 2].operator = 2;
970 	dispatch[MEM_START + 3].operator = 3;
971 	dispatch[MEM_START + 4].operator = 4;
972 	dispatch[MEM_START + 5].operator = 5;
973 	dispatch[MEM_START + 6].operator = 6;
974 	dispatch[MEM_START + 7].operator = 7;
975 	dispatch[MEM_START + 8].operator = 8;
976 	dispatch[MEM_START + 9].operator = 9;
977 	dispatch[MEM_START].routine = dispatch[MEM_START + 1].routine =
978 		dispatch[MEM_START + 2].routine = dispatch[MEM_START + 3].routine =
979 		dispatch[MEM_START + 4].routine = dispatch[MEM_START + 5].routine =
980 		dispatch[MEM_START + 6].routine = dispatch[MEM_START + 7].routine =
981 		dispatch[MEM_START + 8].routine = dispatch[MEM_START + 9].routine =
982 		dispatch[MEM_START + 10].routine = dispatch[MEM_START + 11].routine =
983 		dispatch[MEM_START + 15].routine = dispatch[MEM_START + 16].routine =
984 		(synthRoutine) masterMemory;
985 	dispatch[MEM_START + 10].controller = 1;
986 	dispatch[MEM_START + 11].controller = 2;
987 	dispatch[MEM_START + 15].controller = 3;
988 	dispatch[MEM_START + 16].controller = 4;
989 
990 	/* Midi */
991 	dispatch[MEM_START + 12].controller = 2;
992 	dispatch[MEM_START + 13].controller = 1;
993 	dispatch[MEM_START + 12].routine = dispatch[MEM_START + 13].routine =
994 		(synthRoutine) masterMidi;
995 
996 	return(0);
997 }
998 
999 /*
1000  * This will be called to make any routine specific parameters available.
1001  */
1002 static int
masterConfigure(brightonWindow * win)1003 masterConfigure(brightonWindow *win)
1004 {
1005 	guiSynth *synth = findSynth(global.synths, win);
1006 	brightonEvent event;
1007 
1008 	if (synth == 0)
1009 	{
1010 		printf("problems going operational\n");
1011 		return(-1);
1012 	}
1013 
1014 	if (synth->flags & OPERATIONAL)
1015 		return(0);
1016 
1017 printf("going operational %p %p\n", synth, synth->win);
1018 
1019 	synth->flags |= OPERATIONAL;
1020 	synth->keypanel = KEY_PANEL;
1021 	synth->keypanel2 = -1;
1022 	synth->transpose = 24;
1023 	synth->dispatch[ALGO_START].other2 = 0;
1024 	synth->dispatch[ALGO_START].other1 = -1;
1025 
1026 	masterLoadMem(synth, "master", 0, synth->location, synth->mem.active,
1027 		FIRST_DEV, 0);
1028 
1029 	brightonPut(win,
1030 		"bitmaps/blueprints/mastershade.xpm", 0, 0, win->width, win->height);
1031 
1032 	/*
1033 	 * Hm. This is a hack for a few bits of bad rendering of a keyboard. Only
1034 	 * occurs on first paint, so we suppress the first paint, and then request
1035 	 * an expose here.
1036 	 */
1037 	event.type = BRIGHTON_EXPOSE;
1038 	event.intvalue = 1;
1039 	brightonParamChange(synth->win, KEY_PANEL, -1, &event);
1040 	configureGlobals(synth);
1041 
1042 	return(0);
1043 }
1044 
1045