File: dimensionD.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 (400 lines) | stat: -rw-r--r-- 11,197 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

/*
 *  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/>.
 *
 */

/*#define DEBUG */

#include <math.h>

#include "bristol.h"
#include "vibrachorus.h"

#define SPEED		0
#define DEPTH		1
#define SCAN		2
#define GAIN		3

#define OPNAME "Dimension"
#define OPDESCRIPTION "Vibrato Chorus"
#define PCOUNT 3
#define IOCOUNT 3

#define VCHORUS_IN_IND 0
#define VCHORUS_LOUT_IND 1
#define VCHORUS_ROUT_IND 2

/*
 * Reset any local memory information.
 */
static int destroy(bristolOP *operator)
{
#ifdef BRISTOL_DBG
	printf("destroy(%x)\n", operator);
#endif

	bristolfree(operator->specs);

	cleanup(operator);
	return(0);
}

/*
 * This is called by the frontend when a parameter is changed.
static int param(param, value)
 */
static int param(bristolOP *operator, bristolOPParams *param,
	unsigned char index, float value)
{
	bristolVCHORUS *specs;

	specs = (bristolVCHORUS *) operator->specs;

#ifdef DEBUG
	printf("checkParams(%f)\n", value);
#endif

	switch (index) {
		case 0:
			/*
			 * We want to go from 0.1Hz up to perhaps 20Hz. That means we need
			 * to step through the 1024 sinewave table at that rate.
			 *
			 * specs->samplerate
			 */
			param->param[index].float_val = 
				(0.1 + value * 20) * 1024 / specs->samplerate;
			return(0);
		case 1:
			if ((param->param[index].int_val = value * 256) <= 0)
				param->param[index].int_val = 1;
			break;
		case 2:
			if (value == 0)
				param->param[index].int_val = 0;
			else
				param->param[index].int_val = 1;
			break;
	}
	param->param[index].float_val = value;
	return(0);
}

static float sinewave[1024];

/*
 * Reset any local memory information.
 */
static int reset(bristolOP *operator, bristolOPParams *param)
{
	int i;

#ifdef BRISTOL_DBG
	printf("reset(%x)\n", operator);
#endif

	if (param->param[0].mem)
		bristolfree(param->param[0].mem);

	param->param[0].mem = bristolmalloc0(sizeof(float) * HISTSIZE);

	param->param[0].int_val = 10;
	param->param[1].int_val = 10;
	param->param[2].int_val = 0;

	// Get a normalised sinewave.
	for (i = 0; i < 1024; i++)
		sinewave[i] = sinf(M_PI * ((float) i) * 2.0 / 1024.0f) + 1.0;

	return(0);
}

static float table[TABSIZE];

/*
 * Stereo chorus. Create a vibrato effect at given speed from mono input signal
 * and mix it in varying amount back into the left and right channels. The
 * left and right should be given a copy of the input data as pass through.
 *
 * We need a parameter for depth that controls how far into the history we are
 * going to scan, the rate at which we scan, the depth of the returned signal
 * that defines the amount of 'flanging', and perhaps a second rate parameter
 * that alters the stereo panning of the signals. Having this separate from
 * the scan speed should improve the helicopter effect. All parameters will 
 * have to be float to ensure we can have subsample scan rates, previous 
 * versions have been a bit limited with single sample steps. We should also 
 * make this a resampling chorus.
 *
 * Extra parameters could be used to adjust the gain of both the original and
 * the vibrato signal, and we could consider different scaning waveforms. The
 * only one at the moment is a triangular which is close enough to sine, but a
 * front or back loaded tri wave would introduce some interesting leading and
 * trailing pumping effects, something that could be done by a factor that 
 * justs affects the ramp rate, applied +ve when ramping up and -ve when down.
 */
