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"
32 #include "pbd/stateful_diff_command.h"
34 #include <gtkmm2ext/choice.h>
36 #include "ardour/audio_track.h"
37 #include "ardour/audiofilesource.h"
38 #include "ardour/audioregion.h"
39 #include "ardour/midi_region.h"
40 #include "ardour/midi_track.h"
41 #include "ardour/operations.h"
42 #include "ardour/region_factory.h"
43 #include "ardour/smf_source.h"
44 #include "ardour/source_factory.h"
45 #include "ardour/utils.h"
46 #include "pbd/memento_command.h"
48 #include "ardour_ui.h"
49 #include "cursor_context.h"
53 #include "audio_time_axis.h"
54 #include "midi_time_axis.h"
55 #include "session_import_dialog.h"
56 #include "gui_thread.h"
57 #include "interthread_progress_window.h"
58 #include "mouse_cursors.h"
59 #include "editor_cursors.h"
64 using namespace ARDOUR;
67 using namespace Gtkmm2ext;
68 using namespace Editing;
71 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
74 Editor::add_external_audio_action (ImportMode mode_hint)
77 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
83 sfbrowser = new SoundFileOmega (_("Add Existing Media"), _session, 0, 0, true, mode_hint);
85 sfbrowser->set_mode (mode_hint);
88 external_audio_dialog ();
92 Editor::external_audio_dialog ()
95 uint32_t audio_track_cnt;
96 uint32_t midi_track_cnt;
99 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
107 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
108 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
111 if (atv->is_audio_track()) {
116 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(*x);
119 if (mtv->is_midi_track()) {
126 if (sfbrowser == 0) {
127 sfbrowser = new SoundFileOmega (_("Add Existing Media"), _session, audio_track_cnt, midi_track_cnt, true);
129 sfbrowser->reset (audio_track_cnt, midi_track_cnt);
132 sfbrowser->show_all ();
136 Editor::session_import_dialog ()
138 SessionImportDialog dialog (_session);
142 typedef std::map<PBD::ID,boost::shared_ptr<ARDOUR::Source> > SourceMap;
145 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_files()
148 * true = show "Update", "Import" and "Skip"
149 * false = show "Import", and "Cancel"
152 * 0 To update an existing source of the same name
153 * 1 To import/embed the file normally (make sure the new name will be unique)
154 * 2 If the user wants to skip this file
157 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
159 string wave_name (Glib::path_get_basename(path));
161 bool already_exists = false;
164 if ((existing = _session->count_sources_by_origin (path)) > 0) {
165 already_exists = true;
170 if (already_exists) {
172 if (all_or_nothing) {
173 // updating is still disabled
174 //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);
175 message = string_compose (_("The session already contains a source file named %1. Do you want to import %1 as a new file, or skip it?"), wave_name);
177 message = string_compose (_("The session already contains a source file named %1. Do you want to import %2 as a new source, or skip it?"), wave_name, wave_name);
180 MessageDialog dialog(message, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
182 if (all_or_nothing) {
184 //dialog.add_button("Update", 0);
185 dialog.add_button("Import", 1);
186 dialog.add_button("Skip", 2);
188 dialog.add_button("Import", 1);
189 dialog.add_button("Cancel", 2);
192 //dialog.add_button("Skip all", 4); // All or rest?
196 function = dialog.run ();
204 boost::shared_ptr<AudioTrack>
205 Editor::get_nth_selected_audio_track (int nth) const
207 AudioTimeAxisView* atv;
208 TrackSelection::iterator x;
210 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
212 atv = dynamic_cast<AudioTimeAxisView*>(*x);
216 } else if (atv->is_audio_track()) {
221 if (x == selection->tracks.end()) {
222 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
224 atv = dynamic_cast<AudioTimeAxisView*>(*x);
227 if (!atv || !atv->is_audio_track()) {
228 return boost::shared_ptr<AudioTrack>();
231 return atv->audio_track();
234 boost::shared_ptr<MidiTrack>
235 Editor::get_nth_selected_midi_track (int nth) const
237 MidiTimeAxisView* mtv;
238 TrackSelection::iterator x;
240 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
242 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
246 } else if (mtv->is_midi_track()) {
251 if (x == selection->tracks.end()) {
252 mtv = dynamic_cast<MidiTimeAxisView*>(selection->tracks.back());
254 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
257 if (!mtv || !mtv->is_midi_track()) {
258 return boost::shared_ptr<MidiTrack>();
261 return mtv->midi_track();
265 Editor::do_import (vector<string> paths,
266 ImportDisposition disposition,
270 ARDOUR::PluginInfoPtr instrument)
272 boost::shared_ptr<Track> track;
273 vector<string> to_import;
275 bool use_timestamp = (pos == -1);
277 current_interthread_info = &import_status;
278 import_status.current = 1;
279 import_status.total = paths.size ();
280 import_status.all_done = false;
282 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
286 if (disposition == Editing::ImportMergeFiles) {
288 /* create 1 region from all paths, add to 1 track,
293 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
294 int check = check_whether_and_how_to_import(*a, false);
305 ok = (import_sndfiles (paths, disposition, mode, quality, pos, 1, 1, track, false, instrument) == 0);
306 import_status.sources.clear();
311 bool replace = false;
313 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
315 const int check = check_whether_and_how_to_import (*a, true);
322 fatal << "Updating existing sources should be disabled!" << endmsg;
323 abort(); /* NOTREACHED*/
329 fatal << "Illegal return " << check << " from check_whether_and_how_to_import()!" << endmsg;
330 abort(); /* NOTREACHED*/
333 /* have to reset this for every file we handle */
341 switch (disposition) {
342 case Editing::ImportDistinctFiles:
345 to_import.push_back (*a);
347 if (mode == Editing::ImportToTrack) {
348 track = get_nth_selected_audio_track (nth++);
351 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, -1, track, replace, instrument) == 0);
352 import_status.sources.clear();
355 case Editing::ImportDistinctChannels:
358 to_import.push_back (*a);
360 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, -1, -1, track, replace, instrument) == 0);
361 import_status.sources.clear();
364 case Editing::ImportSerializeFiles:
367 to_import.push_back (*a);
369 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, 1, track, replace, instrument) == 0);
370 import_status.sources.clear();
373 case Editing::ImportMergeFiles:
374 // Not entered, handled in earlier if() branch
381 _session->save_state ("");
384 import_status.all_done = true;
388 Editor::do_embed (vector<string> paths, ImportDisposition import_as, ImportMode mode, framepos_t& pos, ARDOUR::PluginInfoPtr instrument)
390 boost::shared_ptr<Track> track;
391 bool check_sample_rate = true;
393 vector<string> to_embed;
394 bool multi = paths.size() > 1;
396 bool use_timestamp = (pos == -1);
399 case Editing::ImportDistinctFiles:
400 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
402 /* have to reset this for every file we handle */
408 to_embed.push_back (*a);
410 if (mode == Editing::ImportToTrack) {
411 track = get_nth_selected_audio_track (nth++);
414 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, 1, -1, track, instrument) < -1) {
420 case Editing::ImportDistinctChannels:
421 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
423 /* have to reset this for every file we handle */
429 to_embed.push_back (*a);
431 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, -1, -1, track, instrument) < -1) {
437 case Editing::ImportMergeFiles:
438 if (embed_sndfiles (paths, multi, check_sample_rate, import_as, mode, pos, 1, 1, track, instrument) < -1) {
443 case Editing::ImportSerializeFiles:
444 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
446 /* have to reset this for every file we handle */
452 to_embed.push_back (*a);
454 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, 1, 1, track, instrument) < -1) {
465 _session->save_state ("");
470 Editor::import_sndfiles (vector<string> paths,
471 ImportDisposition disposition,
477 boost::shared_ptr<Track>& track,
479 ARDOUR::PluginInfoPtr instrument)
481 import_status.paths = paths;
482 import_status.done = false;
483 import_status.cancel = false;
484 import_status.freeze = false;
485 import_status.quality = quality;
486 import_status.replace_existing_source = replace;
487 import_status.split_midi_channels = (disposition == Editing::ImportDistinctChannels);
489 import_status.mode = mode;
490 import_status.pos = pos;
491 import_status.target_tracks = target_tracks;
492 import_status.target_regions = target_regions;
493 import_status.track = track;
494 import_status.replace = replace;
496 CursorContext::Handle cursor_ctx = CursorContext::create(*this, _cursors->wait);
499 /* start import thread for this spec. this will ultimately call Session::import_files()
500 which, if successful, will add the files as regions to the region list. its up to us
501 (the GUI) to direct additional steps after that.
504 pthread_create_and_store ("import", &import_status.thread, _import_thread, this);
505 pthread_detach (import_status.thread);
507 while (!import_status.done && !import_status.cancel) {
508 gtk_main_iteration ();
511 // wait for thread to terminate
512 while (!import_status.done) {
513 gtk_main_iteration ();
518 if (!import_status.cancel && !import_status.sources.empty()) {
519 result = add_sources (
521 import_status.sources,
525 import_status.target_regions,
526 import_status.target_tracks,
527 track, false, instrument
530 /* update position from results */
532 pos = import_status.pos;
539 Editor::embed_sndfiles (vector<string> paths,
541 bool& check_sample_rate,
542 ImportDisposition disposition,
547 boost::shared_ptr<Track>& track,
548 ARDOUR::PluginInfoPtr instrument)
550 boost::shared_ptr<AudioFileSource> source;
555 CursorContext::Handle cursor_ctx = CursorContext::create(*this, _cursors->wait);
558 for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
563 /* note that we temporarily truncated _id at the colon */
565 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
566 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
570 if (check_sample_rate && (finfo.samplerate != (int) _session->frame_rate())) {
571 vector<string> choices;
574 choices.push_back (_("Cancel entire import"));
575 choices.push_back (_("Don't embed it"));
576 choices.push_back (_("Embed all without questions"));
578 Gtkmm2ext::Choice rate_choice (
580 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
581 short_path (path, 40)),
585 int resx = rate_choice.run ();
588 case 0: /* stop a multi-file import */
590 case 1: /* don't embed this one */
592 case 2: /* do it, and the rest without asking */
593 check_sample_rate = false;
601 choices.push_back (_("Cancel"));
602 choices.push_back (_("Embed it anyway"));
604 Gtkmm2ext::Choice rate_choice (
606 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
610 int resx = rate_choice.run ();
613 case 0: /* don't import */
623 for (int n = 0; n < finfo.channels; ++n) {
627 /* check if we have this thing embedded already */
629 boost::shared_ptr<Source> s;
631 if ((s = _session->audio_source_by_path_and_channel (path, n)) == 0) {
633 source = boost::dynamic_pointer_cast<AudioFileSource> (
634 SourceFactory::createExternal (DataType::AUDIO, *_session,
636 (mode == ImportAsTapeTrack
637 ? Source::Destructive
641 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
644 sources.push_back(source);
647 catch (failed_constructor& err) {
648 error << string_compose(_("could not open %1"), path) << endmsg;
652 gtk_main_iteration();
656 if (!sources.empty()) {
657 return add_sources (paths, sources, pos, disposition, mode, target_regions, target_tracks, track, true, instrument);
664 Editor::add_sources (vector<string> paths,
667 ImportDisposition disposition,
671 boost::shared_ptr<Track>& track,
672 bool /*add_channel_suffix*/,
673 ARDOUR::PluginInfoPtr instrument)
675 vector<boost::shared_ptr<Region> > regions;
677 uint32_t input_chan = 0;
678 uint32_t output_chan = 0;
680 vector<string> track_names;
682 use_timestamp = (pos == -1);
684 // kludge (for MIDI we're abusing "channel" for "track" here)
685 if (SMFSource::safe_midi_file_extension (paths.front())) {
689 if (target_regions == 1) {
691 /* take all the sources we have and package them up as a region */
693 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
695 /* we checked in import_sndfiles() that there were not too many */
697 while (RegionFactory::region_by_name (region_name)) {
698 region_name = bump_name_once (region_name, '.');
703 plist.add (ARDOUR::Properties::start, 0);
704 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
705 plist.add (ARDOUR::Properties::name, region_name);
706 plist.add (ARDOUR::Properties::layer, 0);
707 plist.add (ARDOUR::Properties::whole_file, true);
708 plist.add (ARDOUR::Properties::external, true);
710 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
712 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
713 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
716 regions.push_back (r);
718 /* if we're creating a new track, name it after the cleaned-up
719 * and "merged" region name.
722 track_names.push_back (region_name);
724 } else if (target_regions == -1 || target_regions > 1) {
726 /* take each source and create a region for each one */
729 SourceList::iterator x;
732 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
735 just_one.push_back (*x);
737 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (*x);
739 if (sources.size() > 1 && disposition == ImportDistinctChannels) {
741 /* generate a per-channel region name so that things work as
748 region_name = basename_nosuffix (fs->path());
750 region_name = (*x)->name();
753 if (sources.size() == 2) {
759 } else if (sources.size() > 2) {
760 region_name += string_compose ("-%1", n+1);
763 track_names.push_back (region_name);
767 region_name = region_name_from_path (fs->path(), false, false, sources.size(), n);
769 region_name = (*x)->name();
772 if (SMFSource::safe_midi_file_extension (paths.front())) {
773 string track_name = string_compose ("%1-t%2", PBD::basename_nosuffix (fs->path()), (n + 1));
774 track_names.push_back (track_name);
776 track_names.push_back (PBD::basename_nosuffix (paths[n]));
782 /* Fudge region length to ensure it is non-zero; make it 1 beat at 120bpm
783 for want of a better idea. It can't be too small, otherwise if this
784 is a MIDI region the conversion from frames -> beats -> frames will
785 round it back down to 0 again.
787 framecnt_t len = (*x)->length (pos);
789 len = (60.0 / 120.0) * _session->frame_rate ();
792 plist.add (ARDOUR::Properties::start, 0);
793 plist.add (ARDOUR::Properties::length, len);
794 plist.add (ARDOUR::Properties::name, region_name);
795 plist.add (ARDOUR::Properties::layer, 0);
796 plist.add (ARDOUR::Properties::whole_file, true);
797 plist.add (ARDOUR::Properties::external, true);
799 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
801 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
802 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
805 regions.push_back (r);
809 if (target_regions == 1) {
810 input_chan = regions.front()->n_channels();
812 if (target_tracks == 1) {
813 input_chan = regions.size();
819 if (Config->get_output_auto_connect() & AutoConnectMaster) {
820 output_chan = (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan);
822 output_chan = input_chan;
828 begin_reversible_command (Operations::insert_file);
830 /* we only use tracks names when importing to new tracks, but we
831 * require that one is defined for every region, just to keep
834 assert (regions.size() == track_names.size());
836 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
837 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
842 /* get timestamp for this region */
844 const boost::shared_ptr<Source> s (ar->sources().front());
845 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
849 if (as->natural_position() != 0) {
850 pos = as->natural_position();
851 } else if (target_tracks == 1) {
852 /* hmm, no timestamp available, put it after the previous region
855 pos = get_preferred_edit_position ();
860 pos = get_preferred_edit_position ();
863 /* should really get first position in MIDI file, but for now, use edit position*/
864 pos = get_preferred_edit_position ();
868 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track, track_names[n], instrument);
870 rlen = (*r)->length();
872 if (target_tracks != 1) {
875 if (!use_timestamp || !ar) {
876 /* line each one up right after the other */
877 pos += (*r)->length();
882 commit_reversible_command ();
884 /* setup peak file building in another thread */
886 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
887 SourceFactory::setup_peakfile (*x, true);
894 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region,
899 boost::shared_ptr<Track>& existing_track,
900 const string& new_track_name,
901 ARDOUR::PluginInfoPtr instrument)
903 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
904 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
908 /* relax, its been done */
913 if (!existing_track) {
916 existing_track = get_nth_selected_audio_track (0);
918 existing_track = get_nth_selected_midi_track (0);
921 if (!existing_track) {
926 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
927 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
928 playlist->clear_changes ();
929 playlist->add_region (copy, pos);
930 if (Config->get_edit_mode() == Ripple)
931 playlist->ripple (pos, copy->length(), copy);
933 _session->add_command (new StatefulDiffCommand (playlist));
939 if (!existing_track) {
941 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, 0, 1, string(), PresentationInfo::max_order, Normal));
946 if (Config->get_strict_io ()) {
947 for (list<boost::shared_ptr<AudioTrack> >::iterator i = at.begin(); i != at.end(); ++i) {
948 (*i)->set_strict_io (true);
952 existing_track = at.front();
954 list<boost::shared_ptr<MidiTrack> > mt (
955 _session->new_midi_track (ChanCount (DataType::MIDI, 1),
956 ChanCount (DataType::MIDI, 1),
957 instrument, (Plugin::PresetRecord*) 0,
961 PresentationInfo::max_order));
966 if (Config->get_strict_io ()) {
967 for (list<boost::shared_ptr<MidiTrack> >::iterator i = mt.begin(); i != mt.end(); ++i) {
968 (*i)->set_strict_io (true);
972 // TODO set strict_io from preferences
973 existing_track = mt.front();
976 if (!new_track_name.empty()) {
977 existing_track->set_name (new_track_name);
979 existing_track->set_name (region->name());
983 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
984 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
985 playlist->clear_changes ();
986 playlist->add_region (copy, pos);
987 _session->add_command (new StatefulDiffCommand (playlist));
991 case ImportAsTapeTrack:
997 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, 0, 1, string(), PresentationInfo::max_order, Destructive));
999 boost::shared_ptr<Playlist> playlist = at.front()->playlist();
1000 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
1001 playlist->clear_changes ();
1002 playlist->add_region (copy, pos);
1003 _session->add_command (new StatefulDiffCommand (playlist));
1005 if (Config->get_strict_io ()) {
1006 for (list<boost::shared_ptr<AudioTrack> >::iterator i = at.begin(); i != at.end(); ++i) {
1007 (*i)->set_strict_io (true);
1018 Editor::_import_thread (void *arg)
1020 SessionEvent::create_per_thread_pool ("import events", 64);
1022 Editor *ed = (Editor *) arg;
1023 return ed->import_thread ();
1027 Editor::import_thread ()
1029 _session->import_files (import_status);