Mercurial > mplayer.hg
annotate libaf/af_scaletempo.c @ 25073:21227667b70a
Use "long" instead of "int" for innermost loop variable.
About 12% faster on x86_64
author | reimar |
---|---|
date | Sun, 18 Nov 2007 17:44:11 +0000 |
parents | 6c3f9215d606 |
children | bbdd0202c238 |
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 ppc += s->samples_overlap - s->num_channels; |
175 ps += s->samples_overlap - s->num_channels; | |
176 i = -(s->samples_overlap - s->num_channels); | |
177 do { | |
24896 | 178 corr += ( *ppc++ * *ps++ ) >> s->shift_corr; |
25072 | 179 } while (++i < 0); |
24896 | 180 if (corr > best_corr) { |
181 best_corr = corr; | |
182 best_off = off; | |
183 } | |
184 search_start += s->num_channels; | |
185 } | |
186 | |
24897 | 187 return best_off * 2 * s->num_channels; |
24896 | 188 } |
189 | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
190 static void output_overlap_float(af_scaletempo_t* s, void* buf_out, |
24899 | 191 int bytes_off) |
192 { | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
193 float* pout = buf_out; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
194 float* pb = s->table_blend; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
195 float* po = s->buf_overlap; |
24896 | 196 float* pin = (float*)(s->buf_queue + bytes_off); |
197 int i; | |
198 for (i=0; i<s->samples_overlap; i++) { | |
199 *pout++ = *po - *pb++ * ( *po - *pin++ ); po++; | |
200 } | |
201 } | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
202 static void output_overlap_s16(af_scaletempo_t* s, void* buf_out, |
24899 | 203 int bytes_off) |
204 { | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
205 int16_t* pout = buf_out; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
206 int32_t* pb = s->table_blend; |
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
207 int16_t* po = s->buf_overlap; |
24896 | 208 int16_t* pin = (int16_t*)(s->buf_queue + bytes_off); |
209 int i; | |
210 for (i=0; i<s->samples_overlap; i++) { | |
211 *pout++ = *po - ( ( *pb++ * ( *po - *pin++ ) ) >> 16 ); po++; | |
212 } | |
213 } | |
214 | |
215 // Filter data through filter | |
216 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
217 { | |
218 af_scaletempo_t* s = af->setup; | |
219 int offset_in; | |
220 int max_bytes_out; | |
221 int8_t* pout; | |
222 | |
223 if (s->scale == 1.0) { | |
224 return data; | |
225 } | |
226 | |
227 // RESIZE_LOCAL_BUFFER - can't use macro | |
228 max_bytes_out = ((int)(data->len / s->bytes_stride_scaled) + 1) * s->bytes_stride; | |
229 if (max_bytes_out > af->data->len) { | |
230 af_msg(AF_MSG_VERBOSE, "[libaf] Reallocating memory in module %s, " | |
231 "old len = %i, new len = %i\n",af->info->name,af->data->len,max_bytes_out); | |
232 af->data->audio = realloc(af->data->audio, max_bytes_out); | |
233 if (!af->data->audio) { | |
234 af_msg(AF_MSG_FATAL, "[libaf] Could not allocate memory\n"); | |
235 return NULL; | |
236 } | |
237 af->data->len = max_bytes_out; | |
238 } | |
239 | |
240 offset_in = fill_queue(af, data, 0); | |
241 pout = af->data->audio; | |
242 while (s->bytes_queued >= s->bytes_queue) { | |
243 int ti; | |
244 float tf; | |
245 int bytes_off = 0; | |
246 | |
247 // output stride | |
248 if (s->output_overlap) { | |
249 if (s->best_overlap_offset) | |
250 bytes_off = s->best_overlap_offset(s); | |
251 s->output_overlap(s, pout, bytes_off); | |
252 } | |
253 memcpy(pout + s->bytes_overlap, | |
254 s->buf_queue + bytes_off + s->bytes_overlap, | |
255 s->bytes_standing); | |
256 pout += s->bytes_stride; | |
257 | |
258 // input stride | |
259 memcpy(s->buf_overlap, | |
260 s->buf_queue + bytes_off + s->bytes_stride, | |
261 s->bytes_overlap); | |
262 tf = s->frames_stride_scaled + s->frames_stride_error; | |
263 ti = (int)tf; | |
264 s->frames_stride_error = tf - ti; | |
265 s->bytes_to_slide = ti * s->bytes_per_frame; | |
266 | |
267 offset_in += fill_queue(af, data, offset_in); | |
268 } | |
269 | |
24900 | 270 // This filter can have a negative delay when scale > 1: |
271 // output corresponding to some length of input can be decided and written | |
272 // after receiving only a part of that input. | |
273 af->delay = s->bytes_queued - s->bytes_to_slide; | |
274 | |
24896 | 275 data->audio = af->data->audio; |
24899 | 276 data->len = pout - (int8_t *)af->data->audio; |
24896 | 277 return data; |
278 } | |
279 | |
280 // Initialization and runtime control | |
281 static int control(struct af_instance_s* af, int cmd, void* arg) | |
282 { | |
283 af_scaletempo_t* s = af->setup; | |
284 switch(cmd){ | |
285 case AF_CONTROL_REINIT:{ | |
286 af_data_t* data = (af_data_t*)arg; | |
287 float srate = data->rate / 1000; | |
288 int nch = data->nch; | |
289 int bps; | |
290 int use_int = 0; | |
291 int frames_stride, frames_overlap; | |
292 int i, j; | |
293 | |
294 af_msg(AF_MSG_VERBOSE, | |
295 "[scaletempo] %.3f speed * %.3f scale_nominal = %.3f\n", | |
296 s->speed, s->scale_nominal, s->scale); | |
297 | |
298 if (s->scale == 1.0) { | |
299 if (s->speed_tempo && s->speed_pitch) | |
300 return AF_DETACH; | |
301 memcpy(af->data, data, sizeof(af_data_t)); | |
302 return af_test_output(af, data); | |
303 } | |
304 | |
305 af->data->rate = data->rate; | |
306 af->data->nch = data->nch; | |
307 if ( data->format == AF_FORMAT_S16_LE | |
308 || data->format == AF_FORMAT_S16_BE ) { | |
309 use_int = 1; | |
310 af->data->format = AF_FORMAT_S16_NE; | |
311 af->data->bps = bps = 2; | |
312 } else { | |
313 af->data->format = AF_FORMAT_FLOAT_NE; | |
314 af->data->bps = bps = 4; | |
315 } | |
316 | |
317 frames_stride = srate * s->ms_stride; | |
318 s->bytes_stride = frames_stride * bps * nch; | |
319 s->bytes_stride_scaled = s->scale * s->bytes_stride; | |
320 s->frames_stride_scaled = s->scale * frames_stride; | |
321 s->frames_stride_error = 0; | |
322 af->mul = (double)s->bytes_stride / s->bytes_stride_scaled; | |
323 | |
324 frames_overlap = frames_stride * s->percent_overlap; | |
325 if (frames_overlap <= 0) { | |
326 s->bytes_standing = s->bytes_stride; | |
327 s->samples_standing = s->bytes_standing / bps; | |
328 s->output_overlap = NULL; | |
329 } else { | |
330 s->samples_overlap = frames_overlap * nch; | |
331 s->bytes_overlap = frames_overlap * nch * bps; | |
332 s->bytes_standing = s->bytes_stride - s->bytes_overlap; | |
333 s->samples_standing = s->bytes_standing / bps; | |
334 s->buf_overlap = realloc(s->buf_overlap, s->bytes_overlap); | |
335 s->table_blend = realloc(s->table_blend, s->bytes_overlap * 4); | |
336 if(!s->buf_overlap || !s->table_blend) { | |
337 af_msg(AF_MSG_FATAL, "[scaletempo] Out of memory\n"); | |
338 return AF_ERROR; | |
339 } | |
340 bzero(s->buf_overlap, s->bytes_overlap); | |
341 if (use_int) { | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
342 int32_t* pb = s->table_blend; |
24896 | 343 int64_t blend = 0; |
344 for (i=0; i<frames_overlap; i++) { | |
345 int32_t v = blend / frames_overlap; | |
346 for (j=0; j<nch; j++) { | |
347 *pb++ = v; | |
348 } | |
349 blend += 65536; // 2^16 | |
350 } | |
24899 | 351 s->output_overlap = output_overlap_s16; |
24896 | 352 } else { |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
353 float* pb = s->table_blend; |
24896 | 354 for (i=0; i<frames_overlap; i++) { |
355 float v = i / (float)frames_overlap; | |
356 for (j=0; j<nch; j++) { | |
357 *pb++ = v; | |
358 } | |
359 } | |
24899 | 360 s->output_overlap = output_overlap_float; |
24896 | 361 } |
362 } | |
363 | |
364 s->frames_search = (frames_overlap > 1) ? srate * s->ms_search : 0; | |
365 if (s->frames_search <= 0) { | |
366 s->best_overlap_offset = NULL; | |
367 } else { | |
368 if (use_int) { | |
369 int64_t t = frames_overlap; | |
370 int32_t n = 8589934588LL / (t * t); // 4 * (2^31 - 1) / t^2 | |
371 int32_t* pw; | |
372 s->buf_pre_corr = realloc(s->buf_pre_corr, s->bytes_overlap * 2); | |
373 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
|
374 if(!s->buf_pre_corr || !s->table_window) { |
24896 | 375 af_msg(AF_MSG_FATAL, "[scaletempo] Out of memory\n"); |
376 return AF_ERROR; | |
377 } | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
378 pw = s->table_window; |
24896 | 379 for (i=1; i<frames_overlap; i++) { |
380 int32_t v = ( i * (t - i) * n ) >> 15; | |
381 for (j=0; j<nch; j++) { | |
382 *pw++ = v; | |
383 } | |
384 } | |
385 s->shift_corr = av_log2( 2*(s->samples_overlap - nch) - 1 ); | |
24899 | 386 s->best_overlap_offset = best_overlap_offset_s16; |
24896 | 387 } else { |
388 float* pw; | |
389 s->buf_pre_corr = realloc(s->buf_pre_corr, s->bytes_overlap); | |
390 s->table_window = realloc(s->table_window, s->bytes_overlap - nch * bps); | |
391 if(!s->buf_pre_corr || !s->table_window) { | |
392 af_msg(AF_MSG_FATAL, "[scaletempo] Out of memory\n"); | |
393 return AF_ERROR; | |
394 } | |
25069
d2f7027f0556
Avoid some casts by changing int8_t* to void* in af_scaletempo
reimar
parents:
24900
diff
changeset
|
395 pw = s->table_window; |
24896 | 396 for (i=1; i<frames_overlap; i++) { |
397 float v = i * (frames_overlap - i); | |
398 for (j=0; j<nch; j++) { | |
399 *pw++ = v; | |
400 } | |
401 } | |
24899 | 402 s->best_overlap_offset = best_overlap_offset_float; |
24896 | 403 } |
404 } | |
405 | |
406 s->bytes_per_frame = bps * nch; | |
407 s->num_channels = nch; | |
408 | |
409 s->bytes_queue | |
410 = (s->frames_search + frames_stride + frames_overlap) * bps * nch; | |
411 s->buf_queue = realloc(s->buf_queue, s->bytes_queue); | |
412 if(!s->buf_queue) { | |
413 af_msg(AF_MSG_FATAL, "[scaletempo] Out of memory\n"); | |
414 return AF_ERROR; | |
415 } | |
416 | |
417 af_msg (AF_MSG_DEBUG0, "[scaletempo] " | |
418 "%.2f stride_in, %i stride_out, %i standing, " | |
419 "%i overlap, %i search, %i queue, %s mode\n", | |
420 s->frames_stride_scaled, | |
421 (int)(s->bytes_stride / nch / bps), | |
422 (int)(s->bytes_standing / nch / bps), | |
423 (int)(s->bytes_overlap / nch / bps), | |
424 s->frames_search, | |
425 (int)(s->bytes_queue / nch / bps), | |
426 (use_int?"s16":"float")); | |
427 | |
428 return af_test_output(af, (af_data_t*)arg); | |
429 } | |
430 case AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET:{ | |
431 if (s->speed_tempo) { | |
432 if (s->speed_pitch) { | |
433 break; | |
434 } | |
435 s->speed = *(float*)arg; | |
436 s->scale = s->speed * s->scale_nominal; | |
437 } else { | |
438 if (s->speed_pitch) { | |
439 s->speed = 1 / *(float*)arg; | |
440 s->scale = s->speed * s->scale_nominal; | |
441 break; | |
442 } | |
443 } | |
444 return AF_OK; | |
445 } | |
446 case AF_CONTROL_SCALETEMPO_AMOUNT | AF_CONTROL_SET:{ | |
447 s->scale = *(float*)arg; | |
448 s->scale = s->speed * s->scale_nominal; | |
449 return AF_OK; | |
450 } | |
451 case AF_CONTROL_SCALETEMPO_AMOUNT | AF_CONTROL_GET: | |
452 *(float*)arg = s->scale; | |
453 return AF_OK; | |
454 case AF_CONTROL_COMMAND_LINE:{ | |
24898 | 455 strarg_t speed = {}; |
24896 | 456 opt_t subopts[] = { |
457 {"scale", OPT_ARG_FLOAT, &s->scale_nominal, NULL}, | |
458 {"stride", OPT_ARG_FLOAT, &s->ms_stride, NULL}, | |
459 {"overlap", OPT_ARG_FLOAT, &s->percent_overlap, NULL}, | |
460 {"search", OPT_ARG_FLOAT, &s->ms_search, NULL}, | |
461 {"speed", OPT_ARG_STR, &speed, NULL}, | |
462 {NULL}, | |
463 }; | |
464 if (subopt_parse(arg, subopts) != 0) { | |
465 return AF_ERROR; | |
466 } | |
467 if (s->scale_nominal <= 0) { | |
468 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
469 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
470 ": scale > 0\n"); | |
471 return AF_ERROR; | |
472 } | |
473 if (s->ms_stride <= 0) { | |
474 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
475 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
476 ": stride > 0\n"); | |
477 return AF_ERROR; | |
478 } | |
479 if (s->percent_overlap < 0 || s->percent_overlap > 1) { | |
480 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
481 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
482 ": 0 <= overlap <= 1\n"); | |
483 return AF_ERROR; | |
484 } | |
485 if (s->ms_search < 0) { | |
486 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
487 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
488 ": search >= 0\n"); | |
489 return AF_ERROR; | |
490 } | |
491 if (speed.len > 0) { | |
492 if (strcmp(speed.str, "pitch") == 0) { | |
493 s->speed_tempo = 0; | |
494 s->speed_pitch = 1; | |
495 } else if (strcmp(speed.str, "tempo") == 0) { | |
496 s->speed_tempo = 1; | |
497 s->speed_pitch = 0; | |
498 } else if (strcmp(speed.str, "none") == 0) { | |
499 s->speed_tempo = 0; | |
500 s->speed_pitch = 0; | |
501 } else if (strcmp(speed.str, "both") == 0) { | |
502 s->speed_tempo = 1; | |
503 s->speed_pitch = 1; | |
504 } else { | |
505 af_msg(AF_MSG_ERROR, "[scaletempo] " | |
506 MSGTR_ErrorParsingCommandLine ": " MSGTR_AF_ValueOutOfRange | |
507 ": speed=[pitch|tempo|none|both]\n"); | |
508 return AF_ERROR; | |
509 } | |
510 } | |
511 s->scale = s->speed * s->scale_nominal; | |
512 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"))); | |
513 return AF_OK; | |
514 } | |
515 } | |
516 return AF_UNKNOWN; | |
517 } | |
518 | |
519 // Deallocate memory | |
520 static void uninit(struct af_instance_s* af) | |
521 { | |
522 af_scaletempo_t* s = af->setup; | |
523 free(af->data->audio); | |
524 free(af->data); | |
525 free(s->buf_queue); | |
526 free(s->buf_overlap); | |
527 free(s->buf_pre_corr); | |
528 free(s->table_blend); | |
529 free(s->table_window); | |
530 free(af->setup); | |
531 } | |
532 | |
533 // Allocate memory and set function pointers | |
534 static int af_open(af_instance_t* af){ | |
535 af_scaletempo_t* s; | |
536 | |
537 af->control = control; | |
538 af->uninit = uninit; | |
539 af->play = play; | |
540 af->mul = 1; | |
541 af->data = calloc(1,sizeof(af_data_t)); | |
542 af->setup = calloc(1,sizeof(af_scaletempo_t)); | |
543 if(af->data == NULL || af->setup == NULL) | |
544 return AF_ERROR; | |
545 | |
546 s = af->setup; | |
547 s->scale = s->speed = s->scale_nominal = 1.0; | |
548 s->speed_tempo = 1; | |
549 s->speed_pitch = 0; | |
550 s->ms_stride = 60; | |
551 s->percent_overlap = .20; | |
552 s->ms_search = 14; | |
553 | |
554 return AF_OK; | |
555 } | |
556 | |
557 // Description of this filter | |
558 af_info_t af_info_scaletempo = { | |
559 "Scale audio tempo while maintaining pitch", | |
560 "scaletempo", | |
561 "Robert Juliano", | |
562 "", | |
563 AF_FLAGS_REENTRANT, | |
564 af_open | |
565 }; |