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>
29 #include <pbd/convert.h>
30 #include <pbd/tokenizer.h>
32 #include <gtkmm2ext/utils.h>
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>
40 #include "ardour_ui.h"
42 #include "gui_thread.h"
49 using namespace ARDOUR;
53 Glib::ustring SoundFileBrowser::persistent_folder;
55 SoundFileBox::SoundFileBox ()
65 set_name (X_("SoundFileBox"));
67 set_size_request (250, 500);
69 border_frame.set_label (_("Soundfile Info"));
70 border_frame.add (main_box);
72 Gtk::Label* tag_label = manage(new Gtk::Label(_("comma seperated tags")));
74 pack_start (border_frame);
77 main_box.set_border_width (4);
79 main_box.pack_start(length, false, false);
80 main_box.pack_start(format, false, false);
81 main_box.pack_start(channels, false, false);
82 main_box.pack_start(samplerate, false, false);
83 main_box.pack_start(timecode, false, false);
84 main_box.pack_start(*tag_label, false, false);
85 main_box.pack_start(tags_entry, false, false);
86 main_box.pack_start(apply_btn, false, false);
87 main_box.pack_start(bottom_box, false, false);
89 bottom_box.set_homogeneous(true);
90 bottom_box.pack_start(play_btn);
91 bottom_box.pack_start(stop_btn);
93 play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::play_btn_clicked));
94 stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
95 apply_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::apply_btn_clicked));
96 tags_entry.signal_activate().connect (mem_fun (*this, &SoundFileBox::apply_btn_clicked));
98 length.set_alignment (0.0f, 0.0f);
99 format.set_alignment (0.0f, 0.0f);
100 channels.set_alignment (0.0f, 0.0f);
101 samplerate.set_alignment (0.0f, 0.0f);
102 timecode.set_alignment (0.0f, 0.0f);
104 stop_btn.set_no_show_all (true);
111 SoundFileBox::set_session(Session* s)
116 play_btn.set_sensitive(false);
118 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
123 SoundFileBox::setup_labels (string filename)
129 if(!AudioFileSource::get_soundfile_info (filename, sf_info, error_msg)) {
130 length.set_text (_("Length: n/a"));
131 format.set_text (_("Format: n/a"));
132 channels.set_text (_("Channels: n/a"));
133 samplerate.set_text (_("Samplerate: n/a"));
134 timecode.set_text (_("Timecode: n/a"));
135 tags_entry.set_text ("");
137 tags_entry.set_sensitive (false);
138 play_btn.set_sensitive (false);
139 apply_btn.set_sensitive (false);
144 length.set_text (string_compose(_("Length: %1"), length2string(sf_info.length, sf_info.samplerate)));
145 format.set_text (sf_info.format_name);
146 channels.set_text (string_compose(_("Channels: %1"), sf_info.channels));
147 samplerate.set_text (string_compose(_("Samplerate: %1"), sf_info.samplerate));
148 timecode.set_text (string_compose (_("Timecode: %1"), length2string(sf_info.timecode, sf_info.samplerate)));
150 vector<string> tags = Library->get_tags (filename);
152 stringstream tag_string;
153 for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
154 if (i != tags.begin()) {
159 tags_entry.set_text (tag_string.str());
161 tags_entry.set_sensitive (true);
163 play_btn.set_sensitive (true);
165 apply_btn.set_sensitive (true);
171 SoundFileBox::tags_entry_left (GdkEventFocus* event)
173 apply_btn_clicked ();
179 SoundFileBox::play_btn_clicked ()
185 _session->cancel_audition();
187 if (access(path.c_str(), R_OK)) {
188 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
192 typedef std::map<string, boost::shared_ptr<AudioRegion> > RegionCache;
193 static RegionCache region_cache;
194 RegionCache::iterator the_region;
196 if ((the_region = region_cache.find (path)) == region_cache.end()) {
198 boost::shared_ptr<AudioFileSource> afs;
200 for (int n = 0; n < sf_info.channels; ++n) {
202 afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (*_session, path, n, AudioFileSource::Flag (0)));
203 srclist.push_back(afs);
205 } catch (failed_constructor& err) {
206 error << _("Could not access soundfile: ") << path << endmsg;
211 if (srclist.empty()) {
217 _session->region_name (rname, Glib::path_get_basename(srclist[0]->name()), false);
219 pair<string,boost::shared_ptr<AudioRegion> > newpair;
220 pair<RegionCache::iterator,bool> res;
222 newpair.first = path;
223 newpair.second = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, 0, srclist[0]->length(), rname, 0, Region::DefaultFlags, false));
225 res = region_cache.insert (newpair);
226 the_region = res.first;
232 boost::shared_ptr<Region> r = boost::static_pointer_cast<Region> (the_region->second);
234 _session->audition_region(r);
238 SoundFileBox::stop_btn_clicked ()
241 _session->cancel_audition();
248 SoundFileBox::apply_btn_clicked ()
250 string tag_string = tags_entry.get_text ();
254 if (!PBD::tokenize (tag_string, string(","), std::back_inserter (tags), true)) {
255 warning << _("SoundFileBox: Could not tokenize string: ") << tag_string << endmsg;
259 Library->set_tags (path, tags);
260 Library->save_changes ();
264 SoundFileBox::audition_status_changed (bool active)
266 ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
273 // this needs to be kept in sync with the ImportMode enum defined in editing.h and editing_syms.h.
274 static const char *import_mode_strings[] = {
275 N_("Add to Region list"),
276 N_("Add to selected Track(s)"),
277 N_("Add as new Track(s)"),
278 N_("Add as new Tape Track(s)"),
282 SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s)
283 : ArdourDialog (title, false),
284 chooser (Gtk::FILE_CHOOSER_ACTION_OPEN),
285 found_list (Gtk::ListStore::create(found_list_columns)),
286 found_list_view (found_list),
287 found_search_btn (_("Search"))
289 set_default_size (700, 500);
290 Gtk::HBox* hbox = manage(new Gtk::HBox);
291 hbox->pack_start(notebook);
292 hbox->pack_start(preview, Gtk::PACK_SHRINK);
293 get_vbox()->pack_start(*hbox);
295 hbox = manage(new Gtk::HBox);
296 hbox->pack_start (found_entry);
297 hbox->pack_start (found_search_btn);
299 Gtk::VBox* vbox = manage(new Gtk::VBox);
300 vbox->pack_start (*hbox, Gtk::PACK_SHRINK);
301 vbox->pack_start (found_list_view);
302 found_list_view.append_column(_("Paths"), found_list_columns.pathname);
304 notebook.append_page (chooser, _("Files"));
305 notebook.append_page (*vbox, _("Tags"));
307 found_list_view.get_selection()->set_mode (Gtk::SELECTION_MULTIPLE);
309 filter.add_custom (Gtk::FILE_FILTER_FILENAME, mem_fun(*this, &SoundFileBrowser::on_custom));
310 chooser.set_filter (filter);
311 chooser.set_select_multiple (true);
312 chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
314 if (!persistent_folder.empty()) {
315 chooser.set_current_folder (persistent_folder);
318 found_list_view.get_selection()->signal_changed().connect(mem_fun(*this, &SoundFileBrowser::found_list_view_selected));
320 found_search_btn.signal_clicked().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
321 found_entry.signal_activate().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
328 SoundFileBrowser::~SoundFileBrowser ()
330 persistent_folder = chooser.get_current_folder();
334 SoundFileBrowser::set_session (Session* s)
336 preview.set_session(s);
340 SoundFileBrowser::on_custom (const Gtk::FileFilter::Info& filter_info)
342 return AudioFileSource::safe_file_extension(filter_info.filename);
346 SoundFileBrowser::update_preview ()
348 preview.setup_labels(chooser.get_filename());
352 SoundFileBrowser::found_list_view_selected ()
356 Gtk::TreeView::Selection::ListHandle_Path rows = found_list_view.get_selection()->get_selected_rows ();
359 Gtk::TreeIter iter = found_list->get_iter(*rows.begin());
360 file = (*iter)[found_list_columns.pathname];
361 chooser.set_filename (file);
363 preview.setup_labels (file);
367 SoundFileBrowser::found_search_clicked ()
369 string tag_string = found_entry.get_text ();
373 if (!PBD::tokenize (tag_string, string(","), std::back_inserter (tags), true)) {
374 warning << _("SoundFileBrowser: Could not tokenize string: ") << tag_string << endmsg;
378 vector<string> results;
379 Library->search_members_and (results, tags);
382 for (vector<string>::iterator i = results.begin(); i != results.end(); ++i) {
383 Gtk::TreeModel::iterator new_row = found_list->append();
384 Gtk::TreeModel::Row row = *new_row;
385 row[found_list_columns.pathname] = *i;
389 SoundFileChooser::SoundFileChooser (string title, ARDOUR::Session* s)
391 SoundFileBrowser(title, s)
393 add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
394 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
396 chooser.set_select_multiple (false);
397 found_list_view.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
402 SoundFileChooser::get_filename ()
404 Gtk::TreeModel::iterator iter;
405 Gtk::TreeModel::Row row;
408 switch (notebook.get_current_page()) {
410 filename = chooser.get_filename();
412 iter = found_list_view.get_selection()->get_selected();
414 filename = row[found_list_columns.pathname];
421 if (stat (filename.c_str(), &buf) || !S_ISREG(buf.st_mode)) {
428 vector<string> SoundFileOmega::mode_strings;
430 SoundFileOmega::SoundFileOmega (string title, ARDOUR::Session* s)
431 : SoundFileBrowser (title, s),
432 split_check (_("Split Channels"))
434 ARDOUR_UI::instance()->tooltips().set_tip(split_check,
435 _("Create a region for each channel"));
437 Gtk::Button* btn = add_button (_("Embed"), ResponseEmbed);
438 ARDOUR_UI::instance()->tooltips().set_tip(*btn,
439 _("Link to an external file"));
441 btn = add_button (_("Import"), ResponseImport);
442 ARDOUR_UI::instance()->tooltips().set_tip(*btn,
443 _("Copy a file to the session folder"));
445 add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
447 if (mode_strings.empty()) {
448 mode_strings = I18N (import_mode_strings);
450 Gtkmm2ext::set_popdown_strings (mode_combo, mode_strings);
452 set_mode (Editing::ImportAsRegion);
454 get_action_area()->pack_start (split_check);
455 get_action_area()->pack_start (mode_combo);
457 mode_combo.signal_changed().connect (mem_fun (*this, &SoundFileOmega::mode_changed));
463 SoundFileOmega::get_split ()
465 return split_check.get_active();
468 vector<Glib::ustring>
469 SoundFileOmega::get_paths ()
471 vector<Glib::ustring> results;
473 int n = notebook.get_current_page ();
476 vector<Glib::ustring> filenames = chooser.get_filenames();
477 vector<Glib::ustring>::iterator i;
478 for (i = filenames.begin(); i != filenames.end(); ++i) {
480 if ((!stat((*i).c_str(), &buf)) && S_ISREG(buf.st_mode)) {
481 results.push_back (*i);
488 typedef Gtk::TreeView::Selection::ListHandle_Path ListPath;
490 ListPath rows = found_list_view.get_selection()->get_selected_rows ();
491 for (ListPath::iterator i = rows.begin() ; i != rows.end(); ++i) {
492 Gtk::TreeIter iter = found_list->get_iter(*i);
493 string str = (*iter)[found_list_columns.pathname];
495 results.push_back (str);
502 SoundFileOmega::set_mode (Editing::ImportMode mode)
504 mode_combo.set_active_text (mode_strings[(int)mode]);
507 case Editing::ImportAsRegion:
508 split_check.set_sensitive (true);
510 case Editing::ImportAsTrack:
511 split_check.set_sensitive (true);
513 case Editing::ImportToTrack:
514 split_check.set_sensitive (false);
516 case Editing::ImportAsTapeTrack:
517 split_check.set_sensitive (true);
523 SoundFileOmega::get_mode ()
525 vector<string>::iterator i;
527 string str = mode_combo.get_active_text ();
529 for (n = 0, i = mode_strings.begin (); i != mode_strings.end(); ++i, ++n) {
535 if (i == mode_strings.end()) {
536 fatal << string_compose (_("programming error: %1"), X_("unknown import mode string")) << endmsg;
540 return (Editing::ImportMode) (n);
544 SoundFileOmega::mode_changed ()
546 Editing::ImportMode mode = get_mode();
549 case Editing::ImportAsRegion:
550 split_check.set_sensitive (true);
552 case Editing::ImportAsTrack:
553 split_check.set_sensitive (true);
555 case Editing::ImportToTrack:
556 split_check.set_sensitive (false);
558 case Editing::ImportAsTapeTrack:
559 split_check.set_sensitive (true);