1 /* GStreamer pitch controller element
2 * Copyright (C) 2006 Wouter Paesen <wouter@blue-gate.be>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 /* FIXME: workaround for SoundTouch.h of version 1.3.1 defining those
25 * variables while it shouldn't. */
27 #undef PACKAGE_VERSION
28 #undef PACKAGE_TARNAME
31 #undef PACKAGE_BUGREPORT
34 #include <soundtouch/SoundTouch.h>
37 #include <gst/audio/audio.h>
39 #include "gstpitch.hh"
42 GST_DEBUG_CATEGORY_STATIC (pitch_debug);
43 #define GST_CAT_DEFAULT pitch_debug
45 #define GST_PITCH_GET_PRIVATE(o) (o->priv)
46 struct _GstPitchPrivate
48 gfloat stream_time_ratio;
50 GstEvent *pending_segment;
52 soundtouch::SoundTouch * st;
64 /* For soundtouch 1.4 */
65 #if defined(INTEGER_SAMPLES)
66 #define SOUNDTOUCH_INTEGER_SAMPLES 1
67 #elif defined(FLOAT_SAMPLES)
68 #define SOUNDTOUCH_FLOAT_SAMPLES 1
71 #if defined(SOUNDTOUCH_FLOAT_SAMPLES)
72 #define SUPPORTED_CAPS \
74 "format = (string) " GST_AUDIO_NE (F32) ", " \
75 "rate = (int) [ 8000, MAX ], " \
76 "channels = (int) [ 1, MAX ]"
77 #elif defined(SOUNDTOUCH_INTEGER_SAMPLES)
78 #define SUPPORTED_CAPS \
80 "format = (string) " GST_AUDIO_NE (S16) ", " \
81 "rate = (int) [ 8000, MAX ], " \
82 "channels = (int) [ 1, MAX ]"
84 #error "Only integer or float samples are supported"
87 static GstStaticPadTemplate gst_pitch_sink_template =
88 GST_STATIC_PAD_TEMPLATE ("sink",
91 GST_STATIC_CAPS (SUPPORTED_CAPS));
93 static GstStaticPadTemplate gst_pitch_src_template =
94 GST_STATIC_PAD_TEMPLATE ("src",
97 GST_STATIC_CAPS (SUPPORTED_CAPS));
99 static void gst_pitch_dispose (GObject * object);
100 static void gst_pitch_set_property (GObject * object,
101 guint prop_id, const GValue * value, GParamSpec * pspec);
102 static void gst_pitch_get_property (GObject * object,
103 guint prop_id, GValue * value, GParamSpec * pspec);
106 static gboolean gst_pitch_setcaps (GstPitch * pitch, GstCaps * caps);
107 static GstFlowReturn gst_pitch_chain (GstPad * pad, GstObject * parent,
109 static GstStateChangeReturn gst_pitch_change_state (GstElement * element,
110 GstStateChange transition);
111 static gboolean gst_pitch_sink_event (GstPad * pad, GstObject * parent,
113 static gboolean gst_pitch_src_event (GstPad * pad, GstObject * parent,
116 static gboolean gst_pitch_src_query (GstPad * pad, GstObject * parent,
119 #define gst_pitch_parent_class parent_class
120 G_DEFINE_TYPE_WITH_PRIVATE (GstPitch, gst_pitch, GST_TYPE_ELEMENT);
121 GST_ELEMENT_REGISTER_DEFINE (pitch, "pitch", GST_RANK_NONE,
125 gst_pitch_class_init (GstPitchClass * klass)
127 GObjectClass *gobject_class;
128 GstElementClass *element_class;
130 gobject_class = G_OBJECT_CLASS (klass);
131 element_class = GST_ELEMENT_CLASS (klass);
133 GST_DEBUG_CATEGORY_INIT (pitch_debug, "pitch", 0,
134 "audio pitch control element");
136 gobject_class->set_property = gst_pitch_set_property;
137 gobject_class->get_property = gst_pitch_get_property;
138 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pitch_dispose);
140 g_object_class_install_property (gobject_class, ARG_PITCH,
141 g_param_spec_float ("pitch", "Pitch",
142 "Audio stream pitch", 0.1, 10.0, 1.0,
143 (GParamFlags) (G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE |
144 G_PARAM_STATIC_STRINGS)));
146 g_object_class_install_property (gobject_class, ARG_TEMPO,
147 g_param_spec_float ("tempo", "Tempo",
148 "Audio stream tempo", 0.1, 10.0, 1.0,
149 (GParamFlags) (G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE |
150 G_PARAM_STATIC_STRINGS)));
152 g_object_class_install_property (gobject_class, ARG_RATE,
153 g_param_spec_float ("rate", "Rate",
154 "Audio stream rate", 0.1, 10.0, 1.0,
155 (GParamFlags) (G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE |
156 G_PARAM_STATIC_STRINGS)));
158 g_object_class_install_property (gobject_class, ARG_OUT_RATE,
159 g_param_spec_float ("output-rate", "Output Rate",
160 "Output rate on downstream segment events", 0.1, 10.0, 1.0,
161 (GParamFlags) (G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE |
162 G_PARAM_STATIC_STRINGS)));
164 element_class->change_state = GST_DEBUG_FUNCPTR (gst_pitch_change_state);
166 gst_element_class_add_static_pad_template (element_class, &gst_pitch_src_template);
167 gst_element_class_add_static_pad_template (element_class, &gst_pitch_sink_template);
169 gst_element_class_set_static_metadata (element_class, "Pitch controller",
170 "Filter/Effect/Audio", "Control the pitch of an audio stream",
171 "Wouter Paesen <wouter@blue-gate.be>");
175 gst_pitch_init (GstPitch * pitch)
177 pitch->priv = (GstPitchPrivate *) gst_pitch_get_instance_private (pitch);
180 gst_pad_new_from_static_template (&gst_pitch_sink_template, "sink");
181 gst_pad_set_chain_function (pitch->sinkpad,
182 GST_DEBUG_FUNCPTR (gst_pitch_chain));
183 gst_pad_set_event_function (pitch->sinkpad,
184 GST_DEBUG_FUNCPTR (gst_pitch_sink_event));
185 GST_PAD_SET_PROXY_CAPS (pitch->sinkpad);
186 gst_element_add_pad (GST_ELEMENT (pitch), pitch->sinkpad);
189 gst_pad_new_from_static_template (&gst_pitch_src_template, "src");
190 gst_pad_set_event_function (pitch->srcpad,
191 GST_DEBUG_FUNCPTR (gst_pitch_src_event));
192 gst_pad_set_query_function (pitch->srcpad,
193 GST_DEBUG_FUNCPTR (gst_pitch_src_query));
194 GST_PAD_SET_PROXY_CAPS (pitch->sinkpad);
195 gst_element_add_pad (GST_ELEMENT (pitch), pitch->srcpad);
197 pitch->priv->st = new soundtouch::SoundTouch ();
201 pitch->out_seg_rate = 1.0;
202 pitch->seg_arate = 1.0;
204 pitch->next_buffer_time = GST_CLOCK_TIME_NONE;
205 pitch->next_buffer_offset = 0;
207 pitch->priv->st->setRate (pitch->rate);
208 pitch->priv->st->setTempo (pitch->tempo * pitch->seg_arate);
209 pitch->priv->st->setPitch (pitch->pitch);
211 pitch->priv->stream_time_ratio = 1.0;
212 pitch->min_latency = pitch->max_latency = 0;
217 gst_pitch_dispose (GObject * object)
219 GstPitch *pitch = GST_PITCH (object);
221 if (pitch->priv->st) {
222 delete pitch->priv->st;
224 pitch->priv->st = NULL;
227 G_OBJECT_CLASS (parent_class)->dispose (object);
231 gst_pitch_update_duration (GstPitch * pitch)
235 m = gst_message_new_duration_changed (GST_OBJECT (pitch));
236 gst_element_post_message (GST_ELEMENT (pitch), m);
240 gst_pitch_set_property (GObject * object, guint prop_id,
241 const GValue * value, GParamSpec * pspec)
243 GstPitch *pitch = GST_PITCH (object);
245 GST_OBJECT_LOCK (pitch);
248 pitch->tempo = g_value_get_float (value);
249 pitch->priv->stream_time_ratio =
250 pitch->tempo * pitch->rate * pitch->seg_arate;
251 pitch->priv->st->setTempo (pitch->tempo * pitch->seg_arate);
252 GST_OBJECT_UNLOCK (pitch);
253 gst_pitch_update_duration (pitch);
256 pitch->rate = g_value_get_float (value);
257 pitch->priv->stream_time_ratio =
258 pitch->tempo * pitch->rate * pitch->seg_arate;
259 pitch->priv->st->setRate (pitch->rate);
260 GST_OBJECT_UNLOCK (pitch);
261 gst_pitch_update_duration (pitch);
264 /* Has no effect until the next input segment */
265 pitch->out_seg_rate = g_value_get_float (value);
266 GST_OBJECT_UNLOCK (pitch);
269 pitch->pitch = g_value_get_float (value);
270 pitch->priv->st->setPitch (pitch->pitch);
271 GST_OBJECT_UNLOCK (pitch);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275 GST_OBJECT_UNLOCK (pitch);
281 gst_pitch_get_property (GObject * object, guint prop_id,
282 GValue * value, GParamSpec * pspec)
284 GstPitch *pitch = GST_PITCH (object);
286 GST_OBJECT_LOCK (pitch);
289 g_value_set_float (value, pitch->tempo);
292 g_value_set_float (value, pitch->rate);
295 g_value_set_float (value, pitch->out_seg_rate);
298 g_value_set_float (value, pitch->pitch);
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304 GST_OBJECT_UNLOCK (pitch);
308 gst_pitch_setcaps (GstPitch * pitch, GstCaps * caps)
310 GstPitchPrivate *priv;
312 priv = GST_PITCH_GET_PRIVATE (pitch);
314 if (!gst_audio_info_from_caps (&pitch->info, caps))
317 GST_OBJECT_LOCK (pitch);
319 /* notify the soundtouch instance of this change */
320 priv->st->setSampleRate (pitch->info.rate);
321 priv->st->setChannels (pitch->info.channels);
323 GST_OBJECT_UNLOCK (pitch);
328 /* send a buffer out */
330 gst_pitch_forward_buffer (GstPitch * pitch, GstBuffer * buffer)
334 GST_BUFFER_TIMESTAMP (buffer) = pitch->next_buffer_time;
335 pitch->next_buffer_time += GST_BUFFER_DURATION (buffer);
337 samples = GST_BUFFER_OFFSET (buffer);
338 GST_BUFFER_OFFSET (buffer) = pitch->next_buffer_offset;
339 pitch->next_buffer_offset += samples;
340 GST_BUFFER_OFFSET_END (buffer) = pitch->next_buffer_offset;
342 GST_LOG ("pushing buffer [%" GST_TIME_FORMAT "]-[%" GST_TIME_FORMAT
343 "] (%d samples)", GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
344 GST_TIME_ARGS (pitch->next_buffer_time), samples);
346 return gst_pad_push (pitch->srcpad, buffer);
349 /* extract a buffer from soundtouch */
351 gst_pitch_prepare_buffer (GstPitch * pitch)
353 GstPitchPrivate *priv;
358 priv = GST_PITCH_GET_PRIVATE (pitch);
360 GST_LOG_OBJECT (pitch, "preparing buffer");
362 samples = pitch->priv->st->numSamples ();
366 buffer = gst_buffer_new_and_alloc (samples * pitch->info.bpf);
368 gst_buffer_map (buffer, &info, (GstMapFlags) GST_MAP_READWRITE);
369 samples = priv->st->receiveSamples ((soundtouch::SAMPLETYPE *) info.data, samples);
370 gst_buffer_unmap (buffer, &info);
373 gst_buffer_unref (buffer);
377 GST_BUFFER_DURATION (buffer) =
378 gst_util_uint64_scale (samples, GST_SECOND, pitch->info.rate);
379 /* temporary store samples here, to avoid having to recalculate this */
380 GST_BUFFER_OFFSET (buffer) = (gint64) samples;
385 /* process the last samples, in a later stage we should make sure no more
386 * samples are sent out here as strictly necessary, because soundtouch could
387 * append zero samples, which could disturb looping. */
389 gst_pitch_flush_buffer (GstPitch * pitch, gboolean send)
393 if (pitch->priv->st->numUnprocessedSamples() != 0) {
394 GST_DEBUG_OBJECT (pitch, "flushing buffer");
395 pitch->priv->st->flush ();
401 buffer = gst_pitch_prepare_buffer (pitch);
406 return gst_pitch_forward_buffer (pitch, buffer);
410 gst_pitch_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
415 pitch = GST_PITCH (parent);
417 GST_DEBUG_OBJECT (pad, "received %s event", GST_EVENT_TYPE_NAME (event));
419 switch (GST_EVENT_TYPE (event)) {
420 case GST_EVENT_SEEK:{
421 /* transform the event upstream, according to the playback rate */
425 GstSeekType cur_type, stop_type;
427 gfloat stream_time_ratio;
430 GST_OBJECT_LOCK (pitch);
431 stream_time_ratio = pitch->priv->stream_time_ratio;
432 GST_OBJECT_UNLOCK (pitch);
434 gst_event_parse_seek (event, &rate, &format, &flags,
435 &cur_type, &cur, &stop_type, &stop);
437 seqnum = gst_event_get_seqnum (event);
439 gst_event_unref (event);
441 if (format == GST_FORMAT_TIME || format == GST_FORMAT_DEFAULT) {
442 cur = (gint64) (cur * stream_time_ratio);
444 stop = (gint64) (stop * stream_time_ratio);
446 event = gst_event_new_seek (rate, format, flags,
447 cur_type, cur, stop_type, stop);
448 gst_event_set_seqnum (event, seqnum);
449 res = gst_pad_event_default (pad, parent, event);
451 GST_WARNING_OBJECT (pitch,
452 "Seeking only supported in TIME or DEFAULT format");
458 res = gst_pad_event_default (pad, parent, event);
464 /* generic convert function based on caps, no rate
468 gst_pitch_convert (GstPitch * pitch,
469 GstFormat src_format, gint64 src_value,
470 GstFormat * dst_format, gint64 * dst_value)
476 g_return_val_if_fail (dst_format && dst_value, FALSE);
478 GST_OBJECT_LOCK (pitch);
479 sample_size = pitch->info.bpf;
480 samplerate = pitch->info.rate;
481 GST_OBJECT_UNLOCK (pitch);
483 if (sample_size == 0 || samplerate == 0) {
487 if (src_format == *dst_format || src_value == -1) {
488 *dst_value = src_value;
492 switch (src_format) {
493 case GST_FORMAT_BYTES:
494 switch (*dst_format) {
495 case GST_FORMAT_TIME:
497 gst_util_uint64_scale_int (src_value, GST_SECOND,
498 sample_size * samplerate);
500 case GST_FORMAT_DEFAULT:
501 *dst_value = gst_util_uint64_scale_int (src_value, 1, sample_size);
508 case GST_FORMAT_TIME:
509 switch (*dst_format) {
510 case GST_FORMAT_BYTES:
512 gst_util_uint64_scale_int (src_value, samplerate * sample_size,
515 case GST_FORMAT_DEFAULT:
517 gst_util_uint64_scale_int (src_value, samplerate, GST_SECOND);
524 case GST_FORMAT_DEFAULT:
525 switch (*dst_format) {
526 case GST_FORMAT_BYTES:
527 *dst_value = gst_util_uint64_scale_int (src_value, sample_size, 1);
529 case GST_FORMAT_TIME:
531 gst_util_uint64_scale_int (src_value, GST_SECOND, samplerate);
547 gst_pitch_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
550 gboolean res = FALSE;
551 gfloat stream_time_ratio;
552 gint64 next_buffer_offset;
553 GstClockTime next_buffer_time;
555 pitch = GST_PITCH (parent);
557 GST_LOG ("%s query", GST_QUERY_TYPE_NAME (query));
559 GST_OBJECT_LOCK (pitch);
560 stream_time_ratio = pitch->priv->stream_time_ratio;
561 next_buffer_time = pitch->next_buffer_time;
562 next_buffer_offset = pitch->next_buffer_offset;
563 GST_OBJECT_UNLOCK (pitch);
565 switch (GST_QUERY_TYPE (query)) {
566 case GST_QUERY_DURATION:{
570 if (!gst_pad_query_default (pad, parent, query)) {
571 GST_DEBUG_OBJECT (pitch, "upstream provided no duration");
575 gst_query_parse_duration (query, &format, &duration);
577 if (format != GST_FORMAT_TIME && format != GST_FORMAT_DEFAULT) {
578 GST_DEBUG_OBJECT (pitch, "not TIME or DEFAULT format");
581 GST_LOG_OBJECT (pitch, "upstream duration: %" G_GINT64_FORMAT, duration);
582 duration = (gint64) (duration / stream_time_ratio);
583 GST_LOG_OBJECT (pitch, "our duration: %" G_GINT64_FORMAT, duration);
584 gst_query_set_duration (query, format, duration);
588 case GST_QUERY_POSITION:{
589 GstFormat dst_format;
592 gst_query_parse_position (query, &dst_format, &dst_value);
594 if (dst_format != GST_FORMAT_TIME && dst_format != GST_FORMAT_DEFAULT) {
595 GST_DEBUG_OBJECT (pitch, "not TIME or DEFAULT format");
599 if (dst_format == GST_FORMAT_TIME) {
600 dst_value = next_buffer_time;
603 dst_value = next_buffer_offset;
608 GST_LOG_OBJECT (pitch, "our position: %" G_GINT64_FORMAT, dst_value);
609 gst_query_set_position (query, dst_format, dst_value);
613 case GST_QUERY_CONVERT:{
614 GstFormat src_format, dst_format;
615 gint64 src_value, dst_value;
617 gst_query_parse_convert (query, &src_format, &src_value,
620 res = gst_pitch_convert (pitch, src_format, src_value,
621 &dst_format, &dst_value);
624 gst_query_set_convert (query, src_format, src_value,
625 dst_format, dst_value);
629 case GST_QUERY_LATENCY:
631 GstClockTime min, max;
635 if ((peer = gst_pad_get_peer (pitch->sinkpad))) {
636 if ((res = gst_pad_query (peer, query))) {
637 gst_query_parse_latency (query, &live, &min, &max);
639 GST_DEBUG ("Peer latency: min %"
640 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
641 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
643 /* add our own latency */
645 GST_DEBUG ("Our latency: min %" GST_TIME_FORMAT
646 ", max %" GST_TIME_FORMAT,
647 GST_TIME_ARGS (pitch->min_latency),
648 GST_TIME_ARGS (pitch->max_latency));
650 min += pitch->min_latency;
651 if (max != GST_CLOCK_TIME_NONE)
652 max += pitch->max_latency;
654 GST_DEBUG ("Calculated total latency : min %"
655 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
656 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
657 gst_query_set_latency (query, live, min, max);
659 gst_object_unref (peer);
664 res = gst_pad_query_default (pad, parent, query);
671 /* this function returns FALSE if not enough data is known to transform the
672 * segment into proper downstream values. If the function does return false
673 * the segment should be stalled until enough information is available.
674 * If the function returns TRUE, event will be replaced by the new downstream
678 gst_pitch_process_segment (GstPitch * pitch, GstEvent ** event)
681 gdouble out_seg_rate, our_arate;
682 gfloat stream_time_ratio;
685 g_return_val_if_fail (event, FALSE);
687 GST_OBJECT_LOCK (pitch);
688 stream_time_ratio = pitch->priv->stream_time_ratio;
689 out_seg_rate = pitch->out_seg_rate;
690 GST_OBJECT_UNLOCK (pitch);
692 gst_event_copy_segment (*event, &seg);
694 if (seg.format != GST_FORMAT_TIME && seg.format != GST_FORMAT_DEFAULT) {
695 GST_WARNING_OBJECT (pitch,
696 "Only NEWSEGMENT in TIME or DEFAULT format supported, sending"
697 "open ended NEWSEGMENT in TIME format.");
698 seg.format = GST_FORMAT_TIME;
704 /* Figure out how much of the incoming 'rate' we'll apply ourselves */
705 our_arate = seg.rate / out_seg_rate;
706 /* update the output rate variables */
707 seg.rate = out_seg_rate;
708 seg.applied_rate *= our_arate;
710 GST_LOG_OBJECT (pitch->sinkpad, "in segment %" GST_SEGMENT_FORMAT, &seg);
712 stream_time_ratio = pitch->tempo * pitch->rate * pitch->seg_arate;
714 if (stream_time_ratio == 0) {
715 GST_LOG_OBJECT (pitch->sinkpad, "stream_time_ratio is zero");
719 /* Update the playback rate */
720 GST_OBJECT_LOCK (pitch);
721 pitch->seg_arate = our_arate;
722 pitch->priv->stream_time_ratio = stream_time_ratio;
723 pitch->priv->st->setTempo (pitch->tempo * pitch->seg_arate);
724 GST_OBJECT_UNLOCK (pitch);
726 seg.start = (gint64) (seg.start / stream_time_ratio);
727 seg.position = (gint64) (seg.position / stream_time_ratio);
728 if (seg.stop != (guint64) - 1)
729 seg.stop = (gint64) (seg.stop / stream_time_ratio);
730 seg.time = (gint64) (seg.time / stream_time_ratio);
732 GST_LOG_OBJECT (pitch->sinkpad, "out segment %" GST_SEGMENT_FORMAT, &seg);
734 seqnum = gst_event_get_seqnum (*event);
735 gst_event_unref (*event);
736 *event = gst_event_new_segment (&seg);
737 gst_event_set_seqnum (*event, seqnum);
743 gst_pitch_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
748 pitch = GST_PITCH (parent);
750 GST_LOG_OBJECT (pad, "received %s event", GST_EVENT_TYPE_NAME (event));
752 switch (GST_EVENT_TYPE (event)) {
753 case GST_EVENT_FLUSH_STOP:
754 gst_pitch_flush_buffer (pitch, FALSE);
755 pitch->priv->st->clear ();
756 pitch->next_buffer_offset = 0;
757 pitch->next_buffer_time = GST_CLOCK_TIME_NONE;
758 pitch->min_latency = pitch->max_latency = 0;
761 gst_pitch_flush_buffer (pitch, TRUE);
762 pitch->priv->st->clear ();
763 pitch->min_latency = pitch->max_latency = 0;
765 case GST_EVENT_SEGMENT:
766 if (!gst_pitch_process_segment (pitch, &event)) {
767 GST_LOG_OBJECT (pad, "not enough data known, stalling segment");
768 if (GST_PITCH_GET_PRIVATE (pitch)->pending_segment)
769 gst_event_unref (GST_PITCH_GET_PRIVATE (pitch)->pending_segment);
770 GST_PITCH_GET_PRIVATE (pitch)->pending_segment = event;
773 pitch->priv->st->clear ();
774 pitch->min_latency = pitch->max_latency = 0;
780 gst_event_parse_caps (event, &caps);
781 res = gst_pitch_setcaps (pitch, caps);
783 gst_event_unref (event);
793 res = gst_pad_event_default (pad, parent, event);
800 gst_pitch_update_latency (GstPitch * pitch, GstClockTime timestamp)
802 GstClockTimeDiff current_latency, min_latency, max_latency;
805 (GstClockTimeDiff) (timestamp / pitch->priv->stream_time_ratio) -
806 pitch->next_buffer_time;
808 min_latency = MIN (pitch->min_latency, current_latency);
809 max_latency = MAX (pitch->max_latency, current_latency);
811 if (pitch->min_latency != min_latency || pitch->max_latency != max_latency) {
812 pitch->min_latency = min_latency;
813 pitch->max_latency = max_latency;
815 /* FIXME: what about the LATENCY event? It only has
816 * one latency value, should it be current, min or max?
817 * Should it include upstream latencies?
820 gst_element_post_message (GST_ELEMENT (pitch),
821 gst_message_new_latency (GST_OBJECT (pitch)));
826 gst_pitch_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
829 GstPitchPrivate *priv;
830 GstClockTime timestamp;
833 pitch = GST_PITCH (parent);
834 priv = GST_PITCH_GET_PRIVATE (pitch);
836 timestamp = GST_BUFFER_TIMESTAMP (buffer);
838 // Remember the first time and corresponding offset
839 if (!GST_CLOCK_TIME_IS_VALID (pitch->next_buffer_time)) {
840 gfloat stream_time_ratio;
841 GstFormat out_format = GST_FORMAT_DEFAULT;
843 GST_OBJECT_LOCK (pitch);
844 stream_time_ratio = priv->stream_time_ratio;
845 GST_OBJECT_UNLOCK (pitch);
847 pitch->next_buffer_time = timestamp / stream_time_ratio;
848 gst_pitch_convert (pitch, GST_FORMAT_TIME, timestamp, &out_format,
849 &pitch->next_buffer_offset);
852 gst_object_sync_values (GST_OBJECT (pitch), pitch->next_buffer_time);
854 /* push the received samples on the soundtouch buffer */
855 GST_LOG_OBJECT (pitch, "incoming buffer (%d samples) %" GST_TIME_FORMAT,
856 (gint) (gst_buffer_get_size (buffer) / pitch->info.bpf),
857 GST_TIME_ARGS (timestamp));
859 if (GST_PITCH_GET_PRIVATE (pitch)->pending_segment) {
861 gst_event_copy (GST_PITCH_GET_PRIVATE (pitch)->pending_segment);
863 GST_LOG_OBJECT (pitch, "processing stalled segment");
864 if (!gst_pitch_process_segment (pitch, &event)) {
865 gst_buffer_unref (buffer);
866 gst_event_unref (event);
867 return GST_FLOW_ERROR;
870 if (!gst_pad_event_default (pitch->sinkpad, parent, event)) {
871 gst_buffer_unref (buffer);
872 gst_event_unref (event);
873 return GST_FLOW_ERROR;
876 gst_event_unref (GST_PITCH_GET_PRIVATE (pitch)->pending_segment);
877 GST_PITCH_GET_PRIVATE (pitch)->pending_segment = NULL;
880 gst_buffer_map (buffer, &info, GST_MAP_READ);
881 GST_OBJECT_LOCK (pitch);
882 priv->st->putSamples ((soundtouch::SAMPLETYPE *) info.data, info.size / pitch->info.bpf);
883 GST_OBJECT_UNLOCK (pitch);
884 gst_buffer_unmap (buffer, &info);
885 gst_buffer_unref (buffer);
887 /* Calculate latency */
889 gst_pitch_update_latency (pitch, timestamp);
890 /* and try to extract some samples from the soundtouch buffer */
891 if (!priv->st->isEmpty ()) {
892 GstBuffer *out_buffer;
894 out_buffer = gst_pitch_prepare_buffer (pitch);
897 #ifdef TIZEN_FEATURE_PITCH_AUDIO_META
898 gint samples = GST_BUFFER_OFFSET (out_buffer);
900 if (GST_AUDIO_INFO_LAYOUT (&pitch->info) ==
901 GST_AUDIO_LAYOUT_NON_INTERLEAVED) {
902 gst_buffer_add_audio_meta (out_buffer, &pitch->info, samples, NULL);
905 return gst_pitch_forward_buffer (pitch, out_buffer);
912 static GstStateChangeReturn
913 gst_pitch_change_state (GstElement * element, GstStateChange transition)
915 GstStateChangeReturn ret;
916 GstPitch *pitch = GST_PITCH (element);
918 switch (transition) {
919 case GST_STATE_CHANGE_NULL_TO_READY:
921 case GST_STATE_CHANGE_READY_TO_PAUSED:
922 pitch->next_buffer_time = GST_CLOCK_TIME_NONE;
923 pitch->next_buffer_offset = 0;
924 pitch->priv->st->clear ();
925 pitch->min_latency = pitch->max_latency = 0;
927 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
933 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
934 if (ret != GST_STATE_CHANGE_SUCCESS)
937 switch (transition) {
938 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
940 case GST_STATE_CHANGE_PAUSED_TO_READY:
941 if (GST_PITCH_GET_PRIVATE (pitch)->pending_segment) {
942 gst_event_unref (GST_PITCH_GET_PRIVATE (pitch)->pending_segment);
943 GST_PITCH_GET_PRIVATE (pitch)->pending_segment = NULL;
946 case GST_STATE_CHANGE_READY_TO_NULL: