2 Copyright (C) 2005 Paul Davis
3 Written by Taybin Rutkin
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.
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.
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.
25 #include <gtkmm/box.h>
26 #include <gtkmm/stock.h>
28 #include <pbd/basename.h>
30 #include <gtkmm2ext/utils.h>
32 #include <ardour/audio_library.h>
33 #include <ardour/audioregion.h>
34 #include <ardour/externalsource.h>
36 #include "ardour_ui.h"
37 #include "gui_thread.h"
44 using namespace ARDOUR;
47 string length2string (const int32_t frames, const float sample_rate);
49 SoundFileBox::SoundFileBox ()
53 fields(Gtk::ListStore::create(label_columns)),
59 add_field_btn(_("Add Field...")),
60 remove_field_btn(_("Remove Field"))
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(field_view, true, true);
76 main_box.pack_start(top_box, false, false);
77 main_box.pack_start(bottom_box, false, false);
79 field_view.set_model (fields);
80 field_view.set_size_request(200, 150);
81 field_view.append_column (_("Field"), label_columns.field);
82 field_view.append_column_editable (_("Value"), label_columns.data);
84 top_box.set_homogeneous(true);
85 top_box.pack_start(add_field_btn);
86 top_box.pack_start(remove_field_btn);
88 remove_field_btn.set_sensitive(false);
90 bottom_box.set_homogeneous(true);
91 bottom_box.pack_start(play_btn);
92 bottom_box.pack_start(stop_btn);
94 play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::play_btn_clicked));
95 stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
97 add_field_btn.signal_clicked().connect
98 (mem_fun (*this, &SoundFileBox::add_field_clicked));
99 remove_field_btn.signal_clicked().connect
100 (mem_fun (*this, &SoundFileBox::remove_field_clicked));
102 field_view.get_selection()->signal_changed().connect (mem_fun (*this, &SoundFileBox::field_selected));
103 Library->fields_changed.connect (mem_fun (*this, &SoundFileBox::setup_fields));
110 SoundFileBox::set_session(Session* s)
115 play_btn.set_sensitive(false);
117 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
122 SoundFileBox::setup_labels (string filename)
127 if(!ExternalSource::get_soundfile_info (filename, sf_info, error_msg)) {
131 length.set_alignment (0.0f, 0.0f);
132 length.set_text (string_compose("Length: %1", length2string(sf_info.length, sf_info.samplerate)));
134 format.set_alignment (0.0f, 0.0f);
135 format.set_text (sf_info.format_name);
137 channels.set_alignment (0.0f, 0.0f);
138 channels.set_text (string_compose("Channels: %1", sf_info.channels));
140 samplerate.set_alignment (0.0f, 0.0f);
141 samplerate.set_text (string_compose("Samplerate: %1", sf_info.samplerate));
149 SoundFileBox::setup_fields ()
151 ENSURE_GUI_THREAD(mem_fun (*this, &SoundFileBox::setup_fields));
155 vector<string> field_list;
156 Library->get_fields(field_list);
158 vector<string>::iterator i;
159 Gtk::TreeModel::iterator iter;
160 Gtk::TreeModel::Row row;
161 for (i = field_list.begin(); i != field_list.end(); ++i) {
162 if (!(*i == _("channels") || *i == _("samplerate") ||
163 *i == _("resolution") || *i == _("format"))) {
164 iter = fields->append();
167 string value = Library->get_field(path, *i);
168 row[label_columns.field] = *i;
169 row[label_columns.data] = value;
175 SoundFileBox::play_btn_clicked ()
181 _session->cancel_audition();
183 if (access(path.c_str(), R_OK)) {
184 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
188 static std::map<string, AudioRegion*> region_cache;
190 if (region_cache.find (path) == region_cache.end()) {
191 AudioRegion::SourceList srclist;
194 for (int n = 0; n < sf_info.channels; ++n) {
196 sfs = ExternalSource::create (path+":"+string_compose("%1", n), false);
197 srclist.push_back(sfs);
199 } catch (failed_constructor& err) {
200 error << _("Could not access soundfile: ") << path << endmsg;
205 if (srclist.empty()) {
210 _session->region_name (result, PBD::basename(srclist[0]->name()), false);
211 AudioRegion* a_region = new AudioRegion(srclist, 0, srclist[0]->length(), result, 0, Region::DefaultFlags, false);
212 region_cache[path] = a_region;
218 _session->audition_region(*region_cache[path]);
222 SoundFileBox::stop_btn_clicked ()
225 _session->cancel_audition();
232 SoundFileBox::add_field_clicked ()
234 ArdourPrompter prompter (true);
237 prompter.set_prompt (_("Name for field"));
239 switch (prompter.run ()) {
240 case Gtk::RESPONSE_ACCEPT:
241 prompter.get_result (name);
243 Library->add_field (name);
244 Library->save_changes ();
254 SoundFileBox::remove_field_clicked ()
256 field_view.get_selection()->selected_foreach_iter(mem_fun(*this, &SoundFileBox::delete_row));
258 Library->save_changes ();
262 SoundFileBox::delete_row (const Gtk::TreeModel::iterator& iter)
264 Gtk::TreeModel::Row row = *iter;
266 Library->remove_field(row[label_columns.field]);
270 SoundFileBox::audition_status_changed (bool active)
272 ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
280 SoundFileBox::field_selected ()
282 if (field_view.get_selection()->count_selected_rows()) {
283 remove_field_btn.set_sensitive(true);
285 remove_field_btn.set_sensitive(false);
289 SoundFileBrowser::SoundFileBrowser (string title)
290 : ArdourDialog (title, false),
291 chooser (Gtk::FILE_CHOOSER_ACTION_OPEN)
293 get_vbox()->pack_start(chooser);
294 chooser.set_preview_widget(preview);
295 chooser.set_select_multiple (true);
297 chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
301 SoundFileBrowser::set_session (Session* s)
303 preview.set_session(s);
307 SoundFileBrowser::update_preview ()
309 chooser.set_preview_widget_active(preview.setup_labels(chooser.get_filename()));
312 SoundFileChooser::SoundFileChooser (string title)
314 SoundFileBrowser(title)
316 add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
317 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
321 static const char *import_mode_strings[] = {
322 X_("Add to Region list"),
323 X_("Add as new Track(s)"),
324 X_("Add to selected Track(s)"),
328 vector<string> SoundFileOmega::mode_strings;
330 SoundFileOmega::SoundFileOmega (string title)
331 : SoundFileBrowser (title),
332 split_check (_("Split Channels"))
334 if (mode_strings.empty()) {
335 mode_strings = internationalize (import_mode_strings);
338 ARDOUR_UI::instance()->tooltips().set_tip(split_check,
339 _("Create a region for each channel"));
341 Gtk::Button* btn = add_button (_("Embed"), ResponseEmbed);
342 ARDOUR_UI::instance()->tooltips().set_tip(*btn,
343 _("Link to an external file"));
345 btn = add_button (_("Import"), ResponseImport);
346 ARDOUR_UI::instance()->tooltips().set_tip(*btn,
347 _("Copy a file to the session folder"));
349 add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
351 Gtk::HBox *box = manage (new Gtk::HBox());
353 Gtkmm2ext::set_popdown_strings (mode_combo, mode_strings);
355 set_mode (Editing::ImportAsRegion);
357 box->pack_start (split_check);
358 box->pack_start (mode_combo);
360 mode_combo.signal_changed().connect (mem_fun (*this, &SoundFileOmega::mode_changed));
362 chooser.set_extra_widget (*box);
368 SoundFileOmega::get_split ()
370 return split_check.get_active();
373 vector<Glib::ustring>
374 SoundFileOmega::get_paths ()
376 return chooser.get_filenames();
380 SoundFileOmega::set_mode (Editing::ImportMode mode)
382 mode_combo.set_active_text (mode_strings[(int)mode]);
385 case Editing::ImportAsRegion:
386 split_check.set_sensitive (true);
388 case Editing::ImportAsTrack:
389 split_check.set_sensitive (true);
391 case Editing::ImportToTrack:
392 split_check.set_sensitive (false);
398 SoundFileOmega::get_mode ()
400 vector<string>::iterator i;
402 string str = mode_combo.get_active_text ();
404 for (n = 0, i = mode_strings.begin (); i != mode_strings.end(); ++i, ++n) {
410 if (i == mode_strings.end()) {
411 fatal << string_compose (_("programming error: %1"), X_("unknown import mode string")) << endmsg;
415 return (Editing::ImportMode) (n);
419 SoundFileOmega::mode_changed ()
421 Editing::ImportMode mode = get_mode();
424 case Editing::ImportAsRegion:
425 split_check.set_sensitive (true);
427 case Editing::ImportAsTrack:
428 split_check.set_sensitive (true);
430 case Editing::ImportToTrack:
431 split_check.set_sensitive (false);