1 /* GStreamer
2  * Copyright (C) 2005 Stefan Kost <ensonic@users.sf.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /**
20  * SECTION:element-audiotestsrc
21  * @title: audiotestsrc
22  *
23  * AudioTestSrc can be used to generate basic audio signals. It support several
24  * different waveforms and allows to set the base frequency and volume. Some
25  * waveforms might use additional properties.
26  *
27  * Waveform specific notes:
28  *
29  * <orderedlist>
30  *   <listitem>
31  *     <itemizedlist><title>Gaussian white noise</title>
32  *
33  *     This waveform produces white (zero mean) Gaussian noise.
34  *     Volume sets the standard deviation of the noise in units of the range
35  *     of values of the sample type, e.g. volume=0.1 produces noise with a
36  *     standard deviation of 0.1*32767=3277 with 16-bit integer samples,
37  *     or 0.1*1.0=0.1 with floating-point samples.
38  *
39  *     </itemizedlist>
40  *   </listitem>
41  *   <listitem>
42  *     <itemizedlist><title>Ticks</title>
43  *
44  *     This waveform is special in that it does not produce one continuous
45  *     signal. Instead, it produces finite-length sine wave pulses (the "ticks").
46  *     It is useful for detecting time shifts between audio signal, for example
47  *     between RTSP audio clients that shall play synchronized. It is also useful
48  *     for generating a signal that feeds the trigger of an oscilloscope.
49  *
50  *     To further help with oscilloscope triggering and time offset detection,
51  *     the waveform can apply a different volume to every Nth tick (this is then
52  *     called the "marker tick"). For instance, one could generate a tick every
53  *     100ms, and make every 20th tick a marker tick (meaning that every 2 seconds
54  *     there is a marker tick). This is useful for detecting large time offsets
55  *     while still frequently triggering an oscilloscope.
56  *
57  *     Also, a "ramp" can be applied to the begin & end of ticks. The sudden
58  *     start of the sine tick is a discontinuity, even if the sine wave starts
59  *     at 0. The* resulting artifacts can often make it more difficult to use the
60  *     ticks for an oscilloscope's trigger. To that end, an initial "ramp" can
61  *     be applied. The first few samples are modulated by a cubic function to
62  *     reduce the impact of the discontinuity, resulting in smaller artifacts.
63  *     The number of samples equals floor(samplerate / sine-wave-frequency).
64  *     Example: with a sample rate of 48 kHz and a sine wave frequency of 10 kHz,
65  *     the first 4 samples are modulated by the cubic function.
66  *     </itemizedlist>
67  *   </listitem>
68  * </orderedlist>
69  *
70  * ## Example launch line
71  * |[
72  * gst-launch-1.0 audiotestsrc ! audioconvert ! autoaudiosink
73  * ]|
74  *  This pipeline produces a sine with default frequency, 440 Hz, and the
75  * default volume, 0.8 (relative to a maximum 1.0).
76  * |[
77  * gst-launch-1.0 audiotestsrc wave=2 freq=200 ! tee name=t ! queue ! audioconvert ! \
78  *     autoaudiosink t. ! queue ! audioconvert ! libvisual_lv_scope ! videoconvert ! autovideosink
79  * ]|
80  *  In this example a saw wave is generated. The wave is shown using a
81  * scope visualizer from libvisual, allowing you to visually verify that
82  * the saw wave is correct.
83  *
84  * |[
85  * gst-launch-1.0 audiotestsrc wave=ticks apply-tick-ramp=true tick-interval=100000000 \
86  *     freq=10000 volume=0.4 marker-tick-period=10 sine-periods-per-tick=20 ! autoaudiosink
87  * ]| This pipeline produces a series of 10 kHz sine wave ticks. Each tick is
88  * 20 sine wave periods long, ticks occur every 100 ms and have a volume of
89  * 0.4. Every 10th tick is a marker tick and has the default marker tick volume
90  * of 1.0. The beginning and end of the ticks are modulated with the ramp.
91  */
92 
93 #ifdef HAVE_CONFIG_H
94 #include "config.h"
95 #endif
96 
97 #include <math.h>
98 #include <stdlib.h>
99 #include <string.h>
100 
101 #include "gstaudiotestsrc.h"
102 
103 
104 #define M_PI_M2 ( G_PI + G_PI )
105 
106 GST_DEBUG_CATEGORY_STATIC (audio_test_src_debug);
107 #define GST_CAT_DEFAULT audio_test_src_debug
108 
109 #define DEFAULT_SAMPLES_PER_BUFFER      1024
110 #define DEFAULT_WAVE                    GST_AUDIO_TEST_SRC_WAVE_SINE
111 #define DEFAULT_FREQ                    440.0
112 #define DEFAULT_VOLUME                  0.8
113 #define DEFAULT_IS_LIVE                 FALSE
114 #define DEFAULT_TIMESTAMP_OFFSET        G_GINT64_CONSTANT (0)
115 #define DEFAULT_SINE_PERIODS_PER_TICK   10
116 #define DEFAULT_TIME_BETWEEN_TICKS      GST_SECOND
117 #define DEFAULT_MARKER_TICK_PERIOD    0
118 #define DEFAULT_MARKER_TICK_VOLUME      1.0
119 #define DEFAULT_APPLY_TICK_RAMP         FALSE
120 #define DEFAULT_CAN_ACTIVATE_PUSH       TRUE
121 #define DEFAULT_CAN_ACTIVATE_PULL       FALSE
122 
123 enum
124 {
125   PROP_0,
126   PROP_SAMPLES_PER_BUFFER,
127   PROP_WAVE,
128   PROP_FREQ,
129   PROP_VOLUME,
130   PROP_IS_LIVE,
131   PROP_TIMESTAMP_OFFSET,
132   PROP_SINE_PERIODS_PER_TICK,
133   PROP_TICK_INTERVAL,
134   PROP_MARKER_TICK_PERIOD,
135   PROP_MARKER_TICK_VOLUME,
136   PROP_APPLY_TICK_RAMP,
137   PROP_CAN_ACTIVATE_PUSH,
138   PROP_CAN_ACTIVATE_PULL
139 };
140 
141 #define FORMAT_STR  " { S16LE, S16BE, U16LE, U16BE, " \
142     "S24_32LE, S24_32BE, U24_32LE, U24_32BE, " \
143     "S32LE, S32BE, U32LE, U32BE, " \
144     "S24LE, S24BE, U24LE, U24BE, " \
145     "S20LE, S20BE, U20LE, U20BE, " \
146     "S18LE, S18BE, U18LE, U18BE, " \
147     "F32LE, F32BE, F64LE, F64BE, " \
148     "S8, U8 }"
149 
150 #define DEFAULT_FORMAT_STR GST_AUDIO_NE ("S16")
151 
152 static GstStaticPadTemplate gst_audio_test_src_src_template =
153 GST_STATIC_PAD_TEMPLATE ("src",
154     GST_PAD_SRC,
155     GST_PAD_ALWAYS,
156     GST_STATIC_CAPS ("audio/x-raw, "
157         "format = (string) " FORMAT_STR ", "
158         "layout = (string) { interleaved, non-interleaved }, "
159         "rate = " GST_AUDIO_RATE_RANGE ", "
160         "channels = " GST_AUDIO_CHANNELS_RANGE)
161     );
162 
163 #define gst_audio_test_src_parent_class parent_class
164 G_DEFINE_TYPE (GstAudioTestSrc, gst_audio_test_src, GST_TYPE_BASE_SRC);
165 
166 #define GST_TYPE_AUDIO_TEST_SRC_WAVE (gst_audiostestsrc_wave_get_type())
167 static GType
gst_audiostestsrc_wave_get_type(void)168 gst_audiostestsrc_wave_get_type (void)
169 {
170   static GType audiostestsrc_wave_type = 0;
171   static const GEnumValue audiostestsrc_waves[] = {
172     {GST_AUDIO_TEST_SRC_WAVE_SINE, "Sine", "sine"},
173     {GST_AUDIO_TEST_SRC_WAVE_SQUARE, "Square", "square"},
174     {GST_AUDIO_TEST_SRC_WAVE_SAW, "Saw", "saw"},
175     {GST_AUDIO_TEST_SRC_WAVE_TRIANGLE, "Triangle", "triangle"},
176     {GST_AUDIO_TEST_SRC_WAVE_SILENCE, "Silence", "silence"},
177     {GST_AUDIO_TEST_SRC_WAVE_WHITE_NOISE, "White uniform noise", "white-noise"},
178     {GST_AUDIO_TEST_SRC_WAVE_PINK_NOISE, "Pink noise", "pink-noise"},
179     {GST_AUDIO_TEST_SRC_WAVE_SINE_TAB, "Sine table", "sine-table"},
180     {GST_AUDIO_TEST_SRC_WAVE_TICKS, "Periodic Ticks", "ticks"},
181     {GST_AUDIO_TEST_SRC_WAVE_GAUSSIAN_WHITE_NOISE, "White Gaussian noise",
182         "gaussian-noise"},
183     {GST_AUDIO_TEST_SRC_WAVE_RED_NOISE, "Red (brownian) noise", "red-noise"},
184     {GST_AUDIO_TEST_SRC_WAVE_BLUE_NOISE, "Blue noise", "blue-noise"},
185     {GST_AUDIO_TEST_SRC_WAVE_VIOLET_NOISE, "Violet noise", "violet-noise"},
186     {0, NULL, NULL},
187   };
188 
189   if (G_UNLIKELY (audiostestsrc_wave_type == 0)) {
190     audiostestsrc_wave_type = g_enum_register_static ("GstAudioTestSrcWave",
191         audiostestsrc_waves);
192   }
193   return audiostestsrc_wave_type;
194 }
195 
196 static void gst_audio_test_src_finalize (GObject * object);
197 
198 static void gst_audio_test_src_set_property (GObject * object,
199     guint prop_id, const GValue * value, GParamSpec * pspec);
200 static void gst_audio_test_src_get_property (GObject * object,
201     guint prop_id, GValue * value, GParamSpec * pspec);
202 
203 static gboolean gst_audio_test_src_setcaps (GstBaseSrc * basesrc,
204     GstCaps * caps);
205 static GstCaps *gst_audio_test_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
206 
207 static gboolean gst_audio_test_src_is_seekable (GstBaseSrc * basesrc);
208 static gboolean gst_audio_test_src_do_seek (GstBaseSrc * basesrc,
209     GstSegment * segment);
210 static gboolean gst_audio_test_src_query (GstBaseSrc * basesrc,
211     GstQuery * query);
212 
213 static void gst_audio_test_src_change_wave (GstAudioTestSrc * src);
214 
215 static void gst_audio_test_src_get_times (GstBaseSrc * basesrc,
216     GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
217 static gboolean gst_audio_test_src_start (GstBaseSrc * basesrc);
218 static gboolean gst_audio_test_src_stop (GstBaseSrc * basesrc);
219 static GstFlowReturn gst_audio_test_src_fill (GstBaseSrc * basesrc,
220     guint64 offset, guint length, GstBuffer * buffer);
221 
222 static void
gst_audio_test_src_class_init(GstAudioTestSrcClass * klass)223 gst_audio_test_src_class_init (GstAudioTestSrcClass * klass)
224 {
225   GObjectClass *gobject_class;
226   GstElementClass *gstelement_class;
227   GstBaseSrcClass *gstbasesrc_class;
228 
229   gobject_class = (GObjectClass *) klass;
230   gstelement_class = (GstElementClass *) klass;
231   gstbasesrc_class = (GstBaseSrcClass *) klass;
232 
233   gobject_class->set_property = gst_audio_test_src_set_property;
234   gobject_class->get_property = gst_audio_test_src_get_property;
235   gobject_class->finalize = gst_audio_test_src_finalize;
236 
237   g_object_class_install_property (gobject_class, PROP_SAMPLES_PER_BUFFER,
238       g_param_spec_int ("samplesperbuffer", "Samples per buffer",
239           "Number of samples in each outgoing buffer",
240           1, G_MAXINT, DEFAULT_SAMPLES_PER_BUFFER,
241           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242   g_object_class_install_property (gobject_class, PROP_WAVE,
243       g_param_spec_enum ("wave", "Waveform", "Oscillator waveform",
244           GST_TYPE_AUDIO_TEST_SRC_WAVE, GST_AUDIO_TEST_SRC_WAVE_SINE,
245           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
246   g_object_class_install_property (gobject_class, PROP_FREQ,
247       g_param_spec_double ("freq", "Frequency", "Frequency of test signal. "
248           "The sample rate needs to be at least 4 times higher.",
249           0.0, (gdouble) G_MAXINT / 4, DEFAULT_FREQ,
250           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
251   g_object_class_install_property (gobject_class, PROP_VOLUME,
252       g_param_spec_double ("volume", "Volume", "Volume of test signal", 0.0,
253           1.0, DEFAULT_VOLUME,
254           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
255   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
256       g_param_spec_boolean ("is-live", "Is Live",
257           "Whether to act as a live source", DEFAULT_IS_LIVE,
258           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259   g_object_class_install_property (G_OBJECT_CLASS (klass),
260       PROP_TIMESTAMP_OFFSET, g_param_spec_int64 ("timestamp-offset",
261           "Timestamp offset",
262           "An offset added to timestamps set on buffers (in ns)", G_MININT64,
263           G_MAXINT64, DEFAULT_TIMESTAMP_OFFSET,
264           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265   g_object_class_install_property (gobject_class, PROP_SINE_PERIODS_PER_TICK,
266       g_param_spec_uint ("sine-periods-per-tick", "Sine periods per tick",
267           "Number of sine wave periods in one tick. Only used if wave = ticks.",
268           1, G_MAXUINT, DEFAULT_SINE_PERIODS_PER_TICK,
269           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270   g_object_class_install_property (gobject_class, PROP_TICK_INTERVAL,
271       g_param_spec_uint64 ("tick-interval", "Time between ticks",
272           "Distance between start of current and start of next tick, in nanoseconds.",
273           1, G_MAXUINT64, DEFAULT_TIME_BETWEEN_TICKS,
274           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275   g_object_class_install_property (gobject_class, PROP_MARKER_TICK_PERIOD,
276       g_param_spec_uint ("marker-tick-period", "Marker tick period",
277           "Make every Nth tick a marker tick (= a tick with different volume). "
278           "Only used if wave = ticks. 0 = no marker ticks.",
279           0, G_MAXUINT, DEFAULT_MARKER_TICK_PERIOD,
280           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281   g_object_class_install_property (gobject_class, PROP_MARKER_TICK_VOLUME,
282       g_param_spec_double ("marker-tick-volume", "Marker tick volume",
283           "Volume of marker ticks. Only used if wave = ticks and"
284           "marker-tick-period is set to a nonzero value.",
285           0.0, 1.0, DEFAULT_MARKER_TICK_VOLUME,
286           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287   g_object_class_install_property (gobject_class, PROP_APPLY_TICK_RAMP,
288       g_param_spec_boolean ("apply-tick-ramp", "Apply tick ramp",
289           "Apply ramp to tick samples", DEFAULT_APPLY_TICK_RAMP,
290           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291   g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PUSH,
292       g_param_spec_boolean ("can-activate-push", "Can activate push",
293           "Can activate in push mode", DEFAULT_CAN_ACTIVATE_PUSH,
294           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295   g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PULL,
296       g_param_spec_boolean ("can-activate-pull", "Can activate pull",
297           "Can activate in pull mode", DEFAULT_CAN_ACTIVATE_PULL,
298           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 
300   gst_element_class_add_static_pad_template (gstelement_class,
301       &gst_audio_test_src_src_template);
302   gst_element_class_set_static_metadata (gstelement_class, "Audio test source",
303       "Source/Audio",
304       "Creates audio test signals of given frequency and volume",
305       "Stefan Kost <ensonic@users.sf.net>");
306 
307   gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_test_src_setcaps);
308   gstbasesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_audio_test_src_fixate);
309   gstbasesrc_class->is_seekable =
310       GST_DEBUG_FUNCPTR (gst_audio_test_src_is_seekable);
311   gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_audio_test_src_do_seek);
312   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_audio_test_src_query);
313   gstbasesrc_class->get_times =
314       GST_DEBUG_FUNCPTR (gst_audio_test_src_get_times);
315   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_audio_test_src_start);
316   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_audio_test_src_stop);
317   gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_audio_test_src_fill);
318 }
319 
320 static void
gst_audio_test_src_init(GstAudioTestSrc * src)321 gst_audio_test_src_init (GstAudioTestSrc * src)
322 {
323   src->volume = DEFAULT_VOLUME;
324   src->freq = DEFAULT_FREQ;
325 
326   /* we operate in time */
327   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
328   gst_base_src_set_live (GST_BASE_SRC (src), DEFAULT_IS_LIVE);
329 
330   src->samples_per_buffer = DEFAULT_SAMPLES_PER_BUFFER;
331   src->generate_samples_per_buffer = src->samples_per_buffer;
332   src->timestamp_offset = DEFAULT_TIMESTAMP_OFFSET;
333   src->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
334 
335   src->sine_periods_per_tick = DEFAULT_SINE_PERIODS_PER_TICK;
336   src->tick_interval = DEFAULT_TIME_BETWEEN_TICKS;
337   src->marker_tick_period = DEFAULT_MARKER_TICK_PERIOD;
338   src->marker_tick_volume = DEFAULT_MARKER_TICK_VOLUME;
339   src->apply_tick_ramp = DEFAULT_APPLY_TICK_RAMP;
340 
341   src->gen = NULL;
342 
343   src->wave = DEFAULT_WAVE;
344   gst_base_src_set_blocksize (GST_BASE_SRC (src), -1);
345 }
346 
347 static void
gst_audio_test_src_finalize(GObject * object)348 gst_audio_test_src_finalize (GObject * object)
349 {
350   GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (object);
351 
352   if (src->gen)
353     g_rand_free (src->gen);
354   src->gen = NULL;
355   g_free (src->tmp);
356   src->tmp = NULL;
357   src->tmpsize = 0;
358 
359   G_OBJECT_CLASS (parent_class)->finalize (object);
360 }
361 
362 static GstCaps *
gst_audio_test_src_fixate(GstBaseSrc * bsrc,GstCaps * caps)363 gst_audio_test_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
364 {
365   GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (bsrc);
366   GstStructure *structure;
367   gint channels, rate;
368 
369   caps = gst_caps_make_writable (caps);
370 
371   structure = gst_caps_get_structure (caps, 0);
372 
373   GST_DEBUG_OBJECT (src, "fixating samplerate to %d", GST_AUDIO_DEF_RATE);
374 
375   rate = MAX (GST_AUDIO_DEF_RATE, src->freq * 4);
376   gst_structure_fixate_field_nearest_int (structure, "rate", rate);
377 
378   gst_structure_fixate_field_string (structure, "format", DEFAULT_FORMAT_STR);
379 
380   gst_structure_fixate_field_string (structure, "layout", "interleaved");
381 
382   /* fixate to mono unless downstream requires stereo, for backwards compat */
383   gst_structure_fixate_field_nearest_int (structure, "channels", 1);
384 
385   if (gst_structure_get_int (structure, "channels", &channels) && channels > 2) {
386     if (!gst_structure_has_field_typed (structure, "channel-mask",
387             GST_TYPE_BITMASK))
388       gst_structure_set (structure, "channel-mask", GST_TYPE_BITMASK, 0ULL,
389           NULL);
390   }
391 
392   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
393 
394   return caps;
395 }
396 
397 static gboolean
gst_audio_test_src_setcaps(GstBaseSrc * basesrc,GstCaps * caps)398 gst_audio_test_src_setcaps (GstBaseSrc * basesrc, GstCaps * caps)
399 {
400   GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (basesrc);
401   GstAudioInfo info;
402 
403   if (!gst_audio_info_from_caps (&info, caps))
404     goto invalid_caps;
405 
406   GST_DEBUG_OBJECT (src, "negotiated to caps %" GST_PTR_FORMAT, caps);
407 
408   src->info = info;
409 
410   gst_base_src_set_blocksize (basesrc,
411       GST_AUDIO_INFO_BPF (&info) * src->samples_per_buffer);
412   gst_audio_test_src_change_wave (src);
413 
414   return TRUE;
415 
416   /* ERROR */
417 invalid_caps:
418   {
419     GST_ERROR_OBJECT (basesrc, "received invalid caps");
420     return FALSE;
421   }
422 }
423 
424 static gboolean
gst_audio_test_src_query(GstBaseSrc * basesrc,GstQuery * query)425 gst_audio_test_src_query (GstBaseSrc * basesrc, GstQuery * query)
426 {
427   GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (basesrc);
428   gboolean res = FALSE;
429 
430   switch (GST_QUERY_TYPE (query)) {
431     case GST_QUERY_CONVERT:
432     {
433       GstFormat src_fmt, dest_fmt;
434       gint64 src_val, dest_val;
435 
436       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
437 
438       if (!gst_audio_info_convert (&src->info, src_fmt, src_val, dest_fmt,
439               &dest_val))
440         goto error;
441 
442       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
443       res = TRUE;
444       break;
445     }
446     case GST_QUERY_SCHEDULING:
447     {
448       /* if we can operate in pull mode */
449       gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
450       gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
451       if (src->can_activate_pull)
452         gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
453 
454       res = TRUE;
455       break;
456     }
457     case GST_QUERY_LATENCY:
458     {
459       if (src->info.rate > 0) {
460         GstClockTime latency;
461 
462         latency =
463             gst_util_uint64_scale (src->generate_samples_per_buffer, GST_SECOND,
464             src->info.rate);
465         gst_query_set_latency (query,
466             gst_base_src_is_live (GST_BASE_SRC_CAST (src)), latency,
467             GST_CLOCK_TIME_NONE);
468         GST_DEBUG_OBJECT (src, "Reporting latency of %" GST_TIME_FORMAT,
469             GST_TIME_ARGS (latency));
470         res = TRUE;
471       }
472       break;
473     }
474     default:
475       res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
476       break;
477   }
478 
479   return res;
480   /* ERROR */
481 error:
482   {
483     GST_DEBUG_OBJECT (src, "query failed");
484     return FALSE;
485   }
486 }
487 
488 #define DEFINE_SINE(type,scale) \
489 static void \
490 gst_audio_test_src_create_sine_##type (GstAudioTestSrc * src, g##type * samples) \
491 { \
492   gint i, c, channels, channel_step, sample_step; \
493   gdouble step, amp; \
494   g##type *ptr; \
495   \
496   channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
497   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
498     channel_step = 1; \
499     sample_step = channels; \
500   } else { \
501     channel_step = src->generate_samples_per_buffer; \
502     sample_step = 1; \
503   } \
504   step = M_PI_M2 * src->freq / GST_AUDIO_INFO_RATE (&src->info); \
505   amp = src->volume * scale; \
506   \
507   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
508     src->accumulator += step; \
509     if (src->accumulator >= M_PI_M2) \
510       src->accumulator -= M_PI_M2; \
511     \
512     ptr = samples; \
513     for (c = 0; c < channels; ++c) { \
514       *ptr = (g##type) (sin (src->accumulator) * amp); \
515       ptr += channel_step; \
516     } \
517     samples += sample_step; \
518   } \
519 }
520 
521 DEFINE_SINE (int16, 32767.0);
522 DEFINE_SINE (int32, 2147483647.0);
523 DEFINE_SINE (float, 1.0);
524 DEFINE_SINE (double, 1.0);
525 
526 static const ProcessFunc sine_funcs[] = {
527   (ProcessFunc) gst_audio_test_src_create_sine_int16,
528   (ProcessFunc) gst_audio_test_src_create_sine_int32,
529   (ProcessFunc) gst_audio_test_src_create_sine_float,
530   (ProcessFunc) gst_audio_test_src_create_sine_double
531 };
532 
533 #define DEFINE_SQUARE(type,scale) \
534 static void \
535 gst_audio_test_src_create_square_##type (GstAudioTestSrc * src, g##type * samples) \
536 { \
537   gint i, c, channels, channel_step, sample_step; \
538   gdouble step, amp; \
539   g##type *ptr; \
540   \
541   channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
542   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
543     channel_step = 1; \
544     sample_step = channels; \
545   } else { \
546     channel_step = src->generate_samples_per_buffer; \
547     sample_step = 1; \
548   } \
549   step = M_PI_M2 * src->freq / GST_AUDIO_INFO_RATE (&src->info); \
550   amp = src->volume * scale; \
551   \
552   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
553     src->accumulator += step; \
554     if (src->accumulator >= M_PI_M2) \
555       src->accumulator -= M_PI_M2; \
556     \
557     ptr = samples; \
558     for (c = 0; c < channels; ++c) { \
559       *ptr = (g##type) ((src->accumulator < G_PI) ? amp : -amp); \
560       ptr += channel_step; \
561     } \
562     samples += sample_step; \
563   } \
564 }
565 
566 DEFINE_SQUARE (int16, 32767.0);
567 DEFINE_SQUARE (int32, 2147483647.0);
568 DEFINE_SQUARE (float, 1.0);
569 DEFINE_SQUARE (double, 1.0);
570 
571 static const ProcessFunc square_funcs[] = {
572   (ProcessFunc) gst_audio_test_src_create_square_int16,
573   (ProcessFunc) gst_audio_test_src_create_square_int32,
574   (ProcessFunc) gst_audio_test_src_create_square_float,
575   (ProcessFunc) gst_audio_test_src_create_square_double
576 };
577 
578 #define DEFINE_SAW(type,scale) \
579 static void \
580 gst_audio_test_src_create_saw_##type (GstAudioTestSrc * src, g##type * samples) \
581 { \
582   gint i, c, channels, channel_step, sample_step; \
583   gdouble step, amp; \
584   g##type *ptr; \
585   \
586   channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
587   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
588     channel_step = 1; \
589     sample_step = channels; \
590   } else { \
591     channel_step = src->generate_samples_per_buffer; \
592     sample_step = 1; \
593   } \
594   step = M_PI_M2 * src->freq / GST_AUDIO_INFO_RATE (&src->info); \
595   amp = (src->volume * scale) / G_PI; \
596   \
597   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
598     src->accumulator += step; \
599     if (src->accumulator >= M_PI_M2) \
600       src->accumulator -= M_PI_M2; \
601     \
602     ptr = samples; \
603     if (src->accumulator < G_PI) { \
604       for (c = 0; c < channels; ++c) { \
605         *ptr = (g##type) (src->accumulator * amp); \
606         ptr += channel_step; \
607       } \
608     } else { \
609       for (c = 0; c < channels; ++c) { \
610         *ptr = (g##type) ((M_PI_M2 - src->accumulator) * -amp); \
611         ptr += channel_step; \
612       } \
613     } \
614     samples += sample_step; \
615   } \
616 }
617 
618 DEFINE_SAW (int16, 32767.0);
619 DEFINE_SAW (int32, 2147483647.0);
620 DEFINE_SAW (float, 1.0);
621 DEFINE_SAW (double, 1.0);
622 
623 static const ProcessFunc saw_funcs[] = {
624   (ProcessFunc) gst_audio_test_src_create_saw_int16,
625   (ProcessFunc) gst_audio_test_src_create_saw_int32,
626   (ProcessFunc) gst_audio_test_src_create_saw_float,
627   (ProcessFunc) gst_audio_test_src_create_saw_double
628 };
629 
630 #define DEFINE_TRIANGLE(type,scale) \
631 static void \
632 gst_audio_test_src_create_triangle_##type (GstAudioTestSrc * src, g##type * samples) \
633 { \
634   gint i, c, channels, channel_step, sample_step; \
635   gdouble step, amp; \
636   g##type *ptr; \
637   \
638   channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
639   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
640     channel_step = 1; \
641     sample_step = channels; \
642   } else { \
643     channel_step = src->generate_samples_per_buffer; \
644     sample_step = 1; \
645   } \
646   step = M_PI_M2 * src->freq / GST_AUDIO_INFO_RATE (&src->info); \
647   amp = (src->volume * scale) / G_PI_2; \
648   \
649   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
650     src->accumulator += step; \
651     if (src->accumulator >= M_PI_M2) \
652       src->accumulator -= M_PI_M2; \
653     \
654     ptr = samples; \
655     if (src->accumulator < (G_PI_2)) { \
656       for (c = 0; c < channels; ++c) { \
657         *ptr = (g##type) (src->accumulator * amp); \
658         ptr += channel_step; \
659       } \
660     } else if (src->accumulator < (G_PI * 1.5)) { \
661       for (c = 0; c < channels; ++c) { \
662         *ptr = (g##type) ((src->accumulator - G_PI) * -amp); \
663         ptr += channel_step; \
664       } \
665     } else { \
666       for (c = 0; c < channels; ++c) { \
667         *ptr = (g##type) ((M_PI_M2 - src->accumulator) * -amp); \
668         ptr += channel_step; \
669       } \
670     } \
671     samples += sample_step; \
672   } \
673 }
674 
675 DEFINE_TRIANGLE (int16, 32767.0);
676 DEFINE_TRIANGLE (int32, 2147483647.0);
677 DEFINE_TRIANGLE (float, 1.0);
678 DEFINE_TRIANGLE (double, 1.0);
679 
680 static const ProcessFunc triangle_funcs[] = {
681   (ProcessFunc) gst_audio_test_src_create_triangle_int16,
682   (ProcessFunc) gst_audio_test_src_create_triangle_int32,
683   (ProcessFunc) gst_audio_test_src_create_triangle_float,
684   (ProcessFunc) gst_audio_test_src_create_triangle_double
685 };
686 
687 #define DEFINE_SILENCE(type) \
688 static void \
689 gst_audio_test_src_create_silence_##type (GstAudioTestSrc * src, g##type * samples) \
690 { \
691   memset (samples, 0, src->generate_samples_per_buffer * sizeof (g##type) * src->info.channels); \
692 }
693 
694 DEFINE_SILENCE (int16);
695 DEFINE_SILENCE (int32);
696 DEFINE_SILENCE (float);
697 DEFINE_SILENCE (double);
698 
699 static const ProcessFunc silence_funcs[] = {
700   (ProcessFunc) gst_audio_test_src_create_silence_int16,
701   (ProcessFunc) gst_audio_test_src_create_silence_int32,
702   (ProcessFunc) gst_audio_test_src_create_silence_float,
703   (ProcessFunc) gst_audio_test_src_create_silence_double
704 };
705 
706 #define DEFINE_WHITE_NOISE(type,scale) \
707 static void \
708 gst_audio_test_src_create_white_noise_##type (GstAudioTestSrc * src, g##type * samples) \
709 { \
710   gint i, c, channel_step, sample_step; \
711   g##type *ptr; \
712   gdouble amp = (src->volume * scale); \
713   gint channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
714   \
715   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
716     channel_step = 1; \
717     sample_step = channels; \
718   } else { \
719     channel_step = src->generate_samples_per_buffer; \
720     sample_step = 1; \
721   } \
722   \
723   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
724     ptr = samples; \
725     for (c = 0; c < channels; ++c) { \
726       *ptr = (g##type) (amp * g_rand_double_range (src->gen, -1.0, 1.0)); \
727       ptr += channel_step; \
728     } \
729     samples += sample_step; \
730   } \
731 }
732 
733 DEFINE_WHITE_NOISE (int16, 32767.0);
734 DEFINE_WHITE_NOISE (int32, 2147483647.0);
735 DEFINE_WHITE_NOISE (float, 1.0);
736 DEFINE_WHITE_NOISE (double, 1.0);
737 
738 static const ProcessFunc white_noise_funcs[] = {
739   (ProcessFunc) gst_audio_test_src_create_white_noise_int16,
740   (ProcessFunc) gst_audio_test_src_create_white_noise_int32,
741   (ProcessFunc) gst_audio_test_src_create_white_noise_float,
742   (ProcessFunc) gst_audio_test_src_create_white_noise_double
743 };
744 
745 /* pink noise calculation is based on
746  * http://www.firstpr.com.au/dsp/pink-noise/phil_burk_19990905_patest_pink.c
747  * which has been released under public domain
748  * Many thanks Phil!
749  */
750 static void
gst_audio_test_src_init_pink_noise(GstAudioTestSrc * src)751 gst_audio_test_src_init_pink_noise (GstAudioTestSrc * src)
752 {
753   gint i;
754   gint num_rows = 12;           /* arbitrary: 1 .. PINK_MAX_RANDOM_ROWS */
755   glong pmax;
756 
757   src->pink.index = 0;
758   src->pink.index_mask = (1 << num_rows) - 1;
759   /* calculate maximum possible signed random value.
760    * Extra 1 for white noise always added. */
761   pmax = (num_rows + 1) * (1 << (PINK_RANDOM_BITS - 1));
762   src->pink.scalar = 1.0f / pmax;
763   /* Initialize rows. */
764   for (i = 0; i < num_rows; i++)
765     src->pink.rows[i] = 0;
766   src->pink.running_sum = 0;
767 }
768 
769 /* Generate Pink noise values between -1.0 and +1.0 */
770 static gdouble
gst_audio_test_src_generate_pink_noise_value(GstAudioTestSrc * src)771 gst_audio_test_src_generate_pink_noise_value (GstAudioTestSrc * src)
772 {
773   GstPinkNoise *pink = &src->pink;
774   glong new_random;
775   glong sum;
776 
777   /* Increment and mask index. */
778   pink->index = (pink->index + 1) & pink->index_mask;
779 
780   /* If index is zero, don't update any random values. */
781   if (pink->index != 0) {
782     /* Determine how many trailing zeros in PinkIndex. */
783     /* This algorithm will hang if n==0 so test first. */
784     gint num_zeros = 0;
785     gint n = pink->index;
786 
787     while ((n & 1) == 0) {
788       n = n >> 1;
789       num_zeros++;
790     }
791 
792     /* Replace the indexed ROWS random value.
793      * Subtract and add back to RunningSum instead of adding all the random
794      * values together. Only one changes each time.
795      */
796     pink->running_sum -= pink->rows[num_zeros];
797     new_random = 32768.0 - (65536.0 * (gulong) g_rand_int (src->gen)
798         / (G_MAXUINT32 + 1.0));
799     pink->running_sum += new_random;
800     pink->rows[num_zeros] = new_random;
801   }
802 
803   /* Add extra white noise value. */
804   new_random = 32768.0 - (65536.0 * (gulong) g_rand_int (src->gen)
805       / (G_MAXUINT32 + 1.0));
806   sum = pink->running_sum + new_random;
807 
808   /* Scale to range of -1.0 to 0.9999. */
809   return (pink->scalar * sum);
810 }
811 
812 #define DEFINE_PINK(type, scale) \
813 static void \
814 gst_audio_test_src_create_pink_noise_##type (GstAudioTestSrc * src, g##type * samples) \
815 { \
816   gint i, c, channels, channel_step, sample_step; \
817   gdouble amp; \
818   g##type *ptr; \
819   \
820   amp = src->volume * scale; \
821   channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
822   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
823     channel_step = 1; \
824     sample_step = channels; \
825   } else { \
826     channel_step = src->generate_samples_per_buffer; \
827     sample_step = 1; \
828   } \
829   \
830   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
831     ptr = samples; \
832     for (c = 0; c < channels; ++c) { \
833       *ptr = (g##type) (gst_audio_test_src_generate_pink_noise_value (src) * amp); \
834       ptr += channel_step; \
835     } \
836     samples += sample_step; \
837   } \
838 }
839 
840 DEFINE_PINK (int16, 32767.0);
841 DEFINE_PINK (int32, 2147483647.0);
842 DEFINE_PINK (float, 1.0);
843 DEFINE_PINK (double, 1.0);
844 
845 static const ProcessFunc pink_noise_funcs[] = {
846   (ProcessFunc) gst_audio_test_src_create_pink_noise_int16,
847   (ProcessFunc) gst_audio_test_src_create_pink_noise_int32,
848   (ProcessFunc) gst_audio_test_src_create_pink_noise_float,
849   (ProcessFunc) gst_audio_test_src_create_pink_noise_double
850 };
851 
852 static void
gst_audio_test_src_init_sine_table(GstAudioTestSrc * src,gboolean use_volume)853 gst_audio_test_src_init_sine_table (GstAudioTestSrc * src, gboolean use_volume)
854 {
855   gint i;
856   gdouble ang = 0.0;
857   gdouble step = M_PI_M2 / 1024.0;
858   gdouble amp = use_volume ? src->volume : 1.0;
859 
860   for (i = 0; i < 1024; i++) {
861     src->wave_table[i] = sin (ang) * amp;
862     ang += step;
863   }
864 }
865 
866 #define DEFINE_SINE_TABLE(type,scale) \
867 static void \
868 gst_audio_test_src_create_sine_table_##type (GstAudioTestSrc * src, g##type * samples) \
869 { \
870   gint i, c, channels, channel_step, sample_step; \
871   gdouble step, scl; \
872   g##type *ptr; \
873   \
874   channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
875   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
876     channel_step = 1; \
877     sample_step = channels; \
878   } else { \
879     channel_step = src->generate_samples_per_buffer; \
880     sample_step = 1; \
881   } \
882   step = M_PI_M2 * src->freq / GST_AUDIO_INFO_RATE (&src->info); \
883   scl = 1024.0 / M_PI_M2; \
884   \
885   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
886     src->accumulator += step; \
887     if (src->accumulator >= M_PI_M2) \
888       src->accumulator -= M_PI_M2; \
889     \
890     ptr = samples; \
891     for (c = 0; c < channels; ++c) { \
892       *ptr = (g##type) scale * src->wave_table[(gint) (src->accumulator * scl)]; \
893       ptr += channel_step; \
894     } \
895     samples += sample_step; \
896   } \
897 }
898 
899 DEFINE_SINE_TABLE (int16, 32767.0);
900 DEFINE_SINE_TABLE (int32, 2147483647.0);
901 DEFINE_SINE_TABLE (float, 1.0);
902 DEFINE_SINE_TABLE (double, 1.0);
903 
904 static const ProcessFunc sine_table_funcs[] = {
905   (ProcessFunc) gst_audio_test_src_create_sine_table_int16,
906   (ProcessFunc) gst_audio_test_src_create_sine_table_int32,
907   (ProcessFunc) gst_audio_test_src_create_sine_table_float,
908   (ProcessFunc) gst_audio_test_src_create_sine_table_double
909 };
910 
911 static inline gdouble
calc_scaled_tick_volume(GstAudioTestSrc * src,gdouble scale)912 calc_scaled_tick_volume (GstAudioTestSrc * src, gdouble scale)
913 {
914   gdouble vol;
915   vol = ((src->marker_tick_period > 0)
916       && ((src->tick_counter % src->marker_tick_period) == 0))
917       ? src->marker_tick_volume : src->volume;
918   return vol * scale;
919 }
920 
921 
922 #define DEFINE_TICKS(type,scale) \
923 static void \
924 gst_audio_test_src_create_tick_##type (GstAudioTestSrc * src, g##type * samples) \
925 { \
926   gint i, c, channels, samplerate, samplemod, channel_step, sample_step; \
927   gint num_nonzero_samples, num_ramp_samples, end_ramp_offset; \
928   gdouble step, scl; \
929   gdouble volscale; \
930   g##type *ptr; \
931   \
932   volscale = calc_scaled_tick_volume (src, scale); \
933   channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
934   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
935     channel_step = 1; \
936     sample_step = channels; \
937   } else { \
938     channel_step = src->generate_samples_per_buffer; \
939     sample_step = 1; \
940   } \
941   samplerate = GST_AUDIO_INFO_RATE (&src->info); \
942   step = M_PI_M2 * src->freq / samplerate; \
943   num_nonzero_samples = samplerate * src->sine_periods_per_tick / src->freq; \
944   scl = 1024.0 / M_PI_M2; \
945   num_ramp_samples = src->apply_tick_ramp ? (samplerate / src->freq) : 0; \
946   end_ramp_offset = num_nonzero_samples - num_ramp_samples; \
947   \
948   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
949     samplemod = (src->next_sample + i)%src->samples_between_ticks; \
950     \
951     ptr = samples; \
952     if (samplemod == 0) { \
953       src->accumulator = 0; \
954       src->tick_counter++; \
955       volscale = calc_scaled_tick_volume (src, scale); \
956     } else if (samplemod < num_nonzero_samples)  { \
957       gdouble ramp; \
958       if (num_ramp_samples > 0) { \
959         ramp = \
960             (samplemod < num_ramp_samples) ? (((gdouble)samplemod) / num_ramp_samples) : \
961             (samplemod >= end_ramp_offset) ? (((gdouble)(num_nonzero_samples - samplemod)) / num_ramp_samples) \
962             : 1.0; \
963         if (ramp > 1.0) \
964           ramp = 1.0; \
965         ramp *= ramp * ramp; \
966       } else \
967         ramp = 1.0; \
968       \
969       for (c = 0; c < channels; ++c) { \
970         *ptr = \
971             (g##type) volscale * ramp * src->wave_table[(gint) (src->accumulator * scl)]; \
972         ptr += channel_step; \
973       } \
974     } else { \
975       for (c = 0; c < channels; ++c) { \
976         *ptr = 0; \
977         ptr += channel_step; \
978       } \
979     } \
980     \
981     src->accumulator += step; \
982     if (src->accumulator >= M_PI_M2) \
983       src->accumulator -= M_PI_M2; \
984     \
985     samples += sample_step; \
986   } \
987 }
988 
989 DEFINE_TICKS (int16, 32767.0);
990 DEFINE_TICKS (int32, 2147483647.0);
991 DEFINE_TICKS (float, 1.0);
992 DEFINE_TICKS (double, 1.0);
993 
994 static const ProcessFunc tick_funcs[] = {
995   (ProcessFunc) gst_audio_test_src_create_tick_int16,
996   (ProcessFunc) gst_audio_test_src_create_tick_int32,
997   (ProcessFunc) gst_audio_test_src_create_tick_float,
998   (ProcessFunc) gst_audio_test_src_create_tick_double
999 };
1000 
1001 /* Gaussian white noise using Box-Muller algorithm.  unit variance
1002  * normally-distributed random numbers are generated in pairs as the real
1003  * and imaginary parts of a compex random variable with
1004  * uniformly-distributed argument and \chi^{2}-distributed modulus.
1005  */
1006 
1007 #define DEFINE_GAUSSIAN_WHITE_NOISE(type,scale) \
1008 static void \
1009 gst_audio_test_src_create_gaussian_white_noise_##type (GstAudioTestSrc * src, g##type * samples) \
1010 { \
1011   gint i, c, channel_step, sample_step; \
1012   g##type *ptr; \
1013   gdouble amp = (src->volume * scale); \
1014   gint channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
1015   \
1016   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
1017     channel_step = 1; \
1018     sample_step = channels; \
1019   } else { \
1020     channel_step = src->generate_samples_per_buffer; \
1021     sample_step = 1; \
1022   } \
1023   \
1024   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
1025     ptr = samples; \
1026     for (c = 0; c < channels; ++c) { \
1027       gdouble mag = sqrt (-2 * log (1.0 - g_rand_double (src->gen))); \
1028       gdouble phs = g_rand_double_range (src->gen, 0.0, M_PI_M2); \
1029       \
1030       *ptr = (g##type) (amp * mag * cos (phs)); \
1031       ptr += channel_step; \
1032       if (++c >= channels) \
1033         break; \
1034       *ptr = (g##type) (amp * mag * sin (phs)); \
1035       ptr += channel_step; \
1036     } \
1037     samples += sample_step; \
1038   } \
1039 }
1040 
1041 DEFINE_GAUSSIAN_WHITE_NOISE (int16, 32767.0);
1042 DEFINE_GAUSSIAN_WHITE_NOISE (int32, 2147483647.0);
1043 DEFINE_GAUSSIAN_WHITE_NOISE (float, 1.0);
1044 DEFINE_GAUSSIAN_WHITE_NOISE (double, 1.0);
1045 
1046 static const ProcessFunc gaussian_white_noise_funcs[] = {
1047   (ProcessFunc) gst_audio_test_src_create_gaussian_white_noise_int16,
1048   (ProcessFunc) gst_audio_test_src_create_gaussian_white_noise_int32,
1049   (ProcessFunc) gst_audio_test_src_create_gaussian_white_noise_float,
1050   (ProcessFunc) gst_audio_test_src_create_gaussian_white_noise_double
1051 };
1052 
1053 /* Brownian (Red) Noise: noise where the power density decreases by 6 dB per
1054  * octave with increasing frequency
1055  *
1056  * taken from http://vellocet.com/dsp/noise/VRand.html
1057  * by Andrew Simper of Vellocet (andy@vellocet.com)
1058  */
1059 
1060 #define DEFINE_RED_NOISE(type,scale) \
1061 static void \
1062 gst_audio_test_src_create_red_noise_##type (GstAudioTestSrc * src, g##type * samples) \
1063 { \
1064   gint i, c, channel_step, sample_step; \
1065   g##type *ptr; \
1066   gdouble amp = (src->volume * scale); \
1067   gdouble state = src->red.state; \
1068   gint channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
1069   \
1070   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
1071     channel_step = 1; \
1072     sample_step = channels; \
1073   } else { \
1074     channel_step = src->generate_samples_per_buffer; \
1075     sample_step = 1; \
1076   } \
1077   \
1078   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
1079     ptr = samples; \
1080     for (c = 0; c < channels; ++c) { \
1081       while (TRUE) { \
1082         gdouble r = g_rand_double_range (src->gen, -1.0, 1.0); \
1083         state += r; \
1084         if (state < -8.0f || state > 8.0f) state -= r; \
1085         else break; \
1086       } \
1087       *ptr = (g##type) (amp * state * 0.0625f); /* /16.0 */ \
1088       ptr += channel_step; \
1089     } \
1090     samples += sample_step; \
1091   } \
1092   src->red.state = state; \
1093 }
1094 
1095 DEFINE_RED_NOISE (int16, 32767.0);
1096 DEFINE_RED_NOISE (int32, 2147483647.0);
1097 DEFINE_RED_NOISE (float, 1.0);
1098 DEFINE_RED_NOISE (double, 1.0);
1099 
1100 static const ProcessFunc red_noise_funcs[] = {
1101   (ProcessFunc) gst_audio_test_src_create_red_noise_int16,
1102   (ProcessFunc) gst_audio_test_src_create_red_noise_int32,
1103   (ProcessFunc) gst_audio_test_src_create_red_noise_float,
1104   (ProcessFunc) gst_audio_test_src_create_red_noise_double
1105 };
1106 
1107 /* Blue Noise: apply spectral inversion to pink noise */
1108 
1109 #define DEFINE_BLUE_NOISE(type) \
1110 static void \
1111 gst_audio_test_src_create_blue_noise_##type (GstAudioTestSrc * src, g##type * samples) \
1112 { \
1113   gint i, c, channel_step, sample_step; \
1114   static gdouble flip=1.0; \
1115   gint channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
1116   g##type *ptr; \
1117   \
1118   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
1119     channel_step = 1; \
1120     sample_step = channels; \
1121   } else { \
1122     channel_step = src->generate_samples_per_buffer; \
1123     sample_step = 1; \
1124   } \
1125   \
1126   gst_audio_test_src_create_pink_noise_##type (src, samples); \
1127   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
1128     ptr = samples; \
1129     for (c = 0; c < channels; ++c) { \
1130       *ptr *= flip; \
1131       ptr += channel_step; \
1132     } \
1133     flip *= -1.0; \
1134     samples += sample_step; \
1135   } \
1136 }
1137 
1138 DEFINE_BLUE_NOISE (int16);
1139 DEFINE_BLUE_NOISE (int32);
1140 DEFINE_BLUE_NOISE (float);
1141 DEFINE_BLUE_NOISE (double);
1142 
1143 static const ProcessFunc blue_noise_funcs[] = {
1144   (ProcessFunc) gst_audio_test_src_create_blue_noise_int16,
1145   (ProcessFunc) gst_audio_test_src_create_blue_noise_int32,
1146   (ProcessFunc) gst_audio_test_src_create_blue_noise_float,
1147   (ProcessFunc) gst_audio_test_src_create_blue_noise_double
1148 };
1149 
1150 
1151 /* Violet Noise: apply spectral inversion to red noise */
1152 
1153 #define DEFINE_VIOLET_NOISE(type) \
1154 static void \
1155 gst_audio_test_src_create_violet_noise_##type (GstAudioTestSrc * src, g##type * samples) \
1156 { \
1157   gint i, c, channel_step, sample_step; \
1158   static gdouble flip=1.0; \
1159   gint channels = GST_AUDIO_INFO_CHANNELS (&src->info); \
1160   g##type *ptr; \
1161   \
1162   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_INTERLEAVED) { \
1163     channel_step = 1; \
1164     sample_step = channels; \
1165   } else { \
1166     channel_step = src->generate_samples_per_buffer; \
1167     sample_step = 1; \
1168   } \
1169   \
1170   gst_audio_test_src_create_red_noise_##type (src, samples); \
1171   for (i = 0; i < src->generate_samples_per_buffer; i++) { \
1172     ptr = samples; \
1173     for (c = 0; c < channels; ++c) { \
1174       *ptr *= flip; \
1175       ptr += channel_step; \
1176     } \
1177     flip *= -1.0; \
1178     samples += sample_step; \
1179   } \
1180 }
1181 
1182 DEFINE_VIOLET_NOISE (int16);
1183 DEFINE_VIOLET_NOISE (int32);
1184 DEFINE_VIOLET_NOISE (float);
1185 DEFINE_VIOLET_NOISE (double);
1186 
1187 static const ProcessFunc violet_noise_funcs[] = {
1188   (ProcessFunc) gst_audio_test_src_create_violet_noise_int16,
1189   (ProcessFunc) gst_audio_test_src_create_violet_noise_int32,
1190   (ProcessFunc) gst_audio_test_src_create_violet_noise_float,
1191   (ProcessFunc) gst_audio_test_src_create_violet_noise_double
1192 };
1193 
1194 
1195 /*
1196  * gst_audio_test_src_change_wave:
1197  * Assign function pointer of wave generator.
1198  */
1199 static void
gst_audio_test_src_change_wave(GstAudioTestSrc * src)1200 gst_audio_test_src_change_wave (GstAudioTestSrc * src)
1201 {
1202   gint idx;
1203 
1204   src->pack_func = NULL;
1205   src->process = NULL;
1206 
1207   /* not negotiated yet? */
1208   if (src->info.finfo == NULL)
1209     return;
1210 
1211   switch (GST_AUDIO_FORMAT_INFO_FORMAT (src->info.finfo)) {
1212     case GST_AUDIO_FORMAT_S16:
1213       idx = 0;
1214       break;
1215     case GST_AUDIO_FORMAT_S32:
1216       idx = 1;
1217       break;
1218     case GST_AUDIO_FORMAT_F32:
1219       idx = 2;
1220       break;
1221     case GST_AUDIO_FORMAT_F64:
1222       idx = 3;
1223       break;
1224     default:
1225       /* special format */
1226       switch (src->info.finfo->unpack_format) {
1227         case GST_AUDIO_FORMAT_S32:
1228           idx = 1;
1229           src->pack_func = src->info.finfo->pack_func;
1230           src->pack_size = sizeof (gint32);
1231           break;
1232         case GST_AUDIO_FORMAT_F64:
1233           idx = 3;
1234           src->pack_func = src->info.finfo->pack_func;
1235           src->pack_size = sizeof (gdouble);
1236           break;
1237         default:
1238           g_assert_not_reached ();
1239           return;
1240       }
1241   }
1242 
1243   switch (src->wave) {
1244     case GST_AUDIO_TEST_SRC_WAVE_SINE:
1245       src->process = sine_funcs[idx];
1246       break;
1247     case GST_AUDIO_TEST_SRC_WAVE_SQUARE:
1248       src->process = square_funcs[idx];
1249       break;
1250     case GST_AUDIO_TEST_SRC_WAVE_SAW:
1251       src->process = saw_funcs[idx];
1252       break;
1253     case GST_AUDIO_TEST_SRC_WAVE_TRIANGLE:
1254       src->process = triangle_funcs[idx];
1255       break;
1256     case GST_AUDIO_TEST_SRC_WAVE_SILENCE:
1257       src->process = silence_funcs[idx];
1258       break;
1259     case GST_AUDIO_TEST_SRC_WAVE_WHITE_NOISE:
1260       if (!(src->gen))
1261         src->gen = g_rand_new ();
1262       src->process = white_noise_funcs[idx];
1263       break;
1264     case GST_AUDIO_TEST_SRC_WAVE_PINK_NOISE:
1265       if (!(src->gen))
1266         src->gen = g_rand_new ();
1267       gst_audio_test_src_init_pink_noise (src);
1268       src->process = pink_noise_funcs[idx];
1269       break;
1270     case GST_AUDIO_TEST_SRC_WAVE_SINE_TAB:
1271       gst_audio_test_src_init_sine_table (src, TRUE);
1272       src->process = sine_table_funcs[idx];
1273       break;
1274     case GST_AUDIO_TEST_SRC_WAVE_TICKS:
1275       gst_audio_test_src_init_sine_table (src, FALSE);
1276       src->process = tick_funcs[idx];
1277       src->samples_between_ticks =
1278           gst_util_uint64_scale_int (src->tick_interval,
1279           GST_AUDIO_INFO_RATE (&(src->info)), GST_SECOND);
1280       break;
1281     case GST_AUDIO_TEST_SRC_WAVE_GAUSSIAN_WHITE_NOISE:
1282       if (!(src->gen))
1283         src->gen = g_rand_new ();
1284       src->process = gaussian_white_noise_funcs[idx];
1285       break;
1286     case GST_AUDIO_TEST_SRC_WAVE_RED_NOISE:
1287       if (!(src->gen))
1288         src->gen = g_rand_new ();
1289       src->red.state = 0.0;
1290       src->process = red_noise_funcs[idx];
1291       break;
1292     case GST_AUDIO_TEST_SRC_WAVE_BLUE_NOISE:
1293       if (!(src->gen))
1294         src->gen = g_rand_new ();
1295       gst_audio_test_src_init_pink_noise (src);
1296       src->process = blue_noise_funcs[idx];
1297       break;
1298     case GST_AUDIO_TEST_SRC_WAVE_VIOLET_NOISE:
1299       if (!(src->gen))
1300         src->gen = g_rand_new ();
1301       src->red.state = 0.0;
1302       src->process = violet_noise_funcs[idx];
1303       break;
1304     default:
1305       GST_ERROR ("invalid wave-form");
1306       break;
1307   }
1308 }
1309 
1310 /*
1311  * gst_audio_test_src_change_volume:
1312  * Recalc wave tables for precalculated waves.
1313  */
1314 static void
gst_audio_test_src_change_volume(GstAudioTestSrc * src)1315 gst_audio_test_src_change_volume (GstAudioTestSrc * src)
1316 {
1317   switch (src->wave) {
1318     case GST_AUDIO_TEST_SRC_WAVE_SINE_TAB:
1319       gst_audio_test_src_init_sine_table (src, TRUE);
1320       break;
1321     default:
1322       break;
1323   }
1324 }
1325 
1326 static void
gst_audio_test_src_get_times(GstBaseSrc * basesrc,GstBuffer * buffer,GstClockTime * start,GstClockTime * end)1327 gst_audio_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
1328     GstClockTime * start, GstClockTime * end)
1329 {
1330   /* for live sources, sync on the timestamp of the buffer */
1331   if (gst_base_src_is_live (basesrc)) {
1332     GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
1333 
1334     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1335       /* get duration to calculate end time */
1336       GstClockTime duration = GST_BUFFER_DURATION (buffer);
1337 
1338       if (GST_CLOCK_TIME_IS_VALID (duration)) {
1339         *end = timestamp + duration;
1340       }
1341       *start = timestamp;
1342     }
1343   } else {
1344     *start = -1;
1345     *end = -1;
1346   }
1347 }
1348 
1349 static gboolean
gst_audio_test_src_start(GstBaseSrc * basesrc)1350 gst_audio_test_src_start (GstBaseSrc * basesrc)
1351 {
1352   GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (basesrc);
1353 
1354   src->next_sample = 0;
1355   src->next_byte = 0;
1356   src->next_time = 0;
1357   src->check_seek_stop = FALSE;
1358   src->eos_reached = FALSE;
1359   src->tags_pushed = FALSE;
1360   src->accumulator = 0;
1361   src->tick_counter = 0;
1362 
1363   return TRUE;
1364 }
1365 
1366 static gboolean
gst_audio_test_src_stop(GstBaseSrc * basesrc)1367 gst_audio_test_src_stop (GstBaseSrc * basesrc)
1368 {
1369   return TRUE;
1370 }
1371 
1372 /* seek to time, will be called when we operate in push mode. In pull mode we
1373  * get the requested byte offset. */
1374 static gboolean
gst_audio_test_src_do_seek(GstBaseSrc * basesrc,GstSegment * segment)1375 gst_audio_test_src_do_seek (GstBaseSrc * basesrc, GstSegment * segment)
1376 {
1377   GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (basesrc);
1378   GstClockTime time;
1379   gint samplerate, bpf;
1380   gint64 next_sample;
1381 
1382   GST_DEBUG_OBJECT (src, "seeking %" GST_SEGMENT_FORMAT, segment);
1383 
1384   time = segment->position;
1385   src->reverse = (segment->rate < 0.0);
1386 
1387   samplerate = GST_AUDIO_INFO_RATE (&src->info);
1388   bpf = GST_AUDIO_INFO_BPF (&src->info);
1389 
1390   /* now move to the time indicated, don't seek to the sample *after* the time */
1391   next_sample = gst_util_uint64_scale_int (time, samplerate, GST_SECOND);
1392   src->next_byte = next_sample * bpf;
1393   if (samplerate == 0)
1394     src->next_time = 0;
1395   else
1396     src->next_time =
1397         gst_util_uint64_scale_round (next_sample, GST_SECOND, samplerate);
1398 
1399   GST_DEBUG_OBJECT (src, "seeking next_sample=%" G_GINT64_FORMAT
1400       " next_time=%" GST_TIME_FORMAT, next_sample,
1401       GST_TIME_ARGS (src->next_time));
1402 
1403   g_assert (src->next_time <= time);
1404 
1405   src->next_sample = next_sample;
1406 
1407   if (segment->rate > 0 && GST_CLOCK_TIME_IS_VALID (segment->stop)) {
1408     time = segment->stop;
1409     src->sample_stop =
1410         gst_util_uint64_scale_round (time, samplerate, GST_SECOND);
1411     src->check_seek_stop = TRUE;
1412   } else if (segment->rate < 0) {
1413     time = segment->start;
1414     src->sample_stop =
1415         gst_util_uint64_scale_round (time, samplerate, GST_SECOND);
1416     src->check_seek_stop = TRUE;
1417   } else {
1418     src->check_seek_stop = FALSE;
1419   }
1420   src->eos_reached = FALSE;
1421 
1422   return TRUE;
1423 }
1424 
1425 static gboolean
gst_audio_test_src_is_seekable(GstBaseSrc * basesrc)1426 gst_audio_test_src_is_seekable (GstBaseSrc * basesrc)
1427 {
1428   /* we're seekable... */
1429   return TRUE;
1430 }
1431 
1432 static GstFlowReturn
gst_audio_test_src_fill(GstBaseSrc * basesrc,guint64 offset,guint length,GstBuffer * buffer)1433 gst_audio_test_src_fill (GstBaseSrc * basesrc, guint64 offset,
1434     guint length, GstBuffer * buffer)
1435 {
1436   GstAudioTestSrc *src;
1437   GstClockTime next_time;
1438   gint64 next_sample, next_byte;
1439   gint bytes, samples;
1440   GstElementClass *eclass;
1441   GstMapInfo map;
1442   gint samplerate, bpf;
1443 
1444   src = GST_AUDIO_TEST_SRC (basesrc);
1445 
1446   /* example for tagging generated data */
1447   if (!src->tags_pushed) {
1448     GstTagList *taglist;
1449 
1450     taglist = gst_tag_list_new (GST_TAG_DESCRIPTION, "audiotest wave", NULL);
1451 
1452     eclass = GST_ELEMENT_CLASS (parent_class);
1453     if (eclass->send_event)
1454       eclass->send_event (GST_ELEMENT_CAST (basesrc),
1455           gst_event_new_tag (taglist));
1456     else
1457       gst_tag_list_unref (taglist);
1458     src->tags_pushed = TRUE;
1459   }
1460 
1461   if (src->eos_reached) {
1462     GST_INFO_OBJECT (src, "eos");
1463     return GST_FLOW_EOS;
1464   }
1465 
1466   samplerate = GST_AUDIO_INFO_RATE (&src->info);
1467   bpf = GST_AUDIO_INFO_BPF (&src->info);
1468 
1469   /* if no length was given, use our default length in samples otherwise convert
1470    * the length in bytes to samples. */
1471   if (length == -1)
1472     samples = src->samples_per_buffer;
1473   else
1474     samples = length / bpf;
1475 
1476   /* if no offset was given, use our next logical byte */
1477   if (offset == -1)
1478     offset = src->next_byte;
1479 
1480   /* now see if we are at the byteoffset we think we are */
1481   if (offset != src->next_byte) {
1482     GST_DEBUG_OBJECT (src, "seek to new offset %" G_GUINT64_FORMAT, offset);
1483     /* we have a discont in the expected sample offset, do a 'seek' */
1484     src->next_sample = offset / bpf;
1485     src->next_time =
1486         gst_util_uint64_scale_int (src->next_sample, GST_SECOND, samplerate);
1487     src->next_byte = offset;
1488   }
1489 
1490   /* check for eos */
1491   if (src->check_seek_stop && !src->reverse &&
1492       (src->sample_stop > src->next_sample) &&
1493       (src->sample_stop < src->next_sample + samples)
1494       ) {
1495     /* calculate only partial buffer */
1496     src->generate_samples_per_buffer = src->sample_stop - src->next_sample;
1497     next_sample = src->sample_stop;
1498     src->eos_reached = TRUE;
1499   } else if (src->check_seek_stop && src->reverse &&
1500       (src->sample_stop > src->next_sample)
1501       ) {
1502     /* calculate only partial buffer */
1503     src->generate_samples_per_buffer = src->sample_stop - src->next_sample;
1504     next_sample = src->sample_stop;
1505     src->eos_reached = TRUE;
1506   } else {
1507     /* calculate full buffer */
1508     src->generate_samples_per_buffer = samples;
1509     next_sample = src->next_sample + (src->reverse ? (-samples) : samples);
1510   }
1511 
1512   bytes = src->generate_samples_per_buffer * bpf;
1513 
1514   next_byte = src->next_byte + (src->reverse ? (-bytes) : bytes);
1515   next_time = gst_util_uint64_scale_int (next_sample, GST_SECOND, samplerate);
1516 
1517   GST_LOG_OBJECT (src, "samplerate %d", samplerate);
1518   GST_LOG_OBJECT (src, "next_sample %" G_GINT64_FORMAT ", ts %" GST_TIME_FORMAT,
1519       next_sample, GST_TIME_ARGS (next_time));
1520 
1521   gst_buffer_set_size (buffer, bytes);
1522 
1523   GST_BUFFER_OFFSET (buffer) = src->next_sample;
1524   GST_BUFFER_OFFSET_END (buffer) = next_sample;
1525   if (!src->reverse) {
1526     GST_BUFFER_TIMESTAMP (buffer) = src->timestamp_offset + src->next_time;
1527     GST_BUFFER_DURATION (buffer) = next_time - src->next_time;
1528   } else {
1529     GST_BUFFER_TIMESTAMP (buffer) = src->timestamp_offset + next_time;
1530     GST_BUFFER_DURATION (buffer) = src->next_time - next_time;
1531   }
1532 
1533   gst_object_sync_values (GST_OBJECT (src), GST_BUFFER_TIMESTAMP (buffer));
1534 
1535   src->next_time = next_time;
1536   src->next_sample = next_sample;
1537   src->next_byte = next_byte;
1538 
1539   GST_LOG_OBJECT (src, "generating %u samples at ts %" GST_TIME_FORMAT,
1540       src->generate_samples_per_buffer,
1541       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
1542 
1543   gst_buffer_map (buffer, &map, GST_MAP_WRITE);
1544   if (src->pack_func) {
1545     gsize tmpsize;
1546 
1547     tmpsize =
1548         src->generate_samples_per_buffer * GST_AUDIO_INFO_CHANNELS (&src->info)
1549         * src->pack_size;
1550 
1551     if (tmpsize > src->tmpsize) {
1552       src->tmp = g_realloc (src->tmp, tmpsize);
1553       src->tmpsize = tmpsize;
1554     }
1555     src->process (src, src->tmp);
1556     src->pack_func (src->info.finfo, 0, src->tmp, map.data,
1557         src->generate_samples_per_buffer *
1558         GST_AUDIO_INFO_CHANNELS (&src->info));
1559   } else {
1560     src->process (src, map.data);
1561   }
1562   gst_buffer_unmap (buffer, &map);
1563 
1564   if (G_UNLIKELY ((src->wave == GST_AUDIO_TEST_SRC_WAVE_SILENCE)
1565           || (src->volume == 0.0))) {
1566     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_GAP);
1567   }
1568 
1569   if (GST_AUDIO_INFO_LAYOUT (&src->info) == GST_AUDIO_LAYOUT_NON_INTERLEAVED) {
1570     gst_buffer_add_audio_meta (buffer, &src->info,
1571         src->generate_samples_per_buffer, NULL);
1572   }
1573 
1574   return GST_FLOW_OK;
1575 }
1576 
1577 static void
gst_audio_test_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1578 gst_audio_test_src_set_property (GObject * object, guint prop_id,
1579     const GValue * value, GParamSpec * pspec)
1580 {
1581   GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (object);
1582 
1583   switch (prop_id) {
1584     case PROP_SAMPLES_PER_BUFFER:
1585       src->samples_per_buffer = g_value_get_int (value);
1586       gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src),
1587           GST_AUDIO_INFO_BPF (&src->info) * src->samples_per_buffer);
1588       break;
1589     case PROP_WAVE:
1590       src->wave = g_value_get_enum (value);
1591       gst_audio_test_src_change_wave (src);
1592       break;
1593     case PROP_FREQ:
1594       src->freq = g_value_get_double (value);
1595       break;
1596     case PROP_VOLUME:
1597       src->volume = g_value_get_double (value);
1598       gst_audio_test_src_change_volume (src);
1599       break;
1600     case PROP_IS_LIVE:
1601       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
1602       break;
1603     case PROP_TIMESTAMP_OFFSET:
1604       src->timestamp_offset = g_value_get_int64 (value);
1605       break;
1606     case PROP_SINE_PERIODS_PER_TICK:
1607       src->sine_periods_per_tick = g_value_get_uint (value);
1608       break;
1609     case PROP_TICK_INTERVAL:
1610       src->tick_interval = g_value_get_uint64 (value);
1611       break;
1612     case PROP_MARKER_TICK_PERIOD:
1613       src->marker_tick_period = g_value_get_uint (value);
1614       break;
1615     case PROP_MARKER_TICK_VOLUME:
1616       src->marker_tick_volume = g_value_get_double (value);
1617       break;
1618     case PROP_APPLY_TICK_RAMP:
1619       src->apply_tick_ramp = g_value_get_boolean (value);
1620       break;
1621     case PROP_CAN_ACTIVATE_PUSH:
1622       GST_BASE_SRC (src)->can_activate_push = g_value_get_boolean (value);
1623       break;
1624     case PROP_CAN_ACTIVATE_PULL:
1625       src->can_activate_pull = g_value_get_boolean (value);
1626       break;
1627     default:
1628       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1629       break;
1630   }
1631 }
1632 
1633 static void
gst_audio_test_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1634 gst_audio_test_src_get_property (GObject * object, guint prop_id,
1635     GValue * value, GParamSpec * pspec)
1636 {
1637   GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (object);
1638 
1639   switch (prop_id) {
1640     case PROP_SAMPLES_PER_BUFFER:
1641       g_value_set_int (value, src->samples_per_buffer);
1642       break;
1643     case PROP_WAVE:
1644       g_value_set_enum (value, src->wave);
1645       break;
1646     case PROP_FREQ:
1647       g_value_set_double (value, src->freq);
1648       break;
1649     case PROP_VOLUME:
1650       g_value_set_double (value, src->volume);
1651       break;
1652     case PROP_IS_LIVE:
1653       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
1654       break;
1655     case PROP_TIMESTAMP_OFFSET:
1656       g_value_set_int64 (value, src->timestamp_offset);
1657       break;
1658     case PROP_SINE_PERIODS_PER_TICK:
1659       g_value_set_uint (value, src->sine_periods_per_tick);
1660       break;
1661     case PROP_TICK_INTERVAL:
1662       g_value_set_uint64 (value, src->tick_interval);
1663       break;
1664     case PROP_MARKER_TICK_PERIOD:
1665       g_value_set_uint (value, src->marker_tick_period);
1666       break;
1667     case PROP_MARKER_TICK_VOLUME:
1668       g_value_set_double (value, src->marker_tick_volume);
1669       break;
1670     case PROP_APPLY_TICK_RAMP:
1671       g_value_set_boolean (value, src->apply_tick_ramp);
1672       break;
1673     case PROP_CAN_ACTIVATE_PUSH:
1674       g_value_set_boolean (value, GST_BASE_SRC (src)->can_activate_push);
1675       break;
1676     case PROP_CAN_ACTIVATE_PULL:
1677       g_value_set_boolean (value, src->can_activate_pull);
1678       break;
1679     default:
1680       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1681       break;
1682   }
1683 }
1684 
1685 static gboolean
plugin_init(GstPlugin * plugin)1686 plugin_init (GstPlugin * plugin)
1687 {
1688   GST_DEBUG_CATEGORY_INIT (audio_test_src_debug, "audiotestsrc", 0,
1689       "Audio Test Source");
1690 
1691   return gst_element_register (plugin, "audiotestsrc",
1692       GST_RANK_NONE, GST_TYPE_AUDIO_TEST_SRC);
1693 }
1694 
1695 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1696     GST_VERSION_MINOR,
1697     audiotestsrc,
1698     "Creates audio test signals of given frequency and volume",
1699     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
1700