3 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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.
22 * SECTION:element-audioecho
25 * audioecho adds an echo or (simple) reverb effect to an audio stream. The echo
26 * delay, intensity and the percentage of feedback can be configured.
28 * For getting an echo effect you have to set the delay to a larger value,
29 * for example 200ms and more. Everything below will result in a simple
30 * reverb effect, which results in a slightly metallic sound.
32 * Use the max-delay property to set the maximum amount of delay that
33 * will be used. This can only be set before going to the PAUSED or PLAYING
34 * state and will be set to the current delay by default.
37 * <title>Example launch line</title>
39 * gst-launch filesrc location="melo1.ogg" ! audioconvert ! audioecho delay=500000000 intensity=0.6 feedback=0.4 ! audioconvert ! autoaudiosink
40 * gst-launch filesrc location="melo1.ogg" ! decodebin ! audioconvert ! audioecho delay=50000000 intensity=0.6 feedback=0.4 ! audioconvert ! autoaudiosink
50 #include <gst/base/gstbasetransform.h>
51 #include <gst/audio/audio.h>
52 #include <gst/audio/gstaudiofilter.h>
53 #include <gst/controller/gstcontroller.h>
55 #include "audioecho.h"
57 #define GST_CAT_DEFAULT gst_audio_echo_debug
58 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
69 #define ALLOWED_CAPS \
70 "audio/x-raw-float," \
71 " width=(int) { 32, 64 }, " \
72 " endianness=(int)BYTE_ORDER," \
73 " rate=(int)[1,MAX]," \
74 " channels=(int)[1,MAX]"
76 #define gst_audio_echo_parent_class parent_class
77 G_DEFINE_TYPE (GstAudioEcho, gst_audio_echo, GST_TYPE_AUDIO_FILTER);
79 static void gst_audio_echo_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec);
81 static void gst_audio_echo_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec);
83 static void gst_audio_echo_finalize (GObject * object);
85 static gboolean gst_audio_echo_setup (GstAudioFilter * self,
86 GstRingBufferSpec * format);
87 static gboolean gst_audio_echo_stop (GstBaseTransform * base);
88 static GstFlowReturn gst_audio_echo_transform_ip (GstBaseTransform * base,
91 static void gst_audio_echo_transform_float (GstAudioEcho * self,
92 gfloat * data, guint num_samples);
93 static void gst_audio_echo_transform_double (GstAudioEcho * self,
94 gdouble * data, guint num_samples);
96 /* GObject vmethod implementations */
99 gst_audio_echo_class_init (GstAudioEchoClass * klass)
101 GObjectClass *gobject_class = (GObjectClass *) klass;
102 GstElementClass *gstelement_class = (GstElementClass *) klass;
103 GstBaseTransformClass *basetransform_class = (GstBaseTransformClass *) klass;
104 GstAudioFilterClass *audioself_class = (GstAudioFilterClass *) klass;
107 GST_DEBUG_CATEGORY_INIT (gst_audio_echo_debug, "audioecho", 0,
108 "audioecho element");
110 gobject_class->set_property = gst_audio_echo_set_property;
111 gobject_class->get_property = gst_audio_echo_get_property;
112 gobject_class->finalize = gst_audio_echo_finalize;
114 g_object_class_install_property (gobject_class, PROP_DELAY,
115 g_param_spec_uint64 ("delay", "Delay",
116 "Delay of the echo in nanoseconds", 1, G_MAXUINT64,
117 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
118 | GST_PARAM_CONTROLLABLE));
120 g_object_class_install_property (gobject_class, PROP_MAX_DELAY,
121 g_param_spec_uint64 ("max-delay", "Maximum Delay",
122 "Maximum delay of the echo in nanoseconds"
123 " (can't be changed in PLAYING or PAUSED state)",
125 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
126 GST_PARAM_MUTABLE_READY));
128 g_object_class_install_property (gobject_class, PROP_INTENSITY,
129 g_param_spec_float ("intensity", "Intensity",
130 "Intensity of the echo", 0.0, 1.0,
131 0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
132 | GST_PARAM_CONTROLLABLE));
134 g_object_class_install_property (gobject_class, PROP_FEEDBACK,
135 g_param_spec_float ("feedback", "Feedback",
136 "Amount of feedback", 0.0, 1.0,
137 0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
138 | GST_PARAM_CONTROLLABLE));
140 gst_element_class_set_details_simple (gstelement_class, "Audio echo",
141 "Filter/Effect/Audio",
142 "Adds an echo or reverb effect to an audio stream",
143 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
145 caps = gst_caps_from_string (ALLOWED_CAPS);
146 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
148 gst_caps_unref (caps);
150 audioself_class->setup = GST_DEBUG_FUNCPTR (gst_audio_echo_setup);
151 basetransform_class->transform_ip =
152 GST_DEBUG_FUNCPTR (gst_audio_echo_transform_ip);
153 basetransform_class->stop = GST_DEBUG_FUNCPTR (gst_audio_echo_stop);
157 gst_audio_echo_init (GstAudioEcho * self)
161 self->intensity = 0.0;
162 self->feedback = 0.0;
164 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (self), TRUE);
168 gst_audio_echo_finalize (GObject * object)
170 GstAudioEcho *self = GST_AUDIO_ECHO (object);
172 g_free (self->buffer);
175 G_OBJECT_CLASS (parent_class)->finalize (object);
179 gst_audio_echo_set_property (GObject * object, guint prop_id,
180 const GValue * value, GParamSpec * pspec)
182 GstAudioEcho *self = GST_AUDIO_ECHO (object);
186 guint64 max_delay, delay;
188 GST_BASE_TRANSFORM_LOCK (self);
189 delay = g_value_get_uint64 (value);
190 max_delay = self->max_delay;
192 if (delay > max_delay && GST_STATE (self) > GST_STATE_READY) {
193 GST_WARNING_OBJECT (self, "New delay (%" GST_TIME_FORMAT ") "
194 "is larger than maximum delay (%" GST_TIME_FORMAT ")",
195 GST_TIME_ARGS (delay), GST_TIME_ARGS (max_delay));
196 self->delay = max_delay;
199 self->max_delay = MAX (delay, max_delay);
201 GST_BASE_TRANSFORM_UNLOCK (self);
204 case PROP_MAX_DELAY:{
205 guint64 max_delay, delay;
207 GST_BASE_TRANSFORM_LOCK (self);
208 max_delay = g_value_get_uint64 (value);
211 if (GST_STATE (self) > GST_STATE_READY) {
212 GST_ERROR_OBJECT (self, "Can't change maximum delay in"
213 " PLAYING or PAUSED state");
216 self->max_delay = max_delay;
218 GST_BASE_TRANSFORM_UNLOCK (self);
221 case PROP_INTENSITY:{
222 GST_BASE_TRANSFORM_LOCK (self);
223 self->intensity = g_value_get_float (value);
224 GST_BASE_TRANSFORM_UNLOCK (self);
228 GST_BASE_TRANSFORM_LOCK (self);
229 self->feedback = g_value_get_float (value);
230 GST_BASE_TRANSFORM_UNLOCK (self);
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 gst_audio_echo_get_property (GObject * object, guint prop_id,
241 GValue * value, GParamSpec * pspec)
243 GstAudioEcho *self = GST_AUDIO_ECHO (object);
247 GST_BASE_TRANSFORM_LOCK (self);
248 g_value_set_uint64 (value, self->delay);
249 GST_BASE_TRANSFORM_UNLOCK (self);
252 GST_BASE_TRANSFORM_LOCK (self);
253 g_value_set_uint64 (value, self->max_delay);
254 GST_BASE_TRANSFORM_UNLOCK (self);
257 GST_BASE_TRANSFORM_LOCK (self);
258 g_value_set_float (value, self->intensity);
259 GST_BASE_TRANSFORM_UNLOCK (self);
262 GST_BASE_TRANSFORM_LOCK (self);
263 g_value_set_float (value, self->feedback);
264 GST_BASE_TRANSFORM_UNLOCK (self);
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 /* GstAudioFilter vmethod implementations */
275 gst_audio_echo_setup (GstAudioFilter * base, GstRingBufferSpec * format)
277 GstAudioEcho *self = GST_AUDIO_ECHO (base);
280 if (format->type == GST_BUFTYPE_FLOAT && format->width == 32)
281 self->process = (GstAudioEchoProcessFunc)
282 gst_audio_echo_transform_float;
283 else if (format->type == GST_BUFTYPE_FLOAT && format->width == 64)
284 self->process = (GstAudioEchoProcessFunc)
285 gst_audio_echo_transform_double;
289 g_free (self->buffer);
291 self->buffer_pos = 0;
292 self->buffer_size = 0;
293 self->buffer_size_frames = 0;
299 gst_audio_echo_stop (GstBaseTransform * base)
301 GstAudioEcho *self = GST_AUDIO_ECHO (base);
303 g_free (self->buffer);
305 self->buffer_pos = 0;
306 self->buffer_size = 0;
307 self->buffer_size_frames = 0;
312 #define TRANSFORM_FUNC(name, type) \
314 gst_audio_echo_transform_##name (GstAudioEcho * self, \
315 type * data, guint num_samples) \
317 type *buffer = (type *) self->buffer; \
318 guint channels = GST_AUDIO_FILTER (self)->format.channels; \
319 guint rate = GST_AUDIO_FILTER (self)->format.rate; \
321 guint echo_index = self->buffer_size_frames - self->delay_frames; \
322 gdouble echo_off = ((((gdouble) self->delay) * rate) / GST_SECOND) - self->delay_frames; \
324 if (echo_off < 0.0) \
327 num_samples /= channels; \
329 for (i = 0; i < num_samples; i++) { \
330 guint echo0_index = ((echo_index + self->buffer_pos) % self->buffer_size_frames) * channels; \
331 guint echo1_index = ((echo_index + self->buffer_pos +1) % self->buffer_size_frames) * channels; \
332 guint rbout_index = (self->buffer_pos % self->buffer_size_frames) * channels; \
333 for (j = 0; j < channels; j++) { \
334 gdouble in = data[i*channels + j]; \
335 gdouble echo0 = buffer[echo0_index + j]; \
336 gdouble echo1 = buffer[echo1_index + j]; \
337 gdouble echo = echo0 + (echo1-echo0)*echo_off; \
338 type out = in + self->intensity * echo; \
340 data[i*channels + j] = out; \
342 buffer[rbout_index + j] = in + self->feedback * echo; \
344 self->buffer_pos = (self->buffer_pos + 1) % self->buffer_size_frames; \
348 TRANSFORM_FUNC (float, gfloat);
349 TRANSFORM_FUNC (double, gdouble);
351 /* GstBaseTransform vmethod implementations */
353 gst_audio_echo_transform_ip (GstBaseTransform * base, GstBuffer * buf)
355 GstAudioEcho *self = GST_AUDIO_ECHO (base);
357 GstClockTime timestamp, stream_time;
361 timestamp = GST_BUFFER_TIMESTAMP (buf);
363 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
365 GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT,
366 GST_TIME_ARGS (timestamp));
368 if (GST_CLOCK_TIME_IS_VALID (stream_time))
369 gst_object_sync_values (G_OBJECT (self), stream_time);
371 if (self->buffer == NULL) {
372 guint width, rate, channels;
374 width = GST_AUDIO_FILTER (self)->format.width / 8;
375 rate = GST_AUDIO_FILTER (self)->format.rate;
376 channels = GST_AUDIO_FILTER (self)->format.channels;
379 MAX (gst_util_uint64_scale (self->delay, rate, GST_SECOND), 1);
380 self->buffer_size_frames =
381 MAX (gst_util_uint64_scale (self->max_delay, rate, GST_SECOND), 1);
383 self->buffer_size = self->buffer_size_frames * width * channels;
384 self->buffer = g_try_malloc0 (self->buffer_size);
385 self->buffer_pos = 0;
387 if (self->buffer == NULL) {
388 GST_ERROR_OBJECT (self, "Failed to allocate %u bytes", self->buffer_size);
389 return GST_FLOW_ERROR;
393 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
394 num_samples = size / (GST_AUDIO_FILTER (self)->format.width / 8);
396 self->process (self, data, num_samples);
398 gst_buffer_unmap (buf, data, size);