File: chorus.c

package info (click to toggle)
sox 12.16-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,180 kB
  • ctags: 1,466
  • sloc: ansic: 16,658; sh: 2,071; makefile: 126
file content (359 lines) | stat: -rw-r--r-- 10,543 bytes parent folder | download
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
/*
 * August 24, 1998
 * Copyright (C) 1998 Juergen Mueller And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained. 
 * Juergen Mueller And Sundry Contributors are not responsible for 
 * the consequences of using this software.
 */

/*
 * 	Chorus effect.
 * 
 * Flow diagram scheme for n delays ( 1 <= n <= MAX_CHORUS ):
 *
 *        * gain-in                                           ___
 * ibuff -----+--------------------------------------------->|   |
 *            |      _________                               |   |
 *            |     |         |                   * decay 1  |   |
 *            +---->| delay 1 |----------------------------->|   |
 *            |     |_________|                              |   |
 *            |        /|\                                   |   |
 *            :         |                                    |   |
 *            : +-----------------+   +--------------+       | + |
 *            : | Delay control 1 |<--| mod. speed 1 |       |   |
 *            : +-----------------+   +--------------+       |   |
 *            |      _________                               |   |
 *            |     |         |                   * decay n  |   |
 *            +---->| delay n |----------------------------->|   |
 *                  |_________|                              |   |
 *                     /|\                                   |___|
 *                      |                                      |  
 *              +-----------------+   +--------------+         | * gain-out
 *              | Delay control n |<--| mod. speed n |         |
 *              +-----------------+   +--------------+         +----->obuff
 *
 *
 * The delay i is controled by a sine or triangle modulation i ( 1 <= i <= n).
 *
 * Usage: 
 *   chorus gain-in gain-out delay-1 decay-1 speed-1 depth-1 -s1|t1 [
 *       delay-2 decay-2 speed-2 depth-2 -s2|-t2 ... ]
 *
 * Where:
 *   gain-in, decay-1 ... decay-n :  0.0 ... 1.0      volume
 *   gain-out :  0.0 ...      volume
 *   delay-1 ... delay-n :  20.0 ... 100.0 msec
 *   speed-1 ... speed-n :  0.1 ... 5.0 Hz       modulation 1 ... n
 *   depth-1 ... depth-n :  0.0 ... 10.0 msec    modulated delay 1 ... n
 *   -s1 ... -sn : modulation by sine 1 ... n
 *   -t1 ... -tn : modulation by triangle 1 ... n
 *
 * Note:
 *   when decay is close to 1.0, the samples can begin clipping and the output
 *   can saturate! 
 *
 * Hint:
 *   1 / out-gain < gain-in ( 1 + decay-1 + ... + decay-n )
 *
*/

/*
 * Sound Tools chorus effect file.
 */

#include <stdlib.h> /* Harmless, and prototypes atof() etc. --dgc */
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <math.h>
#include <string.h>
#include "st.h"

#define MOD_SINE	0
#define MOD_TRIANGLE	1
#define MAX_CHORUS	7

/* Private data for SKEL file */
typedef struct chorusstuff {
	int	num_chorus;
	int	modulation[MAX_CHORUS];
	int	counter;			
	long	phase[MAX_CHORUS];
	float	*chorusbuf;
	float	in_gain, out_gain;
	float	delay[MAX_CHORUS], decay[MAX_CHORUS];
	float	speed[MAX_CHORUS], depth[MAX_CHORUS];
	long	length[MAX_CHORUS];
	int	*lookup_tab[MAX_CHORUS];
	int	depth_samples[MAX_CHORUS], samples[MAX_CHORUS];
	int	maxsamples, fade_out;
} *chorus_t;

/* Private data for SKEL file */

LONG chorus_clip24(l)
LONG l;
{
	if (l >= ((LONG)1 << 24))
		return ((LONG)1 << 24) - 1;
	else if (l <= -((LONG)1 << 24))
		return -((LONG)1 << 24) + 1;
	else
		return l;
}

/* This was very painful.  We need a sine library. */

void chorus_sine(buf, len, max, depth)
int *buf;
long len;
int max;
int depth;
{
	long i;
	int offset;
	double val;

	offset = max - depth;
	for (i = 0; i < len; i++) {
		val = sin((double)i/(double)len * 2.0 * M_PI);
		buf[i] = offset + (int) (val * (double)depth);
	}
}

