Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / wearable / tests / examples / controller / audio-example.c
1 /*
2  * audio-example.c
3  *
4  * Builds a pipeline with audiotestsource->alsasink and sweeps frequency and
5  * volume.
6  *
7  * Needs gst-plugin-base installed.
8  */
9
10 #include <gst/gst.h>
11 #include <gst/controller/gstcontroller.h>
12 #include <gst/controller/gstinterpolationcontrolsource.h>
13
14 gint
15 main (gint argc, gchar ** argv)
16 {
17   gint res = 1;
18   GstElement *src, *sink;
19   GstElement *bin;
20   GstController *ctrl;
21   GstInterpolationControlSource *csource1, *csource2;
22   GstClock *clock;
23   GstClockID clock_id;
24   GstClockReturn wait_ret;
25   GValue vol = { 0, };
26
27   gst_init (&argc, &argv);
28   gst_controller_init (&argc, &argv);
29
30   /* build pipeline */
31   bin = gst_pipeline_new ("pipeline");
32   clock = gst_pipeline_get_clock (GST_PIPELINE (bin));
33   src = gst_element_factory_make ("audiotestsrc", "gen_audio");
34   if (!src) {
35     GST_WARNING ("need audiotestsrc from gst-plugins-base");
36     goto Error;
37   }
38   sink = gst_element_factory_make ("autoaudiosink", "play_audio");
39   if (!sink) {
40     GST_WARNING ("need autoaudiosink from gst-plugins-base");
41     goto Error;
42   }
43
44   gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
45   if (!gst_element_link (src, sink)) {
46     GST_WARNING ("can't link elements");
47     goto Error;
48   }
49
50   /* square wave
51      g_object_set (G_OBJECT(src), "wave", 1, NULL);
52    */
53
54   /* add a controller to the source */
55   if (!(ctrl = gst_controller_new (G_OBJECT (src), "freq", "volume", NULL))) {
56     GST_WARNING ("can't control source element");
57     goto Error;
58   }
59
60   csource1 = gst_interpolation_control_source_new ();
61   csource2 = gst_interpolation_control_source_new ();
62
63   gst_controller_set_control_source (ctrl, "volume",
64       GST_CONTROL_SOURCE (csource1));
65   gst_controller_set_control_source (ctrl, "freq",
66       GST_CONTROL_SOURCE (csource2));
67
68   /* Set interpolation mode */
69
70   gst_interpolation_control_source_set_interpolation_mode (csource1,
71       GST_INTERPOLATE_LINEAR);
72   gst_interpolation_control_source_set_interpolation_mode (csource2,
73       GST_INTERPOLATE_LINEAR);
74
75   /* set control values */
76   g_value_init (&vol, G_TYPE_DOUBLE);
77   g_value_set_double (&vol, 0.0);
78   gst_interpolation_control_source_set (csource1, 0 * GST_SECOND, &vol);
79   g_value_set_double (&vol, 1.0);
80   gst_interpolation_control_source_set (csource1, 5 * GST_SECOND, &vol);
81
82   g_object_unref (csource1);
83
84   g_value_set_double (&vol, 220.0);
85   gst_interpolation_control_source_set (csource2, 0 * GST_SECOND, &vol);
86   g_value_set_double (&vol, 3520.0);
87   gst_interpolation_control_source_set (csource2, 3 * GST_SECOND, &vol);
88   g_value_set_double (&vol, 440.0);
89   gst_interpolation_control_source_set (csource2, 6 * GST_SECOND, &vol);
90
91   g_object_unref (csource2);
92
93   clock_id =
94       gst_clock_new_single_shot_id (clock,
95       gst_clock_get_time (clock) + (7 * GST_SECOND));
96
97   /* run for 7 seconds */
98   if (gst_element_set_state (bin, GST_STATE_PLAYING)) {
99     if ((wait_ret = gst_clock_id_wait (clock_id, NULL)) != GST_CLOCK_OK) {
100       GST_WARNING ("clock_id_wait returned: %d", wait_ret);
101     }
102     gst_element_set_state (bin, GST_STATE_NULL);
103   }
104
105   /* cleanup */
106   g_object_unref (G_OBJECT (ctrl));
107   gst_clock_id_unref (clock_id);
108   gst_object_unref (G_OBJECT (clock));
109   gst_object_unref (G_OBJECT (bin));
110   res = 0;
111 Error:
112   return (res);
113 }