static int operate(bristolOP *operator,
	bristolVoice *voice,
	bristolOPParams *param,
	void *lcl)
{
	bristolVCHORUS *specs;
	bristolVCHORUSlocal *local = (bristolVCHORUSlocal *) lcl;
	register float *source, *ldest, *rdest;
	register int count;
	register float *history, depth, speed, scan, fdir;
	register float histout, value, scanp, gain, cg, dir, scanr;
	register int histin, i;

	specs = (bristolVCHORUS *) operator->specs;

	count = specs->spec.io[VCHORUS_LOUT_IND].samplecount;
	source = specs->spec.io[VCHORUS_IN_IND].buf;
	ldest = specs->spec.io[VCHORUS_LOUT_IND].buf;
	rdest = specs->spec.io[VCHORUS_ROUT_IND].buf;

	/*
	 * operational parameters. Speed should be a function of depth, ie, if 
	 * depth changes, speed should be adjusted to maintain scan rate.
	 *
	 * Let speed be 10 seconds require to reach the given depth. That can be 
	 * calculated as 441000 samples, so we divide speed by depth * 44100;
	 */
	depth = param->param[DEPTH].float_val * 1024;
	speed = param->param[SPEED].float_val;

	/*
	 * Let scan be ten seconds to reach gain
	 */
	gain = param->param[GAIN].float_val * 1.5;
	scan = param->param[SCAN].float_val * 0.0005 * gain;

	history = param->param[0].mem;

#ifdef DEBUG
	printf("dimensionD()\n");
#endif

	histin = local->Histin;
	histout = local->Histout;
	scanr = local->scanr;
	scanp = local->scanp;
	cg = local->cg;
	dir = local->dir;
	fdir = local->fdir;

	for (i = 0; i < count; i++) {
		/*
		 * Save our current signal into the history buffer.
		 */
		history[histin] = *source;

		/*
		 * Take our sample. Resample the two nearest for the position of our
		 * current scan through the history.
		 *
		 * Resampling is to take the current sample, plus the difference of the
		 * distance between this and the next sample.
		 value = history[histout]
		 	+ (history[histout + 1] - history[histout])
				* histout - (int) histout)
		 */
		if ((histout + 1) >= HISTSIZE)
		{
			value = history[(int) histout] +
				(history[0] - history[(int) histout])
					* (histout - ((float) ((int) histout)));
		} else {
			value = history[(int) histout] +
				(history[(int) histout + 1] - history[(int) histout])
					* (histout - ((float) ((int) histout)));
		}

		/*
		 * This is wrong. We take our sample and add to one channel and then
		 * subtract from the other. Correct operation is to take the sample
		 * in value, and then also alter the add/subtract value. We actually
		 * need three parameters, the speed at which we scan through the data
		 * history, the depth to which we scan, and the gain to apply the
		 * phasing effect. FIXED.
		 */
		if (dir == 0)
		{
			if ((cg += scan) > gain)
				dir = 1;
		} else {
			if ((cg -= scan) < 0.0)
				dir = 0;
		}
	
		if (scan == 0)
		{
			*rdest++ = (*source * (1.5 - gain)) + value * gain;
			*ldest++ = (*source * (1.5 - gain)) + value * gain;
		} else {
			*rdest++ = (*source * (1.5 - gain)) + value * (gain - cg);
			*ldest++ = (*source * (1.5 - gain)) + value * cg;
		}

		history[histin] += value * gain * 0.5;
		source++;

		if (++histin >= HISTSIZE) histin = 0;

		if ((histout = ((float) histin) - scanp) < 0)
			while (histout < 0)
				histout += HISTSIZE;

		while (histout >= HISTSIZE)
			histout -= HISTSIZE;

		/*
		 * Adjust the scan rate through memory. Initially out is zero, and we
		 * tend it towards depth by adding the speed increments. When we reach
		 * depth we change the direction.
		if (fdir == 0)
		{
			if ((scanp += speed) > depth)
				fdir = 1;
		} else {
			if ((scanp -= speed) < speed)
				fdir = 0;
		}
		 */
		if ((scanr += speed) >= 1024)
			scanr -= 1024;
		scanp = sinewave[(int) scanr] * depth;
	}

	local->Histin = histin;
	local->Histout = histout;
	local->scanr = scanr;
	local->scanp = scanp;
	local->cg = cg;
	local->dir = dir;
	local->fdir = fdir;
	return(0);
}

