]> err.no Git - mapper/blob - src/audio-note.c
Move some variables around
[mapper] / src / audio-note.c
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2007 Kaj-Michael Lang
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 /*
22  * Quick record audio notes on the road.
23  */
24
25 #include "config.h"
26 #include <gst/gst.h>
27
28 #include "audio-note.h"
29
30 #ifdef WITH_DEVICE_770
31 #define AUDIO_SRC "dsppcmsrc"
32 #define AUDIO_SINK "dsppcmsink"
33 #else
34 #define AUDIO_SRC "alsasrc"
35 #define AUDIO_SINK "autoaudiosink"
36 #endif
37
38 typedef struct _note_pipeline note_pipeline;
39 struct _note_pipeline {
40         GstElement *pipeline;
41         GstElement *src;
42         GstElement *sink;
43         GstElement *filter;
44         gboolean rec;
45 };
46 static note_pipeline note_play;
47 static note_pipeline note_record;
48
49 static GstBus *bus;
50
51 static gboolean
52 audio_note_bus_cb(GstBus *bus, GstMessage *msg, gpointer data)
53 {
54 gchar *debug;
55 GError *err;
56
57 switch (GST_MESSAGE_TYPE (msg)) {
58         case GST_MESSAGE_EOS:
59         g_print ("EOS\n");
60         break;
61         case GST_MESSAGE_ERROR:
62                 gst_message_parse_error (msg, &err, &debug);
63                 g_free(debug);
64
65                 g_printf("Error: %s\n", err->message);
66                 g_error_free(err);
67         break;
68         case GST_MESSAGE_STATE_CHANGED:
69                 g_printf("State changed\n");
70         break;
71     default:
72                 g_printf("GST: %s\n", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
73         break;
74         }
75 return TRUE;
76 }
77
78 static gboolean
79 audio_create_pipeline(note_pipeline *np, gboolean rec)
80 {
81 np->pipeline=gst_pipeline_new("pipeline");
82 g_assert(np->pipeline);
83 if (rec==TRUE) {
84         np->src=gst_element_factory_make(AUDIO_SRC, "source");
85         np->filter=gst_element_factory_make("wavenc", "filter");
86         np->sink=gst_element_factory_make("filesink", "sink");
87 } else {
88         np->src=gst_element_factory_make("filesrc", "source");
89         np->filter=gst_element_factory_make("wavenc", "filter");
90         np->sink=gst_element_factory_make(AUDIO_SINK, "sink");
91 }
92 np->rec=rec;
93 g_assert(np->src);
94 g_assert(np->sink);
95 g_assert(np->filter);
96 return TRUE;
97 }
98
99 /**
100  * Set the given elements (filesrc or filesink) file location
101  */
102 static void
103 audio_set_filename(GstElement *e, gchar *file)
104 {
105 g_object_set(G_OBJECT(e), "location", file, NULL);
106 }
107
108 /**
109  * Play given audio note file
110  */
111 gboolean
112 audio_note_play(gchar *file)
113 {
114 audio_set_filename(note_play.src, file);
115 if (gst_element_set_state(note_play.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
116         g_printf("Failed to play file %s\n", file);
117         return FALSE;
118 }
119 return TRUE;
120 }
121
122 /**
123  * Record to given file
124  */
125 gboolean
126 audio_note_record(gchar *file)
127 {
128 audio_set_filename(note_record.sink, file);
129 if (gst_element_set_state(note_record.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
130         g_printf("Failed to record to file %s\n", file);
131         return FALSE;
132 }
133 return TRUE;
134 }
135
136 gboolean
137 audio_note_stop()
138 {
139 return TRUE;
140 }
141
142 /**
143  * Init gst pipelines for note play and record
144  */
145 gboolean
146 audio_note_init(void)
147 {
148 audio_create_pipeline(&note_play, FALSE);
149 audio_create_pipeline(&note_record, TRUE);
150
151 bus=gst_pipeline_get_bus(GST_PIPELINE(note_play.pipeline));
152 g_assert(bus);
153 gst_bus_add_watch(bus, audio_note_bus_cb, NULL);
154
155 bus=gst_pipeline_get_bus(GST_PIPELINE(note_record.pipeline));
156 g_assert(bus);
157 gst_bus_add_watch(bus, audio_note_bus_cb, NULL);
158
159 return TRUE;
160 }
161
162 void
163 audio_note_deinit(void)
164 {
165 gst_element_set_state(note_play.pipeline, GST_STATE_NULL);
166 gst_object_unref(note_play.pipeline);
167
168 gst_element_set_state(note_record.pipeline, GST_STATE_NULL);
169 gst_object_unref(note_record.pipeline);
170 }