1 // sourceaudio.c
2 //
3 // Copyright 2013 Richard Shann <rshann@virgin.net>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 // MA 02110-1301, USA.
19
20
21 #include <stdio.h>
22 #include <sndfile.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include "export/audiofile.h"
26 #include "export/file.h"
27 #include "core/prefops.h"
28 #include "core/utils.h"
29
30 const gchar *
recorded_audio_filename(void)31 recorded_audio_filename (void)
32 {
33 static gchar *filename;
34 if (filename == NULL)
35 filename = g_build_filename (get_user_data_dir (TRUE), "denemo_audio.1channel-floats", NULL);
36 return filename;
37 }
38
39 gboolean
export_recorded_audio()40 export_recorded_audio ()
41 {
42 const gchar *filename = recorded_audio_filename ();
43 SF_INFO out;
44 if (filename)
45 {
46 gsize length;
47 float *data;
48 if (g_file_get_contents (filename, (gchar **)&data, &length, NULL))
49 {
50 gchar *outfile = file_dialog ("Give output audio file name, with .ogg or .wav extension", FALSE, Denemo.prefs.denemopath->str);
51 if (outfile)
52 {
53 gint len = strlen (outfile);
54 if (len > 4)
55 {
56 if (*(outfile + len - 3) == 'o')
57 {
58 out.format = SF_FORMAT_VORBIS | SF_FORMAT_OGG;
59 }
60 else
61 {
62 out.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
63 }
64 out.channels = 1;
65 out.samplerate = 44100;
66 gpointer outsnd = sf_open (outfile, SFM_WRITE, &out);
67 if (outsnd)
68 {
69 sf_write_float (outsnd, data, length/sizeof(float));
70 g_free(data);
71 sf_close (outsnd);
72 return TRUE;
73 }
74 else
75 {
76 g_warning ("Unable to open file %s for writing this format", outfile);
77 }
78 g_free (outfile);
79 }
80 }
81
82 }
83 else
84 {
85 if (Denemo.prefs.maxrecordingtime)
86 warningdialog (_("No audio recording has been made.\nSee Playback Controls - Record Button"));
87 else
88 warningdialog (_("The preference set for recording time is 0 - nothing is recorded.\nSee Edit → Change Preferences Audio/Midi Tab"));
89 }
90 }
91 return FALSE;
92 }
93