2 * cutter.c - cut audio into pieces based on silence - thomas@apestaart.org
4 * construct a simple pipeline osssrc ! cutter ! filesink
5 * pause when necessary, change output
7 * Latest change : 03/06/2001
19 gboolean playing = TRUE;
20 gboolean cut_start_signalled = FALSE;
21 gboolean cut_stop_signalled = FALSE;
23 int id = 0; /* increment this for each new cut */
33 /* signal callbacks */
36 cut_start (GstElement * element)
38 g_print ("\nDEBUG: main: cut start\n");
39 /* we should pause the pipeline, unlink cutter and filesink
40 * create a new filesink to a real file, relink, and set to play
42 g_print ("DEBUG: cut_start: main_bin pausing\n");
43 gst_element_set_state (main_bin, GST_STATE_PAUSED);
44 g_print ("DEBUG: cut_start: main_bin paused\n");
51 ct = localtime (&seconds);
52 /* sprintf (buffer, "/news/incoming/audio/cutter.%06d.wav", id); */
54 "/news/incoming/audio/cutter.%04d%02d%02d.%02d%02d%02d.wav",
55 ct->tm_year + 1900, ct->tm_mon, ct->tm_mday, ct->tm_hour, ct->tm_min,
58 g_print ("DEBUG: cut_start: setting new location to %s\n", buffer);
59 g_object_set (G_OBJECT (filesink), "location", buffer, NULL);
60 g_object_set (G_OBJECT (filesink), "type", 4, NULL);
62 gst_element_set_state (main_bin, GST_STATE_PLAYING);
64 g_print ("start_cut_signal done\n");
69 cut_start_signal (GstElement * element)
71 g_print ("\nDEBUG: main: cut start signal\n");
72 cut_start_signalled = TRUE;
76 cut_stop (GstElement * element)
78 g_print ("\nDEBUG: main: cut stop\n");
79 /* we should pause the pipeline, unlink filesink, create a fake filesink,
80 * link to pipeline, and set to play
82 g_print ("DEBUG: cut_stop: main_bin paused\n");
83 gst_element_set_state (main_bin, GST_STATE_PAUSED);
85 g_print ("DEBUG: cut_stop: setting new location\n");
86 g_object_set (G_OBJECT (filesink), "location", "/dev/null", NULL);
88 gst_element_set_state (main_bin, GST_STATE_PLAYING);
89 g_print ("stop_cut_signal done\n");
94 cut_stop_signal (GstElement * element)
96 g_print ("\nDEBUG: main: cut stop signal\n");
97 cut_stop_signalled = TRUE;
101 main (int argc, char *argv[])
106 /*char buffer[20]; */
108 /*output_channel_t *channel_out; */
110 GstElement *audiosrc;
112 gst_init (&argc, &argv);
116 g_print("usage: %s <filename1> <filename2> <...>\n", argv[0]);
120 /* set up input channel and main bin */
122 g_print ("creating main bin\n");
124 cutter = gst_element_factory_make ("cutter", "cutter");
126 g_object_set (G_OBJECT (cutter),
127 "threshold_dB", -40.0, "runlength", 0.5, "prelength", 1.0, NULL);
129 /* create an audio src */
130 if (!(audiosrc = gst_element_factory_make ("osssrc", "audio_src")))
131 g_error ("Could not create 'osssrc' element !\n");
135 g_object_set (G_OBJECT (audiosrc), "frequency", 44100,
136 "channels", 1, "format", 16, NULL);
138 if (!(encoder = gst_element_factory_make ("passthrough", "encoder")))
139 g_error ("Could not create 'passthrough' element !\n");
141 if (!(filesink = gst_element_factory_make ("afsink", "disk_sink")))
142 g_error ("Could not create 'afsink' element !\n");
144 g_object_set (G_OBJECT (filesink), "location", "/dev/null", NULL);
146 thread = gst_thread_new ("thread");
147 g_assert (thread != NULL);
149 /* create main bin */
150 main_bin = gst_pipeline_new ("bin");
151 g_assert (main_bin != NULL);
153 queue = gst_element_factory_make ("queue", "queue");
156 /* add elements to bin */
157 gst_bin_add (GST_BIN (main_bin), audiosrc);
158 gst_bin_add (GST_BIN (thread), queue);
160 gst_bin_add_many (GST_BIN (thread), cutter, encoder, filesink, NULL);
162 gst_element_link_many (audiosrc, queue, cutter, encoder, filesink, NULL);
163 gst_bin_add (GST_BIN (main_bin), thread);
165 /* set signal handlers */
166 g_print ("setting signal handlers\n");
167 g_signal_connect (G_OBJECT (cutter), "cut_start",
168 (GCallback) cut_start_signal, NULL);
169 g_signal_connect (G_OBJECT (cutter), "cut_stop",
170 (GCallback) cut_stop_signal, NULL);
173 g_print ("setting to play\n");
174 gst_element_set_state (main_bin, GST_STATE_PLAYING);
176 g_print ("setting thread to play\n");
177 gst_element_set_state (GST_ELEMENT (thread), GST_STATE_PLAYING);
180 /* g_print ("> "); */
181 gst_bin_iterate (GST_BIN (main_bin));
182 /* g_print (" <"); */
183 if (cut_start_signalled) {
184 g_print ("\nDEBUG: main: cut_start_signalled true !\n");
186 cut_start_signalled = FALSE;
188 if (cut_stop_signalled) {
189 g_print ("\nDEBUG: main: cut_stop_signalled true !\n");
191 cut_stop_signalled = FALSE;
194 g_print ("we're done iterating.\n");
197 gst_element_set_state (main_bin, GST_STATE_NULL);
199 gst_object_unref (GST_OBJECT (filesink));
200 gst_object_unref (GST_OBJECT (main_bin));