• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
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  * Freeverb
22  *
23  * Written by Jezar at Dreampoint, June 2000
24  * http://www.dreampoint.co.uk
25  * This code is public domain
26  *
27  * Translated to C by Peter Hanappe, Mai 2001
28  * Transformed into a GStreamer plugin by Stefan Sauer, Nov 2011
29  */
30 
31 /**
32  * SECTION:element-freeverb
33  * @title: freeverb
34  *
35  * Reverberation/room effect.
36  *
37  * ## Example launch line
38  * |[
39  * gst-launch-1.0 audiotestsrc wave=saw ! freeverb ! autoaudiosink
40  * gst-launch-1.0 filesrc location="melo1.ogg" ! decodebin ! audioconvert ! freeverb ! autoaudiosink
41  * ]|
42  *
43  */
44 
45 /* FIXME:
46  * - add mono-to-mono, then we might also need stereo-to-mono ?
47  */
48 
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 
53 #include <math.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include <gst/gst.h>
58 #include <gst/base/gstbasetransform.h>
59 
60 #include "gstfreeverb.h"
61 
62 #define GST_CAT_DEFAULT gst_freeverb_debug
63 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
64 
65 enum
66 {
67   PROP_0,
68   PROP_ROOM_SIZE,
69   PROP_DAMPING,
70   PROP_PAN_WIDTH,
71   PROP_LEVEL
72 };
73 
74 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
75     GST_PAD_SINK,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS ("audio/x-raw, "
78         "format = (string) { " GST_AUDIO_NE (F32) ", " GST_AUDIO_NE (S16) "}, "
79         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ], "
80         "layout = (string) interleaved")
81     );
82 
83 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
84     GST_PAD_SRC,
85     GST_PAD_ALWAYS,
86     GST_STATIC_CAPS ("audio/x-raw, "
87         "format = (string) { " GST_AUDIO_NE (F32) ", " GST_AUDIO_NE (S16) "}, "
88         "rate = (int) [ 1, MAX ], " "channels = (int) 2, "
89         "layout = (string) interleaved")
90     );
91 
92 static void gst_freeverb_set_property (GObject * object, guint prop_id,
93     const GValue * value, GParamSpec * pspec);
94 static void gst_freeverb_get_property (GObject * object, guint prop_id,
95     GValue * value, GParamSpec * pspec);
96 
97 static void gst_freeverb_finalize (GObject * object);
98 
99 static gboolean gst_freeverb_get_unit_size (GstBaseTransform * base,
100     GstCaps * caps, gsize * size);
101 static GstCaps *gst_freeverb_transform_caps (GstBaseTransform * base,
102     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
103 static gboolean gst_freeverb_set_caps (GstBaseTransform * base,
104     GstCaps * incaps, GstCaps * outcaps);
105 
106 static GstFlowReturn gst_freeverb_transform (GstBaseTransform * base,
107     GstBuffer * inbuf, GstBuffer * outbuf);
108 
109 static gboolean gst_freeverb_transform_m2s_int (GstFreeverb * filter,
110     gint16 * idata, gint16 * odata, guint num_samples);
111 static gboolean gst_freeverb_transform_s2s_int (GstFreeverb * filter,
112     gint16 * idata, gint16 * odata, guint num_samples);
113 static gboolean gst_freeverb_transform_m2s_float (GstFreeverb * filter,
114     gfloat * idata, gfloat * odata, guint num_samples);
115 static gboolean gst_freeverb_transform_s2s_float (GstFreeverb * filter,
116     gfloat * idata, gfloat * odata, guint num_samples);
117 
118 
119 /* Table with processing functions: [channels][format] */
120 static const GstFreeverbProcessFunc process_functions[2][2] = {
121   {
122         (GstFreeverbProcessFunc) gst_freeverb_transform_m2s_int,
123         (GstFreeverbProcessFunc) gst_freeverb_transform_m2s_float,
124       },
125   {
126         (GstFreeverbProcessFunc) gst_freeverb_transform_s2s_int,
127         (GstFreeverbProcessFunc) gst_freeverb_transform_s2s_float,
128       }
129 };
130 
131 /***************************************************************
132  *
133  *                           REVERB
134  */
135 
136 /* Denormalising:
137  *
138  * Another method fixes the problem cheaper: Use a small DC-offset in
139  * the filter calculations.  Now the signals converge not against 0,
140  * but against the offset.  The constant offset is invisible from the
141  * outside world (i.e. it does not appear at the output.  There is a
142  * very small turn-on transient response, which should not cause
143  * problems.
144  */
145 
146 //#define DC_OFFSET 0
147 #define DC_OFFSET 1e-8
148 //#define DC_OFFSET 0.001f
149 
150 /* all pass filter */
151 
152 typedef struct _freeverb_allpass
153 {
154   gfloat feedback;
155   gfloat *buffer;
156   gint bufsize;
157   gint bufidx;
158 } freeverb_allpass;
159 
160 static void
freeverb_allpass_setbuffer(freeverb_allpass * allpass,gint size)161 freeverb_allpass_setbuffer (freeverb_allpass * allpass, gint size)
162 {
163   allpass->bufidx = 0;
164   allpass->buffer = g_new (gfloat, size);
165   allpass->bufsize = size;
166 }
167 
168 static void
freeverb_allpass_release(freeverb_allpass * allpass)169 freeverb_allpass_release (freeverb_allpass * allpass)
170 {
171   g_free (allpass->buffer);
172 }
173 
174 static void
freeverb_allpass_init(freeverb_allpass * allpass)175 freeverb_allpass_init (freeverb_allpass * allpass)
176 {
177   gint i, len = allpass->bufsize;
178   gfloat *buf = allpass->buffer;
179 
180   for (i = 0; i < len; i++) {
181     buf[i] = (gfloat) DC_OFFSET;        /* this is not 100 % correct. */
182   }
183 }
184 
185 static void
freeverb_allpass_setfeedback(freeverb_allpass * allpass,gfloat val)186 freeverb_allpass_setfeedback (freeverb_allpass * allpass, gfloat val)
187 {
188   allpass->feedback = val;
189 }
190 
191 /*
192 static gfloat
193 freeverb_allpass_getfeedback(freeverb_allpass* allpass)
194 {
195   return allpass->feedback;
196 }*/
197 
198 #define freeverb_allpass_process(_allpass, _input_1) \
199 { \
200   gfloat output; \
201   gfloat bufout; \
202   bufout = _allpass.buffer[_allpass.bufidx]; \
203   output = bufout-_input_1; \
204   _allpass.buffer[_allpass.bufidx] = _input_1 + (bufout * _allpass.feedback); \
205   if (++_allpass.bufidx >= _allpass.bufsize) { \
206     _allpass.bufidx = 0; \
207   } \
208   _input_1 = output; \
209 }
210 
211 /* comb filter */
212 
213 typedef struct _freeverb_comb
214 {
215   gfloat feedback;
216   gfloat filterstore;
217   gfloat damp1;
218   gfloat damp2;
219   gfloat *buffer;
220   gint bufsize;
221   gint bufidx;
222 } freeverb_comb;
223 
224 static void
freeverb_comb_setbuffer(freeverb_comb * comb,gint size)225 freeverb_comb_setbuffer (freeverb_comb * comb, gint size)
226 {
227   comb->filterstore = 0;
228   comb->bufidx = 0;
229   comb->buffer = g_new (gfloat, size);
230   comb->bufsize = size;
231 }
232 
233 static void
freeverb_comb_release(freeverb_comb * comb)234 freeverb_comb_release (freeverb_comb * comb)
235 {
236   g_free (comb->buffer);
237 }
238 
239 static void
freeverb_comb_init(freeverb_comb * comb)240 freeverb_comb_init (freeverb_comb * comb)
241 {
242   gint i, len = comb->bufsize;
243   gfloat *buf = comb->buffer;
244 
245   for (i = 0; i < len; i++) {
246     buf[i] = (gfloat) DC_OFFSET;        /* This is not 100 % correct. */
247   }
248 }
249 
250 static void
freeverb_comb_setdamp(freeverb_comb * comb,gfloat val)251 freeverb_comb_setdamp (freeverb_comb * comb, gfloat val)
252 {
253   comb->damp1 = val;
254   comb->damp2 = 1 - val;
255 }
256 
257 /*
258 static gfloat
259 freeverb_comb_getdamp(freeverb_comb* comb)
260 {
261   return comb->damp1;
262 }*/
263 
264 static void
freeverb_comb_setfeedback(freeverb_comb * comb,gfloat val)265 freeverb_comb_setfeedback (freeverb_comb * comb, gfloat val)
266 {
267   comb->feedback = val;
268 }
269 
270 /*
271 static gfloat
272 freeverb_comb_getfeedback(freeverb_comb* comb)
273 {
274   return comb->feedback;
275 }*/
276 
277 #define freeverb_comb_process(_comb, _input_1, _output) \
278 { \
279   gfloat _tmp = _comb.buffer[_comb.bufidx]; \
280   _comb.filterstore = (_tmp * _comb.damp2) + (_comb.filterstore * _comb.damp1); \
281   _comb.buffer[_comb.bufidx] = _input_1 + (_comb.filterstore * _comb.feedback); \
282   if (++_comb.bufidx >= _comb.bufsize) { \
283     _comb.bufidx = 0; \
284   } \
285   _output += _tmp; \
286 }
287 
288 #define numcombs 8
289 #define numallpasses 4
290 #define	fixedgain 0.015f
291 #define scalewet 1.0f
292 #define scaledry 1.0f
293 #define scaledamp 1.0f
294 #define scaleroom 0.28f
295 #define offsetroom 0.7f
296 #define stereospread 23
297 
298 /* These values assume 44.1KHz sample rate
299  * they will need scaling for 96KHz (or other) sample rates.
300  * The values were obtained by listening tests.
301  */
302 #define combtuningL1 1116
303 #define combtuningR1 (1116 + stereospread)
304 #define combtuningL2 1188
305 #define combtuningR2 (1188 + stereospread)
306 #define combtuningL3 1277
307 #define combtuningR3 (1277 + stereospread)
308 #define combtuningL4 1356
309 #define combtuningR4 (1356 + stereospread)
310 #define combtuningL5 1422
311 #define combtuningR5 (1422 + stereospread)
312 #define combtuningL6 1491
313 #define combtuningR6 (1491 + stereospread)
314 #define combtuningL7 1557
315 #define combtuningR7 (1557 + stereospread)
316 #define combtuningL8 1617
317 #define combtuningR8 (1617 + stereospread)
318 #define allpasstuningL1 556
319 #define allpasstuningR1 (556 + stereospread)
320 #define allpasstuningL2 441
321 #define allpasstuningR2 (441 + stereospread)
322 #define allpasstuningL3 341
323 #define allpasstuningR3 (341 + stereospread)
324 #define allpasstuningL4 225
325 #define allpasstuningR4 (225 + stereospread)
326 
327 struct _GstFreeverbPrivate
328 {
329   gfloat roomsize;
330   gfloat damp;
331   gfloat wet, wet1, wet2, dry;
332   gfloat width;
333   gfloat gain;
334   /*
335      The following are all declared inline
336      to remove the need for dynamic allocation
337      with its subsequent error-checking messiness
338    */
339   /* Comb filters */
340   freeverb_comb combL[numcombs];
341   freeverb_comb combR[numcombs];
342   /* Allpass filters */
343   freeverb_allpass allpassL[numallpasses];
344   freeverb_allpass allpassR[numallpasses];
345 };
346 
347 G_DEFINE_TYPE_WITH_CODE (GstFreeverb, gst_freeverb, GST_TYPE_BASE_TRANSFORM,
348     G_ADD_PRIVATE (GstFreeverb)
349     G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL));
350 GST_ELEMENT_REGISTER_DEFINE (freeverb, "freeverb",
351     GST_RANK_NONE, GST_TYPE_FREEVERB);
352 
353 static void
freeverb_revmodel_init(GstFreeverb * filter)354 freeverb_revmodel_init (GstFreeverb * filter)
355 {
356   GstFreeverbPrivate *priv = filter->priv;
357   gint i;
358 
359   for (i = 0; i < numcombs; i++) {
360     freeverb_comb_init (&priv->combL[i]);
361     freeverb_comb_init (&priv->combR[i]);
362   }
363   for (i = 0; i < numallpasses; i++) {
364     freeverb_allpass_init (&priv->allpassL[i]);
365     freeverb_allpass_init (&priv->allpassR[i]);
366   }
367 }
368 
369 static void
freeverb_revmodel_free(GstFreeverb * filter)370 freeverb_revmodel_free (GstFreeverb * filter)
371 {
372   GstFreeverbPrivate *priv = filter->priv;
373   gint i;
374 
375   for (i = 0; i < numcombs; i++) {
376     freeverb_comb_release (&priv->combL[i]);
377     freeverb_comb_release (&priv->combR[i]);
378   }
379   for (i = 0; i < numallpasses; i++) {
380     freeverb_allpass_release (&priv->allpassL[i]);
381     freeverb_allpass_release (&priv->allpassR[i]);
382   }
383 }
384 
385 /* GObject vmethod implementations */
386 
387 static void
gst_freeverb_class_init(GstFreeverbClass * klass)388 gst_freeverb_class_init (GstFreeverbClass * klass)
389 {
390   GObjectClass *gobject_class;
391   GstElementClass *element_class;
392 
393   GST_DEBUG_CATEGORY_INIT (gst_freeverb_debug, "freeverb", 0,
394       "freeverb element");
395 
396   gobject_class = (GObjectClass *) klass;
397   element_class = (GstElementClass *) klass;
398 
399   gobject_class->set_property = gst_freeverb_set_property;
400   gobject_class->get_property = gst_freeverb_get_property;
401   gobject_class->finalize = gst_freeverb_finalize;
402 
403   g_object_class_install_property (gobject_class, PROP_ROOM_SIZE,
404       g_param_spec_float ("room-size", "Room size",
405           "Size of the simulated room", 0.0, 1.0, 0.5,
406           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE |
407           G_PARAM_STATIC_STRINGS));
408   g_object_class_install_property (gobject_class, PROP_DAMPING,
409       g_param_spec_float ("damping", "Damping", "Damping of high frequencies",
410           0.0, 1.0, 0.2f,
411           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE |
412           G_PARAM_STATIC_STRINGS));
413   g_object_class_install_property (gobject_class, PROP_PAN_WIDTH,
414       g_param_spec_float ("width", "Width", "Stereo panorama width", 0.0, 1.0,
415           1.0,
416           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE |
417           G_PARAM_STATIC_STRINGS));
418   g_object_class_install_property (gobject_class, PROP_LEVEL,
419       g_param_spec_float ("level", "Level", "dry/wet level", 0.0, 1.0, 0.5,
420           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE |
421           G_PARAM_STATIC_STRINGS));
422 
423   gst_element_class_set_static_metadata (element_class,
424       "Reverberation/room effect", "Filter/Effect/Audio",
425       "Add reverberation to audio streams",
426       "Stefan Sauer <ensonic@users.sf.net>");
427 
428   gst_element_class_add_static_pad_template (element_class, &src_template);
429   gst_element_class_add_static_pad_template (element_class, &sink_template);
430 
431   GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
432       GST_DEBUG_FUNCPTR (gst_freeverb_get_unit_size);
433   GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
434       GST_DEBUG_FUNCPTR (gst_freeverb_transform_caps);
435   GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
436       GST_DEBUG_FUNCPTR (gst_freeverb_set_caps);
437   GST_BASE_TRANSFORM_CLASS (klass)->transform =
438       GST_DEBUG_FUNCPTR (gst_freeverb_transform);
439 }
440 
441 static void
gst_freeverb_init(GstFreeverb * filter)442 gst_freeverb_init (GstFreeverb * filter)
443 {
444   filter->priv = gst_freeverb_get_instance_private (filter);
445 
446   gst_audio_info_init (&filter->info);
447   filter->process = NULL;
448 
449   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
450 
451   freeverb_revmodel_init (filter);
452 }
453 
454 static void
gst_freeverb_finalize(GObject * object)455 gst_freeverb_finalize (GObject * object)
456 {
457   GstFreeverb *filter = GST_FREEVERB (object);
458 
459   freeverb_revmodel_free (filter);
460 
461   G_OBJECT_CLASS (gst_freeverb_parent_class)->finalize (object);
462 }
463 
464 static gboolean
gst_freeverb_set_process_function(GstFreeverb * filter,GstAudioInfo * info)465 gst_freeverb_set_process_function (GstFreeverb * filter, GstAudioInfo * info)
466 {
467   gint channel_index, format_index;
468   const GstAudioFormatInfo *finfo = info->finfo;
469 
470   /* set processing function */
471   channel_index = GST_AUDIO_INFO_CHANNELS (info) - 1;
472   if (channel_index > 1 || channel_index < 0) {
473     filter->process = NULL;
474     return FALSE;
475   }
476 
477   format_index = GST_AUDIO_FORMAT_INFO_IS_FLOAT (finfo) ? 1 : 0;
478 
479   filter->process = process_functions[channel_index][format_index];
480   return TRUE;
481 }
482 
483 static void
gst_freeverb_init_rev_model(GstFreeverb * filter)484 gst_freeverb_init_rev_model (GstFreeverb * filter)
485 {
486   gfloat srfactor = GST_AUDIO_INFO_RATE (&filter->info) / 44100.0f;
487   GstFreeverbPrivate *priv = filter->priv;
488 
489   freeverb_revmodel_free (filter);
490 
491   priv->gain = fixedgain;
492 
493   freeverb_comb_setbuffer (&priv->combL[0], combtuningL1 * srfactor);
494   freeverb_comb_setbuffer (&priv->combR[0], combtuningR1 * srfactor);
495   freeverb_comb_setbuffer (&priv->combL[1], combtuningL2 * srfactor);
496   freeverb_comb_setbuffer (&priv->combR[1], combtuningR2 * srfactor);
497   freeverb_comb_setbuffer (&priv->combL[2], combtuningL3 * srfactor);
498   freeverb_comb_setbuffer (&priv->combR[2], combtuningR3 * srfactor);
499   freeverb_comb_setbuffer (&priv->combL[3], combtuningL4 * srfactor);
500   freeverb_comb_setbuffer (&priv->combR[3], combtuningR4 * srfactor);
501   freeverb_comb_setbuffer (&priv->combL[4], combtuningL5 * srfactor);
502   freeverb_comb_setbuffer (&priv->combR[4], combtuningR5 * srfactor);
503   freeverb_comb_setbuffer (&priv->combL[5], combtuningL6 * srfactor);
504   freeverb_comb_setbuffer (&priv->combR[5], combtuningR6 * srfactor);
505   freeverb_comb_setbuffer (&priv->combL[6], combtuningL7 * srfactor);
506   freeverb_comb_setbuffer (&priv->combR[6], combtuningR7 * srfactor);
507   freeverb_comb_setbuffer (&priv->combL[7], combtuningL8 * srfactor);
508   freeverb_comb_setbuffer (&priv->combR[7], combtuningR8 * srfactor);
509   freeverb_allpass_setbuffer (&priv->allpassL[0], allpasstuningL1 * srfactor);
510   freeverb_allpass_setbuffer (&priv->allpassR[0], allpasstuningR1 * srfactor);
511   freeverb_allpass_setbuffer (&priv->allpassL[1], allpasstuningL2 * srfactor);
512   freeverb_allpass_setbuffer (&priv->allpassR[1], allpasstuningR2 * srfactor);
513   freeverb_allpass_setbuffer (&priv->allpassL[2], allpasstuningL3 * srfactor);
514   freeverb_allpass_setbuffer (&priv->allpassR[2], allpasstuningR3 * srfactor);
515   freeverb_allpass_setbuffer (&priv->allpassL[3], allpasstuningL4 * srfactor);
516   freeverb_allpass_setbuffer (&priv->allpassR[3], allpasstuningR4 * srfactor);
517 
518   /* clear buffers */
519   freeverb_revmodel_init (filter);
520 
521   /* set default values */
522   freeverb_allpass_setfeedback (&priv->allpassL[0], 0.5f);
523   freeverb_allpass_setfeedback (&priv->allpassR[0], 0.5f);
524   freeverb_allpass_setfeedback (&priv->allpassL[1], 0.5f);
525   freeverb_allpass_setfeedback (&priv->allpassR[1], 0.5f);
526   freeverb_allpass_setfeedback (&priv->allpassL[2], 0.5f);
527   freeverb_allpass_setfeedback (&priv->allpassR[2], 0.5f);
528   freeverb_allpass_setfeedback (&priv->allpassL[3], 0.5f);
529   freeverb_allpass_setfeedback (&priv->allpassR[3], 0.5f);
530 }
531 
532 static void
gst_freeverb_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)533 gst_freeverb_set_property (GObject * object, guint prop_id,
534     const GValue * value, GParamSpec * pspec)
535 {
536   GstFreeverb *filter = GST_FREEVERB (object);
537   GstFreeverbPrivate *priv = filter->priv;
538   gint i;
539 
540   switch (prop_id) {
541     case PROP_ROOM_SIZE:
542       filter->room_size = g_value_get_float (value);
543       priv->roomsize = (filter->room_size * scaleroom) + offsetroom;
544       for (i = 0; i < numcombs; i++) {
545         freeverb_comb_setfeedback (&priv->combL[i], priv->roomsize);
546         freeverb_comb_setfeedback (&priv->combR[i], priv->roomsize);
547       }
548       break;
549     case PROP_DAMPING:
550       filter->damping = g_value_get_float (value);
551       priv->damp = filter->damping * scaledamp;
552       for (i = 0; i < numcombs; i++) {
553         freeverb_comb_setdamp (&priv->combL[i], priv->damp);
554         freeverb_comb_setdamp (&priv->combR[i], priv->damp);
555       }
556       break;
557     case PROP_PAN_WIDTH:
558       filter->pan_width = g_value_get_float (value);
559       priv->width = filter->pan_width;
560       priv->wet1 = priv->wet * (priv->width / 2.0f + 0.5f);
561       priv->wet2 = priv->wet * ((1.0f - priv->width) / 2.0f);
562       break;
563     case PROP_LEVEL:
564       filter->level = g_value_get_float (value);
565       priv->wet = filter->level * scalewet;
566       priv->dry = (1.0 - filter->level) * scaledry;
567       priv->wet1 = priv->wet * (priv->width / 2.0f + 0.5f);
568       priv->wet2 = priv->wet * ((1.0f - priv->width) / 2.0f);
569       break;
570     default:
571       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
572       break;
573   }
574 }
575 
576 static void
gst_freeverb_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)577 gst_freeverb_get_property (GObject * object, guint prop_id,
578     GValue * value, GParamSpec * pspec)
579 {
580   GstFreeverb *filter = GST_FREEVERB (object);
581 
582   switch (prop_id) {
583     case PROP_ROOM_SIZE:
584       g_value_set_float (value, filter->room_size);
585       break;
586     case PROP_DAMPING:
587       g_value_set_float (value, filter->damping);
588       break;
589     case PROP_PAN_WIDTH:
590       g_value_set_float (value, filter->pan_width);
591       break;
592     case PROP_LEVEL:
593       g_value_set_float (value, filter->level);
594       break;
595     default:
596       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
597       break;
598   }
599 }
600 
601 /* GstBaseTransform vmethod implementations */
602 
603 static gboolean
gst_freeverb_get_unit_size(GstBaseTransform * base,GstCaps * caps,gsize * size)604 gst_freeverb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
605     gsize * size)
606 {
607   GstAudioInfo info;
608 
609   g_assert (size);
610 
611   if (!gst_audio_info_from_caps (&info, caps))
612     return FALSE;
613 
614   *size = GST_AUDIO_INFO_BPF (&info);
615 
616   GST_INFO_OBJECT (base, "unit size: %" G_GSIZE_FORMAT, *size);
617 
618   return TRUE;
619 }
620 
621 static GstCaps *
gst_freeverb_transform_caps(GstBaseTransform * base,GstPadDirection direction,GstCaps * caps,GstCaps * filter)622 gst_freeverb_transform_caps (GstBaseTransform * base,
623     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
624 {
625   GstCaps *res;
626   GstStructure *structure;
627   gint i;
628 
629   /* replace the channel property with our range. */
630   res = gst_caps_copy (caps);
631   for (i = 0; i < gst_caps_get_size (res); i++) {
632     structure = gst_caps_get_structure (res, i);
633     if (direction == GST_PAD_SRC) {
634       GST_INFO_OBJECT (base, "[%d] allow 1-2 channels", i);
635       gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
636     } else {
637       GST_INFO_OBJECT (base, "[%d] allow 2 channels", i);
638       gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
639     }
640     gst_structure_remove_field (structure, "channel-mask");
641   }
642   GST_DEBUG_OBJECT (base, "transformed %" GST_PTR_FORMAT, res);
643 
644   if (filter) {
645     GstCaps *intersection;
646 
647     GST_DEBUG_OBJECT (base, "Using filter caps %" GST_PTR_FORMAT, filter);
648     intersection =
649         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
650     gst_caps_unref (res);
651     res = intersection;
652     GST_DEBUG_OBJECT (base, "Intersection %" GST_PTR_FORMAT, res);
653   }
654 
655   return res;
656 }
657 
658 static gboolean
gst_freeverb_set_caps(GstBaseTransform * base,GstCaps * incaps,GstCaps * outcaps)659 gst_freeverb_set_caps (GstBaseTransform * base, GstCaps * incaps,
660     GstCaps * outcaps)
661 {
662   GstFreeverb *filter = GST_FREEVERB (base);
663   GstAudioInfo info;
664 
665   /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
666   if (!gst_audio_info_from_caps (&info, incaps))
667     goto no_format;
668 
669   GST_DEBUG ("try to process %d input with %d channels",
670       GST_AUDIO_INFO_FORMAT (&info), GST_AUDIO_INFO_CHANNELS (&info));
671 
672   if (!gst_freeverb_set_process_function (filter, &info))
673     goto no_format;
674 
675   filter->info = info;
676 
677   gst_freeverb_init_rev_model (filter);
678   filter->drained = FALSE;
679   GST_INFO_OBJECT (base, "model configured");
680 
681   return TRUE;
682 
683 no_format:
684   {
685     GST_DEBUG ("invalid caps");
686     return FALSE;
687   }
688 }
689 
690 static gboolean
gst_freeverb_transform_m2s_int(GstFreeverb * filter,gint16 * idata,gint16 * odata,guint num_samples)691 gst_freeverb_transform_m2s_int (GstFreeverb * filter,
692     gint16 * idata, gint16 * odata, guint num_samples)
693 {
694   GstFreeverbPrivate *priv = filter->priv;
695   gint i, k;
696   gfloat out_l1, out_r1, input_1;
697   gfloat out_l2, out_r2, input_2;
698   gboolean drained = TRUE;
699 
700   for (k = 0; k < num_samples; k++) {
701     out_l1 = out_r1 = 0.0;
702 
703     /* The original Freeverb code expects a stereo signal and 'input_1'
704      * is set to the sum of the left and right input_1 sample. Since
705      * this code works on a mono signal, 'input_1' is set to twice the
706      * input_1 sample. */
707     input_2 = (gfloat) * idata++;
708     input_1 = (2.0f * input_2 + DC_OFFSET) * priv->gain;
709 
710     /* Accumulate comb filters in parallel */
711     for (i = 0; i < numcombs; i++) {
712       freeverb_comb_process (priv->combL[i], input_1, out_l1);
713       freeverb_comb_process (priv->combR[i], input_1, out_r1);
714     }
715     /* Feed through allpasses in series */
716     for (i = 0; i < numallpasses; i++) {
717       freeverb_allpass_process (priv->allpassL[i], out_l1);
718       freeverb_allpass_process (priv->allpassR[i], out_r1);
719     }
720 
721     /* Remove the DC offset */
722     out_l1 -= (gfloat) DC_OFFSET;
723     out_r1 -= (gfloat) DC_OFFSET;
724 
725     /* Calculate output */
726     out_l2 = out_l1 * priv->wet1 + out_r1 * priv->wet2 + input_2 * priv->dry;
727     out_r2 = out_r1 * priv->wet1 + out_l1 * priv->wet2 + input_2 * priv->dry;
728     out_l2 = CLAMP (out_l2, G_MININT16, G_MAXINT16);
729     out_r2 = CLAMP (out_r2, G_MININT16, G_MAXINT16);
730     *odata++ = (gint16) out_l2;
731     *odata++ = (gint16) out_r2;
732 
733     if (abs ((gint16) out_l2) > 0 || abs ((gint16) out_r2) > 0)
734       drained = FALSE;
735   }
736   return drained;
737 }
738 
739 static gboolean
gst_freeverb_transform_s2s_int(GstFreeverb * filter,gint16 * idata,gint16 * odata,guint num_samples)740 gst_freeverb_transform_s2s_int (GstFreeverb * filter,
741     gint16 * idata, gint16 * odata, guint num_samples)
742 {
743   GstFreeverbPrivate *priv = filter->priv;
744   gint i, k;
745   gfloat out_l1, out_r1, input_1l, input_1r;
746   gfloat out_l2, out_r2, input_2l, input_2r;
747   gboolean drained = TRUE;
748 
749   for (k = 0; k < num_samples; k++) {
750     out_l1 = out_r1 = 0.0;
751 
752     input_2l = (gfloat) * idata++;
753     input_2r = (gfloat) * idata++;
754     input_1l = (input_2l + DC_OFFSET) * priv->gain;
755     input_1r = (input_2r + DC_OFFSET) * priv->gain;
756 
757     /* Accumulate comb filters in parallel */
758     for (i = 0; i < numcombs; i++) {
759       freeverb_comb_process (priv->combL[i], input_1l, out_l1);
760       freeverb_comb_process (priv->combR[i], input_1r, out_r1);
761     }
762     /* Feed through allpasses in series */
763     for (i = 0; i < numallpasses; i++) {
764       freeverb_allpass_process (priv->allpassL[i], out_l1);
765       freeverb_allpass_process (priv->allpassR[i], out_r1);
766     }
767 
768     /* Remove the DC offset */
769     out_l1 -= (gfloat) DC_OFFSET;
770     out_r1 -= (gfloat) DC_OFFSET;
771 
772     /* Calculate output */
773     out_l2 = out_l1 * priv->wet1 + out_r1 * priv->wet2 + input_2l * priv->dry;
774     out_r2 = out_r1 * priv->wet1 + out_l1 * priv->wet2 + input_2r * priv->dry;
775     out_l2 = CLAMP (out_l2, G_MININT16, G_MAXINT16);
776     out_r2 = CLAMP (out_r2, G_MININT16, G_MAXINT16);
777     *odata++ = (gint16) out_l2;
778     *odata++ = (gint16) out_r2;
779 
780     if (abs ((gint16) out_l2) > 0 || abs ((gint16) out_r2) > 0)
781       drained = FALSE;
782   }
783   return drained;
784 }
785 
786 static gboolean
gst_freeverb_transform_m2s_float(GstFreeverb * filter,gfloat * idata,gfloat * odata,guint num_samples)787 gst_freeverb_transform_m2s_float (GstFreeverb * filter,
788     gfloat * idata, gfloat * odata, guint num_samples)
789 {
790   GstFreeverbPrivate *priv = filter->priv;
791   gint i, k;
792   gfloat out_l1, out_r1, input_1;
793   gfloat out_l2, out_r2, input_2;
794   gboolean drained = TRUE;
795 
796   for (k = 0; k < num_samples; k++) {
797     out_l1 = out_r1 = 0.0;
798 
799     /* The original Freeverb code expects a stereo signal and 'input_1'
800      * is set to the sum of the left and right input_1 sample. Since
801      * this code works on a mono signal, 'input_1' is set to twice the
802      * input_1 sample. */
803     input_2 = *idata++;
804     input_1 = (2.0f * input_2 + DC_OFFSET) * priv->gain;
805 
806     /* Accumulate comb filters in parallel */
807     for (i = 0; i < numcombs; i++) {
808       freeverb_comb_process (priv->combL[i], input_1, out_l1);
809       freeverb_comb_process (priv->combR[i], input_1, out_r1);
810     }
811     /* Feed through allpasses in series */
812     for (i = 0; i < numallpasses; i++) {
813       freeverb_allpass_process (priv->allpassL[i], out_l1);
814       freeverb_allpass_process (priv->allpassR[i], out_r1);
815     }
816 
817     /* Remove the DC offset */
818     out_l1 -= (gfloat) DC_OFFSET;
819     out_r1 -= (gfloat) DC_OFFSET;
820 
821     /* Calculate output */
822     out_l2 = out_l1 * priv->wet1 + out_r1 * priv->wet2 + input_2 * priv->dry;
823     out_r2 = out_r1 * priv->wet1 + out_l1 * priv->wet2 + input_2 * priv->dry;
824     *odata++ = out_l2;
825     *odata++ = out_r2;
826 
827     if (fabs (out_l2) > 0 || fabs (out_r2) > 0)
828       drained = FALSE;
829   }
830   return drained;
831 }
832 
833 static gboolean
gst_freeverb_transform_s2s_float(GstFreeverb * filter,gfloat * idata,gfloat * odata,guint num_samples)834 gst_freeverb_transform_s2s_float (GstFreeverb * filter,
835     gfloat * idata, gfloat * odata, guint num_samples)
836 {
837   GstFreeverbPrivate *priv = filter->priv;
838   gint i, k;
839   gfloat out_l1, out_r1, input_1l, input_1r;
840   gfloat out_l2, out_r2, input_2l, input_2r;
841   gboolean drained = TRUE;
842 
843   for (k = 0; k < num_samples; k++) {
844     out_l1 = out_r1 = 0.0;
845 
846     input_2l = *idata++;
847     input_2r = *idata++;
848     input_1l = (input_2l + DC_OFFSET) * priv->gain;
849     input_1r = (input_2r + DC_OFFSET) * priv->gain;
850 
851     /* Accumulate comb filters in parallel */
852     for (i = 0; i < numcombs; i++) {
853       freeverb_comb_process (priv->combL[i], input_1l, out_l1);
854       freeverb_comb_process (priv->combR[i], input_1r, out_r1);
855     }
856     /* Feed through allpasses in series */
857     for (i = 0; i < numallpasses; i++) {
858       freeverb_allpass_process (priv->allpassL[i], out_l1);
859       freeverb_allpass_process (priv->allpassR[i], out_r1);
860     }
861 
862     /* Remove the DC offset */
863     out_l1 -= (gfloat) DC_OFFSET;
864     out_r1 -= (gfloat) DC_OFFSET;
865 
866     /* Calculate output */
867     out_l2 = out_l1 * priv->wet1 + out_r1 * priv->wet2 + input_2l * priv->dry;
868     out_r2 = out_r1 * priv->wet1 + out_l1 * priv->wet2 + input_2r * priv->dry;
869     *odata++ = out_l2;
870     *odata++ = out_r2;
871 
872     if (fabs (out_l2) > 0 || fabs (out_r2) > 0)
873       drained = FALSE;
874   }
875   return drained;
876 }
877 
878 /* this function does the actual processing
879  */
880 static GstFlowReturn
gst_freeverb_transform(GstBaseTransform * base,GstBuffer * inbuf,GstBuffer * outbuf)881 gst_freeverb_transform (GstBaseTransform * base, GstBuffer * inbuf,
882     GstBuffer * outbuf)
883 {
884   GstFreeverb *filter = GST_FREEVERB (base);
885   guint num_samples;
886   GstClockTime timestamp;
887   GstMapInfo inmap, outmap;
888 
889   timestamp = GST_BUFFER_TIMESTAMP (inbuf);
890   timestamp =
891       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
892 
893   gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
894   gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
895   num_samples = outmap.size / (2 * GST_AUDIO_INFO_BPS (&filter->info));
896 
897   GST_DEBUG_OBJECT (filter, "processing %u samples at %" GST_TIME_FORMAT,
898       num_samples, GST_TIME_ARGS (timestamp));
899 
900   if (GST_CLOCK_TIME_IS_VALID (timestamp))
901     gst_object_sync_values (GST_OBJECT (filter), timestamp);
902 
903   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DISCONT))) {
904     filter->drained = FALSE;
905   }
906   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))) {
907     if (filter->drained) {
908       memset (outmap.data, 0, outmap.size);
909     }
910   } else {
911     filter->drained = FALSE;
912   }
913 
914   if (!filter->drained) {
915     filter->drained =
916         filter->process (filter, inmap.data, outmap.data, num_samples);
917   }
918 
919   if (filter->drained) {
920     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
921   }
922 
923   gst_buffer_unmap (inbuf, &inmap);
924   gst_buffer_unmap (outbuf, &outmap);
925 
926   return GST_FLOW_OK;
927 }
928 
929 
930 static gboolean
plugin_init(GstPlugin * plugin)931 plugin_init (GstPlugin * plugin)
932 {
933   return GST_ELEMENT_REGISTER (freeverb, plugin);
934 }
935 
936 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
937     GST_VERSION_MINOR,
938     freeverb,
939     "Reverberation/room effect",
940     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
941