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