void chorus_triangle(buf, len, max, depth)
int *buf;
long len;
int max;
int depth;
{
	long i;
	int offset;
	double val;

	offset = max - 2 * depth;
	for (i = 0; i < len / 2; i++) {
		val = i * 2.0 / len;
		buf[i] = offset + (int) (val * 2.0 * (double)depth);
	}
	for (i = len / 2; i < len ; i++) {
		val = (len - i) * 2.0 / len;
		buf[i] = offset + (int) (val * 2.0 * (double)depth);
	}
}

/*
 * Process options
 */
void chorus_getopts(effp, n, argv) 
eff_t effp;
int n;
char **argv;
{
	chorus_t chorus = (chorus_t) effp->priv;
	int i;

	chorus->num_chorus = 0;
	i = 0;

	if ( ( n < 7 ) || (( n - 2 ) % 5 ) )
	    fail("Usage: chorus gain-in gain-out delay decay speed depth [ -s | -t ]");

	sscanf(argv[i++], "%f", &chorus->in_gain);
	sscanf(argv[i++], "%f", &chorus->out_gain);
	while ( i < n ) {
		if ( chorus->num_chorus > MAX_CHORUS )
			fail("chorus: to many delays, use less than %i delays", MAX_CHORUS);
		sscanf(argv[i++], "%f", &chorus->delay[chorus->num_chorus]);
		sscanf(argv[i++], "%f", &chorus->decay[chorus->num_chorus]);
		sscanf(argv[i++], "%f", &chorus->speed[chorus->num_chorus]);
		sscanf(argv[i++], "%f", &chorus->depth[chorus->num_chorus]);
		if ( !strcmp(argv[i], "-s"))
			chorus->modulation[chorus->num_chorus] = MOD_SINE;
		else if ( ! strcmp(argv[i], "-t"))
			chorus->modulation[chorus->num_chorus] = MOD_TRIANGLE;
		else
    			fail("Usage: chorus gain-in gain-out delay decay speed [ -s | -t ]");
		i++;
		chorus->num_chorus++;
	}
}

/*
 * Prepare for processing.
 */
void chorus_start(effp)
eff_t effp;
{
	chorus_t chorus = (chorus_t) effp->priv;
	int i;
	float sum_in_volume;

	chorus->maxsamples = 0;

	if ( chorus->in_gain < 0.0 )
		fail("chorus: gain-in must be positive!\n");
	if ( chorus->in_gain > 1.0 )
		fail("chorus: gain-in must be less than 1.0!\n");
	if ( chorus->out_gain < 0.0 )
		fail("chorus: gain-out must be positive!\n");
	for ( i = 0; i < chorus->num_chorus; i++ ) {
		chorus->samples[i] = (int) ( ( chorus->delay[i] + 
			chorus->depth[i] ) * effp->ininfo.rate / 1000.0);
		chorus->depth_samples[i] = (int) (chorus->depth[i] * 
			effp->ininfo.rate / 1000.0);

		if ( chorus->delay[i] < 20.0 )
	    		fail("chorus: delay must be more than 20.0 msec!\n");
		if ( chorus->delay[i] > 100.0 )
	    		fail("chorus: delay must be less than 100.0 msec!\n");
		if ( chorus->speed[i] < 0.1 )
	    		fail("chorus: speed must be more than 0.1 Hz!\n");
		if ( chorus->speed[i] > 5.0 )
	    		fail("chorus: speed must be less than 5.0 Hz!\n");
		if ( chorus->depth[i] < 0.0 )
	    		fail("chorus: delay must be more positive!\n");
		if ( chorus->depth[i] > 10.0 )
	    		fail("chorus: delay must be less than 10.0 msec!\n");
		if ( chorus->decay[i] < 0.0 )
	    		fail("chorus: decay must be positive!\n" );
		if ( chorus->decay[i] > 1.0 )
	    		fail("chorus: decay must be less that 1.0!\n" );
		chorus->length[i] = effp->ininfo.rate / chorus->speed[i];
		if (! (chorus->lookup_tab[i] = 
			(int *) malloc(sizeof (int) * chorus->length[i])))
			fail("chorus: Cannot malloc %d bytes!\n", 
				sizeof(int) * chorus->length[i]);
		if ( chorus->modulation[i] == MOD_SINE )
			chorus_sine(chorus->lookup_tab[i], chorus->length[i], 
				chorus->samples[i] - 1, chorus->depth_samples[i]);
		else
			chorus_triangle(chorus->lookup_tab[i], chorus->length[i], 
				chorus->samples[i] - 1, chorus->depth_samples[i]);
		chorus->phase[i] = 0;

		if ( chorus->samples[i] > chorus->maxsamples )
			chorus->maxsamples = chorus->samples[i];
	}

	/* Be nice and check the hint with warning, if... */
	sum_in_volume = 1.0;
	for ( i = 0; i < chorus->num_chorus; i++ )
		sum_in_volume += chorus->decay[i];
	if ( chorus->in_gain * ( sum_in_volume ) > 1.0 / chorus->out_gain )
	warn("chorus: warning >>> gain-out can cause saturation or clipping of output <<<");


	if (! (chorus->chorusbuf = 
		(float *) malloc(sizeof (float) * chorus->maxsamples)))
		fail("chorus: Cannot malloc %d bytes!\n", 
			sizeof(float) * chorus->maxsamples);
	for ( i = 0; i < chorus->maxsamples; i++ )
		chorus->chorusbuf[i] = 0.0;

	chorus->counter = 0;
	chorus->fade_out = chorus->maxsamples;
}

