2001-06-13 16:20:49 +00:00
|
|
|
/*
|
|
|
|
* cutter.c - cut audio into pieces based on silence - thomas@apestaart.org
|
|
|
|
*
|
2004-05-03 01:06:56 +00:00
|
|
|
* construct a simple pipeline osssrc ! cutter ! filesink
|
2001-06-13 16:20:49 +00:00
|
|
|
* pause when necessary, change output
|
|
|
|
*
|
|
|
|
* Latest change : 03/06/2001
|
|
|
|
*
|
|
|
|
* Version : 0.3
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <unistd.h>
|
2001-06-15 16:42:12 +00:00
|
|
|
#include <time.h>
|
2001-06-13 16:20:49 +00:00
|
|
|
|
|
|
|
#define DEBUG
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gboolean playing = TRUE;
|
|
|
|
gboolean cut_start_signalled = FALSE;
|
|
|
|
gboolean cut_stop_signalled = FALSE;
|
|
|
|
|
2004-03-15 19:27:17 +00:00
|
|
|
int id = 0; /* increment this for each new cut */
|
2004-03-13 15:27:01 +00:00
|
|
|
GstElement *main_bin;
|
|
|
|
GstElement *audiosrc;
|
|
|
|
GstElement *queue;
|
|
|
|
GstElement *thread;
|
|
|
|
GstElement *cutter;
|
2004-05-03 01:06:56 +00:00
|
|
|
GstElement *filesink;
|
2004-03-13 15:27:01 +00:00
|
|
|
GstElement *encoder;
|
|
|
|
char buffer[255];
|
2001-06-13 16:20:49 +00:00
|
|
|
|
|
|
|
/* signal callbacks */
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
void
|
|
|
|
cut_start (GstElement * element)
|
2001-06-13 16:20:49 +00:00
|
|
|
{
|
|
|
|
g_print ("\nDEBUG: main: cut start\n");
|
2004-05-03 01:06:56 +00:00
|
|
|
/* we should pause the pipeline, unlink cutter and filesink
|
|
|
|
* create a new filesink to a real file, relink, and set to play
|
2001-06-13 16:20:49 +00:00
|
|
|
*/
|
2001-06-14 18:04:16 +00:00
|
|
|
g_print ("DEBUG: cut_start: main_bin pausing\n");
|
2001-06-13 16:20:49 +00:00
|
|
|
gst_element_set_state (main_bin, GST_STATE_PAUSED);
|
2001-06-14 18:04:16 +00:00
|
|
|
g_print ("DEBUG: cut_start: main_bin paused\n");
|
2001-06-13 16:20:49 +00:00
|
|
|
|
2001-06-15 16:42:12 +00:00
|
|
|
{
|
2003-02-23 20:15:00 +00:00
|
|
|
time_t seconds;
|
2001-06-15 16:42:12 +00:00
|
|
|
struct tm *ct;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2001-06-15 16:42:12 +00:00
|
|
|
time (&seconds);
|
|
|
|
ct = localtime (&seconds);
|
2002-03-19 04:10:13 +00:00
|
|
|
/* sprintf (buffer, "/news/incoming/audio/cutter.%06d.wav", id); */
|
2004-03-13 15:27:01 +00:00
|
|
|
sprintf (buffer,
|
2004-03-15 19:27:17 +00:00
|
|
|
"/news/incoming/audio/cutter.%04d%02d%02d.%02d%02d%02d.wav",
|
|
|
|
ct->tm_year + 1900, ct->tm_mon, ct->tm_mday, ct->tm_hour, ct->tm_min,
|
|
|
|
ct->tm_sec);
|
2001-06-15 16:42:12 +00:00
|
|
|
}
|
2001-06-13 16:20:49 +00:00
|
|
|
g_print ("DEBUG: cut_start: setting new location to %s\n", buffer);
|
2004-05-03 01:06:56 +00:00
|
|
|
g_object_set (G_OBJECT (filesink), "location", buffer, NULL);
|
|
|
|
g_object_set (G_OBJECT (filesink), "type", 4, NULL);
|
2001-06-13 22:09:58 +00:00
|
|
|
|
2001-06-13 16:20:49 +00:00
|
|
|
gst_element_set_state (main_bin, GST_STATE_PLAYING);
|
|
|
|
++id;
|
|
|
|
g_print ("start_cut_signal done\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
void
|
|
|
|
cut_start_signal (GstElement * element)
|
2001-06-14 18:04:16 +00:00
|
|
|
{
|
|
|
|
g_print ("\nDEBUG: main: cut start signal\n");
|
|
|
|
cut_start_signalled = TRUE;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
void
|
|
|
|
cut_stop (GstElement * element)
|
2001-06-13 16:20:49 +00:00
|
|
|
{
|
|
|
|
g_print ("\nDEBUG: main: cut stop\n");
|
2004-05-03 01:06:56 +00:00
|
|
|
/* we should pause the pipeline, unlink filesink, create a fake filesink,
|
2003-01-09 20:02:34 +00:00
|
|
|
* link to pipeline, and set to play
|
2001-06-13 16:20:49 +00:00
|
|
|
*/
|
|
|
|
g_print ("DEBUG: cut_stop: main_bin paused\n");
|
|
|
|
gst_element_set_state (main_bin, GST_STATE_PAUSED);
|
|
|
|
|
|
|
|
g_print ("DEBUG: cut_stop: setting new location\n");
|
2004-05-03 01:06:56 +00:00
|
|
|
g_object_set (G_OBJECT (filesink), "location", "/dev/null", NULL);
|
2001-06-13 16:20:49 +00:00
|
|
|
|
|
|
|
gst_element_set_state (main_bin, GST_STATE_PLAYING);
|
|
|
|
g_print ("stop_cut_signal done\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
void
|
|
|
|
cut_stop_signal (GstElement * element)
|
2001-06-14 18:04:16 +00:00
|
|
|
{
|
|
|
|
g_print ("\nDEBUG: main: cut stop signal\n");
|
|
|
|
cut_stop_signalled = TRUE;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2001-06-13 16:20:49 +00:00
|
|
|
{
|
2002-03-19 04:10:13 +00:00
|
|
|
/*int i, j; */
|
|
|
|
/*gboolean done; */
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-03-19 04:10:13 +00:00
|
|
|
/*char buffer[20]; */
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-03-19 04:10:13 +00:00
|
|
|
/*output_channel_t *channel_out; */
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2001-06-13 16:20:49 +00:00
|
|
|
GstElement *audiosrc;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_init (&argc, &argv);
|
2001-06-13 16:20:49 +00:00
|
|
|
/*
|
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
g_print("usage: %s <filename1> <filename2> <...>\n", argv[0]);
|
|
|
|
exit(-1);
|
|
|
|
}*/
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2001-06-13 16:20:49 +00:00
|
|
|
/* set up input channel and main bin */
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
g_print ("creating main bin\n");
|
2001-06-13 16:20:49 +00:00
|
|
|
/* create cutter */
|
2002-04-11 20:35:18 +00:00
|
|
|
cutter = gst_element_factory_make ("cutter", "cutter");
|
2001-06-13 16:20:49 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
g_object_set (G_OBJECT (cutter),
|
|
|
|
"threshold_dB", -40.0, "runlength", 0.5, "prelength", 1.0, NULL);
|
2001-06-13 16:20:49 +00:00
|
|
|
|
|
|
|
/* create an audio src */
|
2002-07-11 15:13:09 +00:00
|
|
|
if (!(audiosrc = gst_element_factory_make ("osssrc", "audio_src")))
|
|
|
|
g_error ("Could not create 'osssrc' element !\n");
|
2001-06-13 16:20:49 +00:00
|
|
|
|
|
|
|
/* set params */
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
g_object_set (G_OBJECT (audiosrc), "frequency", 44100,
|
|
|
|
"channels", 1, "format", 16, NULL);
|
2001-06-13 16:20:49 +00:00
|
|
|
|
2002-07-11 15:13:09 +00:00
|
|
|
if (!(encoder = gst_element_factory_make ("passthrough", "encoder")))
|
|
|
|
g_error ("Could not create 'passthrough' element !\n");
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2004-05-03 01:06:56 +00:00
|
|
|
if (!(filesink = gst_element_factory_make ("afsink", "disk_sink")))
|
2002-07-11 15:13:09 +00:00
|
|
|
g_error ("Could not create 'afsink' element !\n");
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2004-05-03 01:06:56 +00:00
|
|
|
g_object_set (G_OBJECT (filesink), "location", "/dev/null", NULL);
|
2001-06-13 16:20:49 +00:00
|
|
|
|
2001-06-14 18:04:16 +00:00
|
|
|
thread = gst_thread_new ("thread");
|
|
|
|
g_assert (thread != NULL);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2001-06-13 16:20:49 +00:00
|
|
|
/* create main bin */
|
|
|
|
main_bin = gst_pipeline_new ("bin");
|
2001-06-14 18:04:16 +00:00
|
|
|
g_assert (main_bin != NULL);
|
|
|
|
|
2002-04-11 20:35:18 +00:00
|
|
|
queue = gst_element_factory_make ("queue", "queue");
|
2002-07-11 15:13:09 +00:00
|
|
|
g_assert (queue);
|
2001-06-13 16:20:49 +00:00
|
|
|
|
|
|
|
/* add elements to bin */
|
|
|
|
gst_bin_add (GST_BIN (main_bin), audiosrc);
|
2001-06-14 18:04:16 +00:00
|
|
|
gst_bin_add (GST_BIN (thread), queue);
|
|
|
|
|
2004-05-03 01:06:56 +00:00
|
|
|
gst_bin_add_many (GST_BIN (thread), cutter, encoder, filesink, NULL);
|
2001-06-13 16:20:49 +00:00
|
|
|
|
2004-05-03 01:06:56 +00:00
|
|
|
gst_element_link_many (audiosrc, queue, cutter, encoder, filesink, NULL);
|
2001-06-14 18:04:16 +00:00
|
|
|
gst_bin_add (GST_BIN (main_bin), thread);
|
|
|
|
|
2001-06-13 16:20:49 +00:00
|
|
|
/* set signal handlers */
|
|
|
|
g_print ("setting signal handlers\n");
|
2004-03-13 15:27:01 +00:00
|
|
|
g_signal_connect (G_OBJECT (cutter), "cut_start",
|
|
|
|
(GCallback) cut_start_signal, NULL);
|
|
|
|
g_signal_connect (G_OBJECT (cutter), "cut_stop",
|
|
|
|
(GCallback) cut_stop_signal, NULL);
|
2001-06-13 16:20:49 +00:00
|
|
|
|
|
|
|
/* start playing */
|
|
|
|
g_print ("setting to play\n");
|
|
|
|
gst_element_set_state (main_bin, GST_STATE_PLAYING);
|
2001-06-14 18:04:16 +00:00
|
|
|
/*
|
|
|
|
g_print ("setting thread to play\n");
|
|
|
|
gst_element_set_state (GST_ELEMENT (thread), GST_STATE_PLAYING);
|
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
while (playing) {
|
2002-03-19 04:10:13 +00:00
|
|
|
/* g_print ("> "); */
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_bin_iterate (GST_BIN (main_bin));
|
2002-03-19 04:10:13 +00:00
|
|
|
/* g_print (" <"); */
|
2004-03-13 15:27:01 +00:00
|
|
|
if (cut_start_signalled) {
|
|
|
|
g_print ("\nDEBUG: main: cut_start_signalled true !\n");
|
|
|
|
cut_start (cutter);
|
|
|
|
cut_start_signalled = FALSE;
|
|
|
|
}
|
|
|
|
if (cut_stop_signalled) {
|
|
|
|
g_print ("\nDEBUG: main: cut_stop_signalled true !\n");
|
|
|
|
cut_stop (cutter);
|
|
|
|
cut_stop_signalled = FALSE;
|
|
|
|
}
|
2001-06-13 16:20:49 +00:00
|
|
|
}
|
|
|
|
g_print ("we're done iterating.\n");
|
|
|
|
/* stop the bin */
|
|
|
|
|
|
|
|
gst_element_set_state (main_bin, GST_STATE_NULL);
|
|
|
|
|
2004-05-03 01:06:56 +00:00
|
|
|
gst_object_unref (GST_OBJECT (filesink));
|
2003-04-15 17:40:56 +00:00
|
|
|
gst_object_unref (GST_OBJECT (main_bin));
|
2001-06-13 16:20:49 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
exit (0);
|
2001-06-13 16:20:49 +00:00
|
|
|
}
|