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.
24 #include <gtkmm/box.h>
25 #include <gtkmm/stock.h>
27 #include <pbd/convert.h>
28 #include <pbd/whitespace.h>
30 #include <gtkmm2ext/utils.h>
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>
38 #include "ardour_ui.h"
40 #include "gui_thread.h"
47 using namespace ARDOUR;
51 SoundFileBox::SoundFileBox ()
62 set_name (X_("SoundFileBox"));
63 border_frame.set_label (_("Soundfile Info"));
64 border_frame.add (main_box);
66 pack_start (border_frame);
69 main_box.set_border_width (4);
71 main_box.pack_start(length, false, false);
72 main_box.pack_start(format, false, false);
73 main_box.pack_start(channels, false, false);
74 main_box.pack_start(samplerate, false, false);
75 main_box.pack_start(timecode, false, false);
76 main_box.pack_start(tags_entry, true, true);
77 main_box.pack_start(top_box, false, false);
78 main_box.pack_start(bottom_box, false, false);
80 top_box.set_homogeneous(true);
81 top_box.pack_start(apply_btn);
83 bottom_box.set_homogeneous(true);
84 bottom_box.pack_start(play_btn);
85 bottom_box.pack_start(stop_btn);
87 // tags_entry.signal_focus_out_event().connect (mem_fun (*this, &SoundFileBox::tags_entry_left));
88 play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::play_btn_clicked));
89 stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
90 apply_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::apply_btn_clicked));
97 SoundFileBox::set_session(Session* s)
102 play_btn.set_sensitive(false);
104 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
109 SoundFileBox::setup_labels (string filename)
115 if(!AudioFileSource::get_soundfile_info (filename, sf_info, error_msg)) {
119 length.set_alignment (0.0f, 0.0f);
120 length.set_text (string_compose(_("Length: %1"), length2string(sf_info.length, sf_info.samplerate)));
122 format.set_alignment (0.0f, 0.0f);
123 format.set_text (sf_info.format_name);
125 channels.set_alignment (0.0f, 0.0f);
126 channels.set_text (string_compose(_("Channels: %1"), sf_info.channels));
128 samplerate.set_alignment (0.0f, 0.0f);
129 samplerate.set_text (string_compose(_("Samplerate: %1"), sf_info.samplerate));
131 timecode.set_alignment (0.0f, 0.0f);
132 timecode.set_text (string_compose (_("Timecode: %1"), length2string(sf_info.timecode, sf_info.samplerate)));
134 vector<string> tags = Library->get_tags (filename);
136 stringstream tag_string;
137 for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
138 if (i != tags.begin()) {
143 tags_entry.set_text (tag_string.str());
149 SoundFileBox::tags_entry_left (GdkEventFocus* event)
151 apply_btn_clicked ();
157 SoundFileBox::play_btn_clicked ()
163 _session->cancel_audition();
165 if (access(path.c_str(), R_OK)) {
166 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
170 typedef std::map<string, boost::shared_ptr<AudioRegion> > RegionCache;
171 static RegionCache region_cache;
172 RegionCache::iterator the_region;
174 if ((the_region = region_cache.find (path)) == region_cache.end()) {
176 boost::shared_ptr<AudioFileSource> afs;
178 for (int n = 0; n < sf_info.channels; ++n) {
180 afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (*_session, path+":"+string_compose("%1", n), AudioFileSource::Flag (0)));
181 srclist.push_back(afs);
183 } catch (failed_constructor& err) {
184 error << _("Could not access soundfile: ") << path << endmsg;
189 if (srclist.empty()) {
195 _session->region_name (rname, Glib::path_get_basename(srclist[0]->name()), false);
197 pair<string,boost::shared_ptr<AudioRegion> > newpair;
198 pair<RegionCache::iterator,bool> res;
200 newpair.first = path;
201 newpair.second = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, 0, srclist[0]->length(), rname, 0, Region::DefaultFlags, false));
203 res = region_cache.insert (newpair);
204 the_region = res.first;
210 boost::shared_ptr<Region> r = boost::static_pointer_cast<Region> (the_region->second);
212 _session->audition_region(r);
216 SoundFileBox::stop_btn_clicked ()
219 _session->cancel_audition();
226 SoundFileBox::apply_btn_clicked ()
228 string tag_string = tags_entry.get_text ();
232 static const string DELIMITERS = ",";
234 // Skip delimiters at beginning.
235 string::size_type last_pos = tag_string.find_first_not_of(DELIMITERS, 0);
236 // Find first "non-delimiter".
237 string::size_type pos = tag_string.find_first_of(DELIMITERS, last_pos);
239 while (string::npos != pos || string::npos != last_pos) {
240 string x = tag_string.substr(last_pos, pos - last_pos);
241 strip_whitespace_edges (x);
245 // Skip delimiters. Note the "not_of"
246 last_pos = tag_string.find_first_not_of(DELIMITERS, pos);
247 // Find next "non-delimiter"
248 pos = tag_string.find_first_of(DELIMITERS, last_pos);
251 Library->set_tags (path, tags);
252 Library->save_changes ();
256 SoundFileBox::audition_status_changed (bool active)
258 ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
265 // this needs to be kept in sync with the ImportMode enum defined in editing.h and editing_syms.h.
266 static const char *import_mode_strings[] = {
267 N_("Add to Region list"),
268 N_("Add to selected Track(s)"),
269 N_("Add as new Track(s)"),
270 N_("Add as new Tape Track(s)"),
274 SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s)
275 : ArdourDialog (title, false),
276 chooser (Gtk::FILE_CHOOSER_ACTION_OPEN)
278 set_default_size (700, 500);
279 get_vbox()->pack_start(chooser);
280 chooser.set_preview_widget(preview);
281 chooser.set_select_multiple (true);
283 chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
289 SoundFileBrowser::set_session (Session* s)
291 preview.set_session(s);
295 SoundFileBrowser::update_preview ()
297 chooser.set_preview_widget_active(preview.setup_labels(chooser.get_filename()));
300 SoundFileChooser::SoundFileChooser (string title, ARDOUR::Session* s)
302 SoundFileBrowser(title, s)
304 add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
305 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
309 vector<string> SoundFileOmega::mode_strings;
311 SoundFileOmega::SoundFileOmega (string title, ARDOUR::Session* s)
312 : SoundFileBrowser (title, s),
313 split_check (_("Split Channels"))
315 if (mode_strings.empty()) {
316 mode_strings = I18N (import_mode_strings);
319 ARDOUR_UI::instance()->tooltips().set_tip(split_check,
320 _("Create a region for each channel"));
322 Gtk::Button* btn = add_button (_("Embed"), ResponseEmbed);
323 ARDOUR_UI::instance()->tooltips().set_tip(*btn,
324 _("Link to an external file"));
326 add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
328 btn = add_button (_("Import"), ResponseImport);
329 ARDOUR_UI::instance()->tooltips().set_tip(*btn,
330 _("Copy a file to the session folder"));
332 Gtk::HBox *box = manage (new Gtk::HBox());
334 Gtkmm2ext::set_popdown_strings (mode_combo, mode_strings);
336 set_mode (Editing::ImportAsRegion);
338 box->pack_start (split_check);
339 box->pack_start (mode_combo);
341 mode_combo.signal_changed().connect (mem_fun (*this, &SoundFileOmega::mode_changed));
343 chooser.set_extra_widget (*box);
349 SoundFileOmega::get_split ()
351 return split_check.get_active();
354 vector<Glib::ustring>
355 SoundFileOmega::get_paths ()
357 return chooser.get_filenames();
361 SoundFileOmega::set_mode (Editing::ImportMode mode)
363 mode_combo.set_active_text (mode_strings[(int)mode]);
366 case Editing::ImportAsRegion:
367 split_check.set_sensitive (true);
369 case Editing::ImportAsTrack:
370 split_check.set_sensitive (true);
372 case Editing::ImportToTrack:
373 split_check.set_sensitive (false);
375 case Editing::ImportAsTapeTrack:
376 split_check.set_sensitive (true);
382 SoundFileOmega::get_mode ()
384 vector<string>::iterator i;
386 string str = mode_combo.get_active_text ();
388 for (n = 0, i = mode_strings.begin (); i != mode_strings.end(); ++i, ++n) {
394 if (i == mode_strings.end()) {
395 fatal << string_compose (_("programming error: %1"), X_("unknown import mode string")) << endmsg;
399 return (Editing::ImportMode) (n);
403 SoundFileOmega::mode_changed ()
405 Editing::ImportMode mode = get_mode();
408 case Editing::ImportAsRegion:
409 split_check.set_sensitive (true);
411 case Editing::ImportAsTrack:
412 split_check.set_sensitive (true);
414 case Editing::ImportToTrack:
415 split_check.set_sensitive (false);
417 case Editing::ImportAsTapeTrack:
418 split_check.set_sensitive (true);