2 * Copyright (C) 2005 Stefan Kost <ensonic@users.sf.net>
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.
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.
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * SECTION:element-audiotestsrc
22 * AudioTestSrc can be used to generate basic audio signals. It support several
23 * different waveforms and allows to set the base frequency and volume.
26 * <title>Example launch line</title>
28 * gst-launch audiotestsrc ! audioconvert ! alsasink
29 * ]| This pipeline produces a sine with default frequency, 440 Hz, and the
30 * default volume, 0.8 (relative to a maximum 1.0).
32 * gst-launch audiotestsrc wave=2 freq=200 ! audioconvert ! tee name=t ! queue ! alsasink t. ! queue ! libvisual_lv_scope ! ffmpegcolorspace ! xvimagesink
33 * ]| In this example a saw wave is generated. The wave is shown using a
34 * scope visualizer from libvisual, allowing you to visually verify that
35 * the saw wave is correct.
46 #include <gst/controller/gstcontroller.h>
48 #include "gstaudiotestsrc.h"
52 #define M_PI 3.14159265358979323846
56 #define M_PI_2 1.57079632679489661923
59 #define M_PI_M2 ( M_PI + M_PI )
61 GST_DEBUG_CATEGORY_STATIC (audio_test_src_debug);
62 #define GST_CAT_DEFAULT audio_test_src_debug
64 #define DEFAULT_SAMPLES_PER_BUFFER 1024
65 #define DEFAULT_WAVE GST_AUDIO_TEST_SRC_WAVE_SINE
66 #define DEFAULT_FREQ 440.0
67 #define DEFAULT_VOLUME 0.8
68 #define DEFAULT_IS_LIVE FALSE
69 #define DEFAULT_TIMESTAMP_OFFSET G_GINT64_CONSTANT (0)
70 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
71 #define DEFAULT_CAN_ACTIVATE_PULL FALSE
76 PROP_SAMPLES_PER_BUFFER,
81 PROP_TIMESTAMP_OFFSET,
82 PROP_CAN_ACTIVATE_PUSH,
83 PROP_CAN_ACTIVATE_PULL,
88 static GstStaticPadTemplate gst_audio_test_src_src_template =
89 GST_STATIC_PAD_TEMPLATE ("src",
92 GST_STATIC_CAPS ("audio/x-raw-int, "
93 "endianness = (int) BYTE_ORDER, "
94 "signed = (boolean) true, "
97 "rate = (int) [ 1, MAX ], "
98 "channels = (int) [ 1, 2 ]; "
100 "endianness = (int) BYTE_ORDER, "
101 "signed = (boolean) true, "
104 "rate = (int) [ 1, MAX ], "
105 "channels = (int) [ 1, 2 ]; "
106 "audio/x-raw-float, "
107 "endianness = (int) BYTE_ORDER, "
108 "width = (int) { 32, 64 }, "
109 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
112 #define gst_audio_test_src_parent_class parent_class
113 G_DEFINE_TYPE (GstAudioTestSrc, gst_audio_test_src, GST_TYPE_BASE_SRC);
115 #define GST_TYPE_AUDIO_TEST_SRC_WAVE (gst_audiostestsrc_wave_get_type())
117 gst_audiostestsrc_wave_get_type (void)
119 static GType audiostestsrc_wave_type = 0;
120 static const GEnumValue audiostestsrc_waves[] = {
121 {GST_AUDIO_TEST_SRC_WAVE_SINE, "Sine", "sine"},
122 {GST_AUDIO_TEST_SRC_WAVE_SQUARE, "Square", "square"},
123 {GST_AUDIO_TEST_SRC_WAVE_SAW, "Saw", "saw"},
124 {GST_AUDIO_TEST_SRC_WAVE_TRIANGLE, "Triangle", "triangle"},
125 {GST_AUDIO_TEST_SRC_WAVE_SILENCE, "Silence", "silence"},
126 {GST_AUDIO_TEST_SRC_WAVE_WHITE_NOISE, "White uniform noise", "white-noise"},
127 {GST_AUDIO_TEST_SRC_WAVE_PINK_NOISE, "Pink noise", "pink-noise"},
128 {GST_AUDIO_TEST_SRC_WAVE_SINE_TAB, "Sine table", "sine-table"},
129 {GST_AUDIO_TEST_SRC_WAVE_TICKS, "Periodic Ticks", "ticks"},
130 {GST_AUDIO_TEST_SRC_WAVE_GAUSSIAN_WHITE_NOISE, "White Gaussian noise",
132 {GST_AUDIO_TEST_SRC_WAVE_RED_NOISE, "Red (brownian) noise", "red-noise"},
133 {GST_AUDIO_TEST_SRC_WAVE_BLUE_NOISE, "Blue noise", "blue-noise"},
134 {GST_AUDIO_TEST_SRC_WAVE_VIOLET_NOISE, "Violet noise", "violet-noise"},
138 if (G_UNLIKELY (audiostestsrc_wave_type == 0)) {
139 audiostestsrc_wave_type = g_enum_register_static ("GstAudioTestSrcWave",
140 audiostestsrc_waves);
142 return audiostestsrc_wave_type;
145 static void gst_audio_test_src_finalize (GObject * object);
147 static void gst_audio_test_src_set_property (GObject * object,
148 guint prop_id, const GValue * value, GParamSpec * pspec);
149 static void gst_audio_test_src_get_property (GObject * object,
150 guint prop_id, GValue * value, GParamSpec * pspec);
152 static gboolean gst_audio_test_src_setcaps (GstBaseSrc * basesrc,
154 static void gst_audio_test_src_src_fixate (GstPad * pad, GstCaps * caps);
156 static gboolean gst_audio_test_src_is_seekable (GstBaseSrc * basesrc);
157 static gboolean gst_audio_test_src_do_seek (GstBaseSrc * basesrc,
158 GstSegment * segment);
159 static gboolean gst_audio_test_src_query (GstBaseSrc * basesrc,
162 static void gst_audio_test_src_change_wave (GstAudioTestSrc * src);
164 static void gst_audio_test_src_get_times (GstBaseSrc * basesrc,
165 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
166 static gboolean gst_audio_test_src_start (GstBaseSrc * basesrc);
167 static gboolean gst_audio_test_src_stop (GstBaseSrc * basesrc);
168 static GstFlowReturn gst_audio_test_src_create (GstBaseSrc * basesrc,
169 guint64 offset, guint length, GstBuffer ** buffer);
172 gst_audio_test_src_class_init (GstAudioTestSrcClass * klass)
174 GObjectClass *gobject_class;
175 GstElementClass *gstelement_class;
176 GstBaseSrcClass *gstbasesrc_class;
178 gobject_class = (GObjectClass *) klass;
179 gstelement_class = (GstElementClass *) klass;
180 gstbasesrc_class = (GstBaseSrcClass *) klass;
182 gobject_class->set_property = gst_audio_test_src_set_property;
183 gobject_class->get_property = gst_audio_test_src_get_property;
184 gobject_class->finalize = gst_audio_test_src_finalize;
186 g_object_class_install_property (gobject_class, PROP_SAMPLES_PER_BUFFER,
187 g_param_spec_int ("samplesperbuffer", "Samples per buffer",
188 "Number of samples in each outgoing buffer",
189 1, G_MAXINT, DEFAULT_SAMPLES_PER_BUFFER,
190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (gobject_class, PROP_WAVE,
192 g_param_spec_enum ("wave", "Waveform", "Oscillator waveform",
193 GST_TYPE_AUDIO_TEST_SRC_WAVE, GST_AUDIO_TEST_SRC_WAVE_SINE,
194 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (gobject_class, PROP_FREQ,
196 g_param_spec_double ("freq", "Frequency", "Frequency of test signal",
197 0.0, 20000.0, DEFAULT_FREQ,
198 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
199 g_object_class_install_property (gobject_class, PROP_VOLUME,
200 g_param_spec_double ("volume", "Volume", "Volume of test signal", 0.0,
202 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
204 g_param_spec_boolean ("is-live", "Is Live",
205 "Whether to act as a live source", DEFAULT_IS_LIVE,
206 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 g_object_class_install_property (G_OBJECT_CLASS (klass),
208 PROP_TIMESTAMP_OFFSET, g_param_spec_int64 ("timestamp-offset",
210 "An offset added to timestamps set on buffers (in ns)", G_MININT64,
211 G_MAXINT64, DEFAULT_TIMESTAMP_OFFSET,
212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
213 g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PUSH,
214 g_param_spec_boolean ("can-activate-push", "Can activate push",
215 "Can activate in push mode", DEFAULT_CAN_ACTIVATE_PUSH,
216 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
217 g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PULL,
218 g_param_spec_boolean ("can-activate-pull", "Can activate pull",
219 "Can activate in pull mode", DEFAULT_CAN_ACTIVATE_PULL,
220 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222 gst_element_class_add_pad_template (gstelement_class,
223 gst_static_pad_template_get (&gst_audio_test_src_src_template));
224 gst_element_class_set_details_simple (gstelement_class,
225 "Audio test source", "Source/Audio",
226 "Creates audio test signals of given frequency and volume",
227 "Stefan Kost <ensonic@users.sf.net>");
229 gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_test_src_setcaps);
230 gstbasesrc_class->is_seekable =
231 GST_DEBUG_FUNCPTR (gst_audio_test_src_is_seekable);
232 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_audio_test_src_do_seek);
233 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_audio_test_src_query);
234 gstbasesrc_class->get_times =
235 GST_DEBUG_FUNCPTR (gst_audio_test_src_get_times);
236 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_audio_test_src_start);
237 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_audio_test_src_stop);
238 gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_audio_test_src_create);
242 gst_audio_test_src_init (GstAudioTestSrc * src)
244 GstPad *pad = GST_BASE_SRC_PAD (src);
246 gst_pad_set_fixatecaps_function (pad, gst_audio_test_src_src_fixate);
248 src->samplerate = 44100;
249 src->format = GST_AUDIO_TEST_SRC_FORMAT_NONE;
251 src->volume = DEFAULT_VOLUME;
252 src->freq = DEFAULT_FREQ;
254 /* we operate in time */
255 gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
256 gst_base_src_set_live (GST_BASE_SRC (src), DEFAULT_IS_LIVE);
258 src->samples_per_buffer = DEFAULT_SAMPLES_PER_BUFFER;
259 src->generate_samples_per_buffer = src->samples_per_buffer;
260 src->timestamp_offset = DEFAULT_TIMESTAMP_OFFSET;
261 src->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
265 src->wave = DEFAULT_WAVE;
266 gst_base_src_set_blocksize (GST_BASE_SRC (src), -1);
270 gst_audio_test_src_finalize (GObject * object)
272 GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (object);
275 g_rand_free (src->gen);
278 G_OBJECT_CLASS (parent_class)->finalize (object);
282 gst_audio_test_src_src_fixate (GstPad * pad, GstCaps * caps)
284 GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (GST_PAD_PARENT (pad));
286 GstStructure *structure;
288 structure = gst_caps_get_structure (caps, 0);
290 GST_DEBUG_OBJECT (src, "fixating samplerate to %d", src->samplerate);
292 gst_structure_fixate_field_nearest_int (structure, "rate", src->samplerate);
294 name = gst_structure_get_name (structure);
295 if (strcmp (name, "audio/x-raw-int") == 0)
296 gst_structure_fixate_field_nearest_int (structure, "width", 32);
297 else if (strcmp (name, "audio/x-raw-float") == 0)
298 gst_structure_fixate_field_nearest_int (structure, "width", 64);
300 /* fixate to mono unless downstream requires stereo, for backwards compat */
301 gst_structure_fixate_field_nearest_int (structure, "channels", 1);
305 gst_audio_test_src_setcaps (GstBaseSrc * basesrc, GstCaps * caps)
307 GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (basesrc);
308 const GstStructure *structure;
313 structure = gst_caps_get_structure (caps, 0);
314 ret = gst_structure_get_int (structure, "rate", &src->samplerate);
316 GST_DEBUG_OBJECT (src, "negotiated to samplerate %d", src->samplerate);
318 name = gst_structure_get_name (structure);
319 if (strcmp (name, "audio/x-raw-int") == 0) {
320 ret &= gst_structure_get_int (structure, "width", &width);
321 src->format = (width == 32) ? GST_AUDIO_TEST_SRC_FORMAT_S32 :
322 GST_AUDIO_TEST_SRC_FORMAT_S16;
324 ret &= gst_structure_get_int (structure, "width", &width);
325 src->format = (width == 32) ? GST_AUDIO_TEST_SRC_FORMAT_F32 :
326 GST_AUDIO_TEST_SRC_FORMAT_F64;
329 /* allocate a new buffer suitable for this pad */
330 switch (src->format) {
331 case GST_AUDIO_TEST_SRC_FORMAT_S16:
332 src->sample_size = sizeof (gint16);
334 case GST_AUDIO_TEST_SRC_FORMAT_S32:
335 src->sample_size = sizeof (gint32);
337 case GST_AUDIO_TEST_SRC_FORMAT_F32:
338 src->sample_size = sizeof (gfloat);
340 case GST_AUDIO_TEST_SRC_FORMAT_F64:
341 src->sample_size = sizeof (gdouble);
344 /* can't really happen */
349 ret &= gst_structure_get_int (structure, "channels", &src->channels);
350 GST_DEBUG_OBJECT (src, "negotiated to %d channels", src->channels);
352 gst_audio_test_src_change_wave (src);
358 gst_audio_test_src_query (GstBaseSrc * basesrc, GstQuery * query)
360 GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (basesrc);
361 gboolean res = FALSE;
363 switch (GST_QUERY_TYPE (query)) {
364 case GST_QUERY_CONVERT:
366 GstFormat src_fmt, dest_fmt;
367 gint64 src_val, dest_val;
369 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
370 if (src_fmt == dest_fmt) {
376 case GST_FORMAT_DEFAULT:
378 case GST_FORMAT_TIME:
379 /* samples to time */
381 gst_util_uint64_scale_int (src_val, GST_SECOND,
388 case GST_FORMAT_TIME:
390 case GST_FORMAT_DEFAULT:
391 /* time to samples */
393 gst_util_uint64_scale_int (src_val, src->samplerate,
404 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
408 case GST_QUERY_SCHEDULING:
410 /* if we can operate in pull mode */
411 gst_query_set_scheduling (query, src->can_activate_pull, TRUE, FALSE, 1,
417 res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
425 GST_DEBUG_OBJECT (src, "query failed");
430 #define DEFINE_SINE(type,scale) \
432 gst_audio_test_src_create_sine_##type (GstAudioTestSrc * src, g##type * samples) \
437 step = M_PI_M2 * src->freq / src->samplerate; \
438 amp = src->volume * scale; \
441 while (i < (src->generate_samples_per_buffer * src->channels)) { \
442 src->accumulator += step; \
443 if (src->accumulator >= M_PI_M2) \
444 src->accumulator -= M_PI_M2; \
446 for (c = 0; c < src->channels; ++c) { \
447 samples[i++] = (g##type) (sin (src->accumulator) * amp); \
452 DEFINE_SINE (int16, 32767.0);
453 DEFINE_SINE (int32, 2147483647.0);
454 DEFINE_SINE (float, 1.0);
455 DEFINE_SINE (double, 1.0);
457 static const ProcessFunc sine_funcs[] = {
458 (ProcessFunc) gst_audio_test_src_create_sine_int16,
459 (ProcessFunc) gst_audio_test_src_create_sine_int32,
460 (ProcessFunc) gst_audio_test_src_create_sine_float,
461 (ProcessFunc) gst_audio_test_src_create_sine_double
464 #define DEFINE_SQUARE(type,scale) \
466 gst_audio_test_src_create_square_##type (GstAudioTestSrc * src, g##type * samples) \
471 step = M_PI_M2 * src->freq / src->samplerate; \
472 amp = src->volume * scale; \
475 while (i < (src->generate_samples_per_buffer * src->channels)) { \
476 src->accumulator += step; \
477 if (src->accumulator >= M_PI_M2) \
478 src->accumulator -= M_PI_M2; \
480 for (c = 0; c < src->channels; ++c) { \
481 samples[i++] = (g##type) ((src->accumulator < M_PI) ? amp : -amp); \
486 DEFINE_SQUARE (int16, 32767.0);
487 DEFINE_SQUARE (int32, 2147483647.0);
488 DEFINE_SQUARE (float, 1.0);
489 DEFINE_SQUARE (double, 1.0);
491 static const ProcessFunc square_funcs[] = {
492 (ProcessFunc) gst_audio_test_src_create_square_int16,
493 (ProcessFunc) gst_audio_test_src_create_square_int32,
494 (ProcessFunc) gst_audio_test_src_create_square_float,
495 (ProcessFunc) gst_audio_test_src_create_square_double
498 #define DEFINE_SAW(type,scale) \
500 gst_audio_test_src_create_saw_##type (GstAudioTestSrc * src, g##type * samples) \
505 step = M_PI_M2 * src->freq / src->samplerate; \
506 amp = (src->volume * scale) / M_PI; \
509 while (i < (src->generate_samples_per_buffer * src->channels)) { \
510 src->accumulator += step; \
511 if (src->accumulator >= M_PI_M2) \
512 src->accumulator -= M_PI_M2; \
514 if (src->accumulator < M_PI) { \
515 for (c = 0; c < src->channels; ++c) \
516 samples[i++] = (g##type) (src->accumulator * amp); \
518 for (c = 0; c < src->channels; ++c) \
519 samples[i++] = (g##type) ((M_PI_M2 - src->accumulator) * -amp); \
524 DEFINE_SAW (int16, 32767.0);
525 DEFINE_SAW (int32, 2147483647.0);
526 DEFINE_SAW (float, 1.0);
527 DEFINE_SAW (double, 1.0);
529 static const ProcessFunc saw_funcs[] = {
530 (ProcessFunc) gst_audio_test_src_create_saw_int16,
531 (ProcessFunc) gst_audio_test_src_create_saw_int32,
532 (ProcessFunc) gst_audio_test_src_create_saw_float,
533 (ProcessFunc) gst_audio_test_src_create_saw_double
536 #define DEFINE_TRIANGLE(type,scale) \
538 gst_audio_test_src_create_triangle_##type (GstAudioTestSrc * src, g##type * samples) \
543 step = M_PI_M2 * src->freq / src->samplerate; \
544 amp = (src->volume * scale) / M_PI_2; \
547 while (i < (src->generate_samples_per_buffer * src->channels)) { \
548 src->accumulator += step; \
549 if (src->accumulator >= M_PI_M2) \
550 src->accumulator -= M_PI_M2; \
552 if (src->accumulator < (M_PI * 0.5)) { \
553 for (c = 0; c < src->channels; ++c) \
554 samples[i++] = (g##type) (src->accumulator * amp); \
555 } else if (src->accumulator < (M_PI * 1.5)) { \
556 for (c = 0; c < src->channels; ++c) \
557 samples[i++] = (g##type) ((src->accumulator - M_PI) * -amp); \
559 for (c = 0; c < src->channels; ++c) \
560 samples[i++] = (g##type) ((M_PI_M2 - src->accumulator) * -amp); \
565 DEFINE_TRIANGLE (int16, 32767.0);
566 DEFINE_TRIANGLE (int32, 2147483647.0);
567 DEFINE_TRIANGLE (float, 1.0);
568 DEFINE_TRIANGLE (double, 1.0);
570 static const ProcessFunc triangle_funcs[] = {
571 (ProcessFunc) gst_audio_test_src_create_triangle_int16,
572 (ProcessFunc) gst_audio_test_src_create_triangle_int32,
573 (ProcessFunc) gst_audio_test_src_create_triangle_float,
574 (ProcessFunc) gst_audio_test_src_create_triangle_double
577 #define DEFINE_SILENCE(type) \
579 gst_audio_test_src_create_silence_##type (GstAudioTestSrc * src, g##type * samples) \
581 memset (samples, 0, src->generate_samples_per_buffer * sizeof (g##type) * src->channels); \
584 DEFINE_SILENCE (int16);
585 DEFINE_SILENCE (int32);
586 DEFINE_SILENCE (float);
587 DEFINE_SILENCE (double);
589 static const ProcessFunc silence_funcs[] = {
590 (ProcessFunc) gst_audio_test_src_create_silence_int16,
591 (ProcessFunc) gst_audio_test_src_create_silence_int32,
592 (ProcessFunc) gst_audio_test_src_create_silence_float,
593 (ProcessFunc) gst_audio_test_src_create_silence_double
596 #define DEFINE_WHITE_NOISE(type,scale) \
598 gst_audio_test_src_create_white_noise_##type (GstAudioTestSrc * src, g##type * samples) \
601 gdouble amp = (src->volume * scale); \
604 while (i < (src->generate_samples_per_buffer * src->channels)) { \
605 for (c = 0; c < src->channels; ++c) \
606 samples[i++] = (g##type) (amp * g_rand_double_range (src->gen, -1.0, 1.0)); \
610 DEFINE_WHITE_NOISE (int16, 32767.0);
611 DEFINE_WHITE_NOISE (int32, 2147483647.0);
612 DEFINE_WHITE_NOISE (float, 1.0);
613 DEFINE_WHITE_NOISE (double, 1.0);
615 static const ProcessFunc white_noise_funcs[] = {
616 (ProcessFunc) gst_audio_test_src_create_white_noise_int16,
617 (ProcessFunc) gst_audio_test_src_create_white_noise_int32,
618 (ProcessFunc) gst_audio_test_src_create_white_noise_float,
619 (ProcessFunc) gst_audio_test_src_create_white_noise_double
622 /* pink noise calculation is based on
623 * http://www.firstpr.com.au/dsp/pink-noise/phil_burk_19990905_patest_pink.c
624 * which has been released under public domain
628 gst_audio_test_src_init_pink_noise (GstAudioTestSrc * src)
631 gint num_rows = 12; /* arbitrary: 1 .. PINK_MAX_RANDOM_ROWS */
635 src->pink.index_mask = (1 << num_rows) - 1;
636 /* calculate maximum possible signed random value.
637 * Extra 1 for white noise always added. */
638 pmax = (num_rows + 1) * (1 << (PINK_RANDOM_BITS - 1));
639 src->pink.scalar = 1.0f / pmax;
640 /* Initialize rows. */
641 for (i = 0; i < num_rows; i++)
642 src->pink.rows[i] = 0;
643 src->pink.running_sum = 0;
646 /* Generate Pink noise values between -1.0 and +1.0 */
648 gst_audio_test_src_generate_pink_noise_value (GstAudioTestSrc * src)
650 GstPinkNoise *pink = &src->pink;
654 /* Increment and mask index. */
655 pink->index = (pink->index + 1) & pink->index_mask;
657 /* If index is zero, don't update any random values. */
658 if (pink->index != 0) {
659 /* Determine how many trailing zeros in PinkIndex. */
660 /* This algorithm will hang if n==0 so test first. */
662 gint n = pink->index;
664 while ((n & 1) == 0) {
669 /* Replace the indexed ROWS random value.
670 * Subtract and add back to RunningSum instead of adding all the random
671 * values together. Only one changes each time.
673 pink->running_sum -= pink->rows[num_zeros];
674 new_random = 32768.0 - (65536.0 * (gulong) g_rand_int (src->gen)
675 / (G_MAXUINT32 + 1.0));
676 pink->running_sum += new_random;
677 pink->rows[num_zeros] = new_random;
680 /* Add extra white noise value. */
681 new_random = 32768.0 - (65536.0 * (gulong) g_rand_int (src->gen)
682 / (G_MAXUINT32 + 1.0));
683 sum = pink->running_sum + new_random;
685 /* Scale to range of -1.0 to 0.9999. */
686 return (pink->scalar * sum);
689 #define DEFINE_PINK(type, scale) \
691 gst_audio_test_src_create_pink_noise_##type (GstAudioTestSrc * src, g##type * samples) \
696 amp = src->volume * scale; \
699 while (i < (src->generate_samples_per_buffer * src->channels)) { \
700 for (c = 0; c < src->channels; ++c) { \
702 (g##type) (gst_audio_test_src_generate_pink_noise_value (src) * \
708 DEFINE_PINK (int16, 32767.0);
709 DEFINE_PINK (int32, 2147483647.0);
710 DEFINE_PINK (float, 1.0);
711 DEFINE_PINK (double, 1.0);
713 static const ProcessFunc pink_noise_funcs[] = {
714 (ProcessFunc) gst_audio_test_src_create_pink_noise_int16,
715 (ProcessFunc) gst_audio_test_src_create_pink_noise_int32,
716 (ProcessFunc) gst_audio_test_src_create_pink_noise_float,
717 (ProcessFunc) gst_audio_test_src_create_pink_noise_double
721 gst_audio_test_src_init_sine_table (GstAudioTestSrc * src)
725 gdouble step = M_PI_M2 / 1024.0;
726 gdouble amp = src->volume;
728 for (i = 0; i < 1024; i++) {
729 src->wave_table[i] = sin (ang) * amp;
734 #define DEFINE_SINE_TABLE(type,scale) \
736 gst_audio_test_src_create_sine_table_##type (GstAudioTestSrc * src, g##type * samples) \
741 step = M_PI_M2 * src->freq / src->samplerate; \
742 scl = 1024.0 / M_PI_M2; \
745 while (i < (src->generate_samples_per_buffer * src->channels)) { \
746 src->accumulator += step; \
747 if (src->accumulator >= M_PI_M2) \
748 src->accumulator -= M_PI_M2; \
750 for (c = 0; c < src->channels; ++c) \
751 samples[i++] = (g##type) scale * src->wave_table[(gint) (src->accumulator * scl)]; \
755 DEFINE_SINE_TABLE (int16, 32767.0);
756 DEFINE_SINE_TABLE (int32, 2147483647.0);
757 DEFINE_SINE_TABLE (float, 1.0);
758 DEFINE_SINE_TABLE (double, 1.0);
760 static const ProcessFunc sine_table_funcs[] = {
761 (ProcessFunc) gst_audio_test_src_create_sine_table_int16,
762 (ProcessFunc) gst_audio_test_src_create_sine_table_int32,
763 (ProcessFunc) gst_audio_test_src_create_sine_table_float,
764 (ProcessFunc) gst_audio_test_src_create_sine_table_double
767 #define DEFINE_TICKS(type,scale) \
769 gst_audio_test_src_create_tick_##type (GstAudioTestSrc * src, g##type * samples) \
774 step = M_PI_M2 * src->freq / src->samplerate; \
775 scl = 1024.0 / M_PI_M2; \
777 for (i = 0; i < src->generate_samples_per_buffer; i++) { \
778 src->accumulator += step; \
779 if (src->accumulator >= M_PI_M2) \
780 src->accumulator -= M_PI_M2; \
782 if ((src->next_sample + i)%src->samplerate < 1600) { \
783 for (c = 0; c < src->channels; ++c) \
784 samples[(i * src->channels) + c] = (g##type) scale * src->wave_table[(gint) (src->accumulator * scl)]; \
786 for (c = 0; c < src->channels; ++c) \
787 samples[(i * src->channels) + c] = 0; \
792 DEFINE_TICKS (int16, 32767.0);
793 DEFINE_TICKS (int32, 2147483647.0);
794 DEFINE_TICKS (float, 1.0);
795 DEFINE_TICKS (double, 1.0);
797 static const ProcessFunc tick_funcs[] = {
798 (ProcessFunc) gst_audio_test_src_create_tick_int16,
799 (ProcessFunc) gst_audio_test_src_create_tick_int32,
800 (ProcessFunc) gst_audio_test_src_create_tick_float,
801 (ProcessFunc) gst_audio_test_src_create_tick_double
804 /* Gaussian white noise using Box-Muller algorithm. unit variance
805 * normally-distributed random numbers are generated in pairs as the real
806 * and imaginary parts of a compex random variable with
807 * uniformly-distributed argument and \chi^{2}-distributed modulus.
810 #define DEFINE_GAUSSIAN_WHITE_NOISE(type,scale) \
812 gst_audio_test_src_create_gaussian_white_noise_##type (GstAudioTestSrc * src, g##type * samples) \
815 gdouble amp = (src->volume * scale); \
817 for (i = 0; i < src->generate_samples_per_buffer * src->channels; ) { \
818 for (c = 0; c < src->channels; ++c) { \
819 gdouble mag = sqrt (-2 * log (1.0 - g_rand_double (src->gen))); \
820 gdouble phs = g_rand_double_range (src->gen, 0.0, M_PI_M2); \
822 samples[i++] = (g##type) (amp * mag * cos (phs)); \
823 if (++c >= src->channels) \
825 samples[i++] = (g##type) (amp * mag * sin (phs)); \
830 DEFINE_GAUSSIAN_WHITE_NOISE (int16, 32767.0);
831 DEFINE_GAUSSIAN_WHITE_NOISE (int32, 2147483647.0);
832 DEFINE_GAUSSIAN_WHITE_NOISE (float, 1.0);
833 DEFINE_GAUSSIAN_WHITE_NOISE (double, 1.0);
835 static const ProcessFunc gaussian_white_noise_funcs[] = {
836 (ProcessFunc) gst_audio_test_src_create_gaussian_white_noise_int16,
837 (ProcessFunc) gst_audio_test_src_create_gaussian_white_noise_int32,
838 (ProcessFunc) gst_audio_test_src_create_gaussian_white_noise_float,
839 (ProcessFunc) gst_audio_test_src_create_gaussian_white_noise_double
842 /* Brownian (Red) Noise: noise where the power density decreases by 6 dB per
843 * octave with increasing frequency
845 * taken from http://vellocet.com/dsp/noise/VRand.html
846 * by Andrew Simper of Vellocet (andy@vellocet.com)
849 #define DEFINE_RED_NOISE(type,scale) \
851 gst_audio_test_src_create_red_noise_##type (GstAudioTestSrc * src, g##type * samples) \
854 gdouble amp = (src->volume * scale); \
855 gdouble state = src->red.state; \
857 for (i = 0; i < src->generate_samples_per_buffer * src->channels; ) { \
858 for (c = 0; c < src->channels; ++c) { \
860 gdouble r = g_rand_double_range (src->gen, -1.0, 1.0); \
862 if (state<-8.0f || state>8.0f) state -= r; \
865 samples[i++] = (g##type) (amp * state * 0.0625f); /* /16.0 */ \
868 src->red.state = state; \
871 DEFINE_RED_NOISE (int16, 32767.0);
872 DEFINE_RED_NOISE (int32, 2147483647.0);
873 DEFINE_RED_NOISE (float, 1.0);
874 DEFINE_RED_NOISE (double, 1.0);
876 static const ProcessFunc red_noise_funcs[] = {
877 (ProcessFunc) gst_audio_test_src_create_red_noise_int16,
878 (ProcessFunc) gst_audio_test_src_create_red_noise_int32,
879 (ProcessFunc) gst_audio_test_src_create_red_noise_float,
880 (ProcessFunc) gst_audio_test_src_create_red_noise_double
883 /* Blue Noise: apply spectral inversion to pink noise */
885 #define DEFINE_BLUE_NOISE(type) \
887 gst_audio_test_src_create_blue_noise_##type (GstAudioTestSrc * src, g##type * samples) \
890 static gdouble flip=1.0; \
892 gst_audio_test_src_create_pink_noise_##type (src, samples); \
893 for (i = 0; i < src->generate_samples_per_buffer * src->channels; ) { \
894 for (c = 0; c < src->channels; ++c) { \
895 samples[i++] *= flip; \
901 DEFINE_BLUE_NOISE (int16);
902 DEFINE_BLUE_NOISE (int32);
903 DEFINE_BLUE_NOISE (float);
904 DEFINE_BLUE_NOISE (double);
906 static const ProcessFunc blue_noise_funcs[] = {
907 (ProcessFunc) gst_audio_test_src_create_blue_noise_int16,
908 (ProcessFunc) gst_audio_test_src_create_blue_noise_int32,
909 (ProcessFunc) gst_audio_test_src_create_blue_noise_float,
910 (ProcessFunc) gst_audio_test_src_create_blue_noise_double
914 /* Violet Noise: apply spectral inversion to red noise */
916 #define DEFINE_VIOLET_NOISE(type) \
918 gst_audio_test_src_create_violet_noise_##type (GstAudioTestSrc * src, g##type * samples) \
921 static gdouble flip=1.0; \
923 gst_audio_test_src_create_red_noise_##type (src, samples); \
924 for (i = 0; i < src->generate_samples_per_buffer * src->channels; ) { \
925 for (c = 0; c < src->channels; ++c) { \
926 samples[i++] *= flip; \
932 DEFINE_VIOLET_NOISE (int16);
933 DEFINE_VIOLET_NOISE (int32);
934 DEFINE_VIOLET_NOISE (float);
935 DEFINE_VIOLET_NOISE (double);
937 static const ProcessFunc violet_noise_funcs[] = {
938 (ProcessFunc) gst_audio_test_src_create_violet_noise_int16,
939 (ProcessFunc) gst_audio_test_src_create_violet_noise_int32,
940 (ProcessFunc) gst_audio_test_src_create_violet_noise_float,
941 (ProcessFunc) gst_audio_test_src_create_violet_noise_double
946 * gst_audio_test_src_change_wave:
947 * Assign function pointer of wave genrator.
950 gst_audio_test_src_change_wave (GstAudioTestSrc * src)
952 if (src->format == -1) {
958 case GST_AUDIO_TEST_SRC_WAVE_SINE:
959 src->process = sine_funcs[src->format];
961 case GST_AUDIO_TEST_SRC_WAVE_SQUARE:
962 src->process = square_funcs[src->format];
964 case GST_AUDIO_TEST_SRC_WAVE_SAW:
965 src->process = saw_funcs[src->format];
967 case GST_AUDIO_TEST_SRC_WAVE_TRIANGLE:
968 src->process = triangle_funcs[src->format];
970 case GST_AUDIO_TEST_SRC_WAVE_SILENCE:
971 src->process = silence_funcs[src->format];
973 case GST_AUDIO_TEST_SRC_WAVE_WHITE_NOISE:
975 src->gen = g_rand_new ();
976 src->process = white_noise_funcs[src->format];
978 case GST_AUDIO_TEST_SRC_WAVE_PINK_NOISE:
980 src->gen = g_rand_new ();
981 gst_audio_test_src_init_pink_noise (src);
982 src->process = pink_noise_funcs[src->format];
984 case GST_AUDIO_TEST_SRC_WAVE_SINE_TAB:
985 gst_audio_test_src_init_sine_table (src);
986 src->process = sine_table_funcs[src->format];
988 case GST_AUDIO_TEST_SRC_WAVE_TICKS:
989 gst_audio_test_src_init_sine_table (src);
990 src->process = tick_funcs[src->format];
992 case GST_AUDIO_TEST_SRC_WAVE_GAUSSIAN_WHITE_NOISE:
994 src->gen = g_rand_new ();
995 src->process = gaussian_white_noise_funcs[src->format];
997 case GST_AUDIO_TEST_SRC_WAVE_RED_NOISE:
999 src->gen = g_rand_new ();
1000 src->red.state = 0.0;
1001 src->process = red_noise_funcs[src->format];
1003 case GST_AUDIO_TEST_SRC_WAVE_BLUE_NOISE:
1005 src->gen = g_rand_new ();
1006 gst_audio_test_src_init_pink_noise (src);
1007 src->process = blue_noise_funcs[src->format];
1009 case GST_AUDIO_TEST_SRC_WAVE_VIOLET_NOISE:
1011 src->gen = g_rand_new ();
1012 src->red.state = 0.0;
1013 src->process = violet_noise_funcs[src->format];
1015 GST_ERROR ("invalid wave-form");
1021 * gst_audio_test_src_change_volume:
1022 * Recalc wave tables for precalculated waves.
1025 gst_audio_test_src_change_volume (GstAudioTestSrc * src)
1027 switch (src->wave) {
1028 case GST_AUDIO_TEST_SRC_WAVE_SINE_TAB:
1029 gst_audio_test_src_init_sine_table (src);
1037 gst_audio_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
1038 GstClockTime * start, GstClockTime * end)
1040 /* for live sources, sync on the timestamp of the buffer */
1041 if (gst_base_src_is_live (basesrc)) {
1042 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
1044 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1045 /* get duration to calculate end time */
1046 GstClockTime duration = GST_BUFFER_DURATION (buffer);
1048 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1049 *end = timestamp + duration;
1060 gst_audio_test_src_start (GstBaseSrc * basesrc)
1062 GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (basesrc);
1064 src->next_sample = 0;
1067 src->check_seek_stop = FALSE;
1068 src->eos_reached = FALSE;
1069 src->tags_pushed = FALSE;
1070 src->accumulator = 0;
1076 gst_audio_test_src_stop (GstBaseSrc * basesrc)
1081 /* seek to time, will be called when we operate in push mode. In pull mode we
1082 * get the requested byte offset. */
1084 gst_audio_test_src_do_seek (GstBaseSrc * basesrc, GstSegment * segment)
1086 GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (basesrc);
1089 GST_DEBUG_OBJECT (src, "seeking %" GST_SEGMENT_FORMAT, segment);
1091 time = segment->position;
1092 src->reverse = (segment->rate < 0.0);
1094 /* now move to the time indicated */
1096 gst_util_uint64_scale_int (time, src->samplerate, GST_SECOND);
1097 src->next_byte = src->next_sample * src->sample_size * src->channels;
1099 gst_util_uint64_scale_int (src->next_sample, GST_SECOND, src->samplerate);
1101 GST_DEBUG_OBJECT (src, "seeking next_sample=%" G_GINT64_FORMAT
1102 " next_time=%" GST_TIME_FORMAT, src->next_sample,
1103 GST_TIME_ARGS (src->next_time));
1105 g_assert (src->next_time <= time);
1107 if (!src->reverse) {
1108 if (GST_CLOCK_TIME_IS_VALID (segment->start)) {
1109 segment->time = segment->start;
1112 if (GST_CLOCK_TIME_IS_VALID (segment->stop)) {
1113 segment->time = segment->stop;
1117 if (GST_CLOCK_TIME_IS_VALID (segment->stop)) {
1118 time = segment->stop;
1119 src->sample_stop = gst_util_uint64_scale_int (time, src->samplerate,
1121 src->check_seek_stop = TRUE;
1123 src->check_seek_stop = FALSE;
1125 src->eos_reached = FALSE;
1131 gst_audio_test_src_is_seekable (GstBaseSrc * basesrc)
1133 /* we're seekable... */
1137 static GstFlowReturn
1138 gst_audio_test_src_create (GstBaseSrc * basesrc, guint64 offset,
1139 guint length, GstBuffer ** buffer)
1141 GstAudioTestSrc *src;
1143 GstClockTime next_time;
1144 gint64 next_sample, next_byte;
1145 gint bytes, samples;
1146 GstElementClass *eclass;
1149 src = GST_AUDIO_TEST_SRC (basesrc);
1151 /* example for tagging generated data */
1152 if (!src->tags_pushed) {
1153 GstTagList *taglist;
1155 taglist = gst_tag_list_new ();
1157 gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
1158 GST_TAG_DESCRIPTION, "audiotest wave", NULL);
1160 eclass = GST_ELEMENT_CLASS (parent_class);
1161 if (eclass->send_event)
1162 eclass->send_event (GST_ELEMENT_CAST (basesrc),
1163 gst_event_new_tag (taglist));
1164 src->tags_pushed = TRUE;
1167 if (src->eos_reached) {
1168 GST_INFO_OBJECT (src, "eos");
1169 return GST_FLOW_UNEXPECTED;
1172 /* if no length was given, use our default length in samples otherwise convert
1173 * the length in bytes to samples. */
1175 samples = src->samples_per_buffer;
1177 samples = length / (src->sample_size * src->channels);
1179 /* if no offset was given, use our next logical byte */
1181 offset = src->next_byte;
1183 /* now see if we are at the byteoffset we think we are */
1184 if (offset != src->next_byte) {
1185 GST_DEBUG_OBJECT (src, "seek to new offset %" G_GUINT64_FORMAT, offset);
1186 /* we have a discont in the expected sample offset, do a 'seek' */
1187 src->next_sample = offset / (src->sample_size * src->channels);
1189 gst_util_uint64_scale_int (src->next_sample, GST_SECOND,
1191 src->next_byte = offset;
1195 if (src->check_seek_stop &&
1196 (src->sample_stop > src->next_sample) &&
1197 (src->sample_stop < src->next_sample + samples)
1199 /* calculate only partial buffer */
1200 src->generate_samples_per_buffer = src->sample_stop - src->next_sample;
1201 next_sample = src->sample_stop;
1202 src->eos_reached = TRUE;
1204 /* calculate full buffer */
1205 src->generate_samples_per_buffer = samples;
1206 next_sample = src->next_sample + (src->reverse ? (-samples) : samples);
1209 bytes = src->generate_samples_per_buffer * src->sample_size * src->channels;
1211 buf = gst_buffer_new_and_alloc (bytes);
1213 next_byte = src->next_byte + (src->reverse ? (-bytes) : bytes);
1214 next_time = gst_util_uint64_scale_int (next_sample, GST_SECOND,
1217 GST_LOG_OBJECT (src, "samplerate %d", src->samplerate);
1218 GST_LOG_OBJECT (src, "next_sample %" G_GINT64_FORMAT ", ts %" GST_TIME_FORMAT,
1219 next_sample, GST_TIME_ARGS (next_time));
1221 GST_BUFFER_OFFSET (buf) = src->next_sample;
1222 GST_BUFFER_OFFSET_END (buf) = next_sample;
1223 if (!src->reverse) {
1224 GST_BUFFER_TIMESTAMP (buf) = src->timestamp_offset + src->next_time;
1225 GST_BUFFER_DURATION (buf) = next_time - src->next_time;
1227 GST_BUFFER_TIMESTAMP (buf) = src->timestamp_offset + next_time;
1228 GST_BUFFER_DURATION (buf) = src->next_time - next_time;
1231 gst_object_sync_values (G_OBJECT (src), GST_BUFFER_TIMESTAMP (buf));
1233 src->next_time = next_time;
1234 src->next_sample = next_sample;
1235 src->next_byte = next_byte;
1237 GST_LOG_OBJECT (src, "generating %u samples at ts %" GST_TIME_FORMAT,
1238 src->generate_samples_per_buffer,
1239 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1241 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
1242 src->process (src, data);
1243 gst_buffer_unmap (buf, data, bytes);
1245 if (G_UNLIKELY ((src->wave == GST_AUDIO_TEST_SRC_WAVE_SILENCE)
1246 || (src->volume == 0.0))) {
1247 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_GAP);
1256 gst_audio_test_src_set_property (GObject * object, guint prop_id,
1257 const GValue * value, GParamSpec * pspec)
1259 GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (object);
1262 case PROP_SAMPLES_PER_BUFFER:
1263 src->samples_per_buffer = g_value_get_int (value);
1266 src->wave = g_value_get_enum (value);
1267 gst_audio_test_src_change_wave (src);
1270 src->freq = g_value_get_double (value);
1273 src->volume = g_value_get_double (value);
1274 gst_audio_test_src_change_volume (src);
1277 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
1279 case PROP_TIMESTAMP_OFFSET:
1280 src->timestamp_offset = g_value_get_int64 (value);
1282 case PROP_CAN_ACTIVATE_PUSH:
1283 GST_BASE_SRC (src)->can_activate_push = g_value_get_boolean (value);
1285 case PROP_CAN_ACTIVATE_PULL:
1286 src->can_activate_pull = g_value_get_boolean (value);
1289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1295 gst_audio_test_src_get_property (GObject * object, guint prop_id,
1296 GValue * value, GParamSpec * pspec)
1298 GstAudioTestSrc *src = GST_AUDIO_TEST_SRC (object);
1301 case PROP_SAMPLES_PER_BUFFER:
1302 g_value_set_int (value, src->samples_per_buffer);
1305 g_value_set_enum (value, src->wave);
1308 g_value_set_double (value, src->freq);
1311 g_value_set_double (value, src->volume);
1314 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
1316 case PROP_TIMESTAMP_OFFSET:
1317 g_value_set_int64 (value, src->timestamp_offset);
1319 case PROP_CAN_ACTIVATE_PUSH:
1320 g_value_set_boolean (value, GST_BASE_SRC (src)->can_activate_push);
1322 case PROP_CAN_ACTIVATE_PULL:
1323 g_value_set_boolean (value, src->can_activate_pull);
1326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1332 plugin_init (GstPlugin * plugin)
1334 /* initialize gst controller library */
1335 gst_controller_init (NULL, NULL);
1337 GST_DEBUG_CATEGORY_INIT (audio_test_src_debug, "audiotestsrc", 0,
1338 "Audio Test Source");
1340 return gst_element_register (plugin, "audiotestsrc",
1341 GST_RANK_NONE, GST_TYPE_AUDIO_TEST_SRC);
1344 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1347 "Creates audio test signals of given frequency and volume",
1348 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);