Release 1.1.4
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audioecho.c
1 /* 
2  * GStreamer
3  * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:element-audioecho
23  * @Since: 0.10.14
24  *
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.
27  *
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.
31  *
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.
35  *
36  * <refsect2>
37  * <title>Example launch line</title>
38  * |[
39  * gst-launch-1.0 filesrc location="melo1.ogg" ! audioconvert ! audioecho delay=500000000 intensity=0.6 feedback=0.4 ! audioconvert ! autoaudiosink
40  * gst-launch-1.0 filesrc location="melo1.ogg" ! decodebin ! audioconvert ! audioecho delay=50000000 intensity=0.6 feedback=0.4 ! audioconvert ! autoaudiosink
41  * ]|
42  * </refsect2>
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <gst/gst.h>
50 #include <gst/base/gstbasetransform.h>
51 #include <gst/audio/audio.h>
52 #include <gst/audio/gstaudiofilter.h>
53
54 #include "audioecho.h"
55
56 #define GST_CAT_DEFAULT gst_audio_echo_debug
57 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
58
59 enum
60 {
61   PROP_0,
62   PROP_DELAY,
63   PROP_MAX_DELAY,
64   PROP_INTENSITY,
65   PROP_FEEDBACK
66 };
67
68 #define ALLOWED_CAPS \
69     "audio/x-raw,"                                                 \
70     " format=(string) {"GST_AUDIO_NE(F32)","GST_AUDIO_NE(F64)"}, " \
71     " rate=(int)[1,MAX],"                                          \
72     " channels=(int)[1,MAX],"                                      \
73     " layout=(string) interleaved"
74
75 #define gst_audio_echo_parent_class parent_class
76 G_DEFINE_TYPE (GstAudioEcho, gst_audio_echo, GST_TYPE_AUDIO_FILTER);
77
78 static void gst_audio_echo_set_property (GObject * object, guint prop_id,
79     const GValue * value, GParamSpec * pspec);
80 static void gst_audio_echo_get_property (GObject * object, guint prop_id,
81     GValue * value, GParamSpec * pspec);
82 static void gst_audio_echo_finalize (GObject * object);
83
84 static gboolean gst_audio_echo_setup (GstAudioFilter * self,
85     const GstAudioInfo * info);
86 static gboolean gst_audio_echo_stop (GstBaseTransform * base);
87 static GstFlowReturn gst_audio_echo_transform_ip (GstBaseTransform * base,
88     GstBuffer * buf);
89
90 static void gst_audio_echo_transform_float (GstAudioEcho * self,
91     gfloat * data, guint num_samples);
92 static void gst_audio_echo_transform_double (GstAudioEcho * self,
93     gdouble * data, guint num_samples);
94
95 /* GObject vmethod implementations */
96
97 static void
98 gst_audio_echo_class_init (GstAudioEchoClass * klass)
99 {
100   GObjectClass *gobject_class = (GObjectClass *) klass;
101   GstElementClass *gstelement_class = (GstElementClass *) klass;
102   GstBaseTransformClass *basetransform_class = (GstBaseTransformClass *) klass;
103   GstAudioFilterClass *audioself_class = (GstAudioFilterClass *) klass;
104   GstCaps *caps;
105
106   GST_DEBUG_CATEGORY_INIT (gst_audio_echo_debug, "audioecho", 0,
107       "audioecho element");
108
109   gobject_class->set_property = gst_audio_echo_set_property;
110   gobject_class->get_property = gst_audio_echo_get_property;
111   gobject_class->finalize = gst_audio_echo_finalize;
112
113   g_object_class_install_property (gobject_class, PROP_DELAY,
114       g_param_spec_uint64 ("delay", "Delay",
115           "Delay of the echo in nanoseconds", 1, G_MAXUINT64,
116           1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
117           | GST_PARAM_CONTROLLABLE));
118
119   g_object_class_install_property (gobject_class, PROP_MAX_DELAY,
120       g_param_spec_uint64 ("max-delay", "Maximum Delay",
121           "Maximum delay of the echo in nanoseconds"
122           " (can't be changed in PLAYING or PAUSED state)",
123           1, G_MAXUINT64, 1,
124           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
125           GST_PARAM_MUTABLE_READY));
126
127   g_object_class_install_property (gobject_class, PROP_INTENSITY,
128       g_param_spec_float ("intensity", "Intensity",
129           "Intensity of the echo", 0.0, 1.0,
130           0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
131           | GST_PARAM_CONTROLLABLE));
132
133   g_object_class_install_property (gobject_class, PROP_FEEDBACK,
134       g_param_spec_float ("feedback", "Feedback",
135           "Amount of feedback", 0.0, 1.0,
136           0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
137           | GST_PARAM_CONTROLLABLE));
138
139   gst_element_class_set_static_metadata (gstelement_class, "Audio echo",
140       "Filter/Effect/Audio",
141       "Adds an echo or reverb effect to an audio stream",
142       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
143
144   caps = gst_caps_from_string (ALLOWED_CAPS);
145   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
146       caps);
147   gst_caps_unref (caps);
148
149   audioself_class->setup = GST_DEBUG_FUNCPTR (gst_audio_echo_setup);
150   basetransform_class->transform_ip =
151       GST_DEBUG_FUNCPTR (gst_audio_echo_transform_ip);
152   basetransform_class->stop = GST_DEBUG_FUNCPTR (gst_audio_echo_stop);
153 }
154
155 static void
156 gst_audio_echo_init (GstAudioEcho * self)
157 {
158   self->delay = 1;
159   self->max_delay = 1;
160   self->intensity = 0.0;
161   self->feedback = 0.0;
162
163   g_mutex_init (&self->lock);
164
165   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (self), TRUE);
166 }
167
168 static void
169 gst_audio_echo_finalize (GObject * object)
170 {
171   GstAudioEcho *self = GST_AUDIO_ECHO (object);
172
173   g_free (self->buffer);
174   self->buffer = NULL;
175
176   g_mutex_clear (&self->lock);
177
178   G_OBJECT_CLASS (parent_class)->finalize (object);
179 }
180
181 static void
182 gst_audio_echo_set_property (GObject * object, guint prop_id,
183     const GValue * value, GParamSpec * pspec)
184 {
185   GstAudioEcho *self = GST_AUDIO_ECHO (object);
186
187   switch (prop_id) {
188     case PROP_DELAY:{
189       guint64 max_delay, delay;
190       guint rate;
191
192       g_mutex_lock (&self->lock);
193       delay = g_value_get_uint64 (value);
194       max_delay = self->max_delay;
195
196       if (delay > max_delay && GST_STATE (self) > GST_STATE_READY) {
197         GST_WARNING_OBJECT (self, "New delay (%" GST_TIME_FORMAT ") "
198             "is larger than maximum delay (%" GST_TIME_FORMAT ")",
199             GST_TIME_ARGS (delay), GST_TIME_ARGS (max_delay));
200         self->delay = max_delay;
201       } else {
202         self->delay = delay;
203         self->max_delay = MAX (delay, max_delay);
204       }
205       rate = GST_AUDIO_FILTER_RATE (self);
206       if (rate > 0)
207         self->delay_frames =
208             MAX (gst_util_uint64_scale (self->delay, rate, GST_SECOND), 1);
209
210       g_mutex_unlock (&self->lock);
211       break;
212     }
213     case PROP_MAX_DELAY:{
214       guint64 max_delay, delay;
215
216       g_mutex_lock (&self->lock);
217       max_delay = g_value_get_uint64 (value);
218       delay = self->delay;
219
220       if (GST_STATE (self) > GST_STATE_READY) {
221         GST_ERROR_OBJECT (self, "Can't change maximum delay in"
222             " PLAYING or PAUSED state");
223       } else {
224         self->delay = delay;
225         self->max_delay = max_delay;
226       }
227       g_mutex_unlock (&self->lock);
228       break;
229     }
230     case PROP_INTENSITY:{
231       g_mutex_lock (&self->lock);
232       self->intensity = g_value_get_float (value);
233       g_mutex_unlock (&self->lock);
234       break;
235     }
236     case PROP_FEEDBACK:{
237       g_mutex_lock (&self->lock);
238       self->feedback = g_value_get_float (value);
239       g_mutex_unlock (&self->lock);
240       break;
241     }
242     default:
243       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244       break;
245   }
246 }
247
248 static void
249 gst_audio_echo_get_property (GObject * object, guint prop_id,
250     GValue * value, GParamSpec * pspec)
251 {
252   GstAudioEcho *self = GST_AUDIO_ECHO (object);
253
254   switch (prop_id) {
255     case PROP_DELAY:
256       g_mutex_lock (&self->lock);
257       g_value_set_uint64 (value, self->delay);
258       g_mutex_unlock (&self->lock);
259       break;
260     case PROP_MAX_DELAY:
261       g_mutex_lock (&self->lock);
262       g_value_set_uint64 (value, self->max_delay);
263       g_mutex_unlock (&self->lock);
264       break;
265     case PROP_INTENSITY:
266       g_mutex_lock (&self->lock);
267       g_value_set_float (value, self->intensity);
268       g_mutex_unlock (&self->lock);
269       break;
270     case PROP_FEEDBACK:
271       g_mutex_lock (&self->lock);
272       g_value_set_float (value, self->feedback);
273       g_mutex_unlock (&self->lock);
274       break;
275     default:
276       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277       break;
278   }
279 }
280
281 /* GstAudioFilter vmethod implementations */
282
283 static gboolean
284 gst_audio_echo_setup (GstAudioFilter * base, const GstAudioInfo * info)
285 {
286   GstAudioEcho *self = GST_AUDIO_ECHO (base);
287   gboolean ret = TRUE;
288
289   switch (GST_AUDIO_INFO_FORMAT (info)) {
290     case GST_AUDIO_FORMAT_F32:
291       self->process = (GstAudioEchoProcessFunc)
292           gst_audio_echo_transform_float;
293       break;
294     case GST_AUDIO_FORMAT_F64:
295       self->process = (GstAudioEchoProcessFunc)
296           gst_audio_echo_transform_double;
297       break;
298     default:
299       ret = FALSE;
300       break;
301   }
302
303   g_free (self->buffer);
304   self->buffer = NULL;
305   self->buffer_pos = 0;
306   self->buffer_size = 0;
307   self->buffer_size_frames = 0;
308
309   return ret;
310 }
311
312 static gboolean
313 gst_audio_echo_stop (GstBaseTransform * base)
314 {
315   GstAudioEcho *self = GST_AUDIO_ECHO (base);
316
317   g_free (self->buffer);
318   self->buffer = NULL;
319   self->buffer_pos = 0;
320   self->buffer_size = 0;
321   self->buffer_size_frames = 0;
322
323   return TRUE;
324 }
325
326 #define TRANSFORM_FUNC(name, type) \
327 static void \
328 gst_audio_echo_transform_##name (GstAudioEcho * self, \
329     type * data, guint num_samples) \
330 { \
331   type *buffer = (type *) self->buffer; \
332   guint channels = GST_AUDIO_FILTER_CHANNELS (self); \
333   guint rate = GST_AUDIO_FILTER_RATE (self); \
334   guint i, j; \
335   guint echo_index = self->buffer_size_frames - self->delay_frames; \
336   gdouble echo_off = ((((gdouble) self->delay) * rate) / GST_SECOND) - self->delay_frames; \
337   \
338   if (echo_off < 0.0) \
339     echo_off = 0.0; \
340   \
341   num_samples /= channels; \
342   \
343   for (i = 0; i < num_samples; i++) { \
344     guint echo0_index = ((echo_index + self->buffer_pos) % self->buffer_size_frames) * channels; \
345     guint echo1_index = ((echo_index + self->buffer_pos +1) % self->buffer_size_frames) * channels; \
346     guint rbout_index = (self->buffer_pos % self->buffer_size_frames) * channels; \
347     for (j = 0; j < channels; j++) { \
348       gdouble in = data[i*channels + j]; \
349       gdouble echo0 = buffer[echo0_index + j]; \
350       gdouble echo1 = buffer[echo1_index + j]; \
351       gdouble echo = echo0 + (echo1-echo0)*echo_off; \
352       type out = in + self->intensity * echo; \
353       \
354       data[i*channels + j] = out; \
355       \
356       buffer[rbout_index + j] = in + self->feedback * echo; \
357     } \
358     self->buffer_pos = (self->buffer_pos + 1) % self->buffer_size_frames; \
359   } \
360 }
361
362 TRANSFORM_FUNC (float, gfloat);
363 TRANSFORM_FUNC (double, gdouble);
364
365 /* GstBaseTransform vmethod implementations */
366 static GstFlowReturn
367 gst_audio_echo_transform_ip (GstBaseTransform * base, GstBuffer * buf)
368 {
369   GstAudioEcho *self = GST_AUDIO_ECHO (base);
370   guint num_samples;
371   GstClockTime timestamp, stream_time;
372   GstMapInfo map;
373
374   g_mutex_lock (&self->lock);
375   timestamp = GST_BUFFER_TIMESTAMP (buf);
376   stream_time =
377       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
378
379   GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT,
380       GST_TIME_ARGS (timestamp));
381
382   if (GST_CLOCK_TIME_IS_VALID (stream_time))
383     gst_object_sync_values (GST_OBJECT (self), stream_time);
384
385   if (self->buffer == NULL) {
386     guint bpf, rate;
387
388     bpf = GST_AUDIO_FILTER_BPF (self);
389     rate = GST_AUDIO_FILTER_RATE (self);
390
391     self->delay_frames =
392         MAX (gst_util_uint64_scale (self->delay, rate, GST_SECOND), 1);
393     self->buffer_size_frames =
394         MAX (gst_util_uint64_scale (self->max_delay, rate, GST_SECOND), 1);
395
396     self->buffer_size = self->buffer_size_frames * bpf;
397     self->buffer = g_try_malloc0 (self->buffer_size);
398     self->buffer_pos = 0;
399
400     if (self->buffer == NULL) {
401       g_mutex_unlock (&self->lock);
402       GST_ERROR_OBJECT (self, "Failed to allocate %u bytes", self->buffer_size);
403       return GST_FLOW_ERROR;
404     }
405   }
406
407   gst_buffer_map (buf, &map, GST_MAP_READWRITE);
408   num_samples = map.size / GST_AUDIO_FILTER_BPS (self);
409
410   self->process (self, map.data, num_samples);
411
412   gst_buffer_unmap (buf, &map);
413   g_mutex_unlock (&self->lock);
414
415   return GST_FLOW_OK;
416 }