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 /*#define BRISTOL_DBG */
22 /*
23  * Need to have basic template for an operator. Will consist of
24  *
25  *	aksdcoinit()
26  *	operate()
27  *	reset()
28  *	destroy()
29  *
30  *	destroy() is in the library.
31  *
32  * Operate will be called when all the inputs have been loaded, and the result
33  * will be an output buffer written to the next operator.
34  */
35 
36 #include <math.h>
37 
38 #include "bristol.h"
39 #include "aksdco.h"
40 
41 static float note_diff, *zbuf;
42 
43 #define BRISTOL_SQR 4
44 #define SINE_LIM 0.05f
45 
46 /*
47  * The name of this operator, IO count, and IO names. This is where possible
48  * identical to the Prophet DCO, but we need separated outputs. We could use
49  * it for the Prophet as well but that would incur the cost of extra mixing
50  * code.
51  */
52 #define OPNAME "DCO"
53 #define OPDESCRIPTION "AKS Digitally Controlled Oscillator"
54 #define PCOUNT 6
55 #define IOCOUNT 4
56 
57 #define DCO_IN_IND 0
58 #define DCO_MOD_IND 1
59 #define DCO_OUT_1 2
60 #define DCO_OUT_2 3
61 
62 static void fillWave(float *, int, int);
63 static void fillSineWave(float *, float, float, float);
64 static void fillTriWave(float *, float, float, float);
65 static void fillPulseWave(float *, float, float, float);
66 
67 /*
68  * Reset any local memory information.
69  */
destroy(bristolOP * operator)70 static int destroy(bristolOP *operator)
71 {
72 #ifdef BRISTOL_DBG
73 	printf("destroy(%x)\n", operator);
74 #endif
75 
76 	/*
77 	 * Unmalloc anything we added to this structure
78 	 */
79 	bristolfree(((bristolAKSDCO *) operator)->wave[0]);
80 	bristolfree(((bristolAKSDCO *) operator)->wave[1]);
81 	bristolfree(((bristolAKSDCO *) operator)->wave[2]);
82 	bristolfree(((bristolAKSDCO *) operator)->wave[3]);
83 	bristolfree(((bristolAKSDCO *) operator)->wave[4]);
84 	bristolfree(((bristolAKSDCO *) operator)->wave[5]);
85 	bristolfree(((bristolAKSDCO *) operator)->wave[6]);
86 	bristolfree(((bristolAKSDCO *) operator)->wave[7]);
87 
88 	bristolfree(operator->specs);
89 
90 	/*
91 	 * Free any local memory. We should also free ourselves, since we did the
92 	 * initial allocation.
93 	 */
94 	cleanup(operator);
95 	return(0);
96 }
97 
98 /*
99  * Reset any local memory information.
100  */
reset(bristolOP * operator,bristolOPParams * param)101 static int reset(bristolOP *operator, bristolOPParams *param)
102 {
103 #ifdef BRISTOL_DBG
104 	printf("aksreset(%x)\n", operator);
105 #endif
106 
107 	if (param->param[0].mem != 0)
108 		bristolfree(param->param[0].mem);
109 	if (param->param[1].mem != 0)
110 		bristolfree(param->param[1].mem);
111 
112 	param->param[0].mem = bristolmalloc0(sizeof(float) * AKSDCO_WAVE_SZE);
113 	param->param[1].mem = bristolmalloc0(sizeof(float) * AKSDCO_WAVE_SZE);
114 
115 	param->param[0].float_val = 0.5;
116 	param->param[1].float_val = 0.5;
117 	param->param[2].float_val = 1.0;
118 	param->param[3].float_val = 1.0;
119 	param->param[4].float_val = 1;
120 	param->param[5].float_val = 1.0;
121 	param->param[7].int_val = 0;
122 	param->param[9].float_val = 1.0;
123 	param->param[10].float_val = 1.0;
124 	param->param[11].float_val = 1.0;
125 	return(0);
126 }
127 
128 /*
129  * Alter an internal parameter of an operator.
130  */
param(bristolOP * operator,bristolOPParams * param,unsigned char index,float value)131 static int param(bristolOP *operator, bristolOPParams *param,
132 	unsigned char index, float value)
133 {
134 #ifdef BRISTOL_DBG
135 	printf("aksdcoparam(%x, %x, %i, %f)\n", operator, param, index, value);
136 #endif
137 
138 	switch (index) {
139 		case 1: /* Tune in small amounts */
140 			/*
141 			 * This should be over a few hertz? That will have to be studied
142 			 * when the coarse tuning has been implemented since the range of
143 			 * this parameter should span any step in that control, it may be
144 			 * in cents or larger steps.
145 			 */
146 			param->param[index].float_val = 1.0 + note_diff * value / 8;
147 			break;
148 		case 0: /* Tune in large amounts */
149 			/*
150 			 * This should be exponential 15 to 15 KHz for an audible oscillator
151 			 * and something like 0.02 to 500 Hz for an 'LFO'.
152 			 */
153 			{
154 				float tune, notes = 1.0;
155 				int i;
156 
157 /*				value = gainTable[(int) (value * C_RANGE_MIN_1)].gain; */
158 /*				if (param->param[5].int_val == 2) */
159 
160 				tune = (value - 0.5) * 2;
161 
162 				/*
163 				 * Up or down wide note range
164 				 */
165 				for (i = 0; i < 80;i++)
166 				{
167 					if (tune > 0)
168 						notes *= note_diff;
169 					else
170 						notes /= note_diff;
171 				}
172 
173 				if (tune > 0)
174 					param->param[index].float_val =
175 						1.0 + (notes - 1) * tune;
176 				else
177 					param->param[index].float_val =
178 						1.0 - (1 - notes) * -tune;
179 
180 				break;
181 			}
182 		case 2: /* distortion of waveform. */
183 		case 3: /* gain */
184 		case 4: /* gain */
185 			param->param[index].float_val = value;
186 			/*
187 			 * This will result in the active waves being built. The distorts
188 			 * will cover pulse width from 10 to 90 (or probably more), a ramp
189 			 * that goes from leading edge to trailing edge including a tri
190 			 * output, and a sign going from M to W.
191 			 * Hm, the oscillator has to be minimally aware of its function as
192 			 * all three are different. This can be taken from the LFO value?
193 			 * We want to have our two desired waveforms put into the first
194 			 * two locations, but the source may be different depending on
195 			 * which oscillator this is.
196 			 */
197 			if (param->param[5].int_val == 0)
198 			{
199 				fillSineWave(param->param[0].mem, AKSDCO_WAVE_SZE,
200 					param->param[2].float_val,
201 					param->param[3].float_val);
202 				fillTriWave(param->param[1].mem, AKSDCO_WAVE_SZE,
203 					SINE_LIM,
204 					param->param[4].float_val);
205 			} else {
206 				fillPulseWave(param->param[0].mem, AKSDCO_WAVE_SZE,
207 					param->param[2].float_val,
208 					param->param[3].float_val);
209 				fillTriWave(param->param[1].mem, AKSDCO_WAVE_SZE,
210 					param->param[2].float_val,
211 					param->param[4].float_val);
212 			}
213 			break;
214 		case 5: /* LFO.... Encodes the type of oscilllator. */
215 			param->param[index].int_val = value * CONTROLLER_RANGE;
216 			break;
217 		case 7: /* sync flag */
218 			if (value == 0)
219 				param->param[index].int_val = 0;
220 			else
221 				param->param[index].int_val = 1;
222 			break;
223 		/* case 8: used for waveform mix. */
224 		case 9: /* semitone tuning. */
225 			/*
226 			 * Transpose in semitones upwards.
227 			 */
228 			{
229 				int semitone = value * CONTROLLER_RANGE;
230 
231 				param->param[index].float_val = 1.0;
232 
233 				while (semitone-- > 0) {
234 					param->param[index].float_val *= (note_diff);
235 				}
236 			}
237 			break;
238 		case 10: /* semitone fine tuning. */
239 			/*
240 			 * finetune by a semitones up/down.
241 			 */
242 			{
243 				float tune, notes = 1.0;
244 
245 				tune = (value - 0.5);
246 
247 				/*
248 				 * Up or down 7 notes.
249 				 */
250 				if (tune > 0)
251 					notes *= note_diff;
252 				else
253 					notes /= note_diff;
254 
255 				if (tune > 0)
256 					param->param[index].float_val =
257 						1.0 + (notes - 1) * tune;
258 				else param->param[index].float_val =
259 						1.0 - (1 - notes) * -tune;
260 
261 				break;
262 			}
263 		case 11: /* This will be used by the OBX pitch bend */
264 			{
265 				float tune, notes = 1.0;
266 				int i;
267 
268 				tune = (value - 0.5) * 2;
269 
270 				/*
271 				 * Up or down 12 notes.
272 				 */
273 				for (i = 0; i < 12;i++)
274 				{
275 					if (tune > 0)
276 						notes *= note_diff;
277 					else
278 						notes /= note_diff;
279 				}
280 
281 				if (tune > 0)
282 					param->param[index].float_val =
283 						1.0 + (notes - 1) * tune;
284 				else
285 					param->param[index].float_val =
286 						1.0 - (1 - notes) * -tune;
287 
288 				break;
289 			}
290 	}
291 
292 	return(0);
293 }
294 
295 #define S_DEC 0.999f
296 
297 /*
298  * This oscillator takes an input frequency buffer that is related to the
299  * midi key, glide and pitch bend, all frequency matched so that glide is
300  * at a constant rate irrespective of pitch and pitch bend is +/- a given
301  * range. It then takes a mod buf which contains anything we want to put into
302  * it - for the AKS it is the result of all the patch pins in the matrix. It
303  * is going to be linear information so may need to be scaled suitably.
304  * [Notes 3 Nov 06: Perhaps the freq buf should not be used. Pitch bend will
305  * be mapped to the X axis of the joystick in the modbuffer, and glide was not
306  * implemented by this synth and will have the value 0 (no glide)].
307  * [Alternatively allow the midi library to map pitch bend (coarse and fine) to
308  * another set of controllers that can then be used here for X axis modulation.]
309  *
310  * There are then two output buffers, one for each of the generated waveforms.
311  *
312  * Target tuning is 0.02 Hz to 16KHz, and we are probably going to have two
313  * coarrse/fine controllers to cover it, 28bits.
314  */
operate(bristolOP * operator,bristolVoice * voice,bristolOPParams * param,void * lcl)315 static int operate(bristolOP *operator,
316 	bristolVoice *voice,
317 	bristolOPParams *param,
318 	void *lcl)
319 {
320 	bristolAKSDCOlocal *local = lcl;
321 	int obp, count;
322 	float *wt1, *wt2;
323 	float *ib, *ob1, *ob2, *mb, wtp, gdelta, transp;
324 	bristolAKSDCO *specs;
325 
326 	specs = (bristolAKSDCO *) operator->specs;
327 
328 #ifdef BRISTOL_DBG
329 	printf("aksdco(%x, %x, %x)\n", operator, param, local);
330 #endif
331 
332 	count = specs->spec.io[DCO_IN_IND].samplecount;
333 	ib = specs->spec.io[DCO_IN_IND].buf;
334 	mb = specs->spec.io[DCO_MOD_IND].buf;
335 	ob1 = specs->spec.io[DCO_OUT_1].buf;
336 	ob2 = specs->spec.io[DCO_OUT_2].buf;
337 
338 	wt1 = (float *) param->param[0].mem;
339 	wt2 = (float *) param->param[1].mem;
340 
341 	/*
342 	 * This needs to be changed, a lot.
343 	transp = param->param[1].float_val * param->param[2].float_val
344 		* param->param[9].float_val * param->param[10].float_val
345 		* param->param[11].float_val;
346 	 */
347 	transp = param->param[0].float_val * param->param[1].float_val;
348 
349 	wtp = local->wtp;
350 
351 	/*
352 	 * Go jumping through the wavetable, with each jump defined by the value
353 	 * given on our input line, making sure we fill one output buffer.
354 	 */
355 	for (obp = 0; obp < count;obp++)
356 	{
357 		/*
358 		 * Take a sample from the wavetable into the output buffer. This
359 		 * should also be scaled by gain parameter.
360 		 *
361 		 * We can seperate this into subroutine calls, or we can take our
362 		 * values and take each wave?
363 		 */
364 		gdelta = wtp - ((float) ((int) wtp));
365 
366 		if (wtp >= AKSDCO_WAVE_SZE_M) {
367 			ob1[obp] = wt1[(int) wtp] + (wt1[0] - wt1[(int) wtp]) * gdelta;
368 			ob2[obp] = wt2[(int) wtp] + (wt2[0] - wt2[(int) wtp]) * gdelta;
369 		} else {
370 			ob1[obp] = wt1[(int) wtp]
371 				+ (wt1[(int) wtp + 1] - wt1[(int) wtp]) * gdelta;
372 			ob2[obp] = wt2[(int) wtp]
373 				+ (wt2[(int) wtp + 1] - wt2[(int) wtp]) * gdelta;
374 		}
375 
376 		/*
377 		 * Move the wavetable pointer forward by amount indicated in input
378 		 * buffer for this sample.
379 		 */
380 		if ((wtp += ib[obp] * transp) >= AKSDCO_WAVE_SZE)
381 		{
382 			while (wtp >= AKSDCO_WAVE_SZE)
383 				wtp -= AKSDCO_WAVE_SZE;
384 		}
385 		/*
386 		 * If we have gone negative, round back up. Allows us to run the
387 		 * oscillator backwards.
388 		 */
389 		while (wtp < 0)
390 			wtp += AKSDCO_WAVE_SZE;
391 	}
392 
393 	local->wtp = wtp;
394 	return(0);
395 }
396 
397 /*
398  * Setup any variables in our OP structure, in our IO structures, and malloc
399  * any memory we need.
400  */
401 bristolOP *
aksdcoinit(bristolOP ** operator,int index,int samplerate,int samplecount)402 aksdcoinit(bristolOP **operator, int index, int samplerate, int samplecount)
403 {
404 	bristolAKSDCO *specs;
405 
406 	*operator = bristolOPinit(operator, index, samplecount);
407 
408 #ifdef BRISTOL_DBG
409 	printf("aksdcoinit(%x(%x), %i, %i, %i)\n",
410 		operator, *operator, index, samplerate, samplecount);
411 #endif
412 
413 	note_diff = pow(2, ((double) 1)/12);
414 
415 	/*
416 	 * Then the local parameters specific to this operator. These will be
417 	 * the same for each operator, but must be inited in the local code.
418 	 */
419 	(*operator)->operate = operate;
420 	(*operator)->destroy = destroy;
421 	(*operator)->reset = reset;
422 	(*operator)->param= param;
423 
424 	specs = (bristolAKSDCO *) bristolmalloc0(sizeof(bristolAKSDCO));
425 	(*operator)->specs = (bristolOPSpec *) specs;
426 	(*operator)->size = sizeof(bristolAKSDCO);
427 
428 	/*
429 	 * These are specific to this operator, and will need to be altered for
430 	 * each operator.
431 	 */
432 	specs->spec.opname = OPNAME;
433 	specs->spec.description = OPDESCRIPTION;
434 	specs->spec.pcount = PCOUNT;
435 	specs->spec.iocount = IOCOUNT;
436 	specs->spec.localsize = sizeof(bristolAKSDCOlocal);
437 
438 	/*
439 	 * We are going to assign multiple waves to this oscillator.
440 	 * sine, ramp, square, triangle?
441 	 */
442 	specs->wave[0] =
443 		(float *) bristolmalloc(AKSDCO_WAVE_SZE * sizeof(float));
444 	specs->wave[1] =
445 		(float *) bristolmalloc(AKSDCO_WAVE_SZE * sizeof(float));
446 	specs->wave[2] =
447 		(float *) bristolmalloc(AKSDCO_WAVE_SZE * sizeof(float));
448 	specs->wave[3] =
449 		(float *) bristolmalloc(AKSDCO_WAVE_SZE * sizeof(float));
450 	specs->wave[4] =
451 		(float *) bristolmalloc(AKSDCO_WAVE_SZE * sizeof(float));
452 	specs->wave[5] =
453 		(float *) bristolmalloc(AKSDCO_WAVE_SZE * sizeof(float));
454 	specs->wave[6] =
455 		(float *) bristolmalloc(AKSDCO_WAVE_SZE * sizeof(float));
456 	specs->wave[7] =
457 		(float *) bristolmalloc(AKSDCO_WAVE_SZE * sizeof(float));
458 	zbuf = (float *) bristolmalloc(AKSDCO_WAVE_SZE * sizeof(float));
459 
460 	/*
461 	 * FillWave is something that should be called as a parameter change, but
462 	 * for testing will load it here.
463 	 */
464 	fillWave(specs->wave[0], AKSDCO_WAVE_SZE, 0);
465 	fillWave(specs->wave[1], AKSDCO_WAVE_SZE, 1);
466 	fillWave(specs->wave[2], AKSDCO_WAVE_SZE, 2);
467 	fillWave(specs->wave[3], AKSDCO_WAVE_SZE, 3);
468 	fillWave(specs->wave[4], AKSDCO_WAVE_SZE, 4);
469 	fillWave(specs->wave[5], AKSDCO_WAVE_SZE, 5);
470 	fillWave(specs->wave[6], AKSDCO_WAVE_SZE, 6);
471 	fillWave(specs->wave[7], AKSDCO_WAVE_SZE, 7);
472 
473 	/*
474 	 * Now fill in the dco specs for this operator. These are specific to an
475 	 * oscillator.
476 	 */
477 	specs->spec.param[0].pname = "Coarse";
478 	specs->spec.param[0].description = "Coarse tuning";
479 	specs->spec.param[0].type = BRISTOL_FLOAT;
480 	specs->spec.param[0].low = 0;
481 	specs->spec.param[0].high = 1;
482 	specs->spec.param[0].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
483 
484 	specs->spec.param[1].pname = "Fine";
485 	specs->spec.param[1].description = "Fine tuning";
486 	specs->spec.param[1].type = BRISTOL_INT;
487 	specs->spec.param[1].low = 0;
488 	specs->spec.param[1].high = 12;
489 	specs->spec.param[1].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
490 
491 	specs->spec.param[2].pname = "Waveform";
492 	specs->spec.param[2].description = "waveform distortion";
493 	specs->spec.param[2].type = BRISTOL_FLOAT;
494 	specs->spec.param[2].low = 0;
495 	specs->spec.param[2].high = 1;
496 	specs->spec.param[2].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;
497 
498 	specs->spec.param[3].pname = "gain1";
499 	specs->spec.param[3].description = "output gain on first signal";
500 	specs->spec.param[3].type = BRISTOL_FLOAT;
501 	specs->spec.param[3].low = 0;
502 	specs->spec.param[3].high = 2;
503 	specs->spec.param[3].flags = BRISTOL_ROTARY|BRISTOL_SLIDER|BRISTOL_HIDE;
504 
505 	specs->spec.param[4].pname = "gain2";
506 	specs->spec.param[4].description = "output gain on second signal";
507 	specs->spec.param[4].type = BRISTOL_INT;
508 	specs->spec.param[4].low = 0;
509 	specs->spec.param[4].high = 1;
510 	specs->spec.param[4].flags = BRISTOL_BUTTON;
511 
512 	specs->spec.param[5].pname = "Frequency";
513 	specs->spec.param[5].description = "audible or LFO oscillation";
514 	specs->spec.param[5].type = BRISTOL_INT;
515 	specs->spec.param[5].low = 0;
516 	specs->spec.param[5].high = 1;
517 	specs->spec.param[5].flags = BRISTOL_BUTTON;
518 
519 	/*
520 	 * Now fill in the dco IO specs.
521 	 */
522 	specs->spec.io[0].ioname = "input";
523 	specs->spec.io[0].description = "input rate modulation signal";
524 	specs->spec.io[0].samplerate = samplerate;
525 	specs->spec.io[0].samplecount = samplecount;
526 	specs->spec.io[0].flags = BRISTOL_DC|BRISTOL_INPUT;
527 
528 	specs->spec.io[1].ioname = "sine output";
529 	specs->spec.io[1].description = "oscillator sine output signal";
530 	specs->spec.io[1].samplerate = samplerate;
531 	specs->spec.io[1].samplecount = samplecount;
532 	specs->spec.io[1].flags = BRISTOL_AC|BRISTOL_OUTPUT;
533 
534 	specs->spec.io[2].ioname = "square output";
535 	specs->spec.io[2].description = "oscillator pulse/square output signal";
536 	specs->spec.io[2].samplerate = samplerate;
537 	specs->spec.io[2].samplecount = samplecount;
538 	specs->spec.io[2].flags = BRISTOL_AC|BRISTOL_OUTPUT;
539 
540 	specs->spec.io[3].ioname = "ramp output";
541 	specs->spec.io[3].description = "oscillator sine output signal";
542 	specs->spec.io[3].samplerate = samplerate;
543 	specs->spec.io[3].samplecount = samplecount;
544 	specs->spec.io[3].flags = BRISTOL_AC|BRISTOL_OUTPUT;
545 
546 	return(*operator);
547 }
548 
549 static void
fillPulseWave(float * mem,float count,float distort,float gain)550 fillPulseWave(float *mem, float count, float distort, float gain)
551 {
552 	float j = 1.0, i;
553 
554 	if (distort <= SINE_LIM)
555 		distort = SINE_LIM;
556 	else if (distort >= 1.0 - SINE_LIM)
557 		distort = 1.0 - SINE_LIM;
558 
559 	/*
560 	 * For now this is going to be a quite mathematical pulse wave, when it
561 	 * works we can change it to use some more interesting phase distorted
562 	 * waveforms.
563 	 */
564 	for (i = 0;i < count; i++)
565 	{
566 		*mem++ = j * gain;
567 
568 		if (i > distort * count)
569 			j = -1.0;
570 	}
571 }
572 
573 static void
fillTriWave(float * mem,float count,float distort,float gain)574 fillTriWave(float *mem, float count, float distort, float gain)
575 {
576 	float j = -1.0, i, inc1, inc2, offset;
577 
578 	if (distort <= SINE_LIM)
579 		distort = SINE_LIM;
580 	else if (distort >= 1.0 - SINE_LIM)
581 		distort = 1.0 - SINE_LIM;
582 
583 	/*
584 	 * We start from a trough and ramp up to the designated midpoint, then back
585 	 * down
586 	 *
587 	 * For now this is going to be a quite mathematical ramp wave, when it
588 	 * works we can change it to use some more interesting phase distorted
589 	 * waveforms, however that might result in it going from ramp to sine back
590 	 * to ramp.
591 	 */
592 	inc1 = 2.0 / (offset = distort * count);
593 	inc2 = -2.0 / (count - offset);
594 /* printf("%f %f %f %f\n", distort, offset, inc1, inc2); */
595 
596 	for (i = 0; i < count; i++)
597 	{
598 		*mem++ = j * gain;
599 
600 		if (i < offset)
601 			j += inc1;
602 		else
603 			j += inc2;
604 	}
605 }
606 
607 static void
fillSineWave(float * mem,float count,float distort,float gain)608 fillSineWave(float *mem, float count, float distort, float gain)
609 {
610 	float j = 0, i, inc1, inc2;
611 
612 	if (distort <= SINE_LIM)
613 		distort = SINE_LIM;
614 	else if (distort >= 1.0 - SINE_LIM)
615 		distort = 1.0 - SINE_LIM;
616 
617 	/*
618 	 * We are going to distort the sine wave by altering the rate at which it
619 	 * scans through each of its two halves. The value can go from 0 to 1.0
620 	 * and that should be controlled.
621 	 * It defines the poing at which we reach  PI, or half of a sine.
622 	 */
623 	inc1 = ((float) M_PI) / (distort * count);
624 	inc2 = ((float) M_PI) / (count - distort * count);
625 
626 	for (i = 0;i < count; i++)
627 	{
628 		*mem++ = sinf(j) * gain;
629 
630 		if (j < M_PI)
631 			j += inc1;
632 		else
633 			j += inc2;
634 	}
635 }
636 
637 static void
fillPDsine(float * mem,int count,int compress)638 fillPDsine(float *mem, int count, int compress)
639 {
640 	float j = 0, i, inc1, inc2;
641 
642 	/*
643 	 * Resample the sine wave with a phase distortion algorithm.
644 	 *
645 	 * We have to get to M_PI/2 in compress steps, hence
646 	 *
647 	 *	 inc1 = M_PI/(2 * compress)
648 	 *
649 	 * Then we have to scan M_PI in (count - 2 * compress), hence:
650 	 *
651 	 *	 inc2 = M_PI/(count - 2 * compress)
652 	 */
653 	inc1 = ((float) M_PI) / (((float) 2) * ((float) compress));
654 	inc2 = ((float) M_PI) / ((float) (count - 2 * compress));
655 
656 	for (i = 0;i < count; i++)
657 	{
658 		*mem++ = sinf(j);
659 
660 		if (i < compress)
661 			j += inc1;
662 		else if (i > (count - 2 * compress))
663 			j += inc1;
664 		else
665 			j += inc2;
666 	}
667 }
668 
669 /*
670  * Waves have a range of 24, which is basically two octaves. For larger
671  * differences will have to apply apms.
672  */
673 static void
fillWave(float * mem,int count,int type)674 fillWave(float *mem, int count, int type)
675 {
676 	int i;
677 
678 #ifdef BRISTOL_DBG
679 	printf("fillWave(%x, %i, %i)\n", mem, count, type);
680 #endif
681 
682 	switch (type) {
683 		case 0:
684 			/*
685 			 * This will be a sine wave. We have count samples, and
686 			 * 2PI radians in a full sine wave. Thus we take
687 			 * 		(2PI * i / count) * 2048.
688 			 */
689 			for (i = 0;i < count; i++)
690 				mem[i] = sin(2 * M_PI * ((double) i) / count);
691 			return;
692 		case 1:
693 		default:
694 		{
695 			float value = BRISTOL_SQR;
696 			/*
697 			 * This is a square wave, with decaying plateaus.
698 			 */
699 			for (i = 0;i < count / 2; i++)
700 				mem[i] = (value * S_DEC);
701 			value = -BRISTOL_SQR;
702 			for (;i < count; i++)
703 				mem[i] = (value * S_DEC);
704 			return;
705 		}
706 		case 2:
707 			/*
708 			 * This is a pulse wave - the aks dco does pwm.
709 			 */
710 			for (i = 0;i < count / 5; i++)
711 				mem[i] = 1.0;
712 			for (;i < count; i++)
713 				mem[i] = -1.0;
714 			return;
715 		case 3:
716 			/*
717 			 * This is a ramp wave. We scale the index from -.5 to .5, and
718 			 * multiply by the range. We go from rear to front to table to make
719 			 * the ramp wave have a positive leading edge.
720 			for (i = 0; i < count / 2; i++)
721 				mem[i] = ((float) i / count) * 2.0;
722 			for (; i < count; i++)
723 				mem[i] = ((float) i / count) * 2.0 - 2;
724 			for (i = count - 1;i >= 0; i--)
725 				mem[i] = (((float) i / count) - 0.5) * 2.0;
726 			mem[0] = 0;
727 			mem[count - 1] = mem[1]
728 				= 1.0;
729 			 */
730 			fillPDsine(mem, count, 5);
731 			return;
732 		case 4:
733 			/*
734 			 * Triangular wave. From MIN point, ramp up at twice the rate of
735 			 * the ramp wave, then ramp down at same rate.
736 			 */
737 			for (i = 0;i < count / 2; i++)
738 				mem[i] = (-1.0 + ((float) i * 2 / (count / 2))) * 2;
739 			for (;i < count; i++)
740 				mem[i] = (1.0 - (((float) (i - count / 2) * 2) / (count / 2)))
741 						* 2;
742 			return;
743 		case 5:
744 			/*
745 			 * Would like to put in a jagged edged ramp wave. Should make some
746 			 * interesting strings sounds.
747 			 */
748 			{
749 				float accum = -1.0;
750 
751 				for (i = 0; i < count / 2; i++)
752 				{
753 					mem[i] = accum +
754 						(0.5 - (((float) i) / count)) * 4;
755 					if (i == count / 8)
756 						accum = -0.75;
757 					if (i == count / 4)
758 						accum = -0.5;
759 					if (i == count * 3 / 8)
760 						accum = -0.25;
761 				}
762 				for (; i < count; i++)
763 					mem[i] = -mem[count - i];
764 			}
765 			return;
766 		case 6:
767 			return;
768 		case 7:
769 			/*
770 			 * Sine wave - added as a part of the OBX extensions.
771 			 */
772 			for (i = 0;i < count; i++)
773 				mem[i] = sin(2 * M_PI * ((double) i) / count);
774 			return;
775 	}
776 }
777 
778