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.
27 #include <pbd/basename.h>
29 #include <gtkmm/box.h>
30 #include <gtkmm/stock.h>
32 #include <ardour/audio_library.h>
33 #include <ardour/audioregion.h>
34 #include <ardour/sndfile_helpers.h>
35 #include <ardour/sndfilesource.h>
37 #include "gui_thread.h"
43 using namespace ARDOUR;
45 std::string length2string (const int32_t frames, const int32_t sample_rate);
47 SoundFileBox::SoundFileBox ()
51 fields(Gtk::ListStore::create(label_columns)),
57 add_field_btn(_("Add Field...")),
58 remove_field_btn(_("Remove Field"))
60 set_name (X_("SoundFileBox"));
61 border_frame.set_label (_("Soundfile Info"));
62 border_frame.add (main_box);
64 pack_start (border_frame);
67 main_box.set_border_width (4);
69 main_box.pack_start(length, false, false);
70 main_box.pack_start(format, false, false);
71 main_box.pack_start(channels, false, false);
72 main_box.pack_start(samplerate, false, false);
73 main_box.pack_start(field_view, true, true);
74 main_box.pack_start(top_box, false, false);
75 main_box.pack_start(bottom_box, false, false);
77 field_view.set_model (fields);
78 field_view.set_size_request(200, 150);
79 field_view.append_column (_("Field"), label_columns.field);
80 field_view.append_column_editable (_("Value"), label_columns.data);
82 top_box.set_homogeneous(true);
83 top_box.pack_start(add_field_btn);
84 top_box.pack_start(remove_field_btn);
86 remove_field_btn.set_sensitive(false);
88 bottom_box.set_homogeneous(true);
89 bottom_box.pack_start(play_btn);
90 bottom_box.pack_start(stop_btn);
92 play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::play_btn_clicked));
93 stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
95 add_field_btn.signal_clicked().connect
96 (mem_fun (*this, &SoundFileBox::add_field_clicked));
97 remove_field_btn.signal_clicked().connect
98 (mem_fun (*this, &SoundFileBox::remove_field_clicked));
100 field_view.get_selection()->signal_changed().connect (mem_fun (*this, &SoundFileBox::field_selected));
101 Library->fields_changed.connect (mem_fun (*this, &SoundFileBox::setup_fields));
108 SoundFileBox::set_session(Session* s)
113 play_btn.set_sensitive(false);
115 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
120 SoundFileBox::setup_labels (string filename)
126 sf_info.format = 0; // libsndfile says to clear this before sf_open().
128 if ((sf = sf_open ((char *) filename.c_str(), SFM_READ, &sf_info)) < 0) {
134 if (sf_info.frames == 0 && sf_info.channels == 0 &&
135 sf_info.samplerate == 0 && sf_info.format == 0 &&
136 sf_info.sections == 0) {
137 /* .. ok, it's not a sound file */
141 length.set_alignment (0.0f, 0.0f);
142 length.set_text (string_compose("Length: %1", length2string(sf_info.frames, sf_info.samplerate)));
144 format.set_alignment (0.0f, 0.0f);
145 format.set_text (string_compose("Format: %1, %2",
146 sndfile_major_format(sf_info.format),
147 sndfile_minor_format(sf_info.format)));
149 channels.set_alignment (0.0f, 0.0f);
150 channels.set_text (string_compose("Channels: %1", sf_info.channels));
152 samplerate.set_alignment (0.0f, 0.0f);
153 samplerate.set_text (string_compose("Samplerate: %1", sf_info.samplerate));
161 SoundFileBox::setup_fields ()
163 ENSURE_GUI_THREAD(mem_fun (*this, &SoundFileBox::setup_fields));
167 vector<string> field_list;
168 Library->get_fields(field_list);
170 vector<string>::iterator i;
171 Gtk::TreeModel::iterator iter;
172 Gtk::TreeModel::Row row;
173 for (i = field_list.begin(); i != field_list.end(); ++i) {
174 if (!(*i == _("channels") || *i == _("samplerate") ||
175 *i == _("resolution") || *i == _("format"))) {
176 iter = fields->append();
179 string value = Library->get_field(path, *i);
180 row[label_columns.field] = *i;
181 row[label_columns.data] = value;
187 SoundFileBox::play_btn_clicked ()
193 _session->cancel_audition();
195 if (access(path.c_str(), R_OK)) {
196 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
200 static std::map<string, AudioRegion*> region_cache;
202 if (region_cache.find (path) == region_cache.end()) {
203 AudioRegion::SourceList srclist;
206 for (int n = 0; n < sf_info.channels; ++n) {
208 sfs = new SndFileSource(path+":"+string_compose("%1", n), false);
209 srclist.push_back(sfs);
211 } catch (failed_constructor& err) {
212 error << _("Could not access soundfile: ") << path << endmsg;
217 if (srclist.empty()) {
222 _session->region_name (result, PBD::basename(srclist[0]->name()), false);
223 AudioRegion* a_region = new AudioRegion(srclist, 0, srclist[0]->length(), result, 0, Region::DefaultFlags, false);
224 region_cache[path] = a_region;
230 _session->audition_region(*region_cache[path]);
234 SoundFileBox::stop_btn_clicked ()
237 _session->cancel_audition();
244 SoundFileBox::add_field_clicked ()
246 ArdourPrompter prompter (true);
249 prompter.set_prompt (_("Name for field"));
251 switch (prompter.run ()) {
252 case Gtk::RESPONSE_ACCEPT:
253 prompter.get_result (name);
255 Library->add_field (name);
256 Library->save_changes ();
266 SoundFileBox::remove_field_clicked ()
270 SoundFileBox::audition_status_changed (bool active)
272 ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
280 SoundFileBox::field_selected ()
283 SoundFileBrowser::SoundFileBrowser (std::string title)
286 chooser(Gtk::FILE_CHOOSER_ACTION_OPEN)
288 get_vbox()->pack_start(chooser);
289 chooser.set_preview_widget(preview);
291 chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
295 SoundFileBrowser::set_session (Session* s)
297 preview.set_session(s);
301 SoundFileBrowser::update_preview ()
303 chooser.set_preview_widget_active(preview.setup_labels(chooser.get_filename()));
306 SoundFileChooser::SoundFileChooser (std::string title)
308 SoundFileBrowser(title)
310 add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
311 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
316 SoundFileOmega::SoundFileOmega (std::string title)
318 SoundFileBrowser(title),
319 embed_btn (_("Embed")),
320 import_btn (_("Import")),
321 split_check (_("Split Channels"))
323 get_action_area()->pack_start(embed_btn);
324 get_action_area()->pack_start(import_btn);
325 add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
327 chooser.set_extra_widget(split_check);
329 embed_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::embed_clicked));
330 import_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::import_clicked));
336 SoundFileOmega::embed_clicked ()
338 Embedded (chooser.get_filenames(), split_check.get_active());
342 SoundFileOmega::import_clicked ()
344 Imported (chooser.get_filenames(), split_check.get_active());
348 length2string (const int32_t frames, const int32_t sample_rate)
350 int secs = (int) (frames / (float) sample_rate);
351 int hrs = secs / 3600;
352 secs -= (hrs * 3600);
353 int mins = secs / 60;
356 int total_secs = (hrs * 3600) + (mins * 60) + secs;
357 int frames_remaining = frames - (total_secs * sample_rate);
358 float fractional_secs = (float) frames_remaining / sample_rate;
360 char duration_str[32];
361 sprintf (duration_str, "%02d:%02d:%05.2f", hrs, mins, (float) secs + fractional_secs);