1 
2 /*
3  *  Diverse Bristol audio routines.
4  *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 3 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*#define BRISTOL_DBG */
23 
24 /*
25  * Need to have basic template for an operator. Will consist of
26  *
27  *	hammondinit()
28  *	operate()
29  *	reset()
30  *	destroy()
31  *
32  *	destroy() is in the library.
33  *
34  * Operate will be called when all the inputs have been loaded, and the result
35  * will be an output buffer written to the next operator.
36  */
37 
38 #include <stdlib.h>
39 #include <math.h>
40 
41 #include "click.h"
42 
43 int clickset[128];
44 
45 #include "bristol.h"
46 #include "hammond.h"
47 
48 float note_diff;
49 int samplecount;
50 
51 static void fillWave(float *, int, int);
52 static void buildHammondSound(bristolOP *, unsigned char);
53 static void fillHammondWave(bristolOP *);
54 
55 /*
56  * Use of these mean that we can only product a single hammond oscillator.
57  * Even though it is reasonably flexible this does lead to problems, and when
58  * we move to a dual manual Hammond it will need to be resolved.
59  *
60  * The same is true of using single global result waves, wave[6] and wave[7].
61  * These need to be moved to private address space, ie, we need more
62  * instantiation.
63 static int sineform[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
64 static int wavelevel[16];
65 static int waveindex[16] = {264, 23, 268, 491, 523, 708, 354, 112, 661};
66 static int percussion[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
67  */
68 static int *sineform;
69 static int *wavelevel;
70 static int *waveindex;
71 static int *percussion;
72 
73 float *wave1;
74 float *wave2;
75 
76 /*
77  * This can be a single list, it is used to generate the different pipes.
78 static int sweeps[16] = {1, 2, 3, 4, 6, 8, 10, 12, 16, 0,0,0,0,0,0,0};
79 static float sweeps[16] = {1, 1.5, 2, 4, 6, 8, 10, 12, 16, 0,0,0,0,0,0,0};
80  */
81 static float sweeps[16] = {2, 6, 4, 8, 12, 16, 20, 24, 32, 0,0,0,0,0,0,0};
82 
83 /*
84  * The name of this operator, IO count, and IO names.
85  */
86 #define OPNAME "Hammond"
87 #define OPDESCRIPTION "Digitally Controlled Oscillator"
88 #define PCOUNT 8
89 #define IOCOUNT 3
90 
91 #define HAMMOND_IN_IND 0
92 #define HAMMOND_OUT_IND 1
93 #define HAMMOND_OUT2_IND 2
94 
95 /*
96  * Reset any local memory information.
97  */
destroy(bristolOP * operator)98 static int destroy(bristolOP *operator)
99 {
100 #ifdef BRISTOL_DBG
101 	printf("hammonddestroy(%x)\n", operator);
102 #endif
103 
104 	/*
105 	 * Unmalloc anything we added to this structure
106 	 */
107 	bristolfree(((bristolHAMMOND *) operator)->wave[0]);
108 	bristolfree(((bristolHAMMOND *) operator)->wave[1]);
109 	bristolfree(((bristolHAMMOND *) operator)->wave[2]);
110 	bristolfree(((bristolHAMMOND *) operator)->wave[3]);
111 	bristolfree(((bristolHAMMOND *) operator)->wave[4]);
112 	bristolfree(((bristolHAMMOND *) operator)->wave[5]);
113 	bristolfree(((bristolHAMMOND *) operator)->wave[6]);
114 	bristolfree(((bristolHAMMOND *) operator)->wave[7]);
115 
116 	bristolfree(operator->specs);
117 
118 	/*
119 	 * Free any local memory. We should also free ourselves, since we did the
120 	 * initial allocation.
121 	 */
122 	cleanup(operator);
123 	return(0);
124 }
125 
126 /*
127  * Reset any local memory information.
128  */
reset(bristolOP * operator,bristolOPParams * param)129 static int reset(bristolOP *operator, bristolOPParams *param)
130 {
131 #ifdef BRISTOL_DBG
132 	printf("hammondreset(%x, %x)\n", operator, param);
133 #endif
134 
135 	if (param->param[0].mem) bristolfree(param->param[0].mem);
136 	if (param->param[1].mem) bristolfree(param->param[1].mem);
137 	if (param->param[2].mem) bristolfree(param->param[2].mem);
138 	if (param->param[3].mem) bristolfree(param->param[3].mem);
139 	if (param->param[4].mem) bristolfree(param->param[4].mem);
140 	if (param->param[5].mem) bristolfree(param->param[5].mem);
141 
142 	/*
143 	 * For the hammond we want to put in a few local memory variables for this
144 	 * operator specifically.
145 	 *
146 	 * These are sineform, waveindex, wavelevel, and percussions, plus two
147 	 * unique wavetables.
148 	 */
149 	param->param[0].mem = bristolmalloc0(sizeof(int) * 16);
150 	param->param[1].mem = bristolmalloc0(sizeof(float) * HAMMOND_WAVE_SZE);
151 	param->param[2].mem = bristolmalloc0(sizeof(int) * 16);
152 	param->param[3].mem = bristolmalloc0(sizeof(int) * 16);
153 	param->param[4].mem = bristolmalloc0(sizeof(float) * HAMMOND_WAVE_SZE);
154 	param->param[5].mem = bristolmalloc0(sizeof(int) * 16);
155 	param->param[7].int_val = 1;
156 	param->param[7].int_val = 0;
157 	param->param[4].float_val = 0.166880;
158 	return(0);
159 }
160 
161 /*
162  * Alter an internal parameter of an operator.
163  */
param(bristolOP * operator,bristolOPParams * param,unsigned char index,float value)164 static int param(bristolOP *operator, bristolOPParams *param,
165 	unsigned char index, float value)
166 {
167 	int parm = value * CONTROLLER_RANGE, controller;
168 
169 	sineform = (int *) param->param[0].mem;
170 	wavelevel = (int *) param->param[2].mem;
171 	percussion = (int *) param->param[3].mem;
172 	waveindex = (int *) param->param[5].mem;
173 
174 	wave1 = (float *) param->param[1].mem;
175 	wave2 = (float *) param->param[4].mem;
176 
177 #ifdef BRISTOL_DBG
178 	printf("hammondparam(%x, %x, %i, %x)\n", operator, param, index,
179 		((int) value * CONTROLLER_RANGE));
180 #endif
181 
182 	switch (index) {
183 		case 0:
184 			if ((controller = parm >> 3) > 16)
185 				return(0);
186 			/*
187 			 * Mask out the level of this wave.
188 			((int *) *param->param[0].mem)[controller] = parm & 0x07;
189 			 */
190 			sineform[controller] = parm & 0x07;
191 
192 			buildHammondSound(operator, -1);
193 			break;
194 		case 1: /* Tune in "small" amounts - not used - using for damp */
195 			/*
196 			 * Want to use this for damping.
197 			 */
198 			param->param[index].float_val = value;
199 			break;
200 		case 2: /* harmonics - redo waveform? */
201 			/*
202 			 * We have a param which is XCCCCLLL, for controller (up to 9 waves)
203 			 * and LLL is level.
204 			 */
205 			buildHammondSound(operator, (int) (value * CONTROLLER_RANGE));
206 
207 			break;
208 		case 3: /* Percussion on a given wave */
209 			if ((controller = parm >> 3) > 16)
210 				return(0);
211 			/*
212 			 * Mask out the level of this wave.
213 			 */
214 			percussion[controller] = parm & 0x07;
215 			buildHammondSound(operator, -1);
216 			break;
217 		case 4: /* gain */
218 			param->param[index].float_val = value;
219 			break;
220 		case 5: /* rand */
221 			if ((controller = parm >> 10) > 16)
222 				return(0);
223 
224 			if ((waveindex[controller] = parm & 0x3ff) == HAMMOND_WAVE_SZE)
225 				waveindex[controller] -= 1;
226 			buildHammondSound(operator, -1);
227 			break;
228 		case 6: /* click */
229 			param->param[index].float_val = value * value * 0.25;
230 			break;
231 		case 7: /* Algo */
232 			param->param[index].int_val = value * CONTROLLER_RANGE;
233 
234 			if (param->param[index].int_val)
235 			break;
236 	}
237 
238 	return(0);
239 }
240 
241 /*
242  * As of the first write, 9/11/01, this takes nearly 20mips to operate a single
243  * oscillator. Will work on optimisation of the code, using non-referenced
244  * variables in register workspace.
245  *
246  *	output buffer pointer.
247  *	output buffer index.
248  *	input buffer index.
249  *	wavetable.
250  *	wavetable index.
251  *	count.
252  *	gain.
253  *
254  * With optimisations this is reduced to a nominal amount. Put most parameters
255  * in registers, and then stretched the for loop to multiples of 16 samples.
256  */
257 extern int preach(float *, float *, int, int *, int *, int, float, float, float, int, float);
258 
operate(bristolOP * operator,bristolVoice * voice,bristolOPParams * param,void * lcl)259 static int operate(bristolOP *operator,
260 	bristolVoice *voice,
261 	bristolOPParams *param,
262 	void *lcl)
263 {
264 	register bristolHAMMONDlocal *local = lcl;
265 	register int obp, count;
266 	register float *ib, *ob, *pb, *wt, *pt, wtp, gain, clicklev, damp;
267 	bristolHAMMOND *specs;
268 
269 	specs = (bristolHAMMOND *) operator->specs;
270 
271 #ifdef BRISTOL_DBG
272 	printf("hammond(%x, %x, %x)\n", operator, param, local);
273 #endif
274 
275 	count = specs->spec.io[HAMMOND_OUT_IND].samplecount;
276 	ib = specs->spec.io[HAMMOND_IN_IND].buf;
277 	ob = specs->spec.io[HAMMOND_OUT_IND].buf;
278 	pb = specs->spec.io[HAMMOND_OUT2_IND].buf;
279 	damp = param->param[1].float_val;
280 	wt = (float *) param->param[1].mem;
281 	pt = (float *) param->param[4].mem;
282 
283 	/*
284 	 * Control the gain under the foot pedal.
285 	 */
286 	gain = param->param[4].float_val * voice->baudio->contcontroller[4];
287 
288 	wtp = local->wtp;
289 
290 	/*
291 	 * Go jumping through the wavetable, with each jump defined by the value
292 	 * given on our input line.
293 	 *
294 	 * Make sure we fill one output buffer.
295 	 *
296 	 * The '-36' may look odd, but the tonewheel generator does not use MIDI
297 	 * note indeces and this puts the key.key back into the gearing numbers.
298 	 */
299 	if (param->param[7].int_val) {
300 		preach(ob, pb, voice->key.key - 36, param->param[2].mem,
301 			param->param[3].mem, count, gain, damp, param->param[6].float_val,
302 			voice->flags, voice->velocity);
303 	} else {
304 
305 		/*
306 		 * Click may be in multiple stages - each click array is 2k samples,
307 		 * so if our sample block size is less than this we need to hit
308 		 * multiple consequetive blocks.
309 		 *
310 		 * We should consider randomising the use of several different key
311 		 * clicks - this is however only done in the preacher.
312 		 */
313 		if (voice->flags & (BRISTOL_KEYON|BRISTOL_KEYREON))
314 		{
315 			wtp = 0.0;
316 			clickset[voice->key.key] = 0;
317 		}
318 
319 		if (clickset[voice->key.key] < (CLICK_SIZE - count))
320 		{
321 			register int i;
322 			register short *thisclick;
323 
324 			thisclick = &click[clickset[voice->key.key]];
325 
326 			if ((clicklev = param->param[6].float_val * gain) != 0)
327 			{
328 				for (i = 0; i < count; i++)
329 					ob[i] += ((float) thisclick[i]) * clicklev;
330 			}
331 			clickset[voice->key.key] += count;
332 		}
333 
334 		for (obp = 0; obp < count;)
335 		{
336 			/*
337 			 * Take a sample from the wavetable into the output buffer. This
338 			 * should also be scaled by gain parameter.
339 			ob[obp] += wt[(int) wtp] * gain;
340 			pb[obp] += pt[(int) wtp] * gain;
341 			 */
342 			if (wtp == HAMMOND_WAVE_SZE)
343 			{
344 				ob[obp] += (wt[0] * (wtp - ((float) ((int) wtp)))
345 					+ wt[(int) wtp] * (1.0 - (wtp - ((float) ((int) wtp)))))
346 					* gain;
347 				pb[obp] += (pt[0] * (wtp - ((float) ((int) wtp)))
348 					+ pt[(int) wtp] * (1.0 - (wtp - ((float) ((int) wtp)))))
349 					* gain;
350 			} else  {
351 				ob[obp] += (wt[(int) wtp + 1] * (wtp - ((float) ((int) wtp)))
352 					+ wt[(int) wtp] * (1.0 - (wtp - ((float) ((int) wtp)))))
353 					* gain;
354 				pb[obp] += (pt[(int) wtp + 1] * (wtp - ((float) ((int) wtp)))
355 					+ pt[(int) wtp] * (1.0 - (wtp - ((float) ((int) wtp)))))
356 					* gain;
357 			}
358 			/*
359 			 * Move the wavetable pointer forward by amount indicated in input
360 			 * buffer for this sample.
361 			 */
362 			if ((wtp += ib[obp++] * 0.25) >= HAMMOND_WAVE_SZE)
363 				wtp -= HAMMOND_WAVE_SZE;
364 			if (wtp < 0) wtp += HAMMOND_WAVE_SZE;
365 		}
366 	}
367 
368 	local->wtp = wtp;
369 	return(0);
370 }
371 
372 /*
373  * Setup any variables in our OP structure, in our IO structures, and malloc
374  * any memory we need.
375  */
376 bristolOP *
hammondinit(bristolOP ** operator,int index,int samplerate,int samplecount)377 hammondinit(bristolOP **operator, int index, int samplerate, int samplecount)
378 {
379 	bristolHAMMOND *specs;
380 
381 #ifdef BRISTOL_DBG
382 	printf("hammondinit(%x(%x), %i, %i, %i)\n",
383 		operator, *operator, index, samplerate, samplecount);
384 #endif
385 
386 	*operator = bristolOPinit(operator, index, samplecount);
387 	note_diff = pow(2, ((float) 1)/12);;
388 
389 	/*
390 	 * Then the local parameters specific to this operator. These will be
391 	 * the same for each operator, but must be inited in the local code.
392 	 */
393 	(*operator)->operate = operate;
394 	(*operator)->destroy = destroy;
395 	(*operator)->reset = reset;
396 	(*operator)->param = param;
397 
398 	specs = (bristolHAMMOND *) bristolmalloc0(sizeof(bristolHAMMOND));
399 	(*operator)->specs = (bristolOPSpec *) specs;
400 	(*operator)->size = sizeof(bristolHAMMOND);
401 
402 	/*
403 	 * These are specific to this operator, and will need to be altered for
404 	 * each operator.
405 	 */
406 	specs->spec.opname = OPNAME;
407 	specs->spec.description = OPDESCRIPTION;
408 	specs->spec.pcount = PCOUNT;
409 	specs->spec.iocount = IOCOUNT;
410 	specs->spec.localsize = sizeof(bristolHAMMONDlocal);
411 
412 	/*
413 	 * We are going to assign multiple waves to this oscillator.
414 	 * sine, ramp, square, triangle?
415 	 */
416 	specs->wave[0] = (float *) bristolmalloc(HAMMOND_WAVE_SZE * sizeof(float));
417 	specs->wave[1] = (float *) bristolmalloc(HAMMOND_WAVE_SZE * sizeof(float));
418 	specs->wave[2] = (float *) bristolmalloc(HAMMOND_WAVE_SZE * sizeof(float));
419 	specs->wave[3] = (float *) bristolmalloc(HAMMOND_WAVE_SZE * sizeof(float));
420 	specs->wave[4] = (float *) bristolmalloc(HAMMOND_WAVE_SZE * sizeof(float));
421 	specs->wave[5] = (float *) bristolmalloc(HAMMOND_WAVE_SZE * sizeof(float));
422 	specs->wave[6] = (float *) bristolmalloc(HAMMOND_WAVE_SZE * sizeof(float));
423 	specs->wave[7] = (float *) bristolmalloc(HAMMOND_WAVE_SZE * sizeof(float));
424 
425 	/*
426 	 * FillWave is something that should be called as a parameter change, but
427 	 * for testing will load it here.
428 	 */
429 	fillWave(specs->wave[0], HAMMOND_WAVE_SZE, 0);
430 	fillWave(specs->wave[1], HAMMOND_WAVE_SZE, 1);
431 	fillWave(specs->wave[2], HAMMOND_WAVE_SZE, 2);
432 	fillWave(specs->wave[3], HAMMOND_WAVE_SZE, 3);
433 	fillWave(specs->wave[4], HAMMOND_WAVE_SZE, 4);
434 	fillWave(specs->wave[5], HAMMOND_WAVE_SZE, 5);
435 	fillWave(specs->wave[6], HAMMOND_WAVE_SZE, 6);
436 	fillWave(specs->wave[7], HAMMOND_WAVE_SZE, 7);
437 
438 	/*
439 	 * Now fill in the hammond specs for this operator. These are specific to an
440 	 * oscillator.
441 	 */
442 	specs->spec.param[0].pname = "Sineform";
443 	specs->spec.param[0].description = "Pulse code of sinewave";
444 	specs->spec.param[0].type = BRISTOL_INT;
445 	specs->spec.param[0].low = 0;
446 	specs->spec.param[0].high = 1;
447 	specs->spec.param[0].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
448 
449 	specs->spec.param[1].pname = "damp";
450 	specs->spec.param[1].description = "tonewheel damping";
451 	specs->spec.param[1].type = BRISTOL_FLOAT;
452 	specs->spec.param[1].low = 0;
453 	specs->spec.param[1].high = 1;
454 	specs->spec.param[1].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
455 
456 	specs->spec.param[2].pname = "oscillator";
457 	specs->spec.param[2].description = "Harmonic content definition";
458 	specs->spec.param[2].type = BRISTOL_INT;
459 	specs->spec.param[2].low = 0;
460 	specs->spec.param[2].high = 127;
461 	specs->spec.param[2].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
462 
463 	specs->spec.param[3].pname = "percussion";
464 	specs->spec.param[3].description = "apply percussion to wave";
465 	specs->spec.param[3].type = BRISTOL_FLOAT;
466 	specs->spec.param[3].low = 0;
467 	specs->spec.param[3].high = 2;
468 	specs->spec.param[3].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
469 
470 	specs->spec.param[4].pname = "gain";
471 	specs->spec.param[4].description = "output gain on signal";
472 	specs->spec.param[4].type = BRISTOL_FLOAT;
473 	specs->spec.param[4].low = 0;
474 	specs->spec.param[4].high = 2;
475 	specs->spec.param[4].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
476 
477 	specs->spec.param[5].pname = "rand";
478 	specs->spec.param[5].description = "phase randomisation";
479 	specs->spec.param[5].type = BRISTOL_FLOAT;
480 	specs->spec.param[5].low = 0;
481 	specs->spec.param[5].high = 1;
482 	specs->spec.param[5].flags = BRISTOL_ROTARY|BRISTOL_SLIDER|BRISTOL_HIDE;
483 
484 	specs->spec.param[6].pname = "click";
485 	specs->spec.param[6].description = "Key click";
486 	specs->spec.param[6].type = BRISTOL_FLOAT;
487 	specs->spec.param[6].low = 0;
488 	specs->spec.param[6].high = 1;
489 	specs->spec.param[6].flags = BRISTOL_ROTARY|BRISTOL_SLIDER|BRISTOL_HIDE;
490 
491 	specs->spec.param[7].pname = "algo";
492 	specs->spec.param[7].description = "Sine generation algorithm";
493 	specs->spec.param[7].type = BRISTOL_INT;
494 	specs->spec.param[7].low = 0;
495 	specs->spec.param[7].high = 1;
496 	specs->spec.param[7].flags = BRISTOL_BUTTON;
497 
498 	/*
499 	 * Now fill in the hammond IO specs.
500 	 */
501 	specs->spec.io[0].ioname = "input";
502 	specs->spec.io[0].description = "input rate modulation signal";
503 	specs->spec.io[0].samplerate = samplerate;
504 	specs->spec.io[0].samplecount = samplecount;
505 	specs->spec.io[0].flags = BRISTOL_DC|BRISTOL_INPUT;
506 
507 	specs->spec.io[1].ioname = "output";
508 	specs->spec.io[1].description = "oscillator output signal";
509 	specs->spec.io[1].samplerate = samplerate;
510 	specs->spec.io[1].samplecount = samplecount;
511 	specs->spec.io[1].flags = BRISTOL_AC|BRISTOL_OUTPUT;
512 
513 	specs->spec.io[2].ioname = "percussive";
514 	specs->spec.io[2].description = "percussive signal output";
515 	specs->spec.io[2].samplerate = samplerate;
516 	specs->spec.io[2].samplecount = samplecount;
517 	specs->spec.io[2].flags = BRISTOL_AC|BRISTOL_OUTPUT;
518 
519 	return(*operator);
520 }
521 
522 static void
filltriwave(register float * mem,register int count,register double reach)523 filltriwave(register float *mem, register int count,
524 register double reach)
525 {
526 	register int i, recalc1 = 1, recalc2 = 1;
527 	register double j = 0, Count = (double) count, inc = reach;
528 
529 	for (i = 0;i < count; i++)
530 	{
531 		mem[i] = sin(((double) (2 * M_PI * j)) / Count) * HAMMOND_WAVE_GAIN;
532 
533 		if ((j > (count * 3 / 4)) && recalc2)
534 		{
535 			recalc2 = 0;
536 			inc = ((float) (count / 4)) / (((float) count) - i);
537 		} else if ((j > (count / 4)) && recalc1) {
538 			recalc1 = 0;
539 			/*
540 			 * J has the sine on a peak, we need to calculate the number
541 			 * of steps required to get the sine back to zero crossing by
542 			 * the time i = count / 2, ie, count /4 steps in total.
543 			 */
544 			inc = ((float) (count / 4)) / (((float) (count / 2)) - i);
545 		}
546 
547 		j += inc;
548 	}
549 
550 /*
551 	printf("\n%f, %f\n", j, inc);
552 	for (i = 0; i < count; i+=8)
553 	{
554 		printf("\n%i: ", i + k);
555 		for (k = 0; k < 8; k++)
556 			printf("%03.1f, ", mem[i + k]);
557 	}
558 	printf("\n");
559 */
560 }
561 
562 static void
fillWave(register float * mem,register int count,int type)563 fillWave(register float *mem, register int count, int type)
564 {
565 	register double Count;
566 
567 #ifdef BRISTOL_DBG
568 	printf("fillWave(%x, %i, %i)\n", mem, count, type);
569 #endif
570 
571 	Count = (double) count;
572 
573 	switch (type) {
574 		case 0:
575 			/*
576 			 * This will be a sine wave. We have count samples, and
577 			 * 2PI radians in a full sine wave. Thus we take
578 			 * 		(2PI * i / count) * 2048.
579 			 */
580 			filltriwave(mem, count, 1.0);
581 		case 1:
582 		default:
583 			filltriwave(mem, count, 1.05);
584 			return;
585 		case 2:
586 			filltriwave(mem, count, 1.10);
587 			return;
588 		case 3:
589 			filltriwave(mem, count, 1.30);
590 			return;
591 		case 4:
592 			filltriwave(mem, count, 1.60);
593 			return;
594 		case 5:
595 			filltriwave(mem, count, 2.0);
596 			return;
597 		case 6:
598 			filltriwave(mem, count, 3.0);
599 			return;
600 		case 7:
601 			filltriwave(mem, count, 5.0);
602 			return;
603 	}
604 }
605 
606 static void
buildHammondSound(bristolOP * operator,unsigned char parm)607 buildHammondSound(bristolOP *operator, unsigned char parm)
608 {
609 	int controller, value;
610 
611 #ifdef BRISTOL_DBG
612 	printf("buildHammondSound(%x, %i, %i, %i)\n", operator, parm, parm >> 3, parm & 0x07);
613 #endif
614 
615 	if (parm != 0xff)
616 	{
617 		if ((controller = parm / 9) > 8)
618 			return;
619 
620 		value = parm - controller * 9;
621 
622 		/*
623 		 * Mask out the level of this wave. Note that if we are running the
624 		 * preacher we could exit here, however, can we know that?
625 		 */
626 		wavelevel[controller] = value;
627 	}
628 
629 	fillHammondWave(operator);
630 }
631 
632 static void
fillHammondWave(bristolOP * operator)633 fillHammondWave(bristolOP *operator)
634 {
635 	register float gain, *source, *dest, oscindex, sweep;
636 	register int i, j;
637 	bristolHAMMOND *specs;
638 
639 	specs = (bristolHAMMOND *) operator->specs;
640 
641 #ifdef BRISTOL_DBG
642 	printf("fillHammondWave(%x)\n", operator);
643 #endif
644 
645 	/*
646 	 * Dest is actually the local wave for this operator.
647 	 */
648 	bristolbzero(wave1, HAMMOND_WAVE_SZE * sizeof(float));
649 	bristolbzero(wave2, HAMMOND_WAVE_SZE * sizeof(float));
650 
651 	/*
652 	 * We now have a volume array - 16 levels, of which we should only use 9.
653 	 * Each represents a base requency in terms of pipe length:
654 	 *	16, 8, 5 1/3, 4, 2 2/3, 2, 1 3/5, 1 1/3, 1
655 	 * These are frequencies:
656 
657 		f
658 		2f
659 		3f
660 		4f
661 		6f
662 		8f
663 		10f
664 		12f
665 		16f
666 
667 printf("%4i %4i %4i %4i %4i %4i %4i %4i %4i\n",
668 wavelevel[0], wavelevel[1], wavelevel[2], wavelevel[3], wavelevel[4],
669 wavelevel[5], wavelevel[6], wavelevel[7], wavelevel[8]);
670 printf("%4i %4i %4i %4i %4i %4i %4i %4i %4i\n",
671 sineform[0], sineform[1], sineform[2], sineform[3], sineform[4],
672 sineform[5], sineform[6], sineform[7], sineform[8]);
673 printf("%4i %4i %4i %4i %4i %4i %4i %4i %4i\n",
674 waveindex[0], waveindex[1], waveindex[2], waveindex[3], waveindex[4],
675 waveindex[5], waveindex[6], waveindex[7], waveindex[8]);
676 printf("%4i %4i %4i %4i %4i %4i %4i %4i %4i\n",
677 percussion[0], percussion[1], percussion[2], percussion[3], percussion[4],
678 percussion[5], percussion[6], percussion[7], percussion[8]);
679 	 */
680 	/*
681 	 * This could be optimised for a single sweep through each sample, but
682 	 * would require a lot of parameters.
683 	 */
684 	for (i = 0; i < 9; i++)
685 	{
686 		source = specs->wave[sineform[i]];
687 
688 		if (percussion[i] == 1)
689 			/*
690 		if ((i > 1) && (i < 6))
691 			 * Percussives.
692 			 */
693 			dest = wave2;
694 		else
695 			dest = wave1;
696 
697 		/*
698 		 * This ensures we do not have direct phase relationships.
699 		while ((oscindex = rand() & 0x03ff) == HAMMOND_WAVE_SZE);
700 		 */
701 		oscindex = waveindex[i];
702 
703 		gain = ((float) wavelevel[i]) / 8.0;
704 
705 		if (gain == 0)
706 			continue;
707 
708 		sweep = sweeps[i];
709 
710 		for (j = 0; j < HAMMOND_WAVE_SZE; j++)
711 		{
712 			*dest++ += source[(int) oscindex] * gain;
713 			if ((oscindex += sweep) >= HAMMOND_WAVE_SZE)
714 				oscindex -= HAMMOND_WAVE_SZE;
715 		}
716 	}
717 }
718 
719