add sampo's synthesize_sources perl script to tools; add scroll-playhead-{forward...
[ardour.git] / gtk2_ardour / sfdb_ui.cc
1 /*
2     Copyright (C) 2005-2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <map>
21 #include <cerrno>
22 #include <sstream>
23
24 #include <sys/stat.h>
25
26 #include <gtkmm/box.h>
27 #include <gtkmm/stock.h>
28
29 #include <pbd/convert.h>
30 #include <pbd/tokenizer.h>
31
32 #include <gtkmm2ext/utils.h>
33
34 #include <ardour/audio_library.h>
35 #include <ardour/audioregion.h>
36 #include <ardour/audiofilesource.h>
37 #include <ardour/region_factory.h>
38 #include <ardour/source_factory.h>
39
40 #include "ardour_ui.h"
41 #include "editing.h"
42 #include "gui_thread.h"
43 #include "prompter.h"
44 #include "sfdb_ui.h"
45 #include "utils.h"
46
47 #include "i18n.h"
48
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace std;
52
53 SoundFileBox::SoundFileBox ()
54         :
55         _session(0),
56         current_pid(0),
57         main_box (false, 3),
58         bottom_box (true, 4),
59         play_btn(_("Play")),
60         stop_btn(_("Stop")),
61         apply_btn(_("Apply"))
62 {
63         set_name (X_("SoundFileBox"));
64         
65         set_size_request (250, 500);
66         
67         border_frame.set_label (_("Soundfile Info"));
68         border_frame.add (main_box);
69
70         Gtk::Label* tag_label = manage(new Gtk::Label(_("comma seperated tags")));
71
72         pack_start (border_frame);
73         set_border_width (4);
74
75         main_box.set_border_width (4);
76
77         main_box.pack_start(length, false, false);
78         main_box.pack_start(format, false, false);
79         main_box.pack_start(channels, false, false);
80         main_box.pack_start(samplerate, false, false);
81         main_box.pack_start(timecode, false, false);
82         main_box.pack_start(*tag_label, false, false);
83         main_box.pack_start(tags_entry, false, false);
84         main_box.pack_start(apply_btn, false, false);
85         main_box.pack_start(bottom_box, false, 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         apply_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::apply_btn_clicked));
94         tags_entry.signal_activate().connect (mem_fun (*this, &SoundFileBox::apply_btn_clicked));
95
96         length.set_alignment (0.0f, 0.0f);
97         format.set_alignment (0.0f, 0.0f);
98         channels.set_alignment (0.0f, 0.0f);
99         samplerate.set_alignment (0.0f, 0.0f);
100         timecode.set_alignment (0.0f, 0.0f);
101
102         stop_btn.set_no_show_all (true);
103         stop_btn.hide();
104         
105         show_all();
106 }
107
108 void
109 SoundFileBox::set_session(Session* s)
110 {
111         _session = s;
112
113         if (!_session) {
114                 play_btn.set_sensitive(false);
115         } else {
116                 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
117         }
118 }
119
120 bool
121 SoundFileBox::setup_labels (string filename) 
122 {
123         path = filename;
124
125         string error_msg;
126
127         if(!AudioFileSource::get_soundfile_info (filename, sf_info, error_msg)) {
128                 length.set_text (_("Length: n/a"));
129                 format.set_text (_("Format: n/a"));
130                 channels.set_text (_("Channels: n/a"));
131                 samplerate.set_text (_("Samplerate: n/a"));
132                 timecode.set_text (_("Timecode: n/a"));
133                 tags_entry.set_text ("");
134                 
135                 tags_entry.set_sensitive (false);
136                 play_btn.set_sensitive (false);
137                 apply_btn.set_sensitive (false);
138                 
139                 return false;
140         }
141
142         length.set_text (string_compose(_("Length: %1"), length2string(sf_info.length, sf_info.samplerate)));
143         format.set_text (sf_info.format_name);
144         channels.set_text (string_compose(_("Channels: %1"), sf_info.channels));
145         samplerate.set_text (string_compose(_("Samplerate: %1"), sf_info.samplerate));
146         timecode.set_text (string_compose (_("Timecode: %1"), length2string(sf_info.timecode, sf_info.samplerate)));
147
148         vector<string> tags = Library->get_tags (filename);
149         
150         stringstream tag_string;
151         for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
152                 if (i != tags.begin()) {
153                         tag_string << ", ";
154                 }
155                 tag_string << *i;
156         }
157         tags_entry.set_text (tag_string.str());
158         
159         tags_entry.set_sensitive (true);
160         if (_session) {
161                 play_btn.set_sensitive (true);
162         }
163         apply_btn.set_sensitive (true);
164         
165         return true;
166 }
167
168 bool
169 SoundFileBox::tags_entry_left (GdkEventFocus* event)
170 {       
171         apply_btn_clicked ();
172         
173         return true;
174 }
175
176 void
177 SoundFileBox::play_btn_clicked ()
178 {
179         if (!_session) {
180                 return;
181         }
182
183         _session->cancel_audition();
184
185         if (access(path.c_str(), R_OK)) {
186                 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
187                 return;
188         }
189
190         typedef std::map<string, boost::shared_ptr<AudioRegion> > RegionCache; 
191         static  RegionCache region_cache;
192         RegionCache::iterator the_region;
193
194         if ((the_region = region_cache.find (path)) == region_cache.end()) {
195                 SourceList srclist;
196                 boost::shared_ptr<AudioFileSource> afs;
197                 
198                 for (int n = 0; n < sf_info.channels; ++n) {
199                         try {
200                                 afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (*_session, path+":"+string_compose("%1", n), AudioFileSource::Flag (0)));
201                                 srclist.push_back(afs);
202
203                         } catch (failed_constructor& err) {
204                                 error << _("Could not access soundfile: ") << path << endmsg;
205                                 return;
206                         }
207                 }
208
209                 if (srclist.empty()) {
210                         return;
211                 }
212
213                 string rname;
214
215                 _session->region_name (rname, Glib::path_get_basename(srclist[0]->name()), false);
216
217                 pair<string,boost::shared_ptr<AudioRegion> > newpair;
218                 pair<RegionCache::iterator,bool> res;
219
220                 newpair.first = path;
221                 newpair.second = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, 0, srclist[0]->length(), rname, 0, Region::DefaultFlags, false));
222
223                 res = region_cache.insert (newpair);
224                 the_region = res.first;
225         }
226
227         play_btn.hide();
228         stop_btn.show();
229
230         boost::shared_ptr<Region> r = boost::static_pointer_cast<Region> (the_region->second);
231
232         _session->audition_region(r);
233 }
234
235 void
236 SoundFileBox::stop_btn_clicked ()
237 {
238         if (_session) {
239                 _session->cancel_audition();
240                 play_btn.show();
241                 stop_btn.hide();
242         }
243 }
244
245 void
246 SoundFileBox::apply_btn_clicked ()
247 {
248         string tag_string = tags_entry.get_text ();
249
250         vector<string> tags;
251
252     if (!PBD::tokenize (tag_string, string(","), std::back_inserter (tags), true)) {
253                 warning << _("SoundFileBox: Could not tokenize string: ") << tag_string << endmsg;
254                 return;
255         }
256         
257         Library->set_tags (path, tags);
258         Library->save_changes ();
259 }
260
261 void
262 SoundFileBox::audition_status_changed (bool active)
263 {
264         ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
265         
266         if (!active) {
267                 stop_btn_clicked ();
268         }
269 }
270
271 // this needs to be kept in sync with the ImportMode enum defined in editing.h and editing_syms.h.
272 static const char *import_mode_strings[] = {
273         N_("Add to Region list"),
274         N_("Add to selected Track(s)"),
275         N_("Add as new Track(s)"),
276         N_("Add as new Tape Track(s)"),
277         0
278 };
279
280 SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s)
281         : ArdourDialog (title, false),
282           chooser (Gtk::FILE_CHOOSER_ACTION_OPEN),
283           found_list (Gtk::ListStore::create(found_list_columns)),
284           found_list_view (found_list),
285           found_search_btn (_("Search"))
286 {
287         set_default_size (700, 500);
288         Gtk::HBox* hbox = manage(new Gtk::HBox);
289         hbox->pack_start(notebook);
290         hbox->pack_start(preview, Gtk::PACK_SHRINK);
291         get_vbox()->pack_start(*hbox);
292
293         hbox = manage(new Gtk::HBox);
294         hbox->pack_start (found_entry);
295         hbox->pack_start (found_search_btn);
296         
297         Gtk::VBox* vbox = manage(new Gtk::VBox);
298         vbox->pack_start (*hbox, Gtk::PACK_SHRINK);
299         vbox->pack_start (found_list_view);
300         found_list_view.append_column(_("Paths"), found_list_columns.pathname);
301         
302         notebook.append_page (chooser, _("Files"));
303         notebook.append_page (*vbox, _("Tags"));
304
305         found_list_view.get_selection()->set_mode (Gtk::SELECTION_MULTIPLE);
306
307         filter.add_custom (Gtk::FILE_FILTER_FILENAME, mem_fun(*this, &SoundFileBrowser::on_custom));
308         chooser.set_filter (filter);
309         chooser.set_select_multiple (true);
310         chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
311         found_list_view.get_selection()->signal_changed().connect(mem_fun(*this, &SoundFileBrowser::found_list_view_selected));
312         
313         found_search_btn.signal_clicked().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
314         found_entry.signal_activate().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
315         
316         show_all ();
317         
318         set_session (s);
319 }
320
321 void
322 SoundFileBrowser::set_session (Session* s)
323 {
324         preview.set_session(s);
325 }
326
327 bool
328 SoundFileBrowser::on_custom (const Gtk::FileFilter::Info& filter_info)
329 {
330         return AudioFileSource::safe_file_extension(filter_info.filename);
331 }
332
333 void
334 SoundFileBrowser::update_preview ()
335 {
336         preview.setup_labels(chooser.get_filename());
337 }
338
339 void
340 SoundFileBrowser::found_list_view_selected ()
341 {
342         string file;
343         
344         Gtk::TreeView::Selection::ListHandle_Path rows = found_list_view.get_selection()->get_selected_rows ();
345         
346         if (!rows.empty()) {
347                 Gtk::TreeIter iter = found_list->get_iter(*rows.begin());
348                 file = (*iter)[found_list_columns.pathname];
349                 chooser.set_filename (file);
350         }
351         preview.setup_labels (file);
352 }
353
354 void
355 SoundFileBrowser::found_search_clicked ()
356 {
357         string tag_string = found_entry.get_text ();
358
359         vector<string> tags;
360
361     if (!PBD::tokenize (tag_string, string(","), std::back_inserter (tags), true)) {
362                 warning << _("SoundFileBrowser: Could not tokenize string: ") << tag_string << endmsg;
363                 return;
364         }
365
366         vector<string> results;
367         Library->search_members_and (results, tags);
368         
369         found_list->clear();
370         for (vector<string>::iterator i = results.begin(); i != results.end(); ++i) {
371                 Gtk::TreeModel::iterator new_row = found_list->append();
372                 Gtk::TreeModel::Row row = *new_row;
373                 row[found_list_columns.pathname] = *i;
374         }
375 }
376
377 SoundFileChooser::SoundFileChooser (string title, ARDOUR::Session* s)
378         :
379         SoundFileBrowser(title, s)
380 {
381         add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
382         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
383         
384         chooser.set_select_multiple (false);
385         found_list_view.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
386         show_all ();
387 }
388
389 string
390 SoundFileChooser::get_filename ()
391 {
392         Gtk::TreeModel::iterator iter;
393         Gtk::TreeModel::Row row;
394         
395         string filename;
396         switch (notebook.get_current_page()) {
397                 case 0:
398                         filename = chooser.get_filename();
399                 case 1:
400                         iter = found_list_view.get_selection()->get_selected();
401                         row = *iter;
402                         filename = row[found_list_columns.pathname];
403                 default:
404                         /* NOT REACHED */
405                         return "";
406         }
407         
408         struct stat buf;
409         if (stat (filename.c_str(), &buf) || !S_ISREG(buf.st_mode)) {
410                 return "";
411         }
412         
413         return filename;
414 }
415
416 vector<string> SoundFileOmega::mode_strings;
417
418 SoundFileOmega::SoundFileOmega (string title, ARDOUR::Session* s)
419         : SoundFileBrowser (title, s),
420           split_check (_("Split Channels"))
421 {
422         ARDOUR_UI::instance()->tooltips().set_tip(split_check, 
423                         _("Create a region for each channel"));
424
425         Gtk::Button* btn = add_button (_("Embed"), ResponseEmbed);
426         ARDOUR_UI::instance()->tooltips().set_tip(*btn, 
427                         _("Link to an external file"));
428
429         btn = add_button (_("Import"), ResponseImport);
430         ARDOUR_UI::instance()->tooltips().set_tip(*btn, 
431                         _("Copy a file to the session folder"));
432
433         add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
434         
435         if (mode_strings.empty()) {
436                 mode_strings = I18N (import_mode_strings);
437         }
438         Gtkmm2ext::set_popdown_strings (mode_combo, mode_strings);
439
440         set_mode (Editing::ImportAsRegion);
441
442         get_action_area()->pack_start (split_check);
443         get_action_area()->pack_start (mode_combo);
444
445         mode_combo.signal_changed().connect (mem_fun (*this, &SoundFileOmega::mode_changed));
446         
447         show_all ();
448 }
449
450 bool
451 SoundFileOmega::get_split ()
452 {
453         return split_check.get_active();
454 }
455
456 vector<Glib::ustring>
457 SoundFileOmega::get_paths ()
458 {
459         vector<Glib::ustring> results;
460         
461         int n = notebook.get_current_page ();
462         
463         if (n == 0) {
464                 vector<Glib::ustring> filenames = chooser.get_filenames();
465                 vector<Glib::ustring>::iterator i;
466                 for (i = filenames.begin(); i != filenames.end(); ++i) {
467                         struct stat buf;
468                         if ((!stat((*i).c_str(), &buf)) && S_ISREG(buf.st_mode)) {
469                                 results.push_back (*i);
470                         }
471                 }
472                 return results;
473                 
474         } else {
475                 
476                 typedef Gtk::TreeView::Selection::ListHandle_Path ListPath;
477                 
478                 ListPath rows = found_list_view.get_selection()->get_selected_rows ();
479                 for (ListPath::iterator i = rows.begin() ; i != rows.end(); ++i) {
480                         Gtk::TreeIter iter = found_list->get_iter(*i);
481                         string str = (*iter)[found_list_columns.pathname];
482                         
483                         results.push_back (str);
484                 }
485                 return results;
486         }
487 }
488
489 void
490 SoundFileOmega::set_mode (Editing::ImportMode mode)
491 {
492         mode_combo.set_active_text (mode_strings[(int)mode]);
493
494         switch (mode) {
495         case Editing::ImportAsRegion:
496                 split_check.set_sensitive (true);
497                 break;
498         case Editing::ImportAsTrack:
499                 split_check.set_sensitive (true);
500                 break;
501         case Editing::ImportToTrack:
502                 split_check.set_sensitive (false);
503                 break;
504         case Editing::ImportAsTapeTrack:
505                 split_check.set_sensitive (true);
506                 break;
507         }
508 }
509
510 Editing::ImportMode
511 SoundFileOmega::get_mode ()
512 {
513         vector<string>::iterator i;
514         uint32_t n;
515         string str = mode_combo.get_active_text ();
516
517         for (n = 0, i = mode_strings.begin (); i != mode_strings.end(); ++i, ++n) {
518                 if (str == (*i)) {
519                         break;
520                 }
521         }
522
523         if (i == mode_strings.end()) {
524                 fatal << string_compose (_("programming error: %1"), X_("unknown import mode string")) << endmsg;
525                 /*NOTREACHED*/
526         }
527
528         return (Editing::ImportMode) (n);
529 }
530
531 void
532 SoundFileOmega::mode_changed ()
533 {
534         Editing::ImportMode mode = get_mode();
535
536         switch (mode) {
537         case Editing::ImportAsRegion:
538                 split_check.set_sensitive (true);
539                 break;
540         case Editing::ImportAsTrack:
541                 split_check.set_sensitive (true);
542                 break;
543         case Editing::ImportToTrack:
544                 split_check.set_sensitive (false);
545                 break;
546         case Editing::ImportAsTapeTrack:
547                 split_check.set_sensitive (true);
548                 break;
549         }
550 }