File: resonator.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 (304 lines) | stat: -rw-r--r-- 8,219 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

/*
 *  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 BRISTOL_DBG */
/*
 * Need to have basic template for an operator. Will consist of
 *
 *	resinit()
 *	operate()
 *	reset()
 *	destroy()
 *
 *	destroy() is in the library.
 *
 * Operate will be called when all the inputs have been loaded, and the result
 * will be an output buffer written to the next operator.
 */

#include "bristol.h"
#include "resonator.h"

/*
 * The name of this operator, IO count, and IO names.
 */
#define OPNAME "RES"
#define OPDESCRIPTION "Digital Filter One"
#define PCOUNT 4
#define IOCOUNT 3

#define RES_IN_IND 0
#define RES_MOD_IND 1
#define RES_OUT_IND 2

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

	/*
	 * Unmalloc anything we added to this structure
	 */
	bristolfree(operator->specs);

	/*
	 * Free any local memory. We should also free ourselves, since we did the
	 * initial allocation.
	 */
	cleanup(operator);
	return(0);
}

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

/*
 * Alter an internal parameter of an operator.
 */
static int param(bristolOP *operator, bristolOPParams *param,
	unsigned char index, float value)
{
#ifdef BRISTOL_DBG
	printf("param(%x, %f)\n", operator,	value);
	printf("resparam(%x, %x, %i, %f)\n", operator, param, index, value);
#endif

	switch (index) {
		case 1:
			param->param[index].float_val =
				gainTable[(int) (value * (CONTROLLER_RANGE - 1))].gain;
			break;
		case 0:
		case 2:
			/*
			 * Convert these into logarithm values from 1 to 0.
			 */
			param->param[index].float_val = value;
			if (value == 0.0)
				param->param[index].float_val = 0;
			break;
		case 3:
			if (value > 0)
				param->param[index].int_val = 1;
			else
				param->param[index].int_val = 0;
			break;
	}

	return(0);
}

/*
 * filter - takes input signal and filters it according to the mod level.
 */
static int operate(register bristolOP *operator, bristolVoice *voice,
	bristolOPParams *param,
	void *lcl)
{
	bristolRESlocal *local = lcl;
	register int count;
	register float *ib, *ob, *mb;
	bristolRES *specs;

	register float velocity, output, vdelta, cutoff, gain, mod, ccut, cvdelta;

	/*
	 * Every operator accesses these variables, the count, and a pointer to
	 * each buffer. We should consider passing them as readymade parameters?
	 *
	 * The Filter now takes normalised inputs, in ranges of 12PO.
	 */
	specs = (bristolRES *) operator->specs;
	count = specs->spec.io[RES_OUT_IND].samplecount;
	ib = specs->spec.io[RES_IN_IND].buf;
	mb = specs->spec.io[RES_MOD_IND].buf;
	ob = specs->spec.io[RES_OUT_IND].buf;

	if (param->param[3].int_val) {
		cutoff = param->param[0].float_val * voice->dFreq / 4;
		vdelta = param->param[1].float_val * voice->dFreq / 4;
	} else {
		cutoff = param->param[0].float_val;
		vdelta = param->param[1].float_val;
	}
	mod = param->param[2].float_val;

	/*
	 * Increase the gain at the lower frequencies, since we are removing large
	 * amounts of the signal content.
	 */
	gain = (1.5 - param->param[0].float_val) * 0.5;

	/*
	 * This is the code from one of the SLab floating point filter routines.
	 * It has been altered here to have a single lowpass filter, and will be
	 * a starting point for the Bristol filters. There will end up being a
	 * number of filter algorithms.
	 */

	if (voice->flags & BRISTOL_KEYON)
	{
		/*
		 * Do any relevant note_on initialisation.
		 */
		output = 0;
		velocity = 0;
	} else {
		output = local->output;
		velocity = local->velocity;
	}

#ifdef BRISTOL_DBG
	printf("resonator(%x, %x, %x)\n", operator, param, local);
#endif
/*printf("%f %f %f\n", cutoff, vdelta, velocity); */

	for (; count > 0; count-=1)
	{
		cvdelta = cutoff * vdelta;
		if ((ccut = (cutoff * (1 - mod)) + *mb * mod) < 0.001)
			ccut = 0.001;
		/*
		 * Needs to build in a mass, and a velocity. Mass is implemented by 
		 * vdelta, which limits the amount by which the velocity can change.
		 */
		velocity += ((*ib++ - output) * cvdelta);
		if (velocity > ccut)
			velocity = ccut;
		if (velocity < -ccut)
			velocity = -ccut;

		*ob++ = (output += velocity) * gain;
	}
/*printf("%f, %f\n", ccut, cvdelta); */
	/*
	 * Put back the local variables
	 */
	local->output = output;
	local->velocity = velocity;
	return(0);
}

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

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

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

	/*
	 * 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 = (bristolRES *) bristolmalloc0(sizeof(bristolRES));
	(*operator)->specs = (bristolOPSpec *) specs;
	(*operator)->size = sizeof(bristolRES);

	/*
	 * 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(bristolRESlocal);

	/*
	 * Now fill in the control specs for this filter.
	 */
	specs->spec.param[0].pname = "cutoff";
	specs->spec.param[0].description = "Filter cutoff frequency";
	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 = "resonance";
	specs->spec.param[1].description = "Filter emphasis";
	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 = "modulation";
	specs->spec.param[2].description = "Depth of modulation control";
	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 = "keyboard tracking";
	specs->spec.param[3].description = "Cutoff tracks keyboard";
	specs->spec.param[3].type = BRISTOL_TOGGLE;
	specs->spec.param[3].low = 0;
	specs->spec.param[3].high = 1;
	specs->spec.param[3].flags = BRISTOL_BUTTON;

	/*
	 * Now fill in the dco IO specs.
	 */
	specs->spec.io[0].ioname = "input";
	specs->spec.io[0].description = "Filter 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 = "mod";
	specs->spec.io[1].description = "Filter Control Signal";
	specs->spec.io[1].samplerate = samplerate;
	specs->spec.io[1].samplecount = samplecount;
	specs->spec.io[1].flags = BRISTOL_DC|BRISTOL_INPUT|BRISTOL_HIDE;

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

	return(*operator);
}