2 Copyright (C) 2000-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.
20 #include <sys/types.h>
27 #include <pbd/pthread_utils.h>
28 #include <pbd/basename.h>
29 #include <pbd/shortpath.h>
31 #include <gtkmm2ext/choice.h>
32 #include <gtkmm2ext/window_title.h>
34 #include <ardour/session.h>
35 #include <ardour/audioplaylist.h>
36 #include <ardour/audioregion.h>
37 #include <ardour/audio_diskstream.h>
38 #include <ardour/utils.h>
39 #include <ardour/audio_track.h>
40 #include <ardour/audioplaylist.h>
41 #include <ardour/audiofilesource.h>
42 #include <ardour/region_factory.h>
43 #include <ardour/source_factory.h>
44 #include <pbd/memento_command.h>
46 #include "ardour_ui.h"
50 #include "audio_time_axis.h"
56 using namespace ARDOUR;
60 using namespace Gtkmm2ext;
61 using namespace Editing;
64 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
67 Editor::add_external_audio_action (ImportMode mode)
69 nframes_t& pos = edit_cursor->current_frame;
70 AudioTrack* track = 0;
72 if (!selection->tracks.empty()) {
73 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
75 track = atv->audio_track();
79 bring_in_external_audio (mode, track, pos);
84 Editor::external_audio_dialog ()
86 vector<Glib::ustring> paths;
89 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
94 SoundFileBrowser browser (*this, _("Add existing audio"), session, selection->tracks.size());
98 int response = browser.run ();
104 // cancel from the browser - we are done
112 AudioTrack* track = 0;
114 if (!selection->tracks.empty()) {
115 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
117 track = atv->audio_track();
120 paths = browser.get_paths ();
122 if (browser.import.get_active()) {
123 do_import (paths, browser.split_files.get_active(), browser.get_mode(), track, edit_cursor->current_frame);
125 do_embed (paths, browser.split_files.get_active(), browser.get_mode(), track, edit_cursor->current_frame);
130 Editor::bring_in_external_audio (ImportMode mode, AudioTrack* track, nframes_t& pos)
135 Editor::do_import (vector<ustring> paths, bool split, ImportMode mode, AudioTrack* track, nframes_t& pos)
137 /* SFDB sets "multichan" to true to indicate "split channels"
138 so reverse the setting to match the way libardour
142 import_status.multichan = !split;
144 if (interthread_progress_window == 0) {
145 build_interthread_progress_window ();
148 vector<ustring> to_import;
150 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
153 to_import.push_back (*a);
155 import_sndfile (to_import, mode, track, pos);
158 interthread_progress_window->hide_all ();
162 Editor::idle_do_embed (vector<ustring> paths, bool split, ImportMode mode, AudioTrack* track, nframes_t& pos)
164 _do_embed (paths, split, mode, track, pos);
169 Editor::do_embed (vector<ustring> paths, bool split, ImportMode mode, AudioTrack* track, nframes_t& pos)
172 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, split, mode, track, pos));
174 _do_embed (paths, split, mode, track, pos);
179 Editor::_do_embed (vector<ustring> paths, bool split, ImportMode mode, AudioTrack* track, nframes_t& pos)
181 bool multiple_files = paths.size() > 1;
182 bool check_sample_rate = true;
183 vector<ustring>::iterator a;
185 for (a = paths.begin(); a != paths.end(); ) {
187 Glib::ustring path = *a;
188 Glib::ustring pair_base;
189 vector<ustring> to_embed;
191 to_embed.push_back (path);
194 if (path_is_paired (path, pair_base)) {
196 ustring::size_type len = pair_base.length();
198 for (vector<Glib::ustring>::iterator b = paths.begin(); b != paths.end(); ) {
200 if (((*b).substr (0, len) == pair_base) && ((*b).length() == path.length())) {
202 to_embed.push_back (*b);
204 /* don't process this one again */
215 if (to_embed.size() > 1) {
217 vector<string> choices;
219 choices.push_back (string_compose (_("Import as a %1 region"),
220 to_embed.size() > 2 ? _("multichannel") : _("stereo")));
221 choices.push_back (_("Import as multiple regions"));
223 Choice chooser (string_compose (_("Paired files detected (%1, %2 ...).\nDo you want to:"),
228 if (chooser.run () == 0) {
230 /* keep them paired */
232 if (embed_sndfile (to_embed, split, multiple_files, check_sample_rate, mode, track, pos) < -1) {
238 /* one thing per file */
242 for (vector<ustring>::iterator x = to_embed.begin(); x != to_embed.end(); ++x) {
247 if (embed_sndfile (foo, split, multiple_files, check_sample_rate, mode, track, pos) < -1) {
255 if (embed_sndfile (to_embed, split, multiple_files, check_sample_rate, mode, track, pos) < -1) {
261 if (a == paths.end()) {
262 session->save_state ("");
267 Editor::import_sndfile (vector<ustring> paths, ImportMode mode, AudioTrack* track, nframes_t& pos)
269 WindowTitle title = string_compose (_("importing %1"), paths.front());
271 interthread_progress_window->set_title (title.get_string());
272 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
273 interthread_progress_window->show_all ();
274 interthread_progress_bar.set_fraction (0.0f);
275 interthread_cancel_label.set_text (_("Cancel Import"));
276 current_interthread_info = &import_status;
278 import_status.paths = paths;
279 import_status.done = false;
280 import_status.cancel = false;
281 import_status.freeze = false;
282 import_status.done = 0.0;
284 interthread_progress_connection = Glib::signal_timeout().connect
285 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
287 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
288 ARDOUR_UI::instance()->flush_pending ();
290 /* start import thread for this spec. this will ultimately call Session::import_audiofile()
291 and if successful will add the file(s) as a region to the session region list.
294 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
295 pthread_detach (import_status.thread);
297 while (!(import_status.done || import_status.cancel)) {
298 gtk_main_iteration ();
301 interthread_progress_window->hide ();
303 import_status.done = true;
304 interthread_progress_connection.disconnect ();
306 /* import thread finished - see if we should build a new track */
308 if (!import_status.new_regions.empty()) {
309 boost::shared_ptr<AudioRegion> region (import_status.new_regions.front());
310 finish_bringing_in_audio (region, region->n_channels(), region->n_channels(), track, pos, mode);
313 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
318 Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_files, bool& check_sample_rate, ImportMode mode,
319 AudioTrack* track, nframes_t& pos)
321 boost::shared_ptr<AudioFileSource> source;
323 boost::shared_ptr<AudioRegion> region;
327 uint32_t input_chan = 0;
328 uint32_t output_chan = 0;
331 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
332 ARDOUR_UI::instance()->flush_pending ();
334 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
338 /* lets see if we can link it into the session */
340 linked_path = session->sound_dir();
342 linked_path += Glib::path_get_basename (path);
344 if (link (path.c_str(), linked_path.c_str()) == 0) {
346 /* there are many reasons why link(2) might have failed.
347 but if it succeeds, we now have a link in the
348 session sound dir that will protect against
349 unlinking of the original path. nice.
356 /* one possible reason is that its already linked */
358 if (errno == EEXIST) {
361 if (stat (linked_path.c_str(), &sb) == 0) {
362 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
369 /* note that we temporarily truncated _id at the colon */
373 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
374 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
378 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
379 vector<string> choices;
381 if (multiple_files) {
382 choices.push_back (_("Cancel entire import"));
383 choices.push_back (_("Don't embed it"));
384 choices.push_back (_("Embed all without questions"));
386 Gtkmm2ext::Choice rate_choice (
387 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
388 short_path (path, 40)),
391 int resx = rate_choice.run ();
394 case 0: /* stop a multi-file import */
397 case 1: /* don't embed this one */
400 case 2: /* do it, and the rest without asking */
401 check_sample_rate = false;
410 choices.push_back (_("Cancel"));
411 choices.push_back (_("Embed it anyway"));
413 Gtkmm2ext::Choice rate_choice (
414 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
417 int resx = rate_choice.run ();
420 case 0: /* don't import */
432 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
433 // ARDOUR_UI::instance()->flush_pending ();
435 /* make the proper number of channels in the region */
437 input_chan += finfo.channels;
439 for (int n = 0; n < finfo.channels; ++n)
443 /* check if we have this thing embedded already */
445 boost::shared_ptr<Source> s;
447 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
448 source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable
450 (mode == ImportAsTapeTrack ?
451 AudioFileSource::Destructive :
452 AudioFileSource::Flag (0))));
454 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
457 sources.push_back(source);
460 catch (failed_constructor& err) {
461 error << string_compose(_("could not open %1"), path) << endmsg;
465 ARDOUR_UI::instance()->flush_pending ();
469 if (sources.empty()) {
473 if (sources[0]->natural_position() != 0) {
474 pos = sources[0]->natural_position();
477 region_name = region_name_from_path (paths.front(), (sources.size() > 1));
479 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
480 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External)));
482 if (Config->get_output_auto_connect() & AutoConnectMaster) {
483 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
485 output_chan = input_chan;
488 finish_bringing_in_audio (region, input_chan, output_chan, track, pos, mode);
491 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
496 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, AudioTrack* track, nframes_t& pos, ImportMode mode)
500 /* relax, its been done */
505 boost::shared_ptr<Playlist> playlist = track->diskstream()->playlist();
507 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
508 begin_reversible_command (_("insert sndfile"));
509 XMLNode &before = playlist->get_state();
510 playlist->add_region (copy, pos);
511 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
512 commit_reversible_command ();
514 pos += region->length();
520 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
522 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
523 at.front()->diskstream()->playlist()->add_region (copy, pos);
528 case ImportAsTapeTrack:
530 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
532 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
533 at.front()->diskstream()->playlist()->add_region (copy, pos);
543 Editor::_import_thread (void *arg)
545 PBD::ThreadCreated (pthread_self(), X_("Import"));
547 Editor *ed = (Editor *) arg;
548 return ed->import_thread ();
552 Editor::import_thread ()
554 session->import_audiofile (import_status);
555 pthread_exit_pbd (0);
561 Editor::import_progress_timeout (void *arg)
563 interthread_progress_label.set_text (import_status.doing_what);
565 if (import_status.freeze) {
566 interthread_cancel_button.set_sensitive(false);
568 interthread_cancel_button.set_sensitive(true);
571 if (import_status.doing_what == "building peak files") {
572 interthread_progress_bar.pulse ();
575 interthread_progress_bar.set_fraction (import_status.progress);
578 return !(import_status.done || import_status.cancel);