File: stretch.c

package info (click to toggle)
sox 12.17.3-4woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,048 kB
  • ctags: 2,183
  • sloc: ansic: 24,796; sh: 3,005; makefile: 228; perl: 133
file content (431 lines) | stat: -rw-r--r-- 10,939 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * (c) march/april 2000 Fabien COELHO <fabien@coelho.net> for sox.
 *
 * Basic time stretcher.
 * cross fade samples so as to go slower of faster.
 *
 * The automaton is based on 6 parameters:
 * - stretch factor f
 * - window size w
 * - input step i
 *   output step o=f*i
 * - steady state of window s, ss = s*w
 * - type of cross fading
 *
 * I decided of the default values of these parameters based
 * on some small non extensive tests. maybe better defaults
 * can be suggested.
 * 
 * It cannot handle different number of channels.
 * It cannot handle rate change.
 */
#include "st_i.h"

#include <stdlib.h> /* malloc and free */
#include <string.h> /* memcpy() */

#ifndef MIN
#define MIN(s1,s2) ((s1)<(s2)?(s1):(s2))
#endif

#ifndef STRETCH_FLOAT
#define STRETCH_FLOAT float
#define STRETCH_FLOAT_SCAN "%f"
#endif

#define STRETCH_USAGE \
    "Usage: stretch factor [window fade shift fading]\n" \
    "\t(expansion, frame in ms, lin/..., unit<1.0, unit<0.5)\n" \
    "\t(defaults: 1.0 20 lin ...)"

/* ok, it looks stupid to have such constant.
   this is because of the cast, if floats are switched to doubles.
 */
#define ZERO                   		((STRETCH_FLOAT)(0.0e0))
#define HALF                   		((STRETCH_FLOAT)(0.5e0))
#define ONE                    		((STRETCH_FLOAT)(1.0e0))
#define MONE                   		((STRETCH_FLOAT)(-1.0e0))
#define ONETHOUSANDS           		((STRETCH_FLOAT)(0.001e0))

#define DEFAULT_SLOW_SHIFT_RATIO    	((STRETCH_FLOAT)(0.8e0))
#define DEFAULT_FAST_SHIFT_RATIO    	ONE

#define DEFAULT_STRETCH_WINDOW 		((STRETCH_FLOAT)(20.0e0))  /* ms */

/* I'm planing to put some common fading stuff outside. 
   It's also used in pitch.c
 */
typedef enum { st_linear_fading } st_fading_t;

#define DEFAULT_FADING st_linear_fading

typedef enum { input_state, output_state } stretch_status_t;

typedef struct 
{
    /* options
     * Q: maybe shift could be allowed > 1.0 with factor < 1.0 ???
     */
    STRETCH_FLOAT factor;   /* strech factor. 1.0 means copy. */
    STRETCH_FLOAT window;   /* window in ms */
    st_fading_t fade;       /* type of fading */
    STRETCH_FLOAT shift;    /* shift ratio wrt window. <1.0 */
    STRETCH_FLOAT fading;   /* fading ratio wrt window. <0.5 */

    /* internal stuff 
     */
    stretch_status_t state; /* automaton status */
    int clipped;            /* number of clipped values. */

    int size;               /* buffer size */
    int index;              /* next available element */
    st_sample_t *ibuf;      /* input buffer */
    int ishift;             /* input shift */

    int oindex;             /* next evailable element */
    STRETCH_FLOAT * obuf;   /* output buffer */
    int oshift;             /* output shift */

    int fsize;              /* fading size */
    STRETCH_FLOAT * fbuf;   /* fading, 1.0 -> 0.0 */

} * stretch_t;

/*
static void debug(stretch_t s, char * where)
{
    fprintf(stderr, 
	    "%s: (f=%.2f w=%.2f r=%.2f f=%.2f)"
	    " st=%d s=%d ii=%d is=%d oi=%d os=%d fs=%d\n",
	    where, s->factor, s->window, s->shift, s->fading,
	    s->state, s->size, s->index, s->ishift,
	    s->oindex, s->oshift, s->fsize);
}
*/

/* clip amplitudes and count number of clipped values.
 */
static st_sample_t clip(stretch_t stretch, STRETCH_FLOAT v)
{
    if (v < -ST_SAMPLE_MAX)
    {
	stretch->clipped++;
	return -ST_SAMPLE_MAX;
    }
    else if (v > ST_SAMPLE_MAX)
    {
	stretch->clipped++;
	return ST_SAMPLE_MAX;
    }
    else
    {
	return (st_sample_t) v;
    }
}

/*
 * Process options
 */
