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