Mercurial > mplayer.hg
annotate libaf/af_scaletempo.c @ 25074:bbdd0202c238
100l, *ppc++ was supposed to be replaced by ppc[i] in r25100, but that is not any faster.
Just removing the += s->samples_overlap - s->num_channels; still provides a
ca. 20% speedup on x86 (AThlon X2 64) with gcc 3.4 (compiler stupidity?)
author | reimar |
---|---|
date | Sun, 18 Nov 2007 18:33:45 +0000 |
parents | 21227667b70a |
children | c18499a044ce |
rev | line source |
---|---|
24896 | 1 /* |
2 * scaletempo audio filter | |
3 * Copyright (c) 2007 Robert Juliano | |
4 * | |
5 * This file is part of MPlayer. | |
6 * | |
7 * MPlayer 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 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * MPlayer 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 MPlayer; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 * | |
21 * scale tempo while maintaining pitch | |
22 * (WSOLA technique with cross correlation) | |
23 * inspired by SoundTouch library by Olli Parviainen | |
24 * | |
25 * basic algorithm | |
26 * - produce 'stride' output samples per loop | |
27 * - consume stride*scale input samples per loop | |
28 * | |
29 * to produce smoother transitions between strides, blend next overlap | |
30 * samples from last stride with correlated samples of current input | |
31 * | |
32 */ | |
33 | |
34 #include <stdlib.h> | |
35 #include <string.h> | |
36 #include <limits.h> | |
37 | |
38 #include "af.h" | |
39 #include "libavutil/common.h" | |
40 #include "subopt-helper.h" | |
41 #include "help_mp.h" | |
42 | |
43 // Data for specific instances of this filter | |
44 typedef struct af_scaletempo_s | |
45 { | |
46 // stride | |
47 float scale; | |
48 float speed; | |
49 float frames_stride_scaled; | |
50 float frames_stride_error; | |
51 int bytes_per_frame; | |
52 int bytes_stride; | |
53 float bytes_stride_scaled; | |
54 int bytes_queue; | |
55 int bytes_queued; | |
56 int bytes_to_slide; | |
57 int8_t* buf_queue; | |
58 // overlap | |
59 int samples_overlap; | |
60 int samples_standing; | |
61 int bytes_overlap; | |
62 int bytes_standing; | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
63 void* buf_overlap; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
64 void* table_blend; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
65 void (*output_overlap)(struct af_scaletempo_s* s, void* out_buf, int bytes_off); |
24896 | 66 // best overlap |
67 int frames_search; | |
68 int num_channels; | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
69 void* buf_pre_corr; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
70 void* table_window; |
24896 | 71 int (*best_overlap_offset)(struct af_scaletempo_s* s); |
72 short shift_corr; | |
73 // command line | |
74 float scale_nominal; | |
75 float ms_stride; | |
76 float percent_overlap; | |
77 float ms_search; | |
78 short speed_tempo; | |
79 short speed_pitch; | |
80 } af_scaletempo_t; | |
81 | |
24899 | 82 static int fill_queue(struct af_instance_s* af, af_data_t* data, int offset) |
83 { | |
24896 | 84 af_scaletempo_t* s = af->setup; |
85 int bytes_in = data->len - offset; | |
86 int offset_unchanged = offset; | |
87 | |
88 if (s->bytes_to_slide > 0) { | |
89 if (s->bytes_to_slide < s->bytes_queued) { | |
90 int bytes_move = s->bytes_queued - s->bytes_to_slide; | |
91 memmove(s->buf_queue, | |
92 s->buf_queue + s->bytes_to_slide, | |
93 bytes_move); | |
94 s->bytes_to_slide = 0; | |
95 s->bytes_queued = bytes_move; | |
96 } else { | |
97 int bytes_skip; | |
98 s->bytes_to_slide -= s->bytes_queued; | |
99 bytes_skip = FFMIN(s->bytes_to_slide, bytes_in); | |
100 s->bytes_queued = 0; | |
101 s->bytes_to_slide -= bytes_skip; | |
102 offset += bytes_skip; | |
103 bytes_in -= bytes_skip; | |
104 } | |
105 } | |
106 | |
107 if (bytes_in > 0) { | |
108 int bytes_copy = FFMIN(s->bytes_queue - s->bytes_queued, bytes_in); | |
109 memcpy(s->buf_queue + s->bytes_queued, | |
110 (int8_t*)data->audio + offset, | |
111 bytes_copy); | |
112 s->bytes_queued += bytes_copy; | |
113 offset += bytes_copy; | |
114 } | |
115 | |
116 return offset - offset_unchanged; | |
117 } | |
118 | |
24899 | 119 static int best_overlap_offset_float(af_scaletempo_t* s) |
120 { | |
24896 | 121 float *pw, *po, *ppc, *search_start; |
122 float best_corr = INT_MIN; | |
123 int best_off = 0; | |
124 int i, off; | |
125 | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
126 pw = s->table_window; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
127 po = s->buf_overlap; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
128 po += s->num_channels; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
129 ppc = s->buf_pre_corr; |
24896 | 130 for (i=s->num_channels; i<s->samples_overlap; i++) { |
131 *ppc++ = *pw++ * *po++; | |
132 } | |
133 | |
134 search_start = (float*)s->buf_queue + s->num_channels; | |
135 for (off=0; off<s->frames_search; off++) { | |
136 float corr = 0; | |
137 float* ps = search_start; | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
138 ppc = s->buf_pre_corr; |
24896 | 139 for (i=s->num_channels; i<s->samples_overlap; i++) { |
140 corr += *ppc++ * *ps++; | |
141 } | |
142 if (corr > best_corr) { | |
143 best_corr = corr; | |
144 best_off = off; | |
145 } | |
146 search_start += s->num_channels; | |
147 } | |
148 | |
24897 | 149 return best_off * 4 * s->num_channels; |
24896 | 150 } |
151 | |
24899 | 152 static int best_overlap_offset_s16(af_scaletempo_t* s) |
153 { | |
24896 | 154 int32_t *pw, *ppc; |
155 int16_t *po, *search_start; | |
156 int32_t best_corr = INT_MIN; | |
157 int best_off = 0; | |
25073
21227667b70a
Use "long" instead of "int" for innermost loop variable.
reimar
parents:
25072
diff
changeset
|
158 int off; |
21227667b70a
Use "long" instead of "int" for innermost loop variable.
reimar
parents:
25072
diff
changeset
|
159 long i; |
24896 | 160 |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
161 pw = s->table_window; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
162 po = s->buf_overlap; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
163 po += s->num_channels; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
164 ppc = s->buf_pre_corr; |
24896 | 165 for (i=s->num_channels; i<s->samples_overlap; i++) { |
166 *ppc++ = ( *pw++ * *po++ ) >> 15; | |
167 } | |
168 | |
169 search_start = (int16_t*)s->buf_queue + s->num_channels; | |
170 for (off=0; off<s->frames_search; off++) { | |
171 int32_t corr = 0; | |
172 int16_t* ps = search_start; | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
173 ppc = s->buf_pre_corr; |
25072 | 174 i = -(s->samples_overlap - s->num_channels); |
175 do { | |
24896 | 176 corr += ( *ppc++ * *ps++ ) >> s->shift_corr; |
25072 | 177 } while (++i < 0); |
24896 | 178 if (corr > best_corr) { |
179 best_corr = corr; | |
180 best_off = off; | |
181 } | |
182 search_start += s->num_channels; | |
183 } | |
184 | |
24897 | 185 return best_off * 2 * s->num_channels; |
24896 | 186 } |
187 | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
188 static void output_overlap_float(af_scaletempo_t* s, void* buf_out, |
24899 | 189 int bytes_off) |
190 { | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
191 float* pout = buf_out; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
192 float* pb = s->table_blend; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
193 float* po = s->buf_overlap; |
24896 | 194 float* pin = (float*)(s->buf_queue + bytes_off); |
195 int i; | |
196 for (i=0; i<s->samples_overlap; i++) { | |
197 *pout++ = *po - *pb++ * ( *po - *pin++ ); po++; | |
198 } | |
199 } | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
200 static void output_overlap_s16(af_scaletempo_t* s, void* buf_out, |
24899 | 201 int bytes_off) |
202 { | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
203 int16_t* pout = buf_out; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
204 int32_t* pb = s->table_blend; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
205 int16_t* po = s->buf_overlap; |
24896 | 206 int16_t* pin = (int16_t*)(s->buf_queue + bytes_off); |
207 int i; | |
208 for (i=0; i<s->samples_overlap; i++) { | |
209 *pout++ = *po - ( ( *pb++ * ( *po - *pin++ ) ) >> 16 ); po++; | |
210 } | |
211 } | |
212 | |
213 // Filter data through filter | |
214 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
215 { | |
216 af_scaletempo_t* s = af->setup; | |
217 int offset_in; | |
218 int max_bytes_out; | |
219 int8_t* pout; | |
220 | |
221 if (s->scale == 1.0) { | |
222 return data; | |
223 } | |
224 | |
225 // RESIZE_LOCAL_BUFFER - can't use macro | |
226 max_bytes_out = ((int)(data->len / s->bytes_stride_scaled) + 1) * s->bytes_stride; | |
227 if (max_bytes_out > af->data->len) { | |
228 af_msg(AF_MSG_VERBOSE, "[libaf] Reallocating memory in module %s, " | |
229 "old len = %i, new len = %i\n",af->info->name,af->data->len,max_bytes_out); | |
230 af->data->audio = realloc(af->data->audio, max_bytes_out); | |
231 if (!af->data->audio) { | |
232 af_msg(AF_MSG_FATAL, "[libaf] Could not allocate memory\n"); | |
233 return NULL; | |
234 } | |
235 af->data->len = max_bytes_out; | |
236 } | |
237 | |
238 offset_in = fill_queue(af, data, 0); | |
239 pout = af->data->audio; | |
240 while (s->bytes_queued >= s->bytes_queue) { | |
241 int ti; | |
242 float tf; | |
243 int bytes_off = 0; | |
244 | |
245 // output stride | |
246 if (s->output_overlap) { | |
247 if (s->best_overlap_offset) | |
248 bytes_off = s->best_overlap_offset(s); | |
249 s->output_overlap(s, pout, bytes_off); | |
250 } | |
251 memcpy(pout + s->bytes_overlap, | |
252 s->buf_queue + bytes_off + s->bytes_overlap, | |
253 s->bytes_standing); | |
254 pout += s->bytes_stride; | |
255 | |
256 // input stride | |
257 memcpy(s->buf_overlap, | |
258 s->buf_queue + bytes_off + s->bytes_stride, | |
259 s->bytes_overlap); | |
260 tf = s->frames_stride_scaled + s->frames_stride_error; | |
261 ti = (int)tf; | |
262 s->frames_stride_error = tf - ti; | |
263 s->bytes_to_slide = ti * s->bytes_per_frame; | |
264 | |
265 offset_in += fill_queue(af, data, offset_in); | |
266 } | |
267 | |
24900 | 268 // This filter can have a negative delay when scale > 1: |
269 // output corresponding to some length of input can be decided and written | |
270 // after receiving only a part of that input. | |
271 af->delay = s->bytes_queued - s->bytes_to_slide; | |
272 | |
24896 | 273 data->audio = af->data->audio; |
24899 | 274 data->len = pout - (int8_t *)af->data->audio; |
24896 | 275 return data; |
276 } | |
277 | |
278 // Initialization and runtime control | |
279 static int control(struct af_instance_s* af, int cmd, void* arg) | |
280 { | |
281 af_scaletempo_t* s = af->setup; | |
282 switch(cmd){ | |
283 case AF_CONTROL_REINIT:{ | |
284 af_data_t* data = (af_data_t*)arg; | |
285 float srate = data->rate / 1000; | |
286 int nch = data->nch; | |
287 int bps; | |
288 int use_int = 0; | |
289 int frames_stride, frames_overlap; | |
290 int i, j; | |
291 | |
292 af_msg(AF_MSG_VERBOSE, | |
293 "[scaletempo] %.3f speed * %.3f scale_nominal = %.3f\n", | |
294 s->speed, s->scale_nominal, s->scale); | |
295 | |
296 if (s->scale == 1.0) { | |
297 if (s->speed_tempo && s->speed_pitch) | |
298 return AF_DETACH; | |
299 memcpy(af->data, data, sizeof(af_data_t)); | |
300 return af_test_output(af, data); | |
301 } | |
302 | |
303 af->data->rate = data->rate; | |
304 af->data->nch = data->nch; | |
305 if ( data->format == AF_FORMAT_S16_LE | |
306 || data->format == AF_FORMAT_S16_BE ) { | |
307 use_int = 1; | |
308 af->data->format = AF_FORMAT_S16_NE; | |
309 af->data->bps = bps = 2; | |
310 } else { | |
311 af->data->format = AF_FORMAT_FLOAT_NE; | |
312 af->data->bps = bps = 4; | |
313 } | |
314 | |
315 frames_stride = srate * s->ms_stride; | |
316 s->bytes_stride = frames_stride * bps * nch; | |
317 s->bytes_stride_scaled = s->scale * s->bytes_stride; | |
318 s->frames_stride_scaled = s->scale * frames_stride; | |
319 s->frames_stride_error = 0; | |
320 af->mul = (double)s->bytes_stride / s->bytes_stride_scaled; | |
321 | |
322 frames_overlap = frames_stride * s->percent_overlap; | |
323 if (frames_overlap <= 0) { | |
324 s->bytes_standing = s->bytes_stride; | |
325 s->samples_standing = s->bytes_standing / bps; | |
326 s->output_overlap = NULL; | |
327 } else { | |
328 s->samples_overlap = frames_overlap * nch; | |
329 s->bytes_overlap = frames_overlap * nch * bps; | |
330 s->bytes_standing = s->bytes_stride - s->bytes_overlap; | |
331 s->samples_standing = s->bytes_standing / bps; | |
332 s->buf_overlap = realloc(s->buf_overlap, s->bytes_overlap); | |
333 s->table_blend = realloc(s->table_blend, s->bytes_overlap * 4); | |
334 if(!s->buf_overlap || !s->table_blend) { | |
335 af_msg(AF_MSG_FATAL, "[scaletempo] Out of memory\n"); | |
336 return AF_ERROR; | |
337 } | |
338 bzero(s->buf_overlap, s->bytes_overlap); | |
339 if (use_int) { | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
340 int32_t* pb = s->table_blend; |
24896 | 341 int64_t blend = 0; |
342 for (i=0; i<frames_overlap; i++) { | |
343 int32_t v = blend / frames_overlap; | |
344 for (j=0; j<nch; j++) { | |
345 *pb++ = v; | |
346 } | |
347 blend += 65536; // 2^16 | |
348 } | |
24899 | 349 s->output_overlap = output_overlap_s16; |
24896 | 350 } else { |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
351 float* pb = s->table_blend; |
24896 | 352 for (i=0; i<frames_overlap; i++) { |
353 float v = i / (float)frames_overlap; | |
354 for (j=0; j<nch; j++) { | |
355 *pb++ = v; | |
356 } | |
357 } | |
24899 | 358 s->output_overlap = output_overlap_float; |
24896 | 359 } |
360 } | |
361 | |
362 s->frames_search = (frames_overlap > 1) ? srate * s->ms_search : 0; | |
363 if (s->frames_search <= 0) { | |
364 s->best_overlap_offset = NULL; | |
365 } else { | |
366 if (use_int) { | |
367 int64_t t = frames_overlap; | |
368 int32_t n = 8589934588LL / (t * t); // 4 * (2^31 - 1) / t^2 | |
369 int32_t* pw; | |
370 s->buf_pre_corr = realloc(s->buf_pre_corr, s->bytes_overlap * 2); | |
371 s->table_window = realloc(s->table_window, s->bytes_overlap * 2 - nch * bps * 2); | |
25070
60b34bf9a41e
100l, someone mixed up && and ||, so if allocation of only one buffers failed
reimar
parents:
25069
diff
changeset
|
372 if(!s->buf_pre_corr || !s->table_window) { |
24896 | 373 af_msg(AF_MSG_FATAL, "[scaletempo] Out of memory\n"); |
374 return AF_ERROR; | |
375 } | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
376 pw = s->table_window; |
24896 | 377 for (i=1; i<frames_overlap; i++) { |
378 int32_t v = ( i * (t - i) * n ) >> 15; | |
379 for (j=0; j<nch; j++) { | |
380 *pw++ = v; | |
381 } | |
382 } | |
383 s->shift_corr = av_log2( 2*(s->samples_overlap - nch) - 1 ); | |
24899 | 384 s->best_overlap_offset = best_overlap_offset_s16; |
24896 | 385 } else { |
386 float* pw; | |
387 s->buf_pre_corr = realloc(s->buf_pre_corr, s->bytes_overlap); | |
388 s->table_window = realloc(s->table_window, s->bytes_overlap - nch * bps); | |
389 if(!s->buf_pre_corr || !s->table_window) { | |
390 af_msg(AF_MSG_FATAL, "[scaletempo] Out of memory\n"); | |
391 return AF_ERROR; | |
392 } | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
393 pw = s->table_window; |
24896 | 394 for (i=1; i<frames_overlap; i++) { |
395 float v = i * (frames_overlap - i); | |
396 for (j=0; j<nch; j++) { | |
397 *pw++ = v; | |
398 } | |
399 } | |
24899 | 400 s->best_overlap_offset = best_overlap_offset_float; |
24896 | 401 } |
402 } | |
403 | |
404 s->bytes_per_frame = bps * nch; | |
405 s->num_channels = nch; | |
406 | |
407 s->bytes_queue | |
408 = (s->frames_search + frames_stride + frames_overlap) * bps * nch; | |
409 s->buf_queue = realloc(s->buf_queue, s->bytes_queue); | |
410 if(!s->buf_queue) { | |
411 af_msg(AF_MSG_FATAL, "[scaletempo] Out of memory\n"); | |
412 return AF_ERROR; | |
413 } | |
414 | |
415 af_msg (AF_MSG_DEBUG0, "[scaletempo] " | |
416 "%.2f stride_in, %i stride_out, %i standing, " | |
417 "%i overlap, %i search, %i queue, %s mode\n", | |
418 s->frames_stride_scaled, | |
419 (int)(s->bytes_stride / nch / bps), | |
420 (int)(s->bytes_standing / nch / bps), | |
421 (int)(s->bytes_overlap / nch / bps), | |
422 s->frames_search, | |
423 (int)(s->bytes_queue / nch / bps), | |
424 (use_int?"s16":"float")); | |
425 | |
426 return af_test_output(af, (af_data_t*)arg); | |
427 } | |
428 case AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET:{ | |
429 if (s->speed_tempo) { | |
430 if (s->speed_pitch) { | |
431 break; | |
432 } | |
433 s->speed = *(float*)arg; | |
434 s->scale = s->speed * s->scale_nominal; | |
435 } else { | |
436 if (s->speed_pitch) { | |
437 s->speed = 1 / *(float*)arg; | |
438 s->scale = s->speed * s->scale_nominal; | |
439 break; | |
440 } | |
441 } | |
442 return AF_OK; | |
443 } | |
444 case AF_CONTROL_SCALETEMPO_AMOUNT | AF_CONTROL_SET:{ | |
445 s->scale = *(float*)arg; | |
446 s->scale = s->speed * s->scale_nominal; | |
447 return AF_OK; | |
448 } | |
449 case AF_CONTROL_SCALETEMPO_AMOUNT | AF_CONTROL_GET: | |
450 *(float*)arg = s->scale; | |
451 return AF_OK; | |
452 case AF_CONTROL_COMMAND_LINE:{ | |
24898 | 453 strarg_t speed = {}; |
24896 | 454 opt_t subopts[] = { |
455 {"scale", OPT_ARG_FLOAT, &s->scale_nominal, NULL}, | |
456 {"stride", OPT_ARG_FLOAT, &s->ms_stride, NULL}, | |
457 {"overlap", OPT_ARG_FLOAT, &s->percent_overlap, NULL}, | |
458 {"search", OPT_ARG_FLOAT, &s->ms_search, NULL}, | |
459 {"speed", OPT_ARG_STR, &speed, NULL}, | |
460 {NULL}, | |
461 }; | |
462 if (subopt_parse(arg, subopts) != 0) { | |
463 return AF_ERROR; | |
464 } | |
465 if (s->scale_nominal <= 0) { | |
466 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
467 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
468 ": scale > 0\n"); | |
469 return AF_ERROR; | |
470 } | |
471 if (s->ms_stride <= 0) { | |
472 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
473 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
474 ": stride > 0\n"); | |
475 return AF_ERROR; | |
476 } | |
477 if (s->percent_overlap < 0 || s->percent_overlap > 1) { | |
478 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
479 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
480 ": 0 <= overlap <= 1\n"); | |
481 return AF_ERROR; | |
482 } | |
483 if (s->ms_search < 0) { | |
484 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
485 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
486 ": search >= 0\n"); | |
487 return AF_ERROR; | |
488 } | |
489 if (speed.len > 0) { | |
490 if (strcmp(speed.str, "pitch") == 0) { | |
491 s->speed_tempo = 0; | |
492 s->speed_pitch = 1; | |
493 } else if (strcmp(speed.str, "tempo") == 0) { | |
494 s->speed_tempo = 1; | |
495 s->speed_pitch = 0; | |
496 } else if (strcmp(speed.str, "none") == 0) { | |
497 s->speed_tempo = 0; | |
498 s->speed_pitch = 0; | |
499 } else if (strcmp(speed.str, "both") == 0) { | |
500 s->speed_tempo = 1; | |
501 s->speed_pitch = 1; | |
502 } else { | |
503 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
504 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
505 ": speed=[pitch|tempo|none|both]\n"); | |
506 return AF_ERROR; | |
507 } | |
508 } | |
509 s->scale = s->speed * s->scale_nominal; | |
510 af_msg(AF_MSG_DEBUG0, "[scaletempo] %6.3f scale, %6.2f stride, %6.2f overlap, %6.2f search, speed = %s\n", s->scale_nominal, s->ms_stride, s->percent_overlap, s->ms_search, (s->speed_tempo?(s->speed_pitch?"tempo and speed":"tempo"):(s->speed_pitch?"pitch":"none"))); | |
511 return AF_OK; | |
512 } | |
513 } | |
514 return AF_UNKNOWN; | |
515 } | |
516 | |
517 // Deallocate memory | |
518 static void uninit(struct af_instance_s* af) | |
519 { | |
520 af_scaletempo_t* s = af->setup; | |
521 free(af->data->audio); | |
522 free(af->data); | |
523 free(s->buf_queue); | |
524 free(s->buf_overlap); | |
525 free(s->buf_pre_corr); | |
526 free(s->table_blend); | |
527 free(s->table_window); | |
528 free(af->setup); | |
529 } | |
530 | |
531 // Allocate memory and set function pointers | |
532 static int af_open(af_instance_t* af){ | |
533 af_scaletempo_t* s; | |
534 | |
535 af->control = control; | |
536 af->uninit = uninit; | |
537 af->play = play; | |
538 af->mul = 1; | |
539 af->data = calloc(1,sizeof(af_data_t)); | |
540 af->setup = calloc(1,sizeof(af_scaletempo_t)); | |
541 if(af->data == NULL || af->setup == NULL) | |
542 return AF_ERROR; | |
543 | |
544 s = af->setup; | |
545 s->scale = s->speed = s->scale_nominal = 1.0; | |
546 s->speed_tempo = 1; | |
547 s->speed_pitch = 0; | |
548 s->ms_stride = 60; | |
549 s->percent_overlap = .20; | |
550 s->ms_search = 14; | |
551 | |
552 return AF_OK; | |
553 } | |
554 | |
555 // Description of this filter | |
556 af_info_t af_info_scaletempo = { | |
557 "Scale audio tempo while maintaining pitch", | |
558 "scaletempo", | |
559 "Robert Juliano", | |
560 "", | |
561 AF_FLAGS_REENTRANT, | |
562 af_open | |
563 }; |