int st_stretch_getopts(eff_t effp, int n, char **argv) 
{
    stretch_t stretch = (stretch_t) effp->priv; 
    
    /* default options */
    stretch->factor = ONE; /* default is no change */
    stretch->window = DEFAULT_STRETCH_WINDOW;
    stretch->fade   = st_linear_fading;

    if (n>0 && !sscanf(argv[0], STRETCH_FLOAT_SCAN, &stretch->factor))
    {
	st_fail(STRETCH_USAGE "\n\terror while parsing factor");
	return ST_EOF;
    }

    if (n>1 && !sscanf(argv[1], STRETCH_FLOAT_SCAN, &stretch->window))
    {
	st_fail(STRETCH_USAGE "\n\terror while parsing window size");
	return ST_EOF;
    }

    if (n>2) 
    {
	switch (argv[2][0])
	{
	case 'l':
	case 'L':
	    stretch->fade = st_linear_fading;
	    break;
	default:
	    st_fail(STRETCH_USAGE "\n\terror while parsing fade type");
	    return ST_EOF;
	}
    }

    /* default shift depends whether we go slower or faster */
    stretch->shift = (stretch->factor <= ONE) ?
	DEFAULT_FAST_SHIFT_RATIO: DEFAULT_SLOW_SHIFT_RATIO;
 
    if (n>3 && !sscanf(argv[3], STRETCH_FLOAT_SCAN, &stretch->shift))
    {
	st_fail(STRETCH_USAGE "\n\terror while parsing shift ratio");
	return ST_EOF;
    }

    if (stretch->shift > ONE || stretch->shift <= ZERO)
    {
	st_fail(STRETCH_USAGE "\n\terror with shift ratio value");
	return ST_EOF;
    }

    /* default fading stuff... 
       it makes sense for factor >= 0.5
    */
    if (stretch->factor<ONE)
	stretch->fading = ONE - (stretch->factor*stretch->shift);
    else
	stretch->fading = ONE - stretch->shift;
    if (stretch->fading > HALF) stretch->fading = HALF;

    if (n>4 && !sscanf(argv[4], STRETCH_FLOAT_SCAN, &stretch->fading))
    {
	st_fail(STRETCH_USAGE "\n\terror while parsing fading ratio");
	return ST_EOF;
    }

    if (stretch->fading > HALF || stretch->fading < ZERO)
    {
	st_fail(STRETCH_USAGE "\n\terror with fading ratio value");
	return ST_EOF;
    }

    return ST_SUCCESS;
}

/*
 * Start processing
 */
int st_stretch_start(eff_t effp)
{
    stretch_t stretch = (stretch_t) effp->priv;
    register int i;

    /* not necessary. taken care by effect processing? */
    if (effp->outinfo.channels != effp->ininfo.channels)
    {
	st_fail("STRETCH cannot handle different channels (in=%d, out=%d)"
	     " use avg or pan", effp->ininfo.channels, effp->outinfo.channels);
	return ST_EOF;
    }

    if (effp->outinfo.rate != effp->ininfo.rate)
    {
	st_fail("STRETCH cannot handle different rates (in=%ld, out=%ld)"
	     " use resample or rate", effp->ininfo.rate, effp->outinfo.rate);
	return ST_EOF;
    }

    stretch->state = input_state;
    stretch->clipped = 0;

    stretch->size = (int)(effp->outinfo.rate * ONETHOUSANDS * stretch->window);
    /* start in the middle of an input to avoid initial fading... */
    stretch->index = stretch->size/2;
    stretch->ibuf  = (st_sample_t *) malloc(stretch->size * 
	                                    sizeof(st_sample_t));

    /* the shift ratio deal with the longest of ishift/oshift
       hence ishift<=size and oshift<=size. should be asserted.
     */
    if (stretch->factor < ONE)
    {
	stretch->ishift = (int) (stretch->shift * stretch->size);
	stretch->oshift = (int) (stretch->factor * stretch->ishift);
    }
    else
    {
	stretch->oshift = (int) (stretch->shift * stretch->size);
	stretch->ishift = (int) (stretch->oshift / stretch->factor);
    }

    stretch->oindex = stretch->index; /* start as synchronized */
    stretch->obuf = (STRETCH_FLOAT *)
	malloc(stretch->size * sizeof(STRETCH_FLOAT));
    
    stretch->fsize = (int) (stretch->fading * stretch->size);

    stretch->fbuf = (STRETCH_FLOAT *)
	malloc(stretch->fsize * sizeof(STRETCH_FLOAT));
	
    if (!stretch->ibuf || !stretch->obuf || !stretch->fbuf) 
    {
	st_fail("some malloc failed");
	return ST_EOF;
    }

    /* initialize buffers
     */
    for (i=0; i<stretch->size; i++)
	stretch->ibuf[i] = 0;

    for (i=0; i<stretch->size; i++)
	stretch->obuf[i] = ZERO;

    if (stretch->fsize>1)
    {
	register STRETCH_FLOAT slope = ONE / (stretch->fsize - 1);
	
	stretch->fbuf[0] = ONE;
	for (i=1; i<stretch->fsize-1; i++)
	    stretch->fbuf[i] = slope * (stretch->fsize-i-1);
	stretch->fbuf[stretch->fsize-1] = ZERO;
    } else if (stretch->fsize==1)
	stretch->fbuf[0] = ONE;

    /* debug(stretch, "start"); */

    return ST_SUCCESS;
}

