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