2006-03-22 14:31:47 +00:00
|
|
|
/* GStreamer pitch controller element
|
|
|
|
* Copyright (C) 2006 Wouter Paesen <wouter@blue-gate.be>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-01-11 19:41:57 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2008-01-26 13:24:17 +00:00
|
|
|
/* FIXME: workaround for SoundTouch.h of version 1.3.1 defining those
|
|
|
|
* variables while it shouldn't. */
|
|
|
|
#undef VERSION
|
|
|
|
#undef PACKAGE_VERSION
|
|
|
|
#undef PACKAGE_TARNAME
|
|
|
|
#undef PACKAGE_STRING
|
|
|
|
#undef PACKAGE_NAME
|
|
|
|
#undef PACKAGE_BUGREPORT
|
|
|
|
#undef PACKAGE
|
|
|
|
|
2010-01-11 19:41:57 +00:00
|
|
|
#define FLOAT_SAMPLES 1
|
|
|
|
#include <soundtouch/SoundTouch.h>
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
#include <gst/gst.h>
|
2012-07-10 15:47:20 +00:00
|
|
|
#include <gst/audio/audio.h>
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
#include "gstpitch.hh"
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (pitch_debug);
|
|
|
|
#define GST_CAT_DEFAULT pitch_debug
|
|
|
|
|
|
|
|
#define GST_PITCH_GET_PRIVATE(o) (o->priv)
|
|
|
|
struct _GstPitchPrivate
|
|
|
|
{
|
|
|
|
gfloat stream_time_ratio;
|
|
|
|
|
2007-11-15 18:53:56 +00:00
|
|
|
GstEvent *pending_segment;
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
soundtouch::SoundTouch * st;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ARG_0,
|
2011-08-30 07:43:22 +00:00
|
|
|
ARG_OUT_RATE,
|
2006-03-22 14:31:47 +00:00
|
|
|
ARG_RATE,
|
|
|
|
ARG_TEMPO,
|
2007-10-10 11:44:01 +00:00
|
|
|
ARG_PITCH
|
2006-03-22 14:31:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define SUPPORTED_CAPS \
|
2012-07-10 15:47:20 +00:00
|
|
|
"audio/x-raw, " \
|
|
|
|
"format = (string) " GST_AUDIO_NE (F32) ", " \
|
2008-01-27 05:56:04 +00:00
|
|
|
"rate = (int) [ 8000, MAX ], " \
|
2012-07-10 15:47:20 +00:00
|
|
|
"channels = (int) [ 1, 2 ]"
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_pitch_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-07-10 15:47:20 +00:00
|
|
|
GST_STATIC_CAPS (SUPPORTED_CAPS));
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_pitch_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-07-10 15:47:20 +00:00
|
|
|
GST_STATIC_CAPS (SUPPORTED_CAPS));
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
static void gst_pitch_dispose (GObject * object);
|
|
|
|
static void gst_pitch_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_pitch_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
|
|
|
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
static gboolean gst_pitch_setcaps (GstPitch * pitch, GstCaps * caps);
|
|
|
|
static GstFlowReturn gst_pitch_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer);
|
2006-03-22 14:31:47 +00:00
|
|
|
static GstStateChangeReturn gst_pitch_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
2012-07-10 15:47:20 +00:00
|
|
|
static gboolean gst_pitch_sink_event (GstPad * pad, GstObject * parent, GstEvent * event);
|
|
|
|
static gboolean gst_pitch_src_event (GstPad * pad, GstObject * parent, GstEvent * event);
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
static gboolean gst_pitch_src_query (GstPad * pad, GstObject * parent, GstQuery * query);
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
#define gst_pitch_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstPitch, gst_pitch, GST_TYPE_ELEMENT);
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pitch_class_init (GstPitchClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *element_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
2008-01-27 05:56:04 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (pitch_debug, "pitch", 0,
|
|
|
|
"audio pitch control element");
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
g_type_class_add_private (gobject_class, sizeof (GstPitchPrivate));
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
gobject_class->set_property = gst_pitch_set_property;
|
|
|
|
gobject_class->get_property = gst_pitch_get_property;
|
|
|
|
gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pitch_dispose);
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, ARG_PITCH,
|
|
|
|
g_param_spec_float ("pitch", "Pitch",
|
|
|
|
"Audio stream pitch", 0.1, 10.0, 1.0,
|
2010-10-19 13:23:23 +00:00
|
|
|
(GParamFlags) (G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)));
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, ARG_TEMPO,
|
|
|
|
g_param_spec_float ("tempo", "Tempo",
|
|
|
|
"Audio stream tempo", 0.1, 10.0, 1.0,
|
2010-10-19 13:23:23 +00:00
|
|
|
(GParamFlags) (G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)));
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, ARG_RATE,
|
|
|
|
g_param_spec_float ("rate", "Rate",
|
|
|
|
"Audio stream rate", 0.1, 10.0, 1.0,
|
2010-10-19 13:23:23 +00:00
|
|
|
(GParamFlags) (G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)));
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2011-08-30 07:43:22 +00:00
|
|
|
g_object_class_install_property (gobject_class, ARG_OUT_RATE,
|
|
|
|
g_param_spec_float ("output-rate", "Output Rate",
|
|
|
|
"Output rate on downstream segment events", 0.1, 10.0, 1.0,
|
|
|
|
(GParamFlags) (G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)));
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
element_class->change_state = GST_DEBUG_FUNCPTR (gst_pitch_change_state);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&gst_pitch_src_template));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&gst_pitch_sink_template));
|
|
|
|
|
|
|
|
gst_element_class_set_details_simple (element_class, "Pitch controller",
|
2012-07-11 21:17:19 +00:00
|
|
|
"Filter/Effect/Audio", "Control the pitch of an audio stream",
|
2012-07-10 15:47:20 +00:00
|
|
|
"Wouter Paesen <wouter@blue-gate.be>");
|
2006-03-22 14:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_pitch_init (GstPitch * pitch)
|
2006-03-22 14:31:47 +00:00
|
|
|
{
|
|
|
|
pitch->priv =
|
|
|
|
G_TYPE_INSTANCE_GET_PRIVATE ((pitch), GST_TYPE_PITCH, GstPitchPrivate);
|
|
|
|
|
|
|
|
pitch->sinkpad =
|
|
|
|
gst_pad_new_from_static_template (&gst_pitch_sink_template, "sink");
|
|
|
|
gst_pad_set_chain_function (pitch->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pitch_chain));
|
|
|
|
gst_pad_set_event_function (pitch->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pitch_sink_event));
|
2012-07-10 15:47:20 +00:00
|
|
|
GST_PAD_SET_PROXY_CAPS (pitch->sinkpad);
|
2006-03-22 14:31:47 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (pitch), pitch->sinkpad);
|
|
|
|
|
|
|
|
pitch->srcpad =
|
|
|
|
gst_pad_new_from_static_template (&gst_pitch_src_template, "src");
|
|
|
|
gst_pad_set_event_function (pitch->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pitch_src_event));
|
|
|
|
gst_pad_set_query_function (pitch->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pitch_src_query));
|
2012-07-10 15:47:20 +00:00
|
|
|
GST_PAD_SET_PROXY_CAPS (pitch->sinkpad);
|
2006-03-22 14:31:47 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (pitch), pitch->srcpad);
|
|
|
|
|
|
|
|
pitch->priv->st = new soundtouch::SoundTouch ();
|
|
|
|
|
|
|
|
pitch->tempo = 1.0;
|
|
|
|
pitch->rate = 1.0;
|
2011-08-30 07:43:22 +00:00
|
|
|
pitch->out_seg_rate = 1.0;
|
|
|
|
pitch->seg_arate = 1.0;
|
2006-03-22 14:31:47 +00:00
|
|
|
pitch->pitch = 1.0;
|
|
|
|
pitch->next_buffer_time = 0;
|
|
|
|
pitch->next_buffer_offset = 0;
|
|
|
|
|
|
|
|
pitch->priv->st->setRate (pitch->rate);
|
2011-08-30 07:43:22 +00:00
|
|
|
pitch->priv->st->setTempo (pitch->tempo * pitch->seg_arate);
|
2006-03-22 14:31:47 +00:00
|
|
|
pitch->priv->st->setPitch (pitch->pitch);
|
|
|
|
|
|
|
|
pitch->priv->stream_time_ratio = 1.0;
|
2008-01-27 07:37:40 +00:00
|
|
|
pitch->min_latency = pitch->max_latency = 0;
|
2006-03-22 14:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pitch_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstPitch *pitch = GST_PITCH (object);
|
|
|
|
|
|
|
|
if (pitch->priv->st) {
|
2008-01-27 17:02:24 +00:00
|
|
|
delete pitch->priv->st;
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
pitch->priv->st = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2008-01-26 15:12:50 +00:00
|
|
|
static void
|
|
|
|
gst_pitch_update_duration (GstPitch * pitch)
|
|
|
|
{
|
|
|
|
GstMessage *m;
|
|
|
|
|
|
|
|
m = gst_message_new_duration (GST_OBJECT (pitch), GST_FORMAT_TIME, -1);
|
|
|
|
gst_element_post_message (GST_ELEMENT (pitch), m);
|
|
|
|
}
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
static void
|
|
|
|
gst_pitch_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstPitch *pitch = GST_PITCH (object);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (pitch);
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_TEMPO:
|
|
|
|
pitch->tempo = g_value_get_float (value);
|
2011-08-30 07:43:22 +00:00
|
|
|
pitch->priv->stream_time_ratio = pitch->tempo * pitch->rate * pitch->seg_arate;
|
|
|
|
pitch->priv->st->setTempo (pitch->tempo * pitch->seg_arate);
|
2008-01-26 15:12:50 +00:00
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
|
|
|
gst_pitch_update_duration (pitch);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
case ARG_RATE:
|
|
|
|
pitch->rate = g_value_get_float (value);
|
2011-08-30 07:43:22 +00:00
|
|
|
pitch->priv->stream_time_ratio = pitch->tempo * pitch->rate * pitch->seg_arate;
|
2006-03-22 14:31:47 +00:00
|
|
|
pitch->priv->st->setRate (pitch->rate);
|
2008-01-26 15:12:50 +00:00
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
|
|
|
gst_pitch_update_duration (pitch);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
2011-08-30 07:43:22 +00:00
|
|
|
case ARG_OUT_RATE:
|
|
|
|
/* Has no effect until the next input segment */
|
|
|
|
pitch->out_seg_rate = g_value_get_float (value);
|
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
2006-03-22 14:31:47 +00:00
|
|
|
case ARG_PITCH:
|
|
|
|
pitch->pitch = g_value_get_float (value);
|
|
|
|
pitch->priv->st->setPitch (pitch->pitch);
|
2008-01-26 15:12:50 +00:00
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2008-01-26 15:12:50 +00:00
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pitch_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstPitch *pitch = GST_PITCH (object);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (pitch);
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_TEMPO:
|
|
|
|
g_value_set_float (value, pitch->tempo);
|
|
|
|
break;
|
|
|
|
case ARG_RATE:
|
|
|
|
g_value_set_float (value, pitch->rate);
|
|
|
|
break;
|
2011-08-30 07:43:22 +00:00
|
|
|
case ARG_OUT_RATE:
|
|
|
|
g_value_set_float (value, pitch->out_seg_rate);
|
|
|
|
break;
|
2006-03-22 14:31:47 +00:00
|
|
|
case ARG_PITCH:
|
|
|
|
g_value_set_float (value, pitch->pitch);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_pitch_setcaps (GstPitch * pitch, GstCaps * caps)
|
2006-03-22 14:31:47 +00:00
|
|
|
{
|
|
|
|
GstPitchPrivate *priv;
|
|
|
|
GstStructure *structure;
|
|
|
|
gint rate, channels;
|
|
|
|
|
|
|
|
priv = GST_PITCH_GET_PRIVATE (pitch);
|
|
|
|
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (structure, "rate", &rate) ||
|
|
|
|
!gst_structure_get_int (structure, "channels", &channels)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (pitch);
|
|
|
|
|
|
|
|
pitch->samplerate = rate;
|
|
|
|
pitch->channels = channels;
|
|
|
|
|
|
|
|
/* notify the soundtouch instance of this change */
|
|
|
|
priv->st->setSampleRate (rate);
|
|
|
|
priv->st->setChannels (channels);
|
|
|
|
|
|
|
|
/* calculate sample size */
|
|
|
|
pitch->sample_size = (sizeof (gfloat) * channels);
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send a buffer out */
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_pitch_forward_buffer (GstPitch * pitch, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
gint samples;
|
|
|
|
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) = pitch->next_buffer_time;
|
|
|
|
pitch->next_buffer_time += GST_BUFFER_DURATION (buffer);
|
|
|
|
|
|
|
|
samples = GST_BUFFER_OFFSET (buffer);
|
|
|
|
GST_BUFFER_OFFSET (buffer) = pitch->next_buffer_offset;
|
|
|
|
pitch->next_buffer_offset += samples;
|
|
|
|
GST_BUFFER_OFFSET_END (buffer) = pitch->next_buffer_offset;
|
|
|
|
|
|
|
|
GST_LOG ("pushing buffer [%" GST_TIME_FORMAT "]-[%" GST_TIME_FORMAT
|
|
|
|
"] (%d samples)", GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
|
|
|
|
GST_TIME_ARGS (pitch->next_buffer_time), samples);
|
|
|
|
|
|
|
|
return gst_pad_push (pitch->srcpad, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extract a buffer from soundtouch */
|
|
|
|
static GstBuffer *
|
|
|
|
gst_pitch_prepare_buffer (GstPitch * pitch)
|
|
|
|
{
|
|
|
|
GstPitchPrivate *priv;
|
|
|
|
guint samples;
|
|
|
|
GstBuffer *buffer;
|
2012-07-10 15:47:20 +00:00
|
|
|
GstMapInfo info;
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
priv = GST_PITCH_GET_PRIVATE (pitch);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pitch, "preparing buffer");
|
|
|
|
|
|
|
|
samples = pitch->priv->st->numSamples ();
|
|
|
|
if (samples == 0)
|
2008-01-26 15:12:50 +00:00
|
|
|
return NULL;
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
buffer = gst_buffer_new_and_alloc (samples * pitch->sample_size);
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_buffer_map (buffer, &info, (GstMapFlags) GST_MAP_READWRITE);
|
2006-03-22 14:31:47 +00:00
|
|
|
samples =
|
2012-07-10 15:47:20 +00:00
|
|
|
priv->st->receiveSamples ((gfloat *) info.data, samples);
|
|
|
|
gst_buffer_unmap (buffer, &info);
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2008-01-26 15:12:50 +00:00
|
|
|
if (samples <= 0) {
|
|
|
|
gst_buffer_unref (buffer);
|
2006-03-22 14:31:47 +00:00
|
|
|
return NULL;
|
2008-01-26 15:12:50 +00:00
|
|
|
}
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2008-01-27 07:37:40 +00:00
|
|
|
GST_BUFFER_DURATION (buffer) =
|
|
|
|
gst_util_uint64_scale (samples, GST_SECOND, pitch->samplerate);
|
2006-03-22 14:31:47 +00:00
|
|
|
/* temporary store samples here, to avoid having to recalculate this */
|
|
|
|
GST_BUFFER_OFFSET (buffer) = (gint64) samples;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* process the last samples, in a later stage we should make sure no more
|
|
|
|
* samples are sent out here as strictly necessary, because soundtouch could
|
|
|
|
* append zero samples, which could disturb looping. */
|
|
|
|
static GstFlowReturn
|
2007-07-18 20:56:22 +00:00
|
|
|
gst_pitch_flush_buffer (GstPitch * pitch, gboolean send)
|
2006-03-22 14:31:47 +00:00
|
|
|
{
|
|
|
|
GstBuffer *buffer;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pitch, "flushing buffer");
|
|
|
|
|
|
|
|
if (pitch->next_buffer_offset == 0)
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
pitch->priv->st->flush ();
|
2007-07-18 20:56:22 +00:00
|
|
|
if (!send)
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
buffer = gst_pitch_prepare_buffer (pitch);
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
return gst_pitch_forward_buffer (pitch, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_pitch_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2006-03-22 14:31:47 +00:00
|
|
|
{
|
|
|
|
GstPitch *pitch;
|
|
|
|
gboolean res;
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
pitch = GST_PITCH (parent);
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pad, "received %s event", GST_EVENT_TYPE_NAME (event));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_SEEK:{
|
|
|
|
/* transform the event upstream, according to the playback rate */
|
|
|
|
gdouble rate;
|
|
|
|
GstFormat format;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstSeekType cur_type, stop_type;
|
|
|
|
gint64 cur, stop;
|
|
|
|
gfloat stream_time_ratio;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (pitch);
|
|
|
|
stream_time_ratio = pitch->priv->stream_time_ratio;
|
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
|
|
|
|
|
|
|
gst_event_parse_seek (event, &rate, &format, &flags,
|
|
|
|
&cur_type, &cur, &stop_type, &stop);
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
|
2008-01-26 15:12:50 +00:00
|
|
|
if (format == GST_FORMAT_TIME || format == GST_FORMAT_DEFAULT) {
|
|
|
|
cur = (gint64) (cur * stream_time_ratio);
|
|
|
|
if (stop != -1)
|
|
|
|
stop = (gint64) (stop * stream_time_ratio);
|
|
|
|
|
|
|
|
event = gst_event_new_seek (rate, format, flags,
|
|
|
|
cur_type, cur, stop_type, stop);
|
2012-07-10 15:47:20 +00:00
|
|
|
res = gst_pad_event_default (pad, parent, event);
|
2008-01-26 15:12:50 +00:00
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (pitch,
|
|
|
|
"Seeking only supported in TIME or DEFAULT format");
|
|
|
|
res = FALSE;
|
|
|
|
}
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2012-07-10 15:47:20 +00:00
|
|
|
res = gst_pad_event_default (pad, parent, event);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic convert function based on caps, no rate
|
|
|
|
* used here
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
gst_pitch_convert (GstPitch * pitch,
|
|
|
|
GstFormat src_format, gint64 src_value,
|
|
|
|
GstFormat * dst_format, gint64 * dst_value)
|
|
|
|
{
|
|
|
|
gboolean res = TRUE;
|
|
|
|
guint sample_size;
|
2008-01-27 07:37:40 +00:00
|
|
|
gint samplerate;
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (dst_format && dst_value, FALSE);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (pitch);
|
|
|
|
sample_size = pitch->sample_size;
|
2008-01-27 07:37:40 +00:00
|
|
|
samplerate = pitch->samplerate;
|
2006-03-22 14:31:47 +00:00
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
|
|
|
|
2008-01-27 07:37:40 +00:00
|
|
|
if (sample_size == 0 || samplerate == 0) {
|
2006-03-22 14:31:47 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-26 15:12:50 +00:00
|
|
|
if (src_format == *dst_format || src_value == -1) {
|
2007-11-15 18:53:56 +00:00
|
|
|
*dst_value = src_value;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
switch (src_format) {
|
|
|
|
case GST_FORMAT_BYTES:
|
|
|
|
switch (*dst_format) {
|
|
|
|
case GST_FORMAT_TIME:
|
2008-01-27 07:37:40 +00:00
|
|
|
*dst_value =
|
|
|
|
gst_util_uint64_scale_int (src_value, GST_SECOND,
|
|
|
|
sample_size * samplerate);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
case GST_FORMAT_DEFAULT:
|
2008-01-27 07:37:40 +00:00
|
|
|
*dst_value = gst_util_uint64_scale_int (src_value, 1, sample_size);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GST_FORMAT_TIME:
|
|
|
|
switch (*dst_format) {
|
|
|
|
case GST_FORMAT_BYTES:
|
2008-01-27 07:37:40 +00:00
|
|
|
*dst_value =
|
|
|
|
gst_util_uint64_scale_int (src_value, samplerate * sample_size,
|
|
|
|
GST_SECOND);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
case GST_FORMAT_DEFAULT:
|
2008-01-27 07:37:40 +00:00
|
|
|
*dst_value =
|
|
|
|
gst_util_uint64_scale_int (src_value, samplerate, GST_SECOND);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GST_FORMAT_DEFAULT:
|
|
|
|
switch (*dst_format) {
|
|
|
|
case GST_FORMAT_BYTES:
|
2008-01-27 07:37:40 +00:00
|
|
|
*dst_value = gst_util_uint64_scale_int (src_value, sample_size, 1);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
case GST_FORMAT_TIME:
|
2008-01-27 07:37:40 +00:00
|
|
|
*dst_value =
|
|
|
|
gst_util_uint64_scale_int (src_value, GST_SECOND, samplerate);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_pitch_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
2006-03-22 14:31:47 +00:00
|
|
|
{
|
|
|
|
GstPitch *pitch;
|
|
|
|
gboolean res = FALSE;
|
|
|
|
gfloat stream_time_ratio;
|
|
|
|
gint64 next_buffer_offset;
|
2008-01-26 15:12:50 +00:00
|
|
|
GstClockTime next_buffer_time;
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
pitch = GST_PITCH (parent);
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
GST_LOG ("%s query", GST_QUERY_TYPE_NAME (query));
|
2012-07-10 15:47:20 +00:00
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
GST_OBJECT_LOCK (pitch);
|
|
|
|
stream_time_ratio = pitch->priv->stream_time_ratio;
|
2008-01-26 15:12:50 +00:00
|
|
|
next_buffer_time = pitch->next_buffer_time;
|
2006-03-22 14:31:47 +00:00
|
|
|
next_buffer_offset = pitch->next_buffer_offset;
|
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_DURATION:{
|
|
|
|
GstFormat format;
|
|
|
|
gint64 duration;
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
if (!gst_pad_query_default (pad, parent, query)) {
|
2006-03-22 14:31:47 +00:00
|
|
|
GST_DEBUG_OBJECT (pitch, "upstream provided no duration");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_query_parse_duration (query, &format, &duration);
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME && format != GST_FORMAT_DEFAULT) {
|
|
|
|
GST_DEBUG_OBJECT (pitch, "not TIME or DEFAULT format");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GST_LOG_OBJECT (pitch, "upstream duration: %" G_GINT64_FORMAT, duration);
|
|
|
|
duration = (gint64) (duration / stream_time_ratio);
|
|
|
|
GST_LOG_OBJECT (pitch, "our duration: %" G_GINT64_FORMAT, duration);
|
|
|
|
gst_query_set_duration (query, format, duration);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_QUERY_POSITION:{
|
|
|
|
GstFormat dst_format;
|
|
|
|
gint64 dst_value;
|
|
|
|
|
|
|
|
gst_query_parse_position (query, &dst_format, &dst_value);
|
|
|
|
|
|
|
|
if (dst_format != GST_FORMAT_TIME && dst_format != GST_FORMAT_DEFAULT) {
|
|
|
|
GST_DEBUG_OBJECT (pitch, "not TIME or DEFAULT format");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-26 15:12:50 +00:00
|
|
|
if (dst_format == GST_FORMAT_TIME) {
|
|
|
|
dst_value = next_buffer_time;
|
|
|
|
res = TRUE;
|
2006-03-22 14:31:47 +00:00
|
|
|
} else {
|
|
|
|
dst_value = next_buffer_offset;
|
|
|
|
res = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
GST_LOG_OBJECT (pitch, "our position: %" G_GINT64_FORMAT, dst_value);
|
|
|
|
gst_query_set_position (query, dst_format, dst_value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_QUERY_CONVERT:{
|
|
|
|
GstFormat src_format, dst_format;
|
|
|
|
gint64 src_value, dst_value;
|
|
|
|
|
|
|
|
gst_query_parse_convert (query, &src_format, &src_value,
|
|
|
|
&dst_format, NULL);
|
|
|
|
|
|
|
|
res = gst_pitch_convert (pitch, src_format, src_value,
|
|
|
|
&dst_format, &dst_value);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
gst_query_set_convert (query, src_format, src_value,
|
|
|
|
dst_format, dst_value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-01-27 07:37:40 +00:00
|
|
|
case GST_QUERY_LATENCY:
|
|
|
|
{
|
|
|
|
GstClockTime min, max;
|
|
|
|
gboolean live;
|
|
|
|
GstPad *peer;
|
|
|
|
|
|
|
|
if ((peer = gst_pad_get_peer (pitch->sinkpad))) {
|
|
|
|
if ((res = gst_pad_query (peer, query))) {
|
|
|
|
gst_query_parse_latency (query, &live, &min, &max);
|
|
|
|
|
|
|
|
GST_DEBUG ("Peer latency: min %"
|
|
|
|
GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (min), GST_TIME_ARGS (max));
|
|
|
|
|
|
|
|
/* add our own latency */
|
|
|
|
|
|
|
|
GST_DEBUG ("Our latency: min %" GST_TIME_FORMAT
|
|
|
|
", max %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (pitch->min_latency),
|
|
|
|
GST_TIME_ARGS (pitch->max_latency));
|
|
|
|
|
|
|
|
min += pitch->min_latency;
|
|
|
|
if (max != GST_CLOCK_TIME_NONE)
|
|
|
|
max += pitch->max_latency;
|
|
|
|
else
|
|
|
|
max = pitch->max_latency;
|
|
|
|
|
|
|
|
GST_DEBUG ("Calculated total latency : min %"
|
|
|
|
GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (min), GST_TIME_ARGS (max));
|
|
|
|
gst_query_set_latency (query, live, min, max);
|
|
|
|
}
|
|
|
|
gst_object_unref (peer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-03-22 14:31:47 +00:00
|
|
|
default:
|
2012-07-10 15:47:20 +00:00
|
|
|
res = gst_pad_query_default (pad, parent, query);
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-11-15 18:53:56 +00:00
|
|
|
/* this function returns FALSE if not enough data is known to transform the
|
|
|
|
* segment into proper downstream values. If the function does return false
|
|
|
|
* the sgement should be stalled until enough information is available.
|
|
|
|
* If the funtion returns TRUE, event will be replaced by the new downstream
|
|
|
|
* compatible event.
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
gst_pitch_process_segment (GstPitch * pitch, GstEvent ** event)
|
|
|
|
{
|
2012-07-10 15:47:20 +00:00
|
|
|
GstFormat conv_format;
|
2007-11-15 18:53:56 +00:00
|
|
|
gint64 next_offset = 0, next_time = 0;
|
2012-07-10 15:47:20 +00:00
|
|
|
gdouble out_seg_rate, our_arate;
|
2007-11-15 18:53:56 +00:00
|
|
|
gfloat stream_time_ratio;
|
2012-07-10 15:47:20 +00:00
|
|
|
GstSegment seg;
|
2007-11-15 18:53:56 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (event, FALSE);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (pitch);
|
|
|
|
stream_time_ratio = pitch->priv->stream_time_ratio;
|
2011-08-30 07:43:22 +00:00
|
|
|
out_seg_rate = pitch->out_seg_rate;
|
2007-11-15 18:53:56 +00:00
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_event_copy_segment (*event, &seg);
|
2007-11-15 18:53:56 +00:00
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
if (seg.format != GST_FORMAT_TIME && seg.format != GST_FORMAT_DEFAULT) {
|
2008-01-26 15:12:50 +00:00
|
|
|
GST_WARNING_OBJECT (pitch,
|
|
|
|
"Only NEWSEGMENT in TIME or DEFAULT format supported, sending"
|
|
|
|
"open ended NEWSEGMENT in TIME format.");
|
2012-07-10 15:47:20 +00:00
|
|
|
seg.format = GST_FORMAT_TIME;
|
|
|
|
seg.start = 0;
|
|
|
|
seg.stop = -1;
|
|
|
|
seg.time = 0;
|
2008-01-26 15:12:50 +00:00
|
|
|
}
|
|
|
|
|
2011-08-30 07:43:22 +00:00
|
|
|
/* Figure out how much of the incoming 'rate' we'll apply ourselves */
|
2012-07-10 15:47:20 +00:00
|
|
|
our_arate = seg.rate / out_seg_rate;
|
2011-08-30 07:43:22 +00:00
|
|
|
/* update the output rate variables */
|
2012-07-10 15:47:20 +00:00
|
|
|
seg.rate = out_seg_rate;
|
|
|
|
seg.applied_rate *= our_arate;
|
2011-08-30 07:43:22 +00:00
|
|
|
|
2009-10-11 12:10:59 +00:00
|
|
|
GST_LOG_OBJECT (pitch->sinkpad,
|
2012-07-10 15:47:20 +00:00
|
|
|
"segment %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT " (%d)", seg.start,
|
|
|
|
seg.stop, seg.format);
|
2007-11-15 18:53:56 +00:00
|
|
|
|
2011-08-30 07:43:22 +00:00
|
|
|
stream_time_ratio = pitch->tempo * pitch->rate * pitch->seg_arate;
|
|
|
|
|
2007-11-15 18:53:56 +00:00
|
|
|
if (stream_time_ratio == 0) {
|
|
|
|
GST_LOG_OBJECT (pitch->sinkpad, "stream_time_ratio is zero");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-08-30 07:43:22 +00:00
|
|
|
/* Update the playback rate */
|
|
|
|
GST_OBJECT_LOCK (pitch);
|
|
|
|
pitch->seg_arate = our_arate;
|
|
|
|
pitch->priv->stream_time_ratio = stream_time_ratio;
|
|
|
|
pitch->priv->st->setTempo (pitch->tempo * pitch->seg_arate);
|
|
|
|
GST_OBJECT_UNLOCK (pitch);
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
seg.start = (gint64) (seg.start / stream_time_ratio);
|
|
|
|
if (seg.stop != (guint64) -1)
|
|
|
|
seg.stop = (gint64) (seg.stop / stream_time_ratio);
|
|
|
|
seg.time = (gint64) (seg.time / stream_time_ratio);
|
2007-11-15 18:53:56 +00:00
|
|
|
|
|
|
|
conv_format = GST_FORMAT_TIME;
|
2012-07-10 15:47:20 +00:00
|
|
|
if (!gst_pitch_convert (pitch, seg.format, seg.start, &conv_format, &next_time)) {
|
2007-11-15 18:53:56 +00:00
|
|
|
GST_LOG_OBJECT (pitch->sinkpad,
|
|
|
|
"could not convert segment start value to time");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
conv_format = GST_FORMAT_DEFAULT;
|
2012-07-10 15:47:20 +00:00
|
|
|
if (!gst_pitch_convert (pitch, seg.format, seg.start, &conv_format,
|
2007-11-15 18:53:56 +00:00
|
|
|
&next_offset)) {
|
|
|
|
GST_LOG_OBJECT (pitch->sinkpad,
|
|
|
|
"could not convert segment start value to offset");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pitch->next_buffer_time = next_time;
|
|
|
|
pitch->next_buffer_offset = next_offset;
|
|
|
|
|
|
|
|
gst_event_unref (*event);
|
2012-07-10 15:47:20 +00:00
|
|
|
*event = gst_event_new_segment (&seg);
|
2007-11-15 18:53:56 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
static gboolean
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_pitch_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2006-03-22 14:31:47 +00:00
|
|
|
{
|
|
|
|
gboolean res = TRUE;
|
|
|
|
GstPitch *pitch;
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
pitch = GST_PITCH (parent);
|
2006-03-22 14:31:47 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (pad, "received %s event", GST_EVENT_TYPE_NAME (event));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2007-07-18 20:56:22 +00:00
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
gst_pitch_flush_buffer (pitch, FALSE);
|
2008-01-27 05:56:04 +00:00
|
|
|
pitch->priv->st->clear ();
|
2011-08-30 07:43:22 +00:00
|
|
|
pitch->next_buffer_offset = 0;
|
|
|
|
pitch->next_buffer_time = 0;
|
2008-01-27 07:37:40 +00:00
|
|
|
pitch->min_latency = pitch->max_latency = 0;
|
2007-07-18 20:56:22 +00:00
|
|
|
break;
|
2006-03-22 14:31:47 +00:00
|
|
|
case GST_EVENT_EOS:
|
2007-07-18 20:56:22 +00:00
|
|
|
gst_pitch_flush_buffer (pitch, TRUE);
|
2008-01-27 05:56:04 +00:00
|
|
|
pitch->priv->st->clear ();
|
2008-01-27 07:37:40 +00:00
|
|
|
pitch->min_latency = pitch->max_latency = 0;
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
2012-07-10 15:47:20 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
2007-11-15 18:53:56 +00:00
|
|
|
if (!gst_pitch_process_segment (pitch, &event)) {
|
|
|
|
GST_LOG_OBJECT (pad, "not enough data known, stalling segment");
|
|
|
|
if (GST_PITCH_GET_PRIVATE (pitch)->pending_segment)
|
|
|
|
gst_event_unref (GST_PITCH_GET_PRIVATE (pitch)->pending_segment);
|
|
|
|
GST_PITCH_GET_PRIVATE (pitch)->pending_segment = event;
|
|
|
|
event = NULL;
|
|
|
|
}
|
2008-01-27 05:56:04 +00:00
|
|
|
pitch->priv->st->clear ();
|
2008-01-27 07:37:40 +00:00
|
|
|
pitch->min_latency = pitch->max_latency = 0;
|
2007-11-15 18:53:56 +00:00
|
|
|
break;
|
2012-07-10 15:47:20 +00:00
|
|
|
case GST_EVENT_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps * caps;
|
|
|
|
|
|
|
|
gst_event_parse_caps (event, &caps);
|
|
|
|
res = gst_pitch_setcaps (pitch, caps);
|
|
|
|
if (!res) {
|
|
|
|
gst_event_unref (event);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2006-03-22 14:31:47 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and forward it */
|
2007-11-15 18:53:56 +00:00
|
|
|
if (event)
|
2012-07-10 15:47:20 +00:00
|
|
|
res = gst_pad_event_default (pad, parent, event);
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
done:
|
2006-03-22 14:31:47 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-01-27 07:37:40 +00:00
|
|
|
static void
|
|
|
|
gst_pitch_update_latency (GstPitch * pitch, GstClockTime timestamp)
|
|
|
|
{
|
|
|
|
GstClockTimeDiff current_latency, min_latency, max_latency;
|
|
|
|
|
|
|
|
current_latency =
|
2008-01-27 17:02:24 +00:00
|
|
|
(GstClockTimeDiff) (timestamp / pitch->priv->stream_time_ratio) -
|
|
|
|
pitch->next_buffer_time;
|
2008-01-27 07:37:40 +00:00
|
|
|
|
|
|
|
min_latency = MIN (pitch->min_latency, current_latency);
|
|
|
|
max_latency = MAX (pitch->max_latency, current_latency);
|
|
|
|
|
|
|
|
if (pitch->min_latency != min_latency || pitch->max_latency != max_latency) {
|
|
|
|
pitch->min_latency = min_latency;
|
|
|
|
pitch->max_latency = max_latency;
|
|
|
|
|
2008-01-27 07:51:06 +00:00
|
|
|
/* FIXME: what about the LATENCY event? It only has
|
|
|
|
* one latency value, should it be current, min or max?
|
|
|
|
* Should it include upstream latencies?
|
|
|
|
*/
|
|
|
|
|
2008-01-27 07:37:40 +00:00
|
|
|
gst_element_post_message (GST_ELEMENT (pitch),
|
|
|
|
gst_message_new_latency (GST_OBJECT (pitch)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
static GstFlowReturn
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_pitch_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
|
2006-03-22 14:31:47 +00:00
|
|
|
{
|
|
|
|
GstPitch *pitch;
|
|
|
|
GstPitchPrivate *priv;
|
2008-01-27 07:37:40 +00:00
|
|
|
GstClockTime timestamp;
|
2012-07-10 15:47:20 +00:00
|
|
|
GstMapInfo info;
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
pitch = GST_PITCH (parent);
|
2006-03-22 14:31:47 +00:00
|
|
|
priv = GST_PITCH_GET_PRIVATE (pitch);
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_object_sync_values (GST_OBJECT (pitch), pitch->next_buffer_time);
|
2006-06-14 18:07:51 +00:00
|
|
|
|
2008-01-27 07:37:40 +00:00
|
|
|
timestamp = GST_BUFFER_TIMESTAMP (buffer);
|
|
|
|
|
2006-03-22 14:31:47 +00:00
|
|
|
/* push the received samples on the soundtouch buffer */
|
|
|
|
GST_LOG_OBJECT (pitch, "incoming buffer (%d samples)",
|
2012-07-10 15:47:20 +00:00
|
|
|
(gint) (gst_buffer_get_size (buffer) / pitch->sample_size));
|
2006-03-22 14:31:47 +00:00
|
|
|
|
2007-11-15 18:53:56 +00:00
|
|
|
if (GST_PITCH_GET_PRIVATE (pitch)->pending_segment) {
|
|
|
|
GstEvent *event =
|
|
|
|
gst_event_copy (GST_PITCH_GET_PRIVATE (pitch)->pending_segment);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pitch, "processing stalled segment");
|
|
|
|
if (!gst_pitch_process_segment (pitch, &event)) {
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_buffer_unref (buffer);
|
2007-11-15 18:53:56 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
if (!gst_pad_event_default (pitch->sinkpad, parent, event)) {
|
|
|
|
gst_buffer_unref (buffer);
|
2007-11-15 18:53:56 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_event_unref (GST_PITCH_GET_PRIVATE (pitch)->pending_segment);
|
|
|
|
GST_PITCH_GET_PRIVATE (pitch)->pending_segment = NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
gst_buffer_map (buffer, &info, GST_MAP_READ);
|
|
|
|
priv->st->putSamples ((gfloat *) info.data, info.size / pitch->sample_size);
|
|
|
|
gst_buffer_unmap (buffer, &info);
|
2006-03-22 14:31:47 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
|
2008-01-27 07:37:40 +00:00
|
|
|
/* Calculate latency */
|
|
|
|
|
|
|
|
gst_pitch_update_latency (pitch, timestamp);
|
2006-03-22 14:31:47 +00:00
|
|
|
/* and try to extract some samples from the soundtouch buffer */
|
|
|
|
if (!priv->st->isEmpty ()) {
|
|
|
|
GstBuffer *out_buffer;
|
|
|
|
|
|
|
|
out_buffer = gst_pitch_prepare_buffer (pitch);
|
2008-01-26 15:12:50 +00:00
|
|
|
if (out_buffer)
|
|
|
|
return gst_pitch_forward_buffer (pitch, out_buffer);
|
2006-03-22 14:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_pitch_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
GstPitch *pitch = GST_PITCH (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
pitch->next_buffer_time = 0;
|
|
|
|
pitch->next_buffer_offset = 0;
|
2008-01-27 05:56:04 +00:00
|
|
|
pitch->priv->st->clear ();
|
2008-01-27 07:37:40 +00:00
|
|
|
pitch->min_latency = pitch->max_latency = 0;
|
2006-03-22 14:31:47 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-07-10 15:47:20 +00:00
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
2006-03-22 14:31:47 +00:00
|
|
|
if (ret != GST_STATE_CHANGE_SUCCESS)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
2007-11-15 18:53:56 +00:00
|
|
|
break;
|
2006-03-22 14:31:47 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2007-11-15 18:53:56 +00:00
|
|
|
if (GST_PITCH_GET_PRIVATE (pitch)->pending_segment) {
|
|
|
|
gst_event_unref (GST_PITCH_GET_PRIVATE (pitch)->pending_segment);
|
|
|
|
GST_PITCH_GET_PRIVATE (pitch)->pending_segment = NULL;
|
|
|
|
}
|
|
|
|
break;
|
2006-03-22 14:31:47 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|