2 * This file is part of mapper
4 * Copyright (C) 2007 Kaj-Michael Lang
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.
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.
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.
22 * Quick record audio notes on the road.
30 #include "ui-common.h"
32 #include "audio-note.h"
34 static note_pipeline note_play;
35 static note_pipeline note_record;
37 static gchar *cfile=NULL;
40 audio_note_position_cb(gpointer data)
42 GstFormat fmt=GST_FORMAT_TIME;
46 audio_note_ui *ui=(audio_note_ui *)data;
48 pipe=ui->note_record->pipeline;
50 if (gst_element_query_position (pipe, &fmt, &pos) && gst_element_query_duration (pipe, &fmt, &len)) {
51 g_snprintf(buffer, sizeof(buffer), "Time: %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT, GST_TIME_ARGS (pos), GST_TIME_ARGS (len));
52 gtk_label_set_text(ui->lbl_time, buffer);
59 audio_note_record_cb(GtkWidget *widget, gpointer data)
63 audio_note_ui *ui=(audio_note_ui *)data;
68 /* XXX: Make this a configuration option */
69 #ifdef WITH_DEVICE_770
70 basedir="/media/mmc1/MapperAudioNotes";
71 if (g_mkdir_with_parents(basedir, 0775)==-1) {
72 MACRO_BANNER_SHOW_INFO(_window, _("Failed to create directory for sound files!"));
76 basedir=g_get_home_dir();
82 MACRO_BANNER_SHOW_INFO(_window, _("Failed to get timestamp for file!"));
85 strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H:%M:%S", tmp);
90 cfile=g_strdup_printf("%s/an-%s.wav", basedir, buffer);
92 audio_note_record(cfile);
93 ui->pos_sid=g_timeout_add (250, (GSourceFunc)audio_note_position_cb, ui);
95 MACRO_BANNER_SHOW_INFO(_window, _("Recording..."));
101 audio_note_play_cb(GtkWidget *widget, gpointer data)
103 audio_note_ui *ui=(audio_note_ui *)data;
105 audio_note_play(cfile);
106 MACRO_BANNER_SHOW_INFO(_window, _("Playing..."));
111 audio_note_stop_cb(GtkWidget *widget, gpointer data)
113 if (audio_note_stop()==TRUE)
114 MACRO_BANNER_SHOW_INFO(_window, _("Stopped..."));
122 ui=g_slice_new(audio_note_ui);
124 ui->vbox=gtk_vbox_new(FALSE, 0);
125 ui->lbl_time=gtk_label_new("");
126 ui->btn_record=gtk_button_new_from_stock(GTK_STOCK_MEDIA_RECORD);
127 ui->btn_play=gtk_button_new_from_stock(GTK_STOCK_MEDIA_PLAY);
128 ui->btn_stop=gtk_button_new_from_stock(GTK_STOCK_MEDIA_STOP);
130 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->lbl_time, TRUE, TRUE, 0);
131 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->btn_record, TRUE, TRUE, 0);
132 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->btn_play, TRUE, TRUE, 0);
133 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->btn_stop, TRUE, TRUE, 0);
135 ui->note_play=¬e_play;
136 ui->note_record=¬e_record;
138 g_signal_connect(G_OBJECT(ui->btn_record), "clicked", G_CALLBACK(audio_note_record_cb), ui);
139 g_signal_connect(G_OBJECT(ui->btn_play), "clicked", G_CALLBACK(audio_note_play_cb), ui);
140 g_signal_connect(G_OBJECT(ui->btn_stop), "clicked", G_CALLBACK(audio_note_stop_cb), ui);
146 audio_note_bus_cb(GstBus *bus, GstMessage *msg, gpointer data)
150 note_pipeline *np=(note_pipeline *)data;
152 switch (GST_MESSAGE_TYPE (msg)) {
153 case GST_MESSAGE_EOS:
157 case GST_MESSAGE_ERROR:
158 gst_message_parse_error (msg, &err, &debug);
159 g_debug("Error: %s", err->message);
164 case GST_MESSAGE_STATE_CHANGED:
165 g_debug("GST: %s", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
168 g_debug("GST: %s", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
175 audio_create_pipeline(note_pipeline *np, gboolean rec)
178 g_debug("GST: Creating record pipeline");
179 np->pipeline=gst_pipeline_new("rpipeline");
180 g_assert(np->pipeline);
182 np->src=gst_element_factory_make(AUDIO_SRC, "asource");
185 np->filter=gst_element_factory_make("wavenc", "wavfilter");
186 g_assert(np->filter);
188 np->sink=gst_element_factory_make("filesink", "fsink");
191 gst_bin_add_many(GST_BIN(np->pipeline), np->src, np->filter, np->sink, NULL);
193 #ifndef WITH_DEVICE_770
194 /* Don't waste space with some hifi format */
195 np->srccaps=gst_caps_new_simple ("audio/x-raw-int",
196 "depth", G_TYPE_INT, 16,
197 "signed", G_TYPE_BOOLEAN, TRUE,
198 "width", G_TYPE_INT, 16,
199 "rate", G_TYPE_INT, 11025,
200 "channels", G_TYPE_INT, 1,
202 if (!gst_element_link_filtered(np->src, np->filter, np->srccaps))
203 g_printerr("Failed to set caps for source\n");
205 gst_element_link(np->src, np->filter);
206 gst_caps_unref(np->srccaps);
208 if (!gst_element_link(np->src, np->filter))
209 g_printerr("Failed to link source to filter\n");
211 if (!gst_element_link(np->filter, np->sink))
212 g_printerr("Failed to link filter to source\n");
214 g_debug("GST: Creating playback pipeline");
215 np->pipeline=gst_pipeline_new("ppipeline");
216 g_assert(np->pipeline);
218 np->src=gst_element_factory_make("filesrc", "fsource");
221 np->filter=gst_element_factory_make("wavparse", "wavparse");
222 g_assert(np->filter);
224 np->sink=gst_element_factory_make(AUDIO_SINK, "asink");
227 gst_bin_add_many(GST_BIN(np->pipeline), np->src, np->filter, np->sink, NULL);
229 if (!gst_element_link(np->src, np->filter))
230 g_printerr("Failed to link source to filter\n");
231 if (!gst_element_link(np->filter, np->sink))
232 g_printerr("Failed to link filter to source\n");
234 bus=gst_pipeline_get_bus(GST_PIPELINE(np->pipeline));
235 gst_bus_add_watch(bus, audio_note_bus_cb, np);
236 gst_object_unref(bus);
243 * Set the given elements (filesrc or filesink) file location
246 audio_set_filename(GstElement *e, gchar *file)
248 g_object_set(G_OBJECT(e), "location", file, NULL);
252 * Play given audio note file
255 audio_note_play(gchar *file)
257 if (note_play.active==TRUE)
259 audio_set_filename(note_play.src, file);
260 if (gst_element_set_state(note_play.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
261 g_printerr("Failed to play file %s\n", file);
262 note_play.active=FALSE;
266 note_play.active=TRUE;
271 * Record to given file
274 audio_note_record(gchar *file)
276 if (note_record.active==TRUE)
278 audio_set_filename(note_record.sink, file);
279 if (gst_element_set_state(note_record.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
280 g_printerr("Failed to record to file %s\n", file);
281 note_record.active=FALSE;
284 g_debug("Recording");
285 note_record.active=TRUE;
295 gst_element_get_state(note_record.pipeline, ¤t, &pending, GST_CLOCK_TIME_NONE);
296 if (current==GST_STATE_PLAYING) {
297 gst_element_set_state(note_record.pipeline, GST_STATE_PAUSED);
298 note_record.active=FALSE;
302 gst_element_get_state(note_play.pipeline, ¤t, &pending, GST_CLOCK_TIME_NONE);
303 if (current==GST_STATE_PLAYING) {
304 gst_element_set_state(note_play.pipeline, GST_STATE_PAUSED);
305 note_play.active=FALSE;
313 * Init gst pipelines for note play and record
316 audio_note_init(void)
318 audio_create_pipeline(¬e_play, FALSE);
319 audio_create_pipeline(¬e_record, TRUE);
324 audio_note_deinit(void)
326 gst_element_set_state(note_play.pipeline, GST_STATE_NULL);
327 gst_object_unref(note_play.pipeline);
329 gst_element_set_state(note_record.pipeline, GST_STATE_NULL);
330 gst_object_unref(note_record.pipeline);