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>
28 #include <pbd/pthread_utils.h>
29 #include <pbd/basename.h>
30 #include <pbd/shortpath.h>
32 #include <gtkmm2ext/choice.h>
33 #include <gtkmm2ext/window_title.h>
35 #include <ardour/session.h>
36 #include <ardour/audioplaylist.h>
37 #include <ardour/audioregion.h>
38 #include <ardour/audio_diskstream.h>
39 #include <ardour/utils.h>
40 #include <ardour/audio_track.h>
41 #include <ardour/audioplaylist.h>
42 #include <ardour/audiofilesource.h>
43 #include <ardour/region_factory.h>
44 #include <ardour/source_factory.h>
45 #include <pbd/memento_command.h>
47 #include "ardour_ui.h"
51 #include "audio_time_axis.h"
57 using namespace ARDOUR;
61 using namespace Gtkmm2ext;
62 using namespace Editing;
65 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
68 Editor::add_external_audio_action (ImportMode mode)
73 Editor::external_audio_dialog ()
75 vector<Glib::ustring> paths;
78 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
84 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, selection->tracks.size());
86 sfbrowser->reset (selection->tracks.size());
89 sfbrowser->show_all ();
92 int response = sfbrowser->run ();
96 // leave the dialog open
104 // cancel from the browser - we are done
111 paths = sfbrowser->get_paths ();
113 ImportPosition pos = sfbrowser->get_position ();
114 ImportMode mode = sfbrowser->get_mode ();
115 ImportDisposition chns = sfbrowser->get_channel_disposition ();
119 case ImportAtEditCursor:
120 where = edit_cursor->current_frame;
122 case ImportAtTimestamp:
125 case ImportAtPlayhead:
126 where = playhead_cursor->current_frame;
129 where = session->current_start_frame();
133 SrcQuality quality = sfbrowser->get_src_quality();
135 if (sfbrowser->copy_files_btn.get_active()) {
136 do_import (paths, chns, mode, quality, where);
138 do_embed (paths, chns, mode, where);
141 if (response == RESPONSE_APPLY) {
142 sfbrowser->clear_selection ();
147 boost::shared_ptr<AudioTrack>
148 Editor::get_nth_selected_audio_track (int nth) const
150 AudioTimeAxisView* atv;
151 TrackSelection::iterator x;
153 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
154 if (dynamic_cast<AudioTimeAxisView*>(*x)) {
159 if (x == selection->tracks.end()) {
160 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
162 atv = dynamic_cast<AudioTimeAxisView*>(*x);
166 return boost::shared_ptr<AudioTrack>();
169 return atv->audio_track();
173 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
175 boost::shared_ptr<AudioTrack> track;
176 vector<ustring> to_import;
180 if (interthread_progress_window == 0) {
181 build_interthread_progress_window ();
185 case Editing::ImportDistinctFiles:
186 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
189 to_import.push_back (*a);
191 if (mode == Editing::ImportToTrack) {
192 track = get_nth_selected_audio_track (nth++);
195 if (import_sndfiles (to_import, mode, quality, pos, 1, -1, track)) {
202 case Editing::ImportDistinctChannels:
203 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
206 to_import.push_back (*a);
208 if (import_sndfiles (to_import, mode, quality, pos, -1, -1, track)) {
215 case Editing::ImportMergeFiles:
216 /* create 1 region from all paths, add to 1 track,
219 if (import_sndfiles (paths, mode, quality, pos, 1, 1, track)) {
224 case Editing::ImportSerializeFiles:
225 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
228 to_import.push_back (*a);
230 /* create 1 region from this path, add to 1 track,
231 reuse "track" across paths
234 if (import_sndfiles (to_import, mode, quality, pos, 1, 1, track)) {
246 session->save_state ("");
249 interthread_progress_window->hide_all ();
253 Editor::idle_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
255 _do_embed (paths, chns, mode, pos);
260 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
263 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, chns, mode, pos));
265 _do_embed (paths, chns, mode, pos);
270 Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
272 boost::shared_ptr<AudioTrack> track;
273 bool check_sample_rate = true;
275 vector<ustring> to_embed;
276 bool multi = paths.size() > 1;
280 case Editing::ImportDistinctFiles:
281 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
284 to_embed.push_back (*a);
286 if (mode == Editing::ImportToTrack) {
287 track = get_nth_selected_audio_track (nth++);
290 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
296 case Editing::ImportDistinctChannels:
297 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
300 to_embed.push_back (*a);
302 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
308 case Editing::ImportMergeFiles:
309 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
314 case Editing::ImportSerializeFiles:
315 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
318 to_embed.push_back (*a);
320 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
331 session->save_state ("");
336 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos,
337 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track)
339 WindowTitle title = string_compose (_("importing %1"), paths.front());
341 interthread_progress_window->set_title (title.get_string());
342 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
343 interthread_progress_window->show_all ();
344 interthread_progress_bar.set_fraction (0.0f);
345 interthread_cancel_label.set_text (_("Cancel Import"));
346 current_interthread_info = &import_status;
348 import_status.paths = paths;
349 import_status.done = false;
350 import_status.cancel = false;
351 import_status.freeze = false;
352 import_status.done = 0.0;
353 import_status.quality = quality;
355 interthread_progress_connection = Glib::signal_timeout().connect
356 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
358 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
359 ARDOUR_UI::instance()->flush_pending ();
361 /* start import thread for this spec. this will ultimately call Session::import_audiofile()
362 and if successful will add the file(s) as a region to the session region list.
365 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
366 pthread_detach (import_status.thread);
368 while (!(import_status.done || import_status.cancel)) {
369 gtk_main_iteration ();
372 interthread_progress_window->hide ();
374 import_status.done = true;
375 interthread_progress_connection.disconnect ();
377 /* import thread finished - see if we should build a new track */
379 boost::shared_ptr<AudioRegion> r;
381 if (import_status.cancel || import_status.sources.empty()) {
385 if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track, false) == 0) {
386 session->save_state ("");
390 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
395 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
396 bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
397 boost::shared_ptr<AudioTrack>& track)
399 boost::shared_ptr<AudioFileSource> source;
405 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
406 ARDOUR_UI::instance()->flush_pending ();
408 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
412 /* lets see if we can link it into the session */
414 linked_path = session->sound_dir();
416 linked_path += Glib::path_get_basename (path);
418 if (link (path.c_str(), linked_path.c_str()) == 0) {
420 /* there are many reasons why link(2) might have failed.
421 but if it succeeds, we now have a link in the
422 session sound dir that will protect against
423 unlinking of the original path. nice.
430 /* one possible reason is that its already linked */
432 if (errno == EEXIST) {
435 if (stat (linked_path.c_str(), &sb) == 0) {
436 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
443 /* note that we temporarily truncated _id at the colon */
447 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
448 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
452 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
453 vector<string> choices;
456 choices.push_back (_("Cancel entire import"));
457 choices.push_back (_("Don't embed it"));
458 choices.push_back (_("Embed all without questions"));
460 Gtkmm2ext::Choice rate_choice (
461 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
462 short_path (path, 40)),
465 int resx = rate_choice.run ();
468 case 0: /* stop a multi-file import */
471 case 1: /* don't embed this one */
474 case 2: /* do it, and the rest without asking */
475 check_sample_rate = false;
484 choices.push_back (_("Cancel"));
485 choices.push_back (_("Embed it anyway"));
487 Gtkmm2ext::Choice rate_choice (
488 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
491 int resx = rate_choice.run ();
494 case 0: /* don't import */
506 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
508 for (int n = 0; n < finfo.channels; ++n) {
511 /* check if we have this thing embedded already */
513 boost::shared_ptr<Source> s;
515 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
516 source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable
518 (mode == ImportAsTapeTrack ?
519 AudioFileSource::Destructive :
520 AudioFileSource::Flag (0)),
523 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
526 sources.push_back(source);
529 catch (failed_constructor& err) {
530 error << string_compose(_("could not open %1"), path) << endmsg;
534 ARDOUR_UI::instance()->flush_pending ();
538 if (sources.empty()) {
542 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
545 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
550 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode,
551 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool add_channel_suffix)
553 vector<boost::shared_ptr<AudioRegion> > regions;
555 uint32_t input_chan = 0;
556 uint32_t output_chan = 0;
558 if (pos == -1) { // "use timestamp"
559 if (sources[0]->natural_position() != 0) {
560 pos = sources[0]->natural_position();
562 // XXX is this the best alternative ?
563 pos = edit_cursor->current_frame;
567 if (target_regions == 1) {
569 /* take all the sources we have and package them up as a region */
571 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
573 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
574 (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
575 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
577 } else if (target_regions == -1) {
579 /* take each source and create a region for each one */
582 SourceList::iterator x;
585 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
588 just_one.push_back (*x);
590 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*x);
592 region_name = region_name_from_path (afs->path(), false, true, sources.size(), n);
594 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
595 (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
596 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
601 if (target_regions == 1) {
602 input_chan = regions.front()->n_channels();
604 if (target_tracks == 1) {
605 input_chan = regions.size();
611 if (Config->get_output_auto_connect() & AutoConnectMaster) {
612 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
614 output_chan = input_chan;
619 for (vector<boost::shared_ptr<AudioRegion> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
621 finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track);
623 if (target_tracks != 1) {
626 pos += (*r)->length();
630 /* setup peak file building in another thread */
632 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
633 SourceFactory::setup_peakfile (*x, true);
640 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos,
641 ImportMode mode, boost::shared_ptr<AudioTrack>& existing_track)
645 /* relax, its been done */
650 if (!existing_track) {
652 if (selection->tracks.empty()) {
656 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
662 existing_track = atv->audio_track();
665 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
666 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
667 begin_reversible_command (_("insert sndfile"));
668 XMLNode &before = playlist->get_state();
669 playlist->add_region (copy, pos);
670 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
671 commit_reversible_command ();
677 if (!existing_track) {
678 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
684 existing_track = at.front();
685 existing_track->set_name (region->name(), this);
688 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
689 existing_track->diskstream()->playlist()->add_region (copy, pos);
694 case ImportAsTapeTrack:
696 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
698 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
699 at.front()->set_name (basename_nosuffix (copy->name()), this);
700 at.front()->diskstream()->playlist()->add_region (copy, pos);
710 Editor::_import_thread (void *arg)
712 PBD::ThreadCreated (pthread_self(), X_("Import"));
714 Editor *ed = (Editor *) arg;
715 return ed->import_thread ();
719 Editor::import_thread ()
721 session->import_audiofile (import_status);
722 pthread_exit_pbd (0);
728 Editor::import_progress_timeout (void *arg)
730 interthread_progress_label.set_text (import_status.doing_what);
732 if (import_status.freeze) {
733 interthread_cancel_button.set_sensitive(false);
735 interthread_cancel_button.set_sensitive(true);
738 if (import_status.doing_what == "building peak files") {
739 interthread_progress_bar.pulse ();
742 interthread_progress_bar.set_fraction (import_status.progress);
745 return !(import_status.done || import_status.cancel);