Merged with trunk R1141
[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 <gtkmm/box.h>
26 #include <gtkmm/stock.h>
27
28 #include <pbd/convert.h>
29
30 #include <gtkmm2ext/utils.h>
31
32 #include <ardour/audio_library.h>
33 #include <ardour/audioregion.h>
34 #include <ardour/audiofilesource.h>
35 #include <ardour/region_factory.h>
36 #include <ardour/source_factory.h>
37
38 #include "ardour_ui.h"
39 #include "editing.h"
40 #include "gui_thread.h"
41 #include "prompter.h"
42 #include "sfdb_ui.h"
43 #include "utils.h"
44
45 #include "i18n.h"
46
47 using namespace ARDOUR;
48 using namespace PBD;
49 using namespace std;
50
51 SoundFileBox::SoundFileBox ()
52         :
53         _session(0),
54         current_pid(0),
55         fields(Gtk::ListStore::create(label_columns)),
56         main_box (false, 3),
57         top_box (true, 4),
58         bottom_box (true, 4),
59         play_btn(_("Play")),
60         stop_btn(_("Stop")),
61         add_field_btn(_("Add Field...")),
62         remove_field_btn(_("Remove Field"))
63 {
64         set_name (X_("SoundFileBox"));
65         border_frame.set_label (_("Soundfile Info"));
66         border_frame.add (main_box);
67
68         pack_start (border_frame);
69         set_border_width (4);
70
71         main_box.set_border_width (4);
72
73         main_box.pack_start(length, false, false);
74         main_box.pack_start(format, false, false);
75         main_box.pack_start(channels, false, false);
76         main_box.pack_start(samplerate, false, false);
77         main_box.pack_start(timecode, false, false);
78         main_box.pack_start(field_view, true, true);
79         main_box.pack_start(top_box, false, false);
80         main_box.pack_start(bottom_box, false, false);
81
82         field_view.set_model (fields);
83         field_view.set_size_request(200, 150);
84         field_view.append_column (_("Field"), label_columns.field);
85         field_view.append_column_editable (_("Value"), label_columns.data);
86
87         top_box.set_homogeneous(true);
88         top_box.pack_start(add_field_btn);
89         top_box.pack_start(remove_field_btn);
90
91         remove_field_btn.set_sensitive(false);
92
93         bottom_box.set_homogeneous(true);
94         bottom_box.pack_start(play_btn);
95         bottom_box.pack_start(stop_btn);
96
97         play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::play_btn_clicked));
98         stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
99
100         add_field_btn.signal_clicked().connect
101                         (mem_fun (*this, &SoundFileBox::add_field_clicked));
102         remove_field_btn.signal_clicked().connect
103                         (mem_fun (*this, &SoundFileBox::remove_field_clicked));
104         
105         Gtk::CellRendererText* cell(dynamic_cast<Gtk::CellRendererText*>(field_view.get_column_cell_renderer(1)));
106         cell->signal_edited().connect (mem_fun (*this, &SoundFileBox::field_edited));
107
108         field_view.get_selection()->signal_changed().connect (mem_fun (*this, &SoundFileBox::field_selected));
109         Library->fields_changed.connect (mem_fun (*this, &SoundFileBox::setup_fields));
110
111         show_all();
112         stop_btn.hide();
113 }
114
115 void
116 SoundFileBox::set_session(Session* s)
117 {
118         _session = s;
119
120         if (!_session) {
121                 play_btn.set_sensitive(false);
122         } else {
123                 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
124         }
125 }
126
127 bool
128 SoundFileBox::setup_labels (string filename) 
129 {
130         path = filename;
131
132         string error_msg;
133
134         if(!AudioFileSource::get_soundfile_info (filename, sf_info, error_msg)) {
135                 return false;
136         }
137
138         length.set_alignment (0.0f, 0.0f);
139         length.set_text (string_compose(_("Length: %1"), PBD::length2string(sf_info.length, sf_info.samplerate)));
140
141         format.set_alignment (0.0f, 0.0f);
142         format.set_text (sf_info.format_name);
143
144         channels.set_alignment (0.0f, 0.0f);
145         channels.set_text (string_compose(_("Channels: %1"), sf_info.channels));
146
147         samplerate.set_alignment (0.0f, 0.0f);
148         samplerate.set_text (string_compose(_("Samplerate: %1"), sf_info.samplerate));
149
150         timecode.set_alignment (0.0f, 0.0f);
151         timecode.set_text (string_compose (_("Timecode: %1"), PBD::length2string(sf_info.timecode, sf_info.samplerate)));
152
153         setup_fields ();
154
155         return true;
156 }
157
158 void
159 SoundFileBox::setup_fields ()
160 {
161         ENSURE_GUI_THREAD(mem_fun (*this, &SoundFileBox::setup_fields));
162
163         fields->clear ();
164
165         vector<string> field_list;
166         Library->get_fields(field_list);
167
168         vector<string>::iterator i;
169         Gtk::TreeModel::iterator iter;
170         Gtk::TreeModel::Row row;
171         for (i = field_list.begin(); i != field_list.end(); ++i) {
172                 if (!(*i == _("channels") || *i == _("samplerate") ||
173                         *i == _("resolution") || *i == _("format"))) {
174                         iter = fields->append();
175                         row = *iter;
176
177                         string value = Library->get_field(path, *i);
178                         row[label_columns.field] = *i;
179                         row[label_columns.data]  = value;
180                 }
181         }
182 }
183
184 void
185 SoundFileBox::play_btn_clicked ()
186 {
187         if (!_session) {
188                 return;
189         }
190
191         _session->cancel_audition();
192
193         if (access(path.c_str(), R_OK)) {
194                 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
195                 return;
196         }
197
198         typedef std::map<string, boost::shared_ptr<AudioRegion> > RegionCache; 
199         static  RegionCache region_cache;
200         RegionCache::iterator the_region;
201
202         if ((the_region = region_cache.find (path)) == region_cache.end()) {
203                 SourceList srclist;
204                 boost::shared_ptr<AudioFileSource> afs;
205                 
206                 for (int n = 0; n < sf_info.channels; ++n) {
207                         try {
208                                 afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (DataType::AUDIO, *_session, path+":"+string_compose("%1", n), AudioFileSource::Flag (0)));
209                                 srclist.push_back(afs);
210
211                         } catch (failed_constructor& err) {
212                                 error << _("Could not access soundfile: ") << path << endmsg;
213                                 return;
214                         }
215                 }
216
217                 if (srclist.empty()) {
218                         return;
219                 }
220
221                 string rname;
222
223                 _session->region_name (rname, Glib::path_get_basename(srclist[0]->name()), false);
224
225                 pair<string,boost::shared_ptr<AudioRegion> > newpair;
226                 pair<RegionCache::iterator,bool> res;
227
228                 newpair.first = path;
229                 newpair.second = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, 0, srclist[0]->length(), rname, 0, Region::DefaultFlags, false));
230
231
232                 res = region_cache.insert (newpair);
233                 the_region = res.first;
234         }
235
236         play_btn.hide();
237         stop_btn.show();
238
239         boost::shared_ptr<Region> r = boost::static_pointer_cast<Region> (the_region->second);
240
241         _session->audition_region(r);
242 }
243
244 void
245 SoundFileBox::stop_btn_clicked ()
246 {
247         if (_session) {
248                 _session->cancel_audition();
249                 play_btn.show();
250                 stop_btn.hide();
251         }
252 }
253
254 void
255 SoundFileBox::add_field_clicked ()
256 {
257     ArdourPrompter prompter (true);
258     string name;
259
260     prompter.set_prompt (_("Name for Field"));
261     prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
262     prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
263
264     switch (prompter.run ()) {
265                 case Gtk::RESPONSE_ACCEPT:
266                 prompter.get_result (name);
267                         if (name.length()) {
268                                 Library->add_field (name);
269                                 Library->save_changes ();
270                         }
271                 break;
272
273             default:
274                 break;
275     }
276 }
277
278 void
279 SoundFileBox::remove_field_clicked ()
280 {
281         field_view.get_selection()->selected_foreach_iter(mem_fun(*this, &SoundFileBox::delete_row));
282
283         Library->save_changes ();
284 }
285
286 void
287 SoundFileBox::field_edited (const Glib::ustring& str1, const Glib::ustring& str2)
288 {
289         Gtk::TreeModel::Children rows(fields->children());
290         Gtk::TreeModel::Row row(rows[atoi(str1.c_str())]);
291         
292         Library->set_field (path, row[label_columns.field], str2);
293         
294         Library->save_changes ();
295 }
296
297 void
298 SoundFileBox::delete_row (const Gtk::TreeModel::iterator& iter)
299 {
300         Gtk::TreeModel::Row row = *iter;
301
302         Library->remove_field(row[label_columns.field]);
303 }
304
305 void
306 SoundFileBox::audition_status_changed (bool active)
307 {
308         ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
309         
310         if (!active) {
311                 stop_btn_clicked ();
312         }
313 }
314
315 void
316 SoundFileBox::field_selected ()
317 {
318         if (field_view.get_selection()->count_selected_rows()) {
319                 remove_field_btn.set_sensitive(true);
320         } else {
321                 remove_field_btn.set_sensitive(false);
322         }
323 }
324
325 // this needs to be kept in sync with the ImportMode enum defined in editing.h and editing_syms.h.
326 static const char *import_mode_strings[] = {
327         N_("Add to Region list"),
328         N_("Add to selected Track(s)"),
329         N_("Add as new Track(s)"),
330         N_("Add as new Tape Track(s)"),
331         0
332 };
333
334 SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s)
335         : ArdourDialog (title, false),
336           chooser (Gtk::FILE_CHOOSER_ACTION_OPEN)
337 {
338         get_vbox()->pack_start(chooser);
339         chooser.set_preview_widget(preview);
340         chooser.set_select_multiple (true);
341
342         chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
343
344         set_session (s);
345 }
346
347 void
348 SoundFileBrowser::set_session (Session* s)
349 {
350         preview.set_session(s);
351 }
352
353 void
354 SoundFileBrowser::update_preview ()
355 {
356         chooser.set_preview_widget_active(preview.setup_labels(chooser.get_filename()));
357 }
358
359 SoundFileChooser::SoundFileChooser (string title, ARDOUR::Session* s)
360         :
361         SoundFileBrowser(title, s)
362 {
363         add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
364         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
365         show_all ();
366 }
367
368 vector<string> SoundFileOmega::mode_strings;
369
370 SoundFileOmega::SoundFileOmega (string title, ARDOUR::Session* s)
371         : SoundFileBrowser (title, s),
372           split_check (_("Split Channels"))
373 {
374         if (mode_strings.empty()) {
375                 mode_strings = I18N (import_mode_strings);
376         }
377
378         ARDOUR_UI::instance()->tooltips().set_tip(split_check, 
379                         _("Create a region for each channel"));
380
381         Gtk::Button* btn = add_button (_("Embed"), ResponseEmbed);
382         ARDOUR_UI::instance()->tooltips().set_tip(*btn, 
383                         _("Link to an external file"));
384
385         add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
386
387         btn = add_button (_("Import"), ResponseImport);
388         ARDOUR_UI::instance()->tooltips().set_tip(*btn, 
389                         _("Copy a file to the session folder"));
390
391         Gtk::HBox *box = manage (new Gtk::HBox());
392
393         Gtkmm2ext::set_popdown_strings (mode_combo, mode_strings);
394
395         set_mode (Editing::ImportAsRegion);
396
397         box->pack_start (split_check);
398         box->pack_start (mode_combo);
399
400         mode_combo.signal_changed().connect (mem_fun (*this, &SoundFileOmega::mode_changed));
401
402         chooser.set_extra_widget (*box);
403         
404         show_all ();
405 }
406
407 bool
408 SoundFileOmega::get_split ()
409 {
410         return split_check.get_active();
411 }
412
413 vector<Glib::ustring>
414 SoundFileOmega::get_paths ()
415 {
416         return chooser.get_filenames();
417 }
418
419 void
420 SoundFileOmega::set_mode (Editing::ImportMode mode)
421 {
422         mode_combo.set_active_text (mode_strings[(int)mode]);
423
424         switch (mode) {
425         case Editing::ImportAsRegion:
426                 split_check.set_sensitive (true);
427                 break;
428         case Editing::ImportAsTrack:
429                 split_check.set_sensitive (true);
430                 break;
431         case Editing::ImportToTrack:
432                 split_check.set_sensitive (false);
433                 break;
434         case Editing::ImportAsTapeTrack:
435                 split_check.set_sensitive (true);
436                 break;
437         }
438 }
439
440 Editing::ImportMode
441 SoundFileOmega::get_mode ()
442 {
443         vector<string>::iterator i;
444         uint32_t n;
445         string str = mode_combo.get_active_text ();
446
447         for (n = 0, i = mode_strings.begin (); i != mode_strings.end(); ++i, ++n) {
448                 if (str == (*i)) {
449                         break;
450                 }
451         }
452
453         if (i == mode_strings.end()) {
454                 fatal << string_compose (_("programming error: %1"), X_("unknown import mode string")) << endmsg;
455                 /*NOTREACHED*/
456         }
457
458         return (Editing::ImportMode) (n);
459 }
460
461 void
462 SoundFileOmega::mode_changed ()
463 {
464         Editing::ImportMode mode = get_mode();
465
466         switch (mode) {
467         case Editing::ImportAsRegion:
468                 split_check.set_sensitive (true);
469                 break;
470         case Editing::ImportAsTrack:
471                 split_check.set_sensitive (true);
472                 break;
473         case Editing::ImportToTrack:
474                 split_check.set_sensitive (false);
475                 break;
476         case Editing::ImportAsTapeTrack:
477                 split_check.set_sensitive (true);
478                 break;
479         }
480 }