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>
29 #include <pbd/pthread_utils.h>
30 #include <pbd/basename.h>
31 #include <pbd/shortpath.h>
33 #include <gtkmm2ext/choice.h>
34 #include <gtkmm2ext/window_title.h>
36 #include <ardour/session.h>
37 #include <ardour/audioplaylist.h>
38 #include <ardour/audioregion.h>
39 #include <ardour/audio_diskstream.h>
40 #include <ardour/utils.h>
41 #include <ardour/audio_track.h>
42 #include <ardour/audioplaylist.h>
43 #include <ardour/audiofilesource.h>
44 #include <ardour/region_factory.h>
45 #include <ardour/source_factory.h>
46 #include <ardour/session.h>
47 #include <pbd/memento_command.h>
49 #include "ardour_ui.h"
53 #include "audio_time_axis.h"
59 using namespace ARDOUR;
63 using namespace Gtkmm2ext;
64 using namespace Editing;
67 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
70 Editor::add_external_audio_action (ImportMode mode_hint)
73 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
79 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, 0, true, mode_hint);
81 sfbrowser->set_mode (mode_hint);
84 external_audio_dialog ();
88 Editor::external_audio_dialog ()
90 vector<Glib::ustring> paths;
94 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
101 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
102 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
106 } else if (atv->is_audio_track()) {
111 if (sfbrowser == 0) {
112 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, track_cnt, true);
114 sfbrowser->reset (track_cnt);
117 sfbrowser->show_all ();
125 int response = sfbrowser->run ();
129 // leave the dialog open
137 // cancel from the browser - we are done
144 paths = sfbrowser->get_paths ();
146 ImportPosition pos = sfbrowser->get_position ();
147 ImportMode mode = sfbrowser->get_mode ();
148 ImportDisposition chns = sfbrowser->get_channel_disposition ();
152 case ImportAtEditPoint:
153 where = get_preferred_edit_position ();
155 case ImportAtTimestamp:
158 case ImportAtPlayhead:
159 where = playhead_cursor->current_frame;
162 where = session->current_start_frame();
166 SrcQuality quality = sfbrowser->get_src_quality();
169 if (sfbrowser->copy_files_btn.get_active()) {
170 do_import (paths, chns, mode, quality, where);
172 do_embed (paths, chns, mode, where);
175 if (response == RESPONSE_APPLY) {
176 sfbrowser->clear_selection ();
180 } while (keepRunning);
183 typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
186 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_audiofiles()
189 * true = show "Update", "Import" and "Skip"
190 * false = show "Import", and "Cancel"
193 * 0 To update an existing source of the same name
194 * 1 To import/embed the file normally (make sure the new name will be unique)
195 * 2 If the user wants to skip this file
198 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
200 string wave_name (Glib::path_get_basename(path));
202 AudioSourceList all_sources = session->get_audio_sources();
203 bool wave_name_exists = false;
205 for (AudioSourceList::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
206 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(i->second);
208 string tmp (Glib::path_get_basename (afs->path()));
210 if (tmp == wave_name) {
211 wave_name_exists = true;
219 if (wave_name_exists) {
221 if (all_or_nothing) {
222 // updating is still disabled
223 //message = string_compose(_("The session already contains a source file named %1. Do you want to update that file (and thus all regions using the file) or import this file as a new file?"),wave_name);
224 message = string_compose(_("The session already contains a source file named %1. This file will be imported as a new file, please confirm."),wave_name);
226 message = string_compose(_("A source file %1 already exists. This operation will not update that source but import the file %2 as a new source, please confirm."), wave_name, wave_name);
229 MessageDialog dialog(message, false,Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
231 if (all_or_nothing) {
233 //dialog.add_button("Update", 0);
234 dialog.add_button("Import", 1);
235 dialog.add_button("Skip", 2);
237 dialog.add_button("Import", 1);
238 dialog.add_button("Cancel", 2);
242 //dialog.add_button("Skip all", 4); // All or rest?
246 function = dialog.run ();
254 boost::shared_ptr<AudioTrack>
255 Editor::get_nth_selected_audio_track (int nth) const
257 AudioTimeAxisView* atv;
258 TrackSelection::iterator x;
260 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
262 atv = dynamic_cast<AudioTimeAxisView*>(*x);
266 } else if (atv->is_audio_track()) {
271 if (x == selection->tracks.end()) {
272 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
274 atv = dynamic_cast<AudioTimeAxisView*>(*x);
277 if (!atv || !atv->is_audio_track()) {
278 return boost::shared_ptr<AudioTrack>();
281 return atv->audio_track();
285 Editor::idle_do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
287 _do_import (paths, chns, mode, quality, pos);
292 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
295 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_import), paths, chns, mode, quality, pos));
297 _do_import (paths, chns, mode, quality, pos);
302 Editor::_do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
304 boost::shared_ptr<AudioTrack> track;
305 vector<ustring> to_import;
309 if (interthread_progress_window == 0) {
310 build_interthread_progress_window ();
313 if (chns == Editing::ImportMergeFiles) {
314 /* create 1 region from all paths, add to 1 track,
318 for (vector<ustring>::iterator a = paths.begin(); a != paths.end() && ok; ++a) {
319 int check = check_whether_and_how_to_import(*a, false);
327 if (import_sndfiles (paths, mode, quality, pos, 1, 1, track, false)) {
335 for (vector<ustring>::iterator a = paths.begin(); a != paths.end() && ok; ++a) {
337 int check = check_whether_and_how_to_import(*a, true);
345 fatal << "Updating existing sources should be disabled!" << endl;
347 } else if (check == 1) {
353 case Editing::ImportDistinctFiles:
356 to_import.push_back (*a);
358 if (mode == Editing::ImportToTrack) {
359 track = get_nth_selected_audio_track (nth++);
362 if (import_sndfiles (to_import, mode, quality, pos, 1, -1, track, replace)) {
368 case Editing::ImportDistinctChannels:
371 to_import.push_back (*a);
373 if (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, replace)) {
379 case Editing::ImportSerializeFiles:
382 to_import.push_back (*a);
384 /* create 1 region from this path, add to 1 track,
385 reuse "track" across paths
388 if (import_sndfiles (to_import, mode, quality, pos, 1, 1, track, replace)) {
394 case Editing::ImportMergeFiles:
401 session->save_state ("");
404 interthread_progress_window->hide_all ();
409 Editor::idle_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
411 _do_embed (paths, chns, mode, pos);
416 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
419 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, chns, mode, pos));
421 _do_embed (paths, chns, mode, pos);
426 Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
428 boost::shared_ptr<AudioTrack> track;
429 bool check_sample_rate = true;
431 vector<ustring> to_embed;
432 bool multi = paths.size() > 1;
436 case Editing::ImportDistinctFiles:
437 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
440 to_embed.push_back (*a);
442 if (mode == Editing::ImportToTrack) {
443 track = get_nth_selected_audio_track (nth++);
446 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
452 case Editing::ImportDistinctChannels:
453 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
456 to_embed.push_back (*a);
458 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
464 case Editing::ImportMergeFiles:
465 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
470 case Editing::ImportSerializeFiles:
471 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
474 to_embed.push_back (*a);
476 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
487 session->save_state ("");
492 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos,
493 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool replace)
495 WindowTitle title = string_compose (_("importing %1"), paths.front());
497 interthread_progress_window->set_title (title.get_string());
498 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
499 interthread_progress_bar.set_fraction (0.0f);
500 interthread_cancel_label.set_text (_("Cancel Import"));
501 current_interthread_info = &import_status;
503 import_status.paths = paths;
504 import_status.done = false;
505 import_status.cancel = false;
506 import_status.freeze = false;
507 import_status.done = 0.0;
508 import_status.quality = quality;
509 import_status.replace_existing_source = replace;
511 interthread_progress_connection = Glib::signal_timeout().connect
512 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 500);
514 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
515 ARDOUR_UI::instance()->flush_pending ();
517 /* start import thread for this spec. this will ultimately call Session::import_audiofile()
518 and if successful will add the file(s) as a region to the session region list.
521 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
522 pthread_detach (import_status.thread);
524 while (!(import_status.done || import_status.cancel)) {
525 gtk_main_iteration ();
528 interthread_progress_window->hide ();
530 import_status.done = true;
531 interthread_progress_connection.disconnect ();
533 /* import thread finished - see if we should build a new track */
535 boost::shared_ptr<AudioRegion> r;
537 if (import_status.cancel || import_status.sources.empty()) {
541 if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track, false) == 0) {
542 session->save_state ("");
546 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
551 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
552 bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
553 boost::shared_ptr<AudioTrack>& track)
555 boost::shared_ptr<AudioFileSource> source;
561 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
562 ARDOUR_UI::instance()->flush_pending ();
564 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
568 /* lets see if we can link it into the session */
570 linked_path = session->sound_dir();
572 linked_path += Glib::path_get_basename (path);
574 if (link (path.c_str(), linked_path.c_str()) == 0) {
576 /* there are many reasons why link(2) might have failed.
577 but if it succeeds, we now have a link in the
578 session sound dir that will protect against
579 unlinking of the original path. nice.
586 /* one possible reason is that its already linked */
588 if (errno == EEXIST) {
591 if (stat (linked_path.c_str(), &sb) == 0) {
592 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
599 /* note that we temporarily truncated _id at the colon */
603 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
604 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
608 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
609 vector<string> choices;
612 choices.push_back (_("Cancel entire import"));
613 choices.push_back (_("Don't embed it"));
614 choices.push_back (_("Embed all without questions"));
616 Gtkmm2ext::Choice rate_choice (
617 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
618 short_path (path, 40)),
621 int resx = rate_choice.run ();
624 case 0: /* stop a multi-file import */
627 case 1: /* don't embed this one */
630 case 2: /* do it, and the rest without asking */
631 check_sample_rate = false;
640 choices.push_back (_("Cancel"));
641 choices.push_back (_("Embed it anyway"));
643 Gtkmm2ext::Choice rate_choice (
644 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
647 int resx = rate_choice.run ();
650 case 0: /* don't import */
662 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
664 for (int n = 0; n < finfo.channels; ++n) {
667 /* check if we have this thing embedded already */
669 boost::shared_ptr<Source> s;
671 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
673 cerr << "add embed/import source with defer_peaks = true\n";
675 source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable
677 (mode == ImportAsTapeTrack ?
678 AudioFileSource::Destructive :
679 AudioFileSource::Flag (0)),
682 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
685 sources.push_back(source);
688 catch (failed_constructor& err) {
689 error << string_compose(_("could not open %1"), path) << endmsg;
693 ARDOUR_UI::instance()->flush_pending ();
697 if (sources.empty()) {
701 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
704 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
709 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode,
710 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool add_channel_suffix)
712 vector<boost::shared_ptr<AudioRegion> > regions;
714 uint32_t input_chan = 0;
715 uint32_t output_chan = 0;
717 if (pos == -1) { // "use timestamp"
718 if (sources[0]->natural_position() != 0) {
719 pos = sources[0]->natural_position();
721 pos = get_preferred_edit_position ();
725 if (target_regions == 1) {
727 /* take all the sources we have and package them up as a region */
729 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
731 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
732 (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
733 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
735 } else if (target_regions == -1) {
737 /* take each source and create a region for each one */
740 SourceList::iterator x;
743 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
746 just_one.push_back (*x);
748 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*x);
750 region_name = region_name_from_path (afs->path(), false, true, sources.size(), n);
752 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
753 (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
754 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
759 if (target_regions == 1) {
760 input_chan = regions.front()->n_channels();
762 if (target_tracks == 1) {
763 input_chan = regions.size();
769 if (Config->get_output_auto_connect() & AutoConnectMaster) {
770 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
772 output_chan = input_chan;
777 for (vector<boost::shared_ptr<AudioRegion> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
779 finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track);
781 if (target_tracks != 1) {
784 pos += (*r)->length();
788 /* setup peak file building in another thread */
790 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
791 SourceFactory::setup_peakfile (*x, true);
798 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos,
799 ImportMode mode, boost::shared_ptr<AudioTrack>& existing_track)
803 /* relax, its been done */
808 if (!existing_track) {
810 existing_track = get_nth_selected_audio_track (0);
812 if (!existing_track) {
817 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
818 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
819 begin_reversible_command (_("insert sndfile"));
820 XMLNode &before = playlist->get_state();
821 playlist->add_region (copy, pos);
822 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
823 commit_reversible_command ();
829 if (!existing_track) {
830 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
836 existing_track = at.front();
837 existing_track->set_name (region->name(), this);
840 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
841 existing_track->diskstream()->playlist()->add_region (copy, pos);
846 case ImportAsTapeTrack:
848 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
850 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
851 at.front()->set_name (basename_nosuffix (copy->name()), this);
852 at.front()->diskstream()->playlist()->add_region (copy, pos);
862 Editor::_import_thread (void *arg)
864 PBD::ThreadCreated (pthread_self(), X_("Import"));
866 Editor *ed = (Editor *) arg;
867 return ed->import_thread ();
871 Editor::import_thread ()
873 session->import_audiofiles (import_status);
874 pthread_exit_pbd (0);
880 Editor::import_progress_timeout (void *arg)
884 if (!interthread_progress_window->is_visible()) {
885 interthread_progress_window->show_all ();
889 interthread_progress_label.set_text (import_status.doing_what);
891 if (import_status.freeze) {
892 interthread_cancel_button.set_sensitive(false);
894 interthread_cancel_button.set_sensitive(true);
897 if (import_status.doing_what == "building peak files") {
898 interthread_progress_bar.pulse ();
901 float val = import_status.progress;
902 interthread_progress_bar.set_fraction (min (max (0.0f, val), 1.0f));
907 /* the window is now visible, speed up the updates */
909 interthread_progress_connection.disconnect ();
910 interthread_progress_connection = Glib::signal_timeout().connect
911 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
914 return !(import_status.done || import_status.cancel);