]> err.no Git - mapper/blob - src/audio-note.c
Set basedir on init. Don't expand time display.
[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 <glib.h>
27 #include <gst/gst.h>
28 #include <gtk/gtk.h>
29
30 #include "ui-common.h"
31 #include "audio.h"
32 #include "audio-note.h"
33 #include "settings.h"
34 #include "dialogs.h"
35
36 static note_pipeline note_play;
37 static note_pipeline note_record;
38 static GstBus *bus;
39
40 static gboolean audio_note_stop(audio_note_ui *ui);
41
42 #define GST_TIME_TO_SECS(t) \
43         (gdouble) (((gdouble)(t)) / (gdouble) GST_SECOND) /* GST_SECOND should be 1e9 */
44
45 #define NOTES_BASEDIR_MMC1 "/media/mmc1/MapperAudioNotes"
46 #define NOTES_BASEDIR_MMC2 "/media/mmc2/MapperAudioNotes"
47
48 static void
49 audio_note_set_length(audio_note_ui *ui, gdouble secs)
50 {
51 gchar buffer[16];
52 guint min=0;
53
54 if (secs<0.0) {
55         gtk_label_set_text(GTK_LABEL(ui->lbl_time), "--:--");
56         return;
57 }
58
59 if (secs>=60.0) {
60         min=secs/60.0;
61         secs-=min*60.0;
62 }
63
64 g_snprintf(buffer, sizeof(buffer), "%u:%05.2f", min, secs);
65 gtk_label_set_text(GTK_LABEL(ui->lbl_time), buffer);
66 }
67
68 static gboolean
69 audio_note_position_cb(gpointer data)
70 {
71 GstFormat fmt=GST_FORMAT_TIME;
72 GstElement *pipe;
73 gint64 pos, len;
74 audio_note_ui *ui=(audio_note_ui *)data;
75
76 if (ui->note_record->active)
77         pipe=ui->note_record->pipeline;
78 else
79         if (ui->note_play->active)
80                 pipe=ui->note_play->pipeline;
81 else
82         return TRUE;
83
84 if (gst_element_query_position(pipe, &fmt, &pos) && gst_element_query_duration(pipe, &fmt, &len)) {
85         audio_note_set_length(ui, GST_TIME_TO_SECS(pos));
86 }
87
88 return TRUE;
89 }
90
91 static void
92 audio_note_position_display(audio_note_ui *ui, gboolean ena)
93 {
94 if (ui->pos_sid!=0) {
95         g_source_remove(ui->pos_sid);
96         ui->pos_sid=0;
97 }
98 if (ena)
99         ui->pos_sid=g_timeout_add(250, (GSourceFunc)audio_note_position_cb, ui);
100 }
101
102 static gboolean
103 audio_note_record_cb(GtkWidget *widget, gpointer data)
104 {
105 const gchar *basedir;
106 gchar buffer[128];
107 audio_note_ui *ui=(audio_note_ui *)data;
108 time_t t;
109 struct tm *tmp;
110
111 t=time(NULL);
112 tmp=localtime(&t);
113 if (tmp == NULL) {
114         MACRO_BANNER_SHOW_INFO(_window, _("Failed to get timestamp for file!"));
115         return TRUE;
116 }
117
118 /* Bah, stupid FAT can't use : in filenames */
119 strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H%M%S", tmp);
120
121 if (ui->cfile)
122         g_free(ui->cfile);
123
124 ui->cfile=g_strdup_printf("%s/an-%s.wav", ui->basedir, buffer);
125
126 if (audio_note_record(ui->cfile)==TRUE) {
127         audio_note_position_display(ui, TRUE);
128         MACRO_BANNER_SHOW_INFO(_window, _("Recording..."));
129         gtk_widget_set_sensitive(ui->btn_play, FALSE);
130         gtk_widget_set_sensitive(ui->btn_record, FALSE);
131         gtk_widget_set_sensitive(ui->btn_stop, TRUE);
132 } else {
133         popup_error(_window, _("Failed to start recording."));
134 }
135
136 return TRUE;
137 }
138
139 static gboolean
140 audio_note_play_cb(GtkWidget *widget, gpointer data)
141 {
142 audio_note_ui *ui=(audio_note_ui *)data;
143
144 if (!ui->cfile) {
145         MACRO_BANNER_SHOW_INFO(_window, _("No active audio note. Record something first."));
146         return TRUE;
147 }
148
149 if (audio_note_play(ui->cfile)==TRUE) {
150         audio_note_position_display(ui, TRUE);
151         MACRO_BANNER_SHOW_INFO(_window, _("Playing..."));
152         gtk_widget_set_sensitive(ui->btn_play, FALSE);
153         gtk_widget_set_sensitive(ui->btn_record, FALSE);
154         gtk_widget_set_sensitive(ui->btn_stop, TRUE);
155 } else {
156         popup_error(_window, _("Failed to start playing."));
157 }
158 return TRUE;
159 }
160
161 static gboolean
162 audio_note_stop_cb(GtkWidget *widget, gpointer data)
163 {
164 audio_note_ui *ui=(audio_note_ui *)data;
165
166 if (audio_note_stop(ui)==TRUE)
167         MACRO_BANNER_SHOW_INFO(_window, _("Stopped..."));
168
169 audio_note_position_display(ui, FALSE);
170 gtk_widget_set_sensitive(ui->btn_record, TRUE);
171 gtk_widget_set_sensitive(ui->btn_play, TRUE);
172 gtk_widget_set_sensitive(ui->btn_stop, FALSE);
173
174 return TRUE;
175 }
176
177 audio_note_ui *
178 audio_note_new(void)
179 {
180 audio_note_ui *ui;
181 ui=g_slice_new(audio_note_ui);
182 ui->pos_sid=0;
183 ui->cfile=NULL;
184 ui->vbox=gtk_vbox_new(FALSE, 0);
185 ui->lbl_time=gtk_label_new("");
186 ui->btn_record=gtk_button_new_from_stock(GTK_STOCK_MEDIA_RECORD);
187 ui->btn_play=gtk_button_new_from_stock(GTK_STOCK_MEDIA_PLAY);
188 ui->btn_stop=gtk_button_new_from_stock(GTK_STOCK_MEDIA_STOP);
189
190 gtk_widget_set_sensitive(ui->btn_play, FALSE);
191 gtk_widget_set_sensitive(ui->btn_stop, FALSE);
192
193 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->lbl_time, TRUE, FALSE, 0);
194
195 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->btn_record, TRUE, TRUE, 0);
196 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->btn_play, TRUE, TRUE, 0);
197 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->btn_stop, TRUE, TRUE, 0);
198
199 ui->note_play=&note_play;
200 ui->note_record=&note_record;
201
202 /* XXX: Make this a configuration option */
203 #ifdef WITH_DEVICE_770
204 ui->basedir=NOTES_BASEDIR_MMC1;
205 if (g_mkdir_with_parents(ui->basedir, 0775)==-1) {
206         MACRO_BANNER_SHOW_INFO(_window, _("Failed to create directory for sound files!"));
207         return TRUE;
208 }
209 #else
210 ui->basedir=g_get_home_dir();
211 #endif
212
213 g_signal_connect(G_OBJECT(ui->btn_record), "clicked", G_CALLBACK(audio_note_record_cb), ui);
214 g_signal_connect(G_OBJECT(ui->btn_play), "clicked", G_CALLBACK(audio_note_play_cb), ui);
215 g_signal_connect(G_OBJECT(ui->btn_stop), "clicked", G_CALLBACK(audio_note_stop_cb), ui);
216
217 return ui;
218 }
219
220 static gboolean
221 audio_note_bus_cb(GstBus *bus, GstMessage *msg, gpointer data)
222 {
223 gchar *debug;
224 GError *err;
225 note_pipeline *np=(note_pipeline *)data;
226
227 switch (GST_MESSAGE_TYPE (msg)) {
228         case GST_MESSAGE_EOS:
229                 np->active=FALSE;
230         g_debug("EOS\n");
231         break;
232         case GST_MESSAGE_ERROR:
233                 gst_message_parse_error (msg, &err, &debug);
234                 g_debug("Error: %s", err->message);
235                 g_free(debug);
236                 g_error_free(err);
237                 np->active=FALSE;
238         break;
239         case GST_MESSAGE_STATE_CHANGED:
240                 g_debug("GST: %s", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
241         break;
242     default:
243                 g_debug("GST: %s", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
244         break;
245         }
246 return TRUE;
247 }
248
249 static gboolean
250 audio_new_pad_cb(GstElement *wavparse, GstPad *new_pad, gpointer data)
251 {
252 GstElement *sink=(GstElement *)data;
253
254 if (!gst_element_link(wavparse, sink))
255                 g_printerr("Failed to link wavparse to sink\n");
256
257 gst_element_sync_state_with_parent(sink);
258
259 return FALSE;
260 }
261
262 static gboolean
263 audio_create_pipeline(note_pipeline *np, gboolean rec)
264 {
265 if (rec==TRUE) {
266         g_debug("GST: Creating record pipeline");
267         np->pipeline=gst_pipeline_new("rpipeline");
268         g_assert(np->pipeline);
269
270         np->src=gst_element_factory_make(AUDIO_SRC, "asource");
271         g_assert(np->src);
272
273         np->filter=gst_element_factory_make("wavenc", "wavfilter");
274         g_assert(np->filter);
275
276         np->sink=gst_element_factory_make("filesink", "fsink");
277         g_assert(np->sink);
278
279         gst_bin_add_many(GST_BIN(np->pipeline), np->src, np->filter, np->sink, NULL);
280
281 #ifndef WITH_DEVICE_770
282         /* Don't waste space with some hifi format */
283         np->srccaps=gst_caps_new_simple ("audio/x-raw-int",
284                         "depth", G_TYPE_INT, 16,
285                         "signed", G_TYPE_BOOLEAN, TRUE, 
286                         "width", G_TYPE_INT, 16,
287                         "rate", G_TYPE_INT, 11025,
288                         "channels", G_TYPE_INT, 1,
289                         NULL);
290         if (!gst_element_link_filtered(np->src, np->filter, np->srccaps))
291                 g_printerr("Failed to set caps for source\n");
292         else
293                 gst_element_link(np->src, np->filter);
294         gst_caps_unref(np->srccaps);
295 #else
296         if (!gst_element_link(np->src, np->filter))
297                 g_printerr("Failed to link source to filter\n");
298 #endif
299         if (!gst_element_link(np->filter, np->sink))
300                 g_printerr("Failed to link filter to source\n");
301 } else {
302         g_debug("GST: Creating playback pipeline");
303         np->pipeline=gst_pipeline_new("ppipeline");
304         g_assert(np->pipeline);
305
306         np->src=gst_element_factory_make("filesrc", "fsource");
307         g_assert(np->src);
308
309         np->filter=gst_element_factory_make("wavparse", "wavparse");
310         g_assert(np->filter);
311
312         np->sink=gst_element_factory_make(AUDIO_SINK, "asink");
313         g_assert(np->sink);
314
315         gst_bin_add_many(GST_BIN(np->pipeline), np->src, np->filter, np->sink, NULL);
316
317         if (!gst_element_link(np->src, np->filter))
318                 g_printerr("Failed to link source to filter\n");
319
320         g_signal_connect(np->filter, "pad_added", G_CALLBACK(audio_new_pad_cb), np->sink);
321 }
322 bus=gst_pipeline_get_bus(GST_PIPELINE(np->pipeline));
323 gst_bus_add_watch(bus, audio_note_bus_cb, np);
324 gst_object_unref(bus);
325 np->rec=rec;
326 np->active=FALSE;
327 return TRUE;
328 }
329
330 /**
331  * Set the given elements (filesrc or filesink) file location
332  */
333 static void
334 audio_set_filename(GstElement *e, gchar *file)
335 {
336 g_object_set(G_OBJECT(e), "location", file, NULL);
337 }
338
339 /**
340  * Play given audio note file
341  */
342 gboolean
343 audio_note_play(gchar *file)
344 {
345 if (note_play.active==TRUE)
346         return TRUE;
347 audio_set_filename(note_play.src, file);
348 if (gst_element_set_state(note_play.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
349         g_printerr("Failed to play file %s\n", file);
350         note_play.active=FALSE;
351         return FALSE;
352 }
353 g_debug("Playing");
354 note_play.active=TRUE;
355 return TRUE;
356 }
357
358 /**
359  * Record to given file
360  */
361 gboolean
362 audio_note_record(gchar *file)
363 {
364 if (note_record.active==TRUE)
365         return TRUE;
366 audio_set_filename(note_record.sink, file);
367 if (gst_element_set_state(note_record.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
368         g_printerr("Failed to record to file %s\n", file);
369         note_record.active=FALSE;
370         return FALSE;
371 }
372 g_debug("Recording");
373 note_record.active=TRUE;
374 return TRUE;
375 }
376
377 static gboolean
378 audio_note_stop(audio_note_ui *ui)
379 {
380 GstState current;
381 GstState pending;
382
383 audio_note_position_display(ui, FALSE);
384
385 gst_element_get_state(note_record.pipeline, &current, &pending, GST_CLOCK_TIME_NONE);
386 if (current==GST_STATE_PLAYING) {
387         g_debug("Stop: recording");
388         gst_element_set_state(note_record.pipeline, GST_STATE_NULL);
389         note_record.active=FALSE;
390         return TRUE;
391 }
392
393 gst_element_get_state(note_play.pipeline, &current, &pending, GST_CLOCK_TIME_NONE);
394 if (current==GST_STATE_PLAYING) {
395         g_debug("Stop: playing");
396         gst_element_set_state(note_play.pipeline, GST_STATE_NULL);
397         note_play.active=FALSE;
398         return TRUE;
399 }
400
401 return FALSE;
402 }
403
404 /**
405  * Init gst pipelines for note play and record
406  */
407 gboolean
408 audio_note_init(void)
409 {
410 audio_create_pipeline(&note_play, FALSE);
411 audio_create_pipeline(&note_record, TRUE);
412 return TRUE;
413 }
414
415 void
416 audio_note_deinit(void)
417 {
418 gst_element_set_state(note_play.pipeline, GST_STATE_NULL);
419 gst_object_unref(note_play.pipeline);
420
421 gst_element_set_state(note_record.pipeline, GST_STATE_NULL);
422 gst_object_unref(note_record.pipeline);
423 }
424