/* accumulates input ibuf to output obuf with fading fbuf
 */
static void combine(stretch_t stretch)
{
    register int i, size, fsize;

    size = stretch->size;
    fsize = stretch->fsize;

    /* fade in */
    for (i=0; i<fsize; i++)
	stretch->obuf[i] += stretch->fbuf[fsize-i-1]*stretch->ibuf[i];

    /* steady state */
    for (; i<size-fsize; i++)
	stretch->obuf[i] += stretch->ibuf[i];

    /* fade out */
    for (; i<size; i++)
	stretch->obuf[i] += stretch->fbuf[i-size+fsize]*stretch->ibuf[i];
}

/*
 * Processes flow.
 */
int st_stretch_flow(eff_t effp, st_sample_t *ibuf, st_sample_t *obuf, 
                    st_size_t *isamp, st_size_t *osamp)
{
    stretch_t stretch = (stretch_t) effp->priv;
    register int iindex, oindex, i;

    iindex = 0;
    oindex = 0;

    while (iindex<*isamp && oindex<*osamp)
    {
	if (stretch->state == input_state)
	{
	    register int tocopy = MIN(*isamp-iindex, 
				      stretch->size-stretch->index);

	    memcpy(stretch->ibuf+stretch->index, 
		   ibuf+iindex, tocopy*sizeof(st_sample_t));

	    iindex += tocopy;
	    stretch->index += tocopy; 

	    if (stretch->index == stretch->size)
	    {
		/* compute */
		combine(stretch);

		/* shift input */
		for (i=0; i+stretch->ishift<stretch->size; i++)
		    stretch->ibuf[i] = stretch->ibuf[i+stretch->ishift];

		stretch->index -= stretch->ishift;

		/* switch to output state */
		stretch->state = output_state;
	    }
	}

	if (stretch->state == output_state)
	{
	    while (stretch->oindex<stretch->oshift && oindex<*osamp)
		obuf[oindex++] = 
		    clip(stretch, stretch->obuf[stretch->oindex++]);

	    if (stretch->oindex >= stretch->oshift && oindex<*osamp)
	    {
		stretch->oindex -= stretch->oshift;

		/* shift internal output buffer */
		for (i=0; i+stretch->oshift<stretch->size; i++)
		    stretch->obuf[i] = stretch->obuf[i+stretch->oshift];

		/* pad with 0 */
		for (; i<stretch->size; i++)
		    stretch->obuf[i] = ZERO;
		    
		stretch->state = input_state;
	    }
	}
    }

    *isamp = iindex;
    *osamp = oindex;

    return ST_SUCCESS;
}


/*
 * Drain buffer at the end
 * maybe not correct ? end might be artificially faded?
 */
int st_stretch_drain(eff_t effp, st_sample_t *obuf, st_size_t *osamp)
{
    stretch_t stretch = (stretch_t) effp->priv;
    register int i, oindex;

    oindex = 0;

    if (stretch->state == input_state)
    {
	for (i=stretch->index; i<stretch->size; i++)
	    stretch->ibuf[i] = 0;

	combine(stretch);
	
	stretch->state = output_state;
    }

    if (stretch->state == output_state)
    {
	for (; oindex<*osamp && stretch->oindex<stretch->index;)
	    obuf[oindex++] = clip(stretch, stretch->obuf[stretch->oindex++]);
    }
    
    *osamp = oindex;

    return ST_SUCCESS;
}


/*
 * Do anything required when you stop reading samples.  
 * Don't close input file! 
 */
int st_stretch_stop(eff_t effp)
{
    stretch_t stretch = (stretch_t) effp->priv;

    free(stretch->ibuf);
    free(stretch->obuf);
    free(stretch->fbuf);

    if (stretch->clipped)
	st_warn("STRETCH clipped %d values...", stretch->clipped);

    return ST_SUCCESS;
}