Moved strip_whitespace_edges() to pbd/whitespace.h
[ardour.git] / gtk2_ardour / sfdb_ui.cc
1 /*
2     Copyright (C) 2005 Paul Davis 
3     Written by Taybin Rutkin
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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19
20 */
21
22 #include <map>
23 #include <cerrno>
24
25 #include <sndfile.h>
26
27 #include <pbd/basename.h>
28
29 #include <gtkmm/box.h>
30 #include <gtkmm/stock.h>
31
32 #include <ardour/audio_library.h>
33 #include <ardour/audioregion.h>
34 #include <ardour/sndfile_helpers.h>
35 #include <ardour/sndfilesource.h>
36
37 #include "gui_thread.h"
38 #include "prompter.h"
39 #include "sfdb_ui.h"
40
41 #include "i18n.h"
42
43 using namespace ARDOUR;
44
45 std::string length2string (const int32_t frames, const int32_t sample_rate);
46
47 SoundFileBox::SoundFileBox ()
48         :
49         _session(0),
50         current_pid(0),
51         fields(Gtk::ListStore::create(label_columns)),
52         main_box (false, 3),
53         top_box (true, 4),
54         bottom_box (true, 4),
55         play_btn(_("Play")),
56         stop_btn(_("Stop")),
57         add_field_btn(_("Add Field...")),
58         remove_field_btn(_("Remove Field"))
59 {
60         set_name (X_("SoundFileBox"));
61         border_frame.set_label (_("Soundfile Info"));
62         border_frame.add (main_box);
63
64         pack_start (border_frame);
65         set_border_width (4);
66
67         main_box.set_border_width (4);
68
69         main_box.pack_start(length, false, false);
70         main_box.pack_start(format, false, false);
71         main_box.pack_start(channels, false, false);
72         main_box.pack_start(samplerate, false, false);
73         main_box.pack_start(field_view, true, true);
74         main_box.pack_start(top_box, false, false);
75         main_box.pack_start(bottom_box, false, false);
76
77         field_view.set_size_request(200, 150);
78         field_view.append_column (_("Field"), label_columns.field);
79         field_view.append_column_editable (_("Value"), label_columns.data);
80
81         top_box.set_homogeneous(true);
82         top_box.pack_start(add_field_btn);
83         top_box.pack_start(remove_field_btn);
84
85         remove_field_btn.set_sensitive(false);
86
87         bottom_box.set_homogeneous(true);
88         bottom_box.pack_start(play_btn);
89         bottom_box.pack_start(stop_btn);
90
91         play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::play_btn_clicked));
92         stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
93
94         add_field_btn.signal_clicked().connect
95                         (mem_fun (*this, &SoundFileBox::add_field_clicked));
96         remove_field_btn.signal_clicked().connect
97                         (mem_fun (*this, &SoundFileBox::remove_field_clicked));
98
99         field_view.get_selection()->signal_changed().connect (mem_fun (*this, &SoundFileBox::field_selected));
100         Library->fields_changed.connect (mem_fun (*this, &SoundFileBox::setup_fields));
101
102         show_all();
103         stop_btn.hide();
104 }
105
106 void
107 SoundFileBox::set_session(Session* s)
108 {
109         _session = s;
110
111     if (!_session) {
112                 play_btn.set_sensitive(false);
113         } else {
114                 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
115         }
116 }
117
118 bool
119 SoundFileBox::setup_labels (string filename) 
120 {
121         path = filename;
122
123     SNDFILE *sf;
124
125         sf_info.format = 0; // libsndfile says to clear this before sf_open().
126
127     if ((sf = sf_open ((char *) filename.c_str(), SFM_READ, &sf_info)) < 0) {
128         return false;
129     }
130
131         sf_close (sf);
132
133     if (sf_info.frames == 0 && sf_info.channels == 0 &&
134                 sf_info.samplerate == 0 && sf_info.format == 0 &&
135                 sf_info.sections == 0) {
136                 /* .. ok, it's not a sound file */
137             return false;
138         }
139
140         length.set_alignment (0.0f, 0.0f);
141         length.set_text (string_compose("Length: %1", length2string(sf_info.frames, sf_info.samplerate)));
142
143         format.set_alignment (0.0f, 0.0f);
144         format.set_text (string_compose("Format: %1, %2", 
145                                 sndfile_major_format(sf_info.format),
146                                 sndfile_minor_format(sf_info.format)));
147
148         channels.set_alignment (0.0f, 0.0f);
149         channels.set_text (string_compose("Channels: %1", sf_info.channels));
150
151         samplerate.set_alignment (0.0f, 0.0f);
152         samplerate.set_text (string_compose("Samplerate: %1", sf_info.samplerate));
153
154         setup_fields ();
155
156         return true;
157 }
158
159 void
160 SoundFileBox::setup_fields ()
161 {
162         ENSURE_GUI_THREAD(mem_fun (*this, &SoundFileBox::setup_fields));
163
164         vector<string> field_list;
165         Library->get_fields(field_list);
166
167         vector<string>::iterator i;
168         Gtk::TreeModel::iterator iter;
169         Gtk::TreeModel::Row row;
170         for (i = field_list.begin(); i != field_list.end(); ++i) {
171                 string value = Library->get_field(path, *i);
172                 iter = fields->append();
173                 row = *iter;
174
175                 row[label_columns.field] = *i;
176                 row[label_columns.data]  = value;
177         }
178 }
179
180 void
181 SoundFileBox::play_btn_clicked ()
182 {
183         if (!_session) {
184                 return;
185         }
186
187         _session->cancel_audition();
188
189         if (access(path.c_str(), R_OK)) {
190                 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
191                 return;
192         }
193
194         static std::map<string, AudioRegion*> region_cache;
195
196         if (region_cache.find (path) == region_cache.end()) {
197                 AudioRegion::SourceList srclist;
198                 SndFileSource* sfs;
199
200                 for (int n = 0; n < sf_info.channels; ++n) {
201                         try {
202                                 sfs = new SndFileSource(path+":"+string_compose("%1", n), false);
203                                 srclist.push_back(sfs);
204
205                         } catch (failed_constructor& err) {
206                                 error << _("Could not access soundfile: ") << path << endmsg;
207                                 return;
208                         }
209                 }
210
211                 if (srclist.empty()) {
212                         return;
213                 }
214
215                 string result;
216                 _session->region_name (result, PBD::basename(srclist[0]->name()), false);
217                 AudioRegion* a_region = new AudioRegion(srclist, 0, srclist[0]->length(), result, 0, Region::DefaultFlags, false);
218                 region_cache[path] = a_region;
219         }
220
221         play_btn.hide();
222         stop_btn.show();
223
224         _session->audition_region(*region_cache[path]);
225 }
226
227 void
228 SoundFileBox::stop_btn_clicked ()
229 {
230         if (_session) {
231                 _session->cancel_audition();
232                 play_btn.show();
233                 stop_btn.hide();
234         }
235 }
236
237 void
238 SoundFileBox::add_field_clicked ()
239 {
240     ArdourPrompter prompter (true);
241     string name;
242
243     prompter.set_prompt (_("Name for field"));
244
245     switch (prompter.run ()) {
246                 case Gtk::RESPONSE_ACCEPT:
247                         cout << name << endl;
248                 prompter.get_result (name);
249                         Library->add_field (name);
250                         Library->save_changes ();
251                 break;
252
253             default:
254                 break;
255     }
256 }
257
258 void
259 SoundFileBox::remove_field_clicked ()
260 {}
261
262 void
263 SoundFileBox::audition_status_changed (bool active)
264 {
265     ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
266
267         if (!active) {
268                 stop_btn_clicked ();
269         }
270 }
271
272 void
273 SoundFileBox::field_selected ()
274 {}
275
276 SoundFileBrowser::SoundFileBrowser (std::string title)
277         :
278         ArdourDialog(title),
279         chooser(Gtk::FILE_CHOOSER_ACTION_OPEN)
280 {
281         get_vbox()->pack_start(chooser);
282         chooser.set_preview_widget(preview);
283
284         chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
285 }
286
287 void
288 SoundFileBrowser::set_session (Session* s)
289 {
290         preview.set_session(s);
291 }
292
293 void
294 SoundFileBrowser::update_preview ()
295 {
296         chooser.set_preview_widget_active(preview.setup_labels(chooser.get_filename()));
297 }
298
299 SoundFileChooser::SoundFileChooser (std::string title)
300         :
301         SoundFileBrowser(title)
302 {
303         add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
304         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
305
306         show_all ();
307 }
308
309 SoundFileOmega::SoundFileOmega (std::string title)
310         :
311         SoundFileBrowser(title),
312         embed_btn (_("Embed")),
313         import_btn (_("Import")),
314         split_check (_("Split Channels"))
315 {
316         get_action_area()->pack_start(embed_btn);
317         get_action_area()->pack_start(import_btn);
318         add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
319
320         chooser.set_extra_widget(split_check);
321
322         embed_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::embed_clicked));
323         import_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::import_clicked));
324
325         show_all ();
326 }
327
328 void
329 SoundFileOmega::embed_clicked ()
330 {
331         Embedded (chooser.get_filenames(), split_check.get_active());
332 }
333
334 void
335 SoundFileOmega::import_clicked ()
336 {
337         Imported (chooser.get_filenames(), split_check.get_active());
338 }
339
340 std::string
341 length2string (const int32_t frames, const int32_t sample_rate)
342 {
343     int secs = (int) (frames / (float) sample_rate);
344     int hrs =  secs / 3600;
345     secs -= (hrs * 3600);
346     int mins = secs / 60;
347     secs -= (mins * 60);
348
349     int total_secs = (hrs * 3600) + (mins * 60) + secs;
350     int frames_remaining = frames - (total_secs * sample_rate);
351     float fractional_secs = (float) frames_remaining / sample_rate;
352
353     char duration_str[32];
354     sprintf (duration_str, "%02d:%02d:%05.2f", hrs, mins, (float) secs + fractional_secs);
355
356     return duration_str;
357 }
358