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 <pbd/basename.h>
27 #include <gtkmm/box.h>
28 #include <gtkmm/stock.h>
30 #include <ardour/audio_library.h>
31 #include <ardour/audioregion.h>
32 #include <ardour/externalsource.h>
34 #include "gui_thread.h"
40 using namespace ARDOUR;
42 std::string length2string (const int32_t frames, const float sample_rate);
44 SoundFileBox::SoundFileBox ()
48 fields(Gtk::ListStore::create(label_columns)),
54 add_field_btn(_("Add Field...")),
55 remove_field_btn(_("Remove Field"))
57 set_name (X_("SoundFileBox"));
58 border_frame.set_label (_("Soundfile Info"));
59 border_frame.add (main_box);
61 pack_start (border_frame);
64 main_box.set_border_width (4);
66 main_box.pack_start(length, false, false);
67 main_box.pack_start(format, false, false);
68 main_box.pack_start(channels, false, false);
69 main_box.pack_start(samplerate, false, false);
70 main_box.pack_start(field_view, true, true);
71 main_box.pack_start(top_box, false, false);
72 main_box.pack_start(bottom_box, false, false);
74 field_view.set_model (fields);
75 field_view.set_size_request(200, 150);
76 field_view.append_column (_("Field"), label_columns.field);
77 field_view.append_column_editable (_("Value"), label_columns.data);
79 top_box.set_homogeneous(true);
80 top_box.pack_start(add_field_btn);
81 top_box.pack_start(remove_field_btn);
83 remove_field_btn.set_sensitive(false);
85 bottom_box.set_homogeneous(true);
86 bottom_box.pack_start(play_btn);
87 bottom_box.pack_start(stop_btn);
89 play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::play_btn_clicked));
90 stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
92 add_field_btn.signal_clicked().connect
93 (mem_fun (*this, &SoundFileBox::add_field_clicked));
94 remove_field_btn.signal_clicked().connect
95 (mem_fun (*this, &SoundFileBox::remove_field_clicked));
97 field_view.get_selection()->signal_changed().connect (mem_fun (*this, &SoundFileBox::field_selected));
98 Library->fields_changed.connect (mem_fun (*this, &SoundFileBox::setup_fields));
105 SoundFileBox::set_session(Session* s)
110 play_btn.set_sensitive(false);
112 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
117 SoundFileBox::setup_labels (string filename)
122 if(!ExternalSource::get_soundfile_info (filename, sf_info, error_msg)) {
126 length.set_alignment (0.0f, 0.0f);
127 length.set_text (string_compose("Length: %1", length2string(sf_info.length, sf_info.samplerate)));
129 format.set_alignment (0.0f, 0.0f);
130 format.set_text (sf_info.format_name);
132 channels.set_alignment (0.0f, 0.0f);
133 channels.set_text (string_compose("Channels: %1", sf_info.channels));
135 samplerate.set_alignment (0.0f, 0.0f);
136 samplerate.set_text (string_compose("Samplerate: %1", sf_info.samplerate));
144 SoundFileBox::setup_fields ()
146 ENSURE_GUI_THREAD(mem_fun (*this, &SoundFileBox::setup_fields));
150 vector<string> field_list;
151 Library->get_fields(field_list);
153 vector<string>::iterator i;
154 Gtk::TreeModel::iterator iter;
155 Gtk::TreeModel::Row row;
156 for (i = field_list.begin(); i != field_list.end(); ++i) {
157 if (!(*i == _("channels") || *i == _("samplerate") ||
158 *i == _("resolution") || *i == _("format"))) {
159 iter = fields->append();
162 string value = Library->get_field(path, *i);
163 row[label_columns.field] = *i;
164 row[label_columns.data] = value;
170 SoundFileBox::play_btn_clicked ()
176 _session->cancel_audition();
178 if (access(path.c_str(), R_OK)) {
179 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
183 static std::map<string, AudioRegion*> region_cache;
185 if (region_cache.find (path) == region_cache.end()) {
186 AudioRegion::SourceList srclist;
189 for (int n = 0; n < sf_info.channels; ++n) {
191 sfs = ExternalSource::create (path+":"+string_compose("%1", n), false);
192 srclist.push_back(sfs);
194 } catch (failed_constructor& err) {
195 error << _("Could not access soundfile: ") << path << endmsg;
200 if (srclist.empty()) {
205 _session->region_name (result, PBD::basename(srclist[0]->name()), false);
206 AudioRegion* a_region = new AudioRegion(srclist, 0, srclist[0]->length(), result, 0, Region::DefaultFlags, false);
207 region_cache[path] = a_region;
213 _session->audition_region(*region_cache[path]);
217 SoundFileBox::stop_btn_clicked ()
220 _session->cancel_audition();
227 SoundFileBox::add_field_clicked ()
229 ArdourPrompter prompter (true);
232 prompter.set_prompt (_("Name for field"));
234 switch (prompter.run ()) {
235 case Gtk::RESPONSE_ACCEPT:
236 prompter.get_result (name);
238 Library->add_field (name);
239 Library->save_changes ();
249 SoundFileBox::remove_field_clicked ()
251 field_view.get_selection()->selected_foreach_iter(mem_fun(*this, &SoundFileBox::delete_row));
253 Library->save_changes ();
257 SoundFileBox::delete_row (const Gtk::TreeModel::iterator& iter)
259 Gtk::TreeModel::Row row = *iter;
261 Library->remove_field(row[label_columns.field]);
265 SoundFileBox::audition_status_changed (bool active)
267 ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
275 SoundFileBox::field_selected ()
277 if (field_view.get_selection()->count_selected_rows()) {
278 remove_field_btn.set_sensitive(true);
280 remove_field_btn.set_sensitive(false);
284 SoundFileBrowser::SoundFileBrowser (std::string title)
287 chooser(Gtk::FILE_CHOOSER_ACTION_OPEN)
289 get_vbox()->pack_start(chooser);
290 chooser.set_preview_widget(preview);
292 chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
296 SoundFileBrowser::set_session (Session* s)
298 preview.set_session(s);
302 SoundFileBrowser::update_preview ()
304 chooser.set_preview_widget_active(preview.setup_labels(chooser.get_filename()));
307 SoundFileChooser::SoundFileChooser (std::string title)
309 SoundFileBrowser(title)
311 add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
312 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
317 SoundFileOmega::SoundFileOmega (std::string title)
319 SoundFileBrowser(title),
320 embed_btn (_("Embed")),
321 import_btn (_("Import")),
322 split_check (_("Split Channels"))
324 get_action_area()->pack_start(embed_btn);
325 get_action_area()->pack_start(import_btn);
326 add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
328 chooser.set_extra_widget(split_check);
330 embed_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::embed_clicked));
331 import_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::import_clicked));
337 SoundFileOmega::embed_clicked ()
339 Embedded (chooser.get_filenames(), split_check.get_active());
343 SoundFileOmega::import_clicked ()
345 Imported (chooser.get_filenames(), split_check.get_active());
349 length2string (const int32_t frames, const float sample_rate)
351 int secs = (int) (frames / sample_rate);
352 int hrs = secs / 3600;
353 secs -= (hrs * 3600);
354 int mins = secs / 60;
357 int total_secs = (hrs * 3600) + (mins * 60) + secs;
358 int frames_remaining = frames - (total_secs * sample_rate);
359 float fractional_secs = (float) frames_remaining / sample_rate;
361 char duration_str[32];
362 sprintf (duration_str, "%02d:%02d:%05.2f", hrs, mins, (float) secs + fractional_secs);