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"
35 static note_pipeline note_play;
36 static note_pipeline note_record;
39 static gboolean audio_note_stop(audio_note_ui *ui);
42 audio_note_position_cb(gpointer data)
44 GstFormat fmt=GST_FORMAT_TIME;
48 audio_note_ui *ui=(audio_note_ui *)data;
50 if (ui->note_record->active)
51 pipe=ui->note_record->pipeline;
53 if (ui->note_play->active)
54 pipe=ui->note_play->pipeline;
58 if (gst_element_query_position (pipe, &fmt, &pos) && gst_element_query_duration (pipe, &fmt, &len)) {
59 g_snprintf(buffer, sizeof(buffer), "Time: %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT, GST_TIME_ARGS (pos), GST_TIME_ARGS (len));
60 gtk_label_set_text(GTK_LABEL(ui->lbl_time), buffer);
67 audio_note_position_display(audio_note_ui *ui, gboolean ena)
70 g_source_remove(ui->pos_sid);
74 ui->pos_sid=g_timeout_add(250, (GSourceFunc)audio_note_position_cb, ui);
78 audio_note_record_cb(GtkWidget *widget, gpointer data)
82 audio_note_ui *ui=(audio_note_ui *)data;
86 /* XXX: Make this a configuration option */
87 #ifdef WITH_DEVICE_770
88 basedir="/media/mmc1/MapperAudioNotes";
89 if (g_mkdir_with_parents(basedir, 0775)==-1) {
90 MACRO_BANNER_SHOW_INFO(_window, _("Failed to create directory for sound files!"));
94 basedir=g_get_home_dir();
100 MACRO_BANNER_SHOW_INFO(_window, _("Failed to get timestamp for file!"));
103 strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H:%M:%S", tmp);
108 ui->cfile=g_strdup_printf("%s/an-%s.wav", basedir, buffer);
110 if (audio_note_record(ui->cfile)==TRUE) {
111 audio_note_position_display(ui, TRUE);
112 MACRO_BANNER_SHOW_INFO(_window, _("Recording..."));
113 gtk_widget_set_sensitive(ui->btn_play, FALSE);
114 gtk_widget_set_sensitive(ui->btn_record, FALSE);
115 gtk_widget_set_sensitive(ui->btn_stop, TRUE);
122 audio_note_play_cb(GtkWidget *widget, gpointer data)
124 audio_note_ui *ui=(audio_note_ui *)data;
127 MACRO_BANNER_SHOW_INFO(_window, _("No active audio note."));
131 if (audio_note_play(ui->cfile)==TRUE) {
132 audio_note_position_display(ui, TRUE);
133 MACRO_BANNER_SHOW_INFO(_window, _("Playing..."));
134 gtk_widget_set_sensitive(ui->btn_play, FALSE);
135 gtk_widget_set_sensitive(ui->btn_record, FALSE);
136 gtk_widget_set_sensitive(ui->btn_stop, TRUE);
142 audio_note_stop_cb(GtkWidget *widget, gpointer data)
144 audio_note_ui *ui=(audio_note_ui *)data;
146 if (audio_note_stop(ui)==TRUE)
147 MACRO_BANNER_SHOW_INFO(_window, _("Stopped..."));
149 audio_note_position_display(ui, FALSE);
150 gtk_widget_set_sensitive(ui->btn_record, TRUE);
151 gtk_widget_set_sensitive(ui->btn_play, TRUE);
152 gtk_widget_set_sensitive(ui->btn_stop, FALSE);
161 ui=g_slice_new(audio_note_ui);
164 ui->vbox=gtk_vbox_new(FALSE, 0);
165 ui->lbl_time=gtk_label_new("");
166 ui->btn_record=gtk_button_new_from_stock(GTK_STOCK_MEDIA_RECORD);
167 ui->btn_play=gtk_button_new_from_stock(GTK_STOCK_MEDIA_PLAY);
168 ui->btn_stop=gtk_button_new_from_stock(GTK_STOCK_MEDIA_STOP);
170 gtk_widget_set_sensitive(ui->btn_play, FALSE);
171 gtk_widget_set_sensitive(ui->btn_stop, FALSE);
173 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->lbl_time, TRUE, TRUE, 0);
174 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->btn_record, TRUE, TRUE, 0);
175 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->btn_play, TRUE, TRUE, 0);
176 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->btn_stop, TRUE, TRUE, 0);
178 ui->note_play=¬e_play;
179 ui->note_record=¬e_record;
181 g_signal_connect(G_OBJECT(ui->btn_record), "clicked", G_CALLBACK(audio_note_record_cb), ui);
182 g_signal_connect(G_OBJECT(ui->btn_play), "clicked", G_CALLBACK(audio_note_play_cb), ui);
183 g_signal_connect(G_OBJECT(ui->btn_stop), "clicked", G_CALLBACK(audio_note_stop_cb), ui);
189 audio_note_bus_cb(GstBus *bus, GstMessage *msg, gpointer data)
193 note_pipeline *np=(note_pipeline *)data;
195 switch (GST_MESSAGE_TYPE (msg)) {
196 case GST_MESSAGE_EOS:
200 case GST_MESSAGE_ERROR:
201 gst_message_parse_error (msg, &err, &debug);
202 g_debug("Error: %s", err->message);
207 case GST_MESSAGE_STATE_CHANGED:
208 g_debug("GST: %s", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
211 g_debug("GST: %s", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
218 audio_new_pad_cb(GstElement *wavparse, GstPad *new_pad, gpointer data)
220 GstElement *sink=(GstElement *)data;
222 if (!gst_element_link(wavparse, sink))
223 g_printerr("Failed to link wavparse to sink\n");
225 gst_element_sync_state_with_parent(sink);
231 audio_create_pipeline(note_pipeline *np, gboolean rec)
234 g_debug("GST: Creating record pipeline");
235 np->pipeline=gst_pipeline_new("rpipeline");
236 g_assert(np->pipeline);
238 np->src=gst_element_factory_make(AUDIO_SRC, "asource");
241 np->filter=gst_element_factory_make("wavenc", "wavfilter");
242 g_assert(np->filter);
244 np->sink=gst_element_factory_make("filesink", "fsink");
247 gst_bin_add_many(GST_BIN(np->pipeline), np->src, np->filter, np->sink, NULL);
249 #ifndef WITH_DEVICE_770
250 /* Don't waste space with some hifi format */
251 np->srccaps=gst_caps_new_simple ("audio/x-raw-int",
252 "depth", G_TYPE_INT, 16,
253 "signed", G_TYPE_BOOLEAN, TRUE,
254 "width", G_TYPE_INT, 16,
255 "rate", G_TYPE_INT, 11025,
256 "channels", G_TYPE_INT, 1,
258 if (!gst_element_link_filtered(np->src, np->filter, np->srccaps))
259 g_printerr("Failed to set caps for source\n");
261 gst_element_link(np->src, np->filter);
262 gst_caps_unref(np->srccaps);
264 if (!gst_element_link(np->src, np->filter))
265 g_printerr("Failed to link source to filter\n");
267 if (!gst_element_link(np->filter, np->sink))
268 g_printerr("Failed to link filter to source\n");
270 g_debug("GST: Creating playback pipeline");
271 np->pipeline=gst_pipeline_new("ppipeline");
272 g_assert(np->pipeline);
274 np->src=gst_element_factory_make("filesrc", "fsource");
277 np->filter=gst_element_factory_make("wavparse", "wavparse");
278 g_assert(np->filter);
280 np->sink=gst_element_factory_make(AUDIO_SINK, "asink");
283 gst_bin_add_many(GST_BIN(np->pipeline), np->src, np->filter, np->sink, NULL);
285 if (!gst_element_link(np->src, np->filter))
286 g_printerr("Failed to link source to filter\n");
288 g_signal_connect(np->filter, "pad_added", G_CALLBACK(audio_new_pad_cb), np->sink);
290 bus=gst_pipeline_get_bus(GST_PIPELINE(np->pipeline));
291 gst_bus_add_watch(bus, audio_note_bus_cb, np);
292 gst_object_unref(bus);
299 * Set the given elements (filesrc or filesink) file location
302 audio_set_filename(GstElement *e, gchar *file)
304 g_object_set(G_OBJECT(e), "location", file, NULL);
308 * Play given audio note file
311 audio_note_play(gchar *file)
313 if (note_play.active==TRUE)
315 audio_set_filename(note_play.src, file);
316 if (gst_element_set_state(note_play.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
317 g_printerr("Failed to play file %s\n", file);
318 note_play.active=FALSE;
322 note_play.active=TRUE;
327 * Record to given file
330 audio_note_record(gchar *file)
332 if (note_record.active==TRUE)
334 audio_set_filename(note_record.sink, file);
335 if (gst_element_set_state(note_record.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
336 g_printerr("Failed to record to file %s\n", file);
337 note_record.active=FALSE;
340 g_debug("Recording");
341 note_record.active=TRUE;
346 audio_note_stop(audio_note_ui *ui)
351 audio_note_position_display(ui, FALSE);
353 gst_element_get_state(note_record.pipeline, ¤t, &pending, GST_CLOCK_TIME_NONE);
354 if (current==GST_STATE_PLAYING) {
355 g_debug("Stop: recording");
356 gst_element_set_state(note_record.pipeline, GST_STATE_NULL);
357 note_record.active=FALSE;
361 gst_element_get_state(note_play.pipeline, ¤t, &pending, GST_CLOCK_TIME_NONE);
362 if (current==GST_STATE_PLAYING) {
363 g_debug("Stop: playing");
364 gst_element_set_state(note_play.pipeline, GST_STATE_NULL);
365 note_play.active=FALSE;
373 * Init gst pipelines for note play and record
376 audio_note_init(void)
378 audio_create_pipeline(¬e_play, FALSE);
379 audio_create_pipeline(¬e_record, TRUE);
384 audio_note_deinit(void)
386 gst_element_set_state(note_play.pipeline, GST_STATE_NULL);
387 gst_object_unref(note_play.pipeline);
389 gst_element_set_state(note_record.pipeline, GST_STATE_NULL);
390 gst_object_unref(note_record.pipeline);