a057321f41043ef8eba70ec0fca585899bd401a1
[platform/upstream/gstreamer.git] / gst / audiofx / gstscaletempo.c
1 /*
2  * GStreamer
3  * Copyright (C) 2008 Rov Juvano <rovjuvano@users.sourceforge.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:element-scaletempo
23  *
24  * Scale tempo while maintaining pitch
25  * (WSOLA-like technique with cross correlation)
26  * Inspired by SoundTouch library by Olli Parviainen
27  *
28  * Use Sceletempo to apply playback rates without the chipmunk effect.
29  *
30  * <refsect2>
31  * <title>Example pipelines</title>
32  * <para>
33  * |[
34  * filesrc location=media.ext ! decodebin name=d \
35  *     d. ! queue ! audioconvert ! audioresample ! scaletempo ! audioconvert ! audioresample ! autoaudiosink \
36  *     d. ! queue ! ffmpegcolorspace ! autovideosink
37  * ]|
38  * OR
39  * |[
40  * playbin uri=... audio_sink="scaletempo ! audioconvert ! audioresample ! autoaudiosink"
41  * ]|
42  * When an application sends a seek event with rate != 1.0, Scaletempo applies
43  * the rate change by scaling the tempo without scaling the pitch.
44  *
45  * Scaletempo works by producing audio in constant sized chunks
46  * (#GstScaletempo:stride) but consuming chunks proportional to the playback
47  * rate.
48  *
49  * Scaletempo then smooths the output by blending the end of one stride with
50  * the next (#GstScaletempo:overlap).
51  *
52  * Scaletempo smooths the overlap further by searching within the input buffer
53  * for the best overlap position.  Scaletempo uses a statistical cross
54  * correlation (roughly a dot-product).  Scaletempo consumes most of its CPU
55  * cycles here. One can use the #GstScaletempo:search propery to tune how far
56  * the algoritm looks.
57  * </para>
58  * </refsect2>
59  */
60
61 /*
62  * Note: frame = audio key unit (i.e. one sample for each channel)
63  */
64
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68
69 #include <gst/gst.h>
70 #include <gst/base/gstbasetransform.h>
71 #include <string.h>             /* for memset */
72
73 #include "gstscaletempo.h"
74
75 GST_DEBUG_CATEGORY_STATIC (gst_scaletempo_debug);
76 #define GST_CAT_DEFAULT gst_scaletempo_debug
77
78 /* Filter signals and args */
79 enum
80 {
81   LAST_SIGNAL
82 };
83
84 enum
85 {
86   PROP_0,
87   PROP_RATE,
88   PROP_STRIDE,
89   PROP_OVERLAP,
90   PROP_SEARCH,
91 };
92
93 #define SUPPORTED_CAPS \
94 GST_STATIC_CAPS ( \
95     "audio/x-raw-float, " \
96       "rate = (int) [ 1, MAX ], "       \
97       "channels = (int) [ 1, MAX ], " \
98       "endianness = (int) BYTE_ORDER, " \
99       "width = (int) 32;" \
100     "audio/x-raw-int, " \
101       "rate = (int) [ 1, MAX ], " \
102       "channels = (int) [ 1, MAX ], " \
103       "endianness = (int) BYTE_ORDER, " \
104       "width = (int) 16, " \
105       "depth = (int) 16, " \
106       "signed = (boolean) true;" \
107 )
108
109 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
110     GST_PAD_SINK,
111     GST_PAD_ALWAYS,
112     SUPPORTED_CAPS);
113
114 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
115     GST_PAD_SRC,
116     GST_PAD_ALWAYS,
117     SUPPORTED_CAPS);
118
119 #define DEBUG_INIT(bla) GST_DEBUG_CATEGORY_INIT (gst_scaletempo_debug, "scaletempo", 0, "scaletempo element");
120
121 GST_BOILERPLATE_FULL (GstScaletempo, gst_scaletempo, GstBaseTransform,
122     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
123
124 typedef struct _GstScaletempoPrivate
125 {
126   gdouble scale;
127   /* parameters */
128   guint ms_stride;
129   gdouble percent_overlap;
130   guint ms_search;
131   /* caps */
132   gboolean use_int;
133   guint samples_per_frame;      /* AKA number of channels */
134   guint bytes_per_sample;
135   guint bytes_per_frame;
136   guint sample_rate;
137   /* stride */
138   gdouble frames_stride_scaled;
139   gdouble frames_stride_error;
140   guint bytes_stride;
141   gdouble bytes_stride_scaled;
142   guint bytes_queue_max;
143   guint bytes_queued;
144   guint bytes_to_slide;
145   gint8 *buf_queue;
146   /* overlap */
147   guint samples_overlap;
148   guint samples_standing;
149   guint bytes_overlap;
150   guint bytes_standing;
151   gpointer buf_overlap;
152   gpointer table_blend;
153   void (*output_overlap) (GstScaletempo * scaletempo, gpointer out_buf,
154       guint bytes_off);
155   /* best overlap */
156   guint frames_search;
157   gpointer buf_pre_corr;
158   gpointer table_window;
159     guint (*best_overlap_offset) (GstScaletempo * scaletempo);
160   /* gstreamer */
161   gint64 segment_start;
162   /* threads */
163   gboolean reinit_buffers;
164 } GstScaletempoPrivate;
165 #define GST_SCALETEMPO_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_SCALETEMPO, GstScaletempoPrivate))
166
167
168 static guint
169 best_overlap_offset_float (GstScaletempo * scaletempo)
170 {
171   GstScaletempoPrivate *p = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
172   gfloat *pw, *po, *ppc, *search_start;
173   gfloat best_corr = G_MININT;
174   guint best_off = 0;
175   gint i, off;
176
177   pw = p->table_window;
178   po = p->buf_overlap;
179   po += p->samples_per_frame;
180   ppc = p->buf_pre_corr;
181   for (i = p->samples_per_frame; i < p->samples_overlap; i++) {
182     *ppc++ = *pw++ * *po++;
183   }
184
185   search_start = (gfloat *) p->buf_queue + p->samples_per_frame;
186   for (off = 0; off < p->frames_search; off++) {
187     gfloat corr = 0;
188     gfloat *ps = search_start;
189     ppc = p->buf_pre_corr;
190     for (i = p->samples_per_frame; i < p->samples_overlap; i++) {
191       corr += *ppc++ * *ps++;
192     }
193     if (corr > best_corr) {
194       best_corr = corr;
195       best_off = off;
196     }
197     search_start += p->samples_per_frame;
198   }
199
200   return best_off * p->bytes_per_frame;
201 }
202
203 /* buffer padding for loop optimization: sizeof(gint32) * (loop_size - 1) */
204 #define UNROLL_PADDING (4*3)
205 static guint
206 best_overlap_offset_s16 (GstScaletempo * scaletempo)
207 {
208   GstScaletempoPrivate *p = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
209   gint32 *pw, *ppc;
210   gint16 *po, *search_start;
211   gint64 best_corr = G_MININT64;
212   guint best_off = 0;
213   guint off;
214   glong i;
215
216   pw = p->table_window;
217   po = p->buf_overlap;
218   po += p->samples_per_frame;
219   ppc = p->buf_pre_corr;
220   for (i = p->samples_per_frame; i < p->samples_overlap; i++) {
221     *ppc++ = (*pw++ * *po++) >> 15;
222   }
223
224   search_start = (gint16 *) p->buf_queue + p->samples_per_frame;
225   for (off = 0; off < p->frames_search; off++) {
226     gint64 corr = 0;
227     gint16 *ps = search_start;
228     ppc = p->buf_pre_corr;
229     ppc += p->samples_overlap - p->samples_per_frame;
230     ps += p->samples_overlap - p->samples_per_frame;
231     i = -((glong) p->samples_overlap - (glong) p->samples_per_frame);
232     do {
233       corr += ppc[i + 0] * ps[i + 0];
234       corr += ppc[i + 1] * ps[i + 1];
235       corr += ppc[i + 2] * ps[i + 2];
236       corr += ppc[i + 3] * ps[i + 3];
237       i += 4;
238     } while (i < 0);
239     if (corr > best_corr) {
240       best_corr = corr;
241       best_off = off;
242     }
243     search_start += p->samples_per_frame;
244   }
245
246   return best_off * p->bytes_per_frame;
247 }
248
249 static void
250 output_overlap_float (GstScaletempo * scaletempo,
251     gpointer buf_out, guint bytes_off)
252 {
253   GstScaletempoPrivate *p = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
254   gfloat *pout = buf_out;
255   gfloat *pb = p->table_blend;
256   gfloat *po = p->buf_overlap;
257   gfloat *pin = (gfloat *) (p->buf_queue + bytes_off);
258   gint i;
259   for (i = 0; i < p->samples_overlap; i++) {
260     *pout++ = *po - *pb++ * (*po - *pin++);
261     po++;
262   }
263 }
264
265 static void
266 output_overlap_s16 (GstScaletempo * scaletempo,
267     gpointer buf_out, guint bytes_off)
268 {
269   GstScaletempoPrivate *p = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
270   gint16 *pout = buf_out;
271   gint32 *pb = p->table_blend;
272   gint16 *po = p->buf_overlap;
273   gint16 *pin = (gint16 *) (p->buf_queue + bytes_off);
274   gint i;
275   for (i = 0; i < p->samples_overlap; i++) {
276     *pout++ = *po - ((*pb++ * (*po - *pin++)) >> 16);
277     po++;
278   }
279 }
280
281 static guint
282 fill_queue (GstScaletempo * scaletempo, GstBuffer * buf_in, guint offset)
283 {
284   GstScaletempoPrivate *p = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
285   guint bytes_in = GST_BUFFER_SIZE (buf_in) - offset;
286   guint offset_unchanged = offset;
287
288   if (p->bytes_to_slide > 0) {
289     if (p->bytes_to_slide < p->bytes_queued) {
290       guint bytes_in_move = p->bytes_queued - p->bytes_to_slide;
291       memmove (p->buf_queue, p->buf_queue + p->bytes_to_slide, bytes_in_move);
292       p->bytes_to_slide = 0;
293       p->bytes_queued = bytes_in_move;
294     } else {
295       guint bytes_in_skip;
296       p->bytes_to_slide -= p->bytes_queued;
297       bytes_in_skip = MIN (p->bytes_to_slide, bytes_in);
298       p->bytes_queued = 0;
299       p->bytes_to_slide -= bytes_in_skip;
300       offset += bytes_in_skip;
301       bytes_in -= bytes_in_skip;
302     }
303   }
304
305   if (bytes_in > 0) {
306     guint bytes_in_copy = MIN (p->bytes_queue_max - p->bytes_queued, bytes_in);
307     memcpy (p->buf_queue + p->bytes_queued,
308         GST_BUFFER_DATA (buf_in) + offset, bytes_in_copy);
309     p->bytes_queued += bytes_in_copy;
310     offset += bytes_in_copy;
311   }
312
313   return offset - offset_unchanged;
314 }
315
316 static void
317 reinit_buffers (GstScaletempo * scaletempo)
318 {
319   GstScaletempoPrivate *p = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
320   gint i, j;
321   guint frames_overlap;
322   guint new_size;
323
324   guint frames_stride = p->ms_stride * p->sample_rate / 1000.0;
325   p->bytes_stride = frames_stride * p->bytes_per_frame;
326
327   /* overlap */
328   frames_overlap = frames_stride * p->percent_overlap;
329   if (frames_overlap < 1) {     /* if no overlap */
330     p->bytes_overlap = 0;
331     p->bytes_standing = p->bytes_stride;
332     p->samples_standing = p->bytes_standing / p->bytes_per_sample;
333     p->output_overlap = NULL;
334   } else {
335     guint prev_overlap = p->bytes_overlap;
336     p->bytes_overlap = frames_overlap * p->bytes_per_frame;
337     p->samples_overlap = frames_overlap * p->samples_per_frame;
338     p->bytes_standing = p->bytes_stride - p->bytes_overlap;
339     p->samples_standing = p->bytes_standing / p->bytes_per_sample;
340     p->buf_overlap = g_realloc (p->buf_overlap, p->bytes_overlap);
341     p->table_blend = g_realloc (p->table_blend, p->samples_overlap * 4);        /* sizeof (gint32|gfloat) */
342     if (p->bytes_overlap > prev_overlap) {
343       memset ((guint8 *) p->buf_overlap + prev_overlap, 0,
344           p->bytes_overlap - prev_overlap);
345     }
346     if (p->use_int) {
347       gint32 *pb = p->table_blend;
348       gint64 blend = 0;
349       for (i = 0; i < frames_overlap; i++) {
350         gint32 v = blend / frames_overlap;
351         for (j = 0; j < p->samples_per_frame; j++) {
352           *pb++ = v;
353         }
354         blend += 65535;         /* 2^16 */
355       }
356       p->output_overlap = output_overlap_s16;
357     } else {
358       gfloat *pb = p->table_blend;
359       gfloat t = (gfloat) frames_overlap;
360       for (i = 0; i < frames_overlap; i++) {
361         gfloat v = i / t;
362         for (j = 0; j < p->samples_per_frame; j++) {
363           *pb++ = v;
364         }
365       }
366       p->output_overlap = output_overlap_float;
367     }
368   }
369
370   /* best overlap */
371   p->frames_search =
372       (frames_overlap <= 1) ? 0 : p->ms_search * p->sample_rate / 1000.0;
373   if (p->frames_search < 1) {   /* if no search */
374     p->best_overlap_offset = NULL;
375   } else {
376     guint bytes_pre_corr = (p->samples_overlap - p->samples_per_frame) * 4;     /* sizeof (gint32|gfloat) */
377     p->buf_pre_corr =
378         g_realloc (p->buf_pre_corr, bytes_pre_corr + UNROLL_PADDING);
379     p->table_window = g_realloc (p->table_window, bytes_pre_corr);
380     if (p->use_int) {
381       gint64 t = frames_overlap;
382       gint32 n = 8589934588LL / (t * t);        /* 4 * (2^31 - 1) / t^2 */
383       gint32 *pw;
384
385       memset ((guint8 *) p->buf_pre_corr + bytes_pre_corr, 0, UNROLL_PADDING);
386       pw = p->table_window;
387       for (i = 1; i < frames_overlap; i++) {
388         gint32 v = (i * (t - i) * n) >> 15;
389         for (j = 0; j < p->samples_per_frame; j++) {
390           *pw++ = v;
391         }
392       }
393       p->best_overlap_offset = best_overlap_offset_s16;
394     } else {
395       gfloat *pw = p->table_window;
396       for (i = 1; i < frames_overlap; i++) {
397         gfloat v = i * (frames_overlap - i);
398         for (j = 0; j < p->samples_per_frame; j++) {
399           *pw++ = v;
400         }
401       }
402       p->best_overlap_offset = best_overlap_offset_float;
403     }
404   }
405
406   new_size =
407       (p->frames_search + frames_stride + frames_overlap) * p->bytes_per_frame;
408   if (p->bytes_queued > new_size) {
409     if (p->bytes_to_slide > p->bytes_queued) {
410       p->bytes_to_slide -= p->bytes_queued;
411       p->bytes_queued = 0;
412     } else {
413       guint new_queued = MIN (p->bytes_queued - p->bytes_to_slide, new_size);
414       memmove (p->buf_queue,
415           p->buf_queue + p->bytes_queued - new_queued, new_queued);
416       p->bytes_to_slide = 0;
417       p->bytes_queued = new_queued;
418     }
419   }
420   p->bytes_queue_max = new_size;
421   p->buf_queue = g_realloc (p->buf_queue, p->bytes_queue_max);
422
423   p->bytes_stride_scaled = p->bytes_stride * p->scale;
424   p->frames_stride_scaled = p->bytes_stride_scaled / p->bytes_per_frame;
425
426   GST_DEBUG
427       ("%.3f scale, %.3f stride_in, %i stride_out, %i standing, %i overlap, %i search, %i queue, %s mode",
428       p->scale, p->frames_stride_scaled,
429       (gint) (p->bytes_stride / p->bytes_per_frame),
430       (gint) (p->bytes_standing / p->bytes_per_frame),
431       (gint) (p->bytes_overlap / p->bytes_per_frame), p->frames_search,
432       (gint) (p->bytes_queue_max / p->bytes_per_frame),
433       (p->use_int ? "s16" : "float"));
434
435   p->reinit_buffers = FALSE;
436 }
437
438
439 /* GstBaseTransform vmethod implementations */
440 static GstFlowReturn
441 gst_scaletempo_transform (GstBaseTransform * trans,
442     GstBuffer * inbuf, GstBuffer * outbuf)
443 {
444   GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
445   GstScaletempoPrivate *p = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
446
447   gint8 *pout = (gint8 *) GST_BUFFER_DATA (outbuf);
448   guint offset_in = fill_queue (scaletempo, inbuf, 0);
449   guint bytes_out = 0;
450   while (p->bytes_queued >= p->bytes_queue_max) {
451     guint bytes_off = 0;
452     gdouble frames_to_slide;
453     guint frames_to_stride_whole;
454
455     // output stride
456     if (p->output_overlap) {
457       if (p->best_overlap_offset) {
458         bytes_off = p->best_overlap_offset (scaletempo);
459       }
460       p->output_overlap (scaletempo, pout, bytes_off);
461     }
462     memcpy (pout + p->bytes_overlap,
463         p->buf_queue + bytes_off + p->bytes_overlap, p->bytes_standing);
464     pout += p->bytes_stride;
465     bytes_out += p->bytes_stride;
466
467     // input stride
468     memcpy (p->buf_overlap,
469         p->buf_queue + bytes_off + p->bytes_stride, p->bytes_overlap);
470     frames_to_slide = p->frames_stride_scaled + p->frames_stride_error;
471     frames_to_stride_whole = (gint) frames_to_slide;
472     p->bytes_to_slide = frames_to_stride_whole * p->bytes_per_frame;
473     p->frames_stride_error = frames_to_slide - frames_to_stride_whole;
474
475     offset_in += fill_queue (scaletempo, inbuf, offset_in);
476   }
477
478   GST_BUFFER_SIZE (outbuf) = bytes_out;
479   GST_BUFFER_TIMESTAMP (outbuf) =
480       (GST_BUFFER_TIMESTAMP (outbuf) - p->segment_start) / p->scale +
481       p->segment_start;
482   //GST_BUFFER_DURATION (outbuf)  = bytes_out * GST_SECOND / (p->bytes_per_frame * p->sample_rate);
483   return GST_FLOW_OK;
484 }
485
486 static gboolean
487 gst_scaletempo_transform_size (GstBaseTransform * trans,
488     GstPadDirection direction,
489     GstCaps * caps, guint size, GstCaps * othercaps, guint * othersize)
490 {
491   if (direction == GST_PAD_SINK) {
492     GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
493     GstScaletempoPrivate *priv = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
494     gint bytes_to_out;
495
496     if (priv->reinit_buffers)
497       reinit_buffers (scaletempo);
498
499     bytes_to_out = size + priv->bytes_queued - priv->bytes_to_slide;
500     if (bytes_to_out < (gint) priv->bytes_queue_max) {
501       *othersize = 0;
502     } else {
503       /* while (total_buffered - stride_length * n >= queue_max) n++ */
504       *othersize = priv->bytes_stride * ((guint) (
505               (bytes_to_out - priv->bytes_queue_max +
506                   /* rounding protection */ priv->bytes_per_frame)
507               / priv->bytes_stride_scaled) + 1);
508     }
509
510     return TRUE;
511   }
512   return FALSE;
513 }
514
515 static gboolean
516 gst_scaletempo_sink_event (GstBaseTransform * trans, GstEvent * event)
517 {
518   if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
519     GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
520     GstScaletempoPrivate *priv = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
521
522     gboolean update;
523     gdouble rate, applied_rate;
524     GstFormat format;
525     gint64 start, stop, position;
526
527     gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
528         &format, &start, &stop, &position);
529
530     if (priv->scale != rate) {
531       if (ABS (rate - 1.0) < 1e-10) {
532         priv->scale = 1.0;
533         gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (scaletempo),
534             TRUE);
535       } else {
536         gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (scaletempo),
537             FALSE);
538         priv->scale = rate;
539         priv->bytes_stride_scaled = priv->bytes_stride * priv->scale;
540         priv->frames_stride_scaled =
541             priv->bytes_stride_scaled / priv->bytes_per_frame;
542         GST_DEBUG ("%.3f scale, %.3f stride_in, %i stride_out", priv->scale,
543             priv->frames_stride_scaled,
544             (gint) (priv->bytes_stride / priv->bytes_per_frame));
545
546         priv->bytes_to_slide = 0;
547       }
548     }
549
550     if (priv->scale != 1.0) {
551       priv->segment_start = start;
552       applied_rate = priv->scale;
553       rate = 1.0;
554       //gst_event_unref (event);
555
556       if (stop != -1) {
557         stop = (stop - start) / applied_rate + start;
558       }
559
560       event = gst_event_new_new_segment_full (update, rate, applied_rate,
561           format, start, stop, position);
562       gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (trans), event);
563       return FALSE;
564     }
565   }
566   return parent_class->event (trans, event);
567 }
568
569 static gboolean
570 gst_scaletempo_set_caps (GstBaseTransform * trans,
571     GstCaps * incaps, GstCaps * outcaps)
572 {
573   GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
574   GstScaletempoPrivate *priv = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
575   GstStructure *s = gst_caps_get_structure (incaps, 0);
576
577   gint width, bps, nch, rate;
578   gboolean use_int;
579   const gchar *type = gst_structure_get_name (s);
580   if (g_str_equal (type, "audio/x-raw-int")) {
581     use_int = TRUE;
582     gst_structure_get_int (s, "depth", &width);
583   } else if (g_str_equal (type, "audio/x-raw-float")) {
584     use_int = FALSE;
585     gst_structure_get_int (s, "width", &width);
586   } else {
587     return FALSE;
588   }
589   bps = width / 8;
590
591   gst_structure_get_int (s, "channels", &nch);
592   gst_structure_get_int (s, "rate", &rate);
593
594   GST_DEBUG ("caps: %s seek, "
595       "%5" G_GUINT32_FORMAT " rate, "
596       "%2" G_GUINT32_FORMAT " nch, "
597       "%2" G_GUINT32_FORMAT " bps", type, rate, nch, bps);
598
599   if (rate != priv->sample_rate
600       || nch != priv->samples_per_frame
601       || bps != priv->bytes_per_sample || use_int != priv->use_int) {
602     priv->sample_rate = rate;
603     priv->samples_per_frame = nch;
604     priv->bytes_per_sample = bps;
605     priv->bytes_per_frame = nch * bps;
606     priv->use_int = use_int;
607     priv->reinit_buffers = TRUE;
608   }
609
610   return TRUE;
611 }
612
613
614 /* GObject vmethod implementations */
615 static void
616 gst_scaletempo_get_property (GObject * object,
617     guint prop_id, GValue * value, GParamSpec * pspec)
618 {
619   GstScaletempo *scaletempo = GST_SCALETEMPO (object);
620   GstScaletempoPrivate *priv = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
621
622   switch (prop_id) {
623     case PROP_RATE:
624       g_value_set_double (value, priv->scale);
625       break;
626     case PROP_STRIDE:
627       g_value_set_uint (value, priv->ms_stride);
628       break;
629     case PROP_OVERLAP:
630       g_value_set_double (value, priv->percent_overlap);
631       break;
632     case PROP_SEARCH:
633       g_value_set_uint (value, priv->ms_search);
634       break;
635     default:
636       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
637       break;
638   }
639 }
640
641 static void
642 gst_scaletempo_set_property (GObject * object,
643     guint prop_id, const GValue * value, GParamSpec * pspec)
644 {
645   GstScaletempo *scaletempo = GST_SCALETEMPO (object);
646   GstScaletempoPrivate *priv = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
647
648   switch (prop_id) {
649     case PROP_STRIDE:{
650       guint new_value = g_value_get_uint (value);
651       if (priv->ms_stride != new_value) {
652         priv->ms_stride = new_value;
653         priv->reinit_buffers = TRUE;
654       }
655       break;
656     }
657     case PROP_OVERLAP:{
658       gdouble new_value = g_value_get_double (value);
659       if (priv->percent_overlap != new_value) {
660         priv->percent_overlap = new_value;
661         priv->reinit_buffers = TRUE;
662       }
663       break;
664     }
665     case PROP_SEARCH:{
666       guint new_value = g_value_get_uint (value);
667       if (priv->ms_search != new_value) {
668         priv->ms_search = new_value;
669         priv->reinit_buffers = TRUE;
670       }
671       break;
672     }
673     default:
674       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
675       break;
676   }
677 }
678
679 static void
680 gst_scaletempo_base_init (gpointer klass)
681 {
682
683   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
684
685   gst_element_class_add_pad_template (element_class,
686       gst_static_pad_template_get (&src_template));
687   gst_element_class_add_pad_template (element_class,
688       gst_static_pad_template_get (&sink_template));
689   gst_element_class_set_details_simple (element_class, "Scaletempo",
690       "Filter/Effect/Rate",
691       "Sync audio tempo with playback rate",
692       "Rov Juvano <rovjuvano@users.sourceforge.net>");
693 }
694
695 static void
696 gst_scaletempo_class_init (GstScaletempoClass * klass)
697 {
698   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
699   GstBaseTransformClass *basetransform_class = GST_BASE_TRANSFORM_CLASS (klass);
700
701   g_type_class_add_private (klass, sizeof (GstScaletempoPrivate));
702
703   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_scaletempo_get_property);
704   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_scaletempo_set_property);
705
706   g_object_class_install_property (gobject_class, PROP_RATE,
707       g_param_spec_double ("rate", "Playback Rate", "Current playback rate",
708           G_MININT, G_MAXINT, 1.0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
709
710   g_object_class_install_property (gobject_class, PROP_STRIDE,
711       g_param_spec_uint ("stride", "Stride Length",
712           "Length in milliseconds to output each stride", 1, 5000, 30,
713           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
714
715   g_object_class_install_property (gobject_class, PROP_OVERLAP,
716       g_param_spec_double ("overlap", "Overlap Length",
717           "Percentage of stride to overlap", 0, 1, .2,
718           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
719
720   g_object_class_install_property (gobject_class, PROP_SEARCH,
721       g_param_spec_uint ("search", "Search Length",
722           "Length in milliseconds to search for best overlap position", 0, 500,
723           14, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
724
725   basetransform_class->event = GST_DEBUG_FUNCPTR (gst_scaletempo_sink_event);
726   basetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_scaletempo_set_caps);
727   basetransform_class->transform_size =
728       GST_DEBUG_FUNCPTR (gst_scaletempo_transform_size);
729   basetransform_class->transform = GST_DEBUG_FUNCPTR (gst_scaletempo_transform);
730 }
731
732 static void
733 gst_scaletempo_init (GstScaletempo * scaletempo, GstScaletempoClass * klass)
734 {
735   GstScaletempoPrivate *priv = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
736   /* defaults */
737   priv->ms_stride = 30;
738   priv->percent_overlap = .2;
739   priv->ms_search = 14;
740
741   /* uninitialized */
742   priv->scale = 0;
743   priv->sample_rate = 0;
744   priv->frames_stride_error = 0;
745   priv->bytes_stride = 0;
746   priv->bytes_queued = 0;
747   priv->bytes_to_slide = 0;
748   priv->segment_start = 0;
749 }