static void
buildSineTable(float *table)
{
	int i;

	for (i = 260; i < (TABSIZE + 260); i++)
		table[i - 260] = (float) sin(2 * M_PI * ((double) i) / TABSIZE);
}

/*
 * Setup any variables in our OP structure, in our IO structures, and malloc
 * any memory we need.
 */
bristolOP *
chorusinit(bristolOP **operator, int index, int samplerate, int samplecount)
{
	bristolVCHORUS *specs;

	*operator = bristolOPinit(operator, index, samplecount);

#ifdef BRISTOL_DBG
	printf("chorusinit(%x(%x), %i, %i, %i)\n",
		operator, *operator, index, samplerate, samplecount);
#endif

	/*
	 * Then the local parameters specific to this operator. These will be
	 * the same for each operator, but must be inited in the local code.
	 */
	(*operator)->operate = operate;
	(*operator)->destroy = destroy;
	(*operator)->reset = reset;
	(*operator)->param= param;

	specs = (bristolVCHORUS *) bristolmalloc0(sizeof(bristolVCHORUS));
	specs->samplerate = samplerate;
	(*operator)->specs = (bristolOPSpec *) specs;
	(*operator)->size = sizeof(bristolVCHORUS);

	/*
	 * These are specific to this operator, and will need to be altered for
	 * each operator.
	 */
	specs->spec.opname = OPNAME;
	specs->spec.description = OPDESCRIPTION;
	specs->spec.pcount = PCOUNT;
	specs->spec.iocount = IOCOUNT;
	specs->spec.localsize = sizeof(bristolVCHORUSlocal);

	/*
	 * Now fill in the specs for this operator.
	 */
	specs->spec.param[0].pname = "speed";
	specs->spec.param[0].description= "rotation speed";
	specs->spec.param[0].type = BRISTOL_FLOAT;
	specs->spec.param[0].low = 0;
	specs->spec.param[0].high = 1;
	specs->spec.param[0].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[1].pname = "depth";
	specs->spec.param[1].description = "depth of rotation";
	specs->spec.param[1].type = BRISTOL_FLOAT;
	specs->spec.param[1].low = 0;
	specs->spec.param[1].high = 1;
	specs->spec.param[1].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[2].pname = "crunch";
	specs->spec.param[2].description = "gain of return signal";
	specs->spec.param[2].type = BRISTOL_FLOAT;
	specs->spec.param[2].low = 0;
	specs->spec.param[2].high = 1;
	specs->spec.param[2].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[3].pname = "scan";
	specs->spec.param[3].description = "stereo pan rate";
	specs->spec.param[3].type = BRISTOL_FLOAT;
	specs->spec.param[3].low = 0;
	specs->spec.param[3].high = 1;
	specs->spec.param[3].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;


	/*
	 * Now fill in the IO specs.
	 */
	specs->spec.io[0].ioname = "input";
	specs->spec.io[0].description = "Input signal";
	specs->spec.io[0].samplerate = samplerate;
	specs->spec.io[0].samplecount = samplecount;
	specs->spec.io[0].flags = BRISTOL_AC|BRISTOL_INPUT;

	specs->spec.io[1].ioname = "left output";
	specs->spec.io[1].description = "output signal";
	specs->spec.io[1].samplerate = samplerate;
	specs->spec.io[1].samplecount = samplecount;
	specs->spec.io[1].flags = BRISTOL_AC|BRISTOL_OUTPUT;

	specs->spec.io[2].ioname = "right output";
	specs->spec.io[2].description = "output signal";
	specs->spec.io[2].samplerate = samplerate;
	specs->spec.io[2].samplecount = samplecount;
	specs->spec.io[2].flags = BRISTOL_AC|BRISTOL_OUTPUT;

	buildSineTable(table);

	return(*operator);
}