2 Copyright (C) 2005-2006 Paul Davis
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.
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.
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.
26 #include <gtkmm/box.h>
27 #include <gtkmm/stock.h>
28 #include <glibmm/fileutils.h>
30 #include <pbd/convert.h>
31 #include <pbd/tokenizer.h>
33 #include <gtkmm2ext/utils.h>
35 #include <ardour/audio_library.h>
36 #include <ardour/audioregion.h>
37 #include <ardour/audiofilesource.h>
38 #include <ardour/region_factory.h>
39 #include <ardour/source_factory.h>
41 #include "ardour_ui.h"
43 #include "gui_thread.h"
51 using namespace ARDOUR;
55 using namespace Editing;
57 Glib::ustring SoundFileBrowser::persistent_folder;
59 SoundFileBox::SoundFileBox ()
65 play_btn (Stock::MEDIA_PLAY),
66 stop_btn (Stock::MEDIA_STOP),
69 set_name (X_("SoundFileBox"));
71 set_size_request (250, 250);
73 Label* label = manage (new Label);
74 label->set_use_markup (true);
75 label->set_text (_("<b>Soundfile Info</b>"));
77 border_frame.set_label_widget (*label);
78 border_frame.add (main_box);
80 Label* tag_label = manage(new Label(_("comma seperated tags")));
82 pack_start (border_frame, FALSE, FALSE);
85 main_box.set_border_width (4);
87 Gtk::Image* w = manage (new Image (Stock::APPLY, ICON_SIZE_BUTTON));
88 apply_btn.set_image (*w);
89 apply_btn.set_label (_("Set tags"));
91 main_box.pack_start(length, false, false);
92 main_box.pack_start(format, false, false);
93 main_box.pack_start(channels, false, false);
94 main_box.pack_start(samplerate, false, false);
95 main_box.pack_start(timecode, false, false);
96 main_box.pack_start(*tag_label, false, false);
98 HBox* hbox = manage (new HBox);
99 hbox->pack_start (apply_btn, false, false);
100 hbox->pack_start (tags_entry, true, true);
102 main_box.pack_start(*hbox, false, false);
103 main_box.pack_start(bottom_box, false, false);
105 bottom_box.set_homogeneous(true);
106 bottom_box.pack_start(play_btn);
107 bottom_box.pack_start(stop_btn);
109 play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::audition));
110 stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
111 apply_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::apply_btn_clicked));
112 tags_entry.signal_activate().connect (mem_fun (*this, &SoundFileBox::apply_btn_clicked));
114 length.set_alignment (0.0f, 0.0f);
115 format.set_alignment (0.0f, 0.0f);
116 channels.set_alignment (0.0f, 0.0f);
117 samplerate.set_alignment (0.0f, 0.0f);
118 timecode.set_alignment (0.0f, 0.0f);
120 stop_btn.set_no_show_all (true);
125 SoundFileBox::set_session(Session* s)
130 play_btn.set_sensitive(false);
132 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
137 SoundFileBox::setup_labels (const Glib::ustring& filename)
143 if(!AudioFileSource::get_soundfile_info (filename, sf_info, error_msg)) {
144 length.set_text (_("Length: n/a"));
145 format.set_text (_("Format: n/a"));
146 channels.set_text (_("Channels: n/a"));
147 samplerate.set_text (_("Sample rate: n/a"));
148 timecode.set_text (_("Timecode: n/a"));
149 tags_entry.set_text ("");
151 tags_entry.set_sensitive (false);
152 play_btn.set_sensitive (false);
153 apply_btn.set_sensitive (false);
158 length.set_text (string_compose(_("Length: %1"), length2string(sf_info.length, sf_info.samplerate)));
159 format.set_text (sf_info.format_name);
160 channels.set_text (string_compose(_("Channels: %1"), sf_info.channels));
161 samplerate.set_text (string_compose(_("Samplerate: %1"), sf_info.samplerate));
162 timecode.set_text (string_compose (_("Timecode: %1"), length2string(sf_info.timecode, sf_info.samplerate)));
164 // this is a hack that is fixed in trunk, i think (august 26th, 2007)
166 vector<string> tags = Library->get_tags (string ("//") + filename);
168 stringstream tag_string;
169 for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
170 if (i != tags.begin()) {
175 tags_entry.set_text (tag_string.str());
177 tags_entry.set_sensitive (true);
179 play_btn.set_sensitive (true);
181 apply_btn.set_sensitive (true);
187 SoundFileBox::tags_entry_left (GdkEventFocus* event)
189 apply_btn_clicked ();
194 SoundFileBox::audition ()
200 _session->cancel_audition();
202 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
203 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
207 typedef std::map<Glib::ustring, boost::shared_ptr<AudioRegion> > RegionCache;
208 static RegionCache region_cache;
209 RegionCache::iterator the_region;
211 if ((the_region = region_cache.find (path)) == region_cache.end()) {
214 boost::shared_ptr<AudioFileSource> afs;
216 for (int n = 0; n < sf_info.channels; ++n) {
218 afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (*_session, path, n, AudioFileSource::Flag (0)));
219 srclist.push_back(afs);
221 } catch (failed_constructor& err) {
222 error << _("Could not access soundfile: ") << path << endmsg;
227 if (srclist.empty()) {
233 _session->region_name (rname, Glib::path_get_basename(srclist[0]->name()), false);
235 pair<string,boost::shared_ptr<AudioRegion> > newpair;
236 pair<RegionCache::iterator,bool> res;
238 newpair.first = path;
239 newpair.second = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, 0, srclist[0]->length(), rname, 0, Region::DefaultFlags, false));
241 res = region_cache.insert (newpair);
242 the_region = res.first;
248 boost::shared_ptr<Region> r = boost::static_pointer_cast<Region> (the_region->second);
250 _session->audition_region(r);
254 SoundFileBox::stop_btn_clicked ()
257 _session->cancel_audition();
264 SoundFileBox::apply_btn_clicked ()
266 string tag_string = tags_entry.get_text ();
268 if (tag_string.empty()) {
274 if (!PBD::tokenize (tag_string, string(","), std::back_inserter (tags), true)) {
275 warning << _("SoundFileBox: Could not tokenize string: ") << tag_string << endmsg;
279 Library->set_tags (string ("//") + path, tags);
280 Library->save_changes ();
284 SoundFileBox::audition_status_changed (bool active)
286 ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
293 SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s)
294 : ArdourDialog (title, false),
295 found_list (ListStore::create(found_list_columns)),
296 chooser (FILE_CHOOSER_ACTION_OPEN),
297 found_list_view (found_list),
298 found_search_btn (_("Search"))
304 vbox = manage (new VBox);
305 vbox->pack_start (preview, false, false);
308 hbox = manage (new HBox);
309 hbox->set_spacing (6);
310 hbox->pack_start (notebook, true, true);
311 hbox->pack_start (*vbox, false, false);
313 get_vbox()->pack_start (*hbox, true, true);
315 hbox = manage(new HBox);
316 hbox->pack_start (found_entry);
317 hbox->pack_start (found_search_btn);
319 vbox = manage(new VBox);
320 vbox->pack_start (*hbox, PACK_SHRINK);
321 vbox->pack_start (found_list_view);
322 found_list_view.append_column(_("Paths"), found_list_columns.pathname);
324 chooser.set_border_width (12);
326 notebook.append_page (chooser, _("Search Files"));
327 notebook.append_page (*vbox, _("Search Tags"));
329 found_list_view.get_selection()->set_mode (SELECTION_MULTIPLE);
330 found_list_view.signal_row_activated().connect (mem_fun (*this, &SoundFileBrowser::found_list_view_activated));
332 custom_filter.add_custom (FILE_FILTER_FILENAME, mem_fun(*this, &SoundFileBrowser::on_custom));
333 custom_filter.set_name (_("Probable audio files"));
335 matchall_filter.add_pattern ("*.*");
336 matchall_filter.set_name (_("All files"));
338 chooser.add_filter (custom_filter);
339 chooser.add_filter (matchall_filter);
340 chooser.set_select_multiple (true);
341 chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
342 chooser.signal_file_activated().connect (mem_fun (*this, &SoundFileBrowser::chooser_file_activated));
344 if (!persistent_folder.empty()) {
345 chooser.set_current_folder (persistent_folder);
348 found_list_view.get_selection()->signal_changed().connect(mem_fun(*this, &SoundFileBrowser::found_list_view_selected));
350 found_search_btn.signal_clicked().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
351 found_entry.signal_activate().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
353 add_button (Stock::OK, RESPONSE_OK);
354 add_button (Stock::CANCEL, RESPONSE_CANCEL);
356 set_response_sensitive (RESPONSE_OK, false);
361 SoundFileBrowser::~SoundFileBrowser ()
363 persistent_folder = chooser.get_current_folder();
367 SoundFileBrowser::chooser_file_activated ()
373 SoundFileBrowser::found_list_view_activated (const TreeModel::Path& path, TreeViewColumn* col)
379 SoundFileBrowser::set_session (Session* s)
381 preview.set_session(s);
385 SoundFileBrowser::on_custom (const FileFilter::Info& filter_info)
387 return AudioFileSource::safe_file_extension(filter_info.filename);
391 SoundFileBrowser::update_preview ()
393 preview.setup_labels (chooser.get_filename());
394 set_response_sensitive (RESPONSE_OK, true);
398 SoundFileBrowser::found_list_view_selected ()
402 TreeView::Selection::ListHandle_Path rows = found_list_view.get_selection()->get_selected_rows ();
405 TreeIter iter = found_list->get_iter(*rows.begin());
406 file = (*iter)[found_list_columns.pathname];
407 chooser.set_filename (file);
409 preview.setup_labels (file);
410 set_response_sensitive (RESPONSE_OK, true);
414 SoundFileBrowser::found_search_clicked ()
416 string tag_string = found_entry.get_text ();
420 if (!PBD::tokenize (tag_string, string(","), std::back_inserter (tags), true)) {
421 warning << _("SoundFileBrowser: Could not tokenize string: ") << tag_string << endmsg;
425 vector<string> results;
426 Library->search_members_and (results, tags);
429 for (vector<string>::iterator i = results.begin(); i != results.end(); ++i) {
430 TreeModel::iterator new_row = found_list->append();
431 TreeModel::Row row = *new_row;
432 string path = Glib::filename_from_uri (string ("file:") + *i);
433 row[found_list_columns.pathname] = path;
437 vector<Glib::ustring>
438 SoundFileBrowser::get_paths ()
440 vector<Glib::ustring> results;
442 int n = notebook.get_current_page ();
445 vector<Glib::ustring> filenames = chooser.get_filenames();
446 vector<Glib::ustring>::iterator i;
447 for (i = filenames.begin(); i != filenames.end(); ++i) {
449 if ((!stat((*i).c_str(), &buf)) && S_ISREG(buf.st_mode)) {
450 results.push_back (*i);
457 typedef TreeView::Selection::ListHandle_Path ListPath;
459 ListPath rows = found_list_view.get_selection()->get_selected_rows ();
460 for (ListPath::iterator i = rows.begin() ; i != rows.end(); ++i) {
461 TreeIter iter = found_list->get_iter(*i);
462 Glib::ustring str = (*iter)[found_list_columns.pathname];
464 results.push_back (str);
470 SoundFileChooser::SoundFileChooser (string title, ARDOUR::Session* s)
471 : ArdourDialog (title, false),
474 set_default_size (700, 300);
476 get_vbox()->pack_start (browser, false, false);
478 add_button (Stock::OPEN, RESPONSE_OK);
479 add_button (Stock::CANCEL, RESPONSE_CANCEL);
481 browser.chooser.set_select_multiple (false);
482 browser.found_list_view.get_selection()->set_mode (SELECTION_SINGLE);
488 SoundFileChooser::get_filename ()
490 vector<Glib::ustring> paths;
492 paths = browser.get_paths ();
495 return Glib::ustring ();
498 if (!Glib::file_test (paths.front(), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
499 return Glib::ustring();
502 return paths.front();
506 SoundFileOptionsDialog::SoundFileOptionsDialog (Window& parent, const vector<Glib::ustring>& p, int selected_tracks)
507 : ArdourDialog (parent, _("External Audio Options"), false),
508 mode (ImportAsTrack),
510 split_files (_("Split non-mono files")),
511 merge_stereo (_("Use files as single stereo track")),
512 as_tracks (rgroup1, _("Use files as new tracks")),
513 to_tracks (rgroup1, _("Add files to selected tracks")),
514 as_regions (rgroup1, _("Add files to region list")),
515 as_tape_tracks (rgroup1, _("Add files as new tape tracks")),
516 import (rgroup2, _("Copy to Ardour-native files")),
517 embed (rgroup2, _("Use file without copying")),
518 selected_track_cnt (selected_tracks)
520 selection_includes_multichannel = check_multichannel_status (paths);
522 block_two.set_border_width (12);
523 block_three.set_border_width (12);
524 block_four.set_border_width (12);
526 block_two.pack_start (as_tracks, false, false);
527 block_two.pack_start (to_tracks, false, false);
528 block_two.pack_start (as_regions, false, false);
529 block_two.pack_start (as_tape_tracks, false, false);
531 as_tracks.signal_toggled().connect (mem_fun (*this, &SoundFileOptionsDialog::mode_changed));
532 to_tracks.signal_toggled().connect (mem_fun (*this, &SoundFileOptionsDialog::mode_changed));
533 as_regions.signal_toggled().connect (mem_fun (*this, &SoundFileOptionsDialog::mode_changed));
534 as_tape_tracks.signal_toggled().connect (mem_fun (*this, &SoundFileOptionsDialog::mode_changed));
536 block_three.pack_start (merge_stereo, false, false);
537 block_three.pack_start (split_files, false, false);
539 block_four.pack_start (import, false, false);
540 block_four.pack_start (embed, false, false);
542 get_vbox()->set_spacing (12);
543 get_vbox()->pack_start (block_two, false, false);
544 get_vbox()->pack_start (block_three, false, false);
545 get_vbox()->pack_start (block_four, false, false);
547 if (selected_track_cnt == 0) {
548 to_tracks.set_sensitive (false);
550 to_tracks.set_sensitive (true);
555 add_button (Stock::OK, RESPONSE_OK);
556 add_button (Stock::CANCEL, RESPONSE_CANCEL);
560 SoundFileOptionsDialog::mode_changed ()
562 if (as_tracks.get_active()) {
563 mode = ImportAsTrack;
564 } else if (to_tracks.get_active()) {
565 mode = ImportToTrack;
566 } else if (as_regions.get_active()) {
567 mode = ImportAsRegion;
569 mode = ImportAsTapeTrack;
572 if ((mode == ImportAsTrack || mode == ImportAsTapeTrack) && paths.size() == 2) {
573 merge_stereo.set_sensitive (true);
575 merge_stereo.set_sensitive (false);
578 if (selection_includes_multichannel) {
579 split_files.set_sensitive (true);
581 split_files.set_sensitive (false);
587 SoundFileOptionsDialog::check_multichannel_status (const vector<Glib::ustring>& paths)
592 for (vector<Glib::ustring>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
594 info.format = 0; // libsndfile says to clear this before sf_open().
596 if ((sf = sf_open ((char*) (*i).c_str(), SFM_READ, &info)) != 0) {
598 if (info.channels > 1) {