/*
 * Processed signed long samples from ibuf to obuf.
 * Return number of samples processed.
 */

void chorus_flow(effp, ibuf, obuf, isamp, osamp)
eff_t effp;
LONG *ibuf, *obuf;
int *isamp, *osamp;
{
	chorus_t chorus = (chorus_t) effp->priv;
	int len, done;
	int i;
	
	float d_in, d_out;
	LONG out;

	len = ((*isamp > *osamp) ? *osamp : *isamp);
	for(done = 0; done < len; done++) {
		/* Store delays as 24-bit signed longs */
		d_in = (float) *ibuf++ / 256;
		/* Compute output first */
		d_out = d_in * chorus->in_gain;
		for ( i = 0; i < chorus->num_chorus; i++ )
			d_out += chorus->chorusbuf[(chorus->maxsamples + 
		        chorus->counter - chorus->lookup_tab[i][chorus->phase[i]]) % 
			chorus->maxsamples] * chorus->decay[i];
		/* Adjust the output volume and size to 24 bit */
		d_out = d_out * chorus->out_gain;
		out = chorus_clip24((LONG) d_out);
		*obuf++ = out * 256;
		/* Mix decay of delay and input */
		chorus->chorusbuf[chorus->counter] = d_in;
		chorus->counter = 
			( chorus->counter + 1 ) % chorus->maxsamples;
		for ( i = 0; i < chorus->num_chorus; i++ )
			chorus->phase[i]  = 
				( chorus->phase[i] + 1 ) % chorus->length[i];
	}
	/* processed all samples */
}

/*
 * Drain out reverb lines. 
 */
void chorus_drain(effp, obuf, osamp)
eff_t effp;
LONG *obuf;
int *osamp;
{
	chorus_t chorus = (chorus_t) effp->priv;
	int done;
	int i;
	
	float d_in, d_out;
	LONG out;

	done = 0;
	while ( ( done < *osamp ) && ( done < chorus->fade_out ) ) {
		d_in = 0;
		d_out = 0;
		/* Compute output first */
		for ( i = 0; i < chorus->num_chorus; i++ )
			d_out += chorus->chorusbuf[(chorus->maxsamples + 
		chorus->counter - chorus->lookup_tab[i][chorus->phase[i]]) % 
		chorus->maxsamples] * chorus->decay[i];
		/* Adjust the output volume and size to 24 bit */
		d_out = d_out * chorus->out_gain;
		out = chorus_clip24((LONG) d_out);
		*obuf++ = out * 256;
		/* Mix decay of delay and input */
		chorus->chorusbuf[chorus->counter] = d_in;
		chorus->counter = 
			( chorus->counter + 1 ) % chorus->maxsamples;
		for ( i = 0; i < chorus->num_chorus; i++ )
			chorus->phase[i]  = 
				( chorus->phase[i] + 1 ) % chorus->length[i];
		done++;
		chorus->fade_out--;
	}
	/* samples played, it remains */
	*osamp = done;
}

/*
 * Clean up chorus effect.
 */
void chorus_stop(effp)
eff_t effp;
{
	chorus_t chorus = (chorus_t) effp->priv;
	int i;

	free((char *) chorus->chorusbuf);
	chorus->chorusbuf = (float *) -1;   /* guaranteed core dump */
	for ( i = 0; i < chorus->num_chorus; i++ ) {
		free((char *) chorus->lookup_tab[i]);
		chorus->lookup_tab[i] = (int *) -1;   /* guaranteed core dump */
	}
}