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"
52 #include "audio_time_axis.h"
53 #include "midi_time_axis.h"
54 #include "session_import_dialog.h"
55 #include "gui_thread.h"
56 #include "interthread_progress_window.h"
57 #include "mouse_cursors.h"
58 #include "editor_cursors.h"
63 using namespace ARDOUR;
66 using namespace Gtkmm2ext;
67 using namespace Editing;
70 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
73 Editor::add_external_audio_action (ImportMode mode_hint)
76 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
82 sfbrowser = new SoundFileOmega (_("Add Existing Media"), _session, 0, true, mode_hint);
84 sfbrowser->set_mode (mode_hint);
87 external_audio_dialog ();
91 Editor::external_audio_dialog ()
94 uint32_t audio_track_cnt;
95 uint32_t midi_track_cnt;
98 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
106 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
107 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
110 if (atv->is_audio_track()) {
115 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(*x);
118 if (mtv->is_midi_track()) {
125 if (sfbrowser == 0) {
126 sfbrowser = new SoundFileOmega (_("Add Existing Media"), _session, audio_track_cnt, midi_track_cnt, true);
128 sfbrowser->reset (audio_track_cnt, midi_track_cnt);
131 sfbrowser->show_all ();
135 Editor::session_import_dialog ()
137 SessionImportDialog dialog (_session);
138 ensure_float (dialog);
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, ImportDisposition disposition, ImportMode mode, SrcQuality quality, framepos_t& pos)
267 boost::shared_ptr<Track> track;
268 vector<string> to_import;
270 bool use_timestamp = (pos == -1);
272 current_interthread_info = &import_status;
273 import_status.current = 1;
274 import_status.total = paths.size ();
275 import_status.all_done = false;
277 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
281 if (disposition == Editing::ImportMergeFiles) {
283 /* create 1 region from all paths, add to 1 track,
288 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
289 int check = check_whether_and_how_to_import(*a, false);
300 ok = (import_sndfiles (paths, disposition, mode, quality, pos, 1, 1, track, false) == 0);
305 bool replace = false;
307 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
309 const int check = check_whether_and_how_to_import (*a, true);
316 fatal << "Updating existing sources should be disabled!" << endmsg;
323 fatal << "Illegal return " << check << " from check_whether_and_how_to_import()!" << endmsg;
327 /* have to reset this for every file we handle */
335 switch (disposition) {
336 case Editing::ImportDistinctFiles:
339 to_import.push_back (*a);
341 if (mode == Editing::ImportToTrack) {
342 track = get_nth_selected_audio_track (nth++);
345 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, -1, track, replace) == 0);
348 case Editing::ImportDistinctChannels:
351 to_import.push_back (*a);
353 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, -1, -1, track, replace) == 0);
356 case Editing::ImportSerializeFiles:
359 to_import.push_back (*a);
361 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, 1, track, replace) == 0);
364 case Editing::ImportMergeFiles:
365 // Not entered, handled in earlier if() branch
372 _session->save_state ("");
375 import_status.all_done = true;
379 Editor::do_embed (vector<string> paths, ImportDisposition import_as, ImportMode mode, framepos_t& pos)
381 boost::shared_ptr<Track> track;
382 bool check_sample_rate = true;
384 vector<string> to_embed;
385 bool multi = paths.size() > 1;
387 bool use_timestamp = (pos == -1);
390 case Editing::ImportDistinctFiles:
391 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
393 /* have to reset this for every file we handle */
399 to_embed.push_back (*a);
401 if (mode == Editing::ImportToTrack) {
402 track = get_nth_selected_audio_track (nth++);
405 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, 1, -1, track) < -1) {
411 case Editing::ImportDistinctChannels:
412 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
414 /* have to reset this for every file we handle */
420 to_embed.push_back (*a);
422 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, -1, -1, track) < -1) {
428 case Editing::ImportMergeFiles:
429 if (embed_sndfiles (paths, multi, check_sample_rate, import_as, mode, pos, 1, 1, track) < -1) {
434 case Editing::ImportSerializeFiles:
435 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
437 /* have to reset this for every file we handle */
443 to_embed.push_back (*a);
445 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, 1, 1, track) < -1) {
456 _session->save_state ("");
461 Editor::import_sndfiles (vector<string> paths, ImportDisposition disposition, ImportMode mode, SrcQuality quality, framepos_t& pos,
462 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool replace)
464 import_status.paths = paths;
465 import_status.done = false;
466 import_status.cancel = false;
467 import_status.freeze = false;
468 import_status.quality = quality;
469 import_status.replace_existing_source = replace;
471 import_status.mode = mode;
472 import_status.pos = pos;
473 import_status.target_tracks = target_tracks;
474 import_status.target_regions = target_regions;
475 import_status.track = track;
476 import_status.replace = replace;
478 set_canvas_cursor (_cursors->wait);
481 /* start import thread for this spec. this will ultimately call Session::import_files()
482 which, if successful, will add the files as regions to the region list. its up to us
483 (the GUI) to direct additional steps after that.
486 pthread_create_and_store ("import", &import_status.thread, _import_thread, this);
487 pthread_detach (import_status.thread);
489 while (!import_status.done && !import_status.cancel) {
490 gtk_main_iteration ();
493 import_status.done = true;
497 if (!import_status.cancel && !import_status.sources.empty()) {
498 result = add_sources (
500 import_status.sources,
504 import_status.target_regions,
505 import_status.target_tracks,
509 /* update position from results */
511 pos = import_status.pos;
514 set_canvas_cursor (current_canvas_cursor);
519 Editor::embed_sndfiles (vector<string> paths, bool multifile,
520 bool& check_sample_rate, ImportDisposition disposition, ImportMode mode,
521 framepos_t& pos, int target_regions, int target_tracks,
522 boost::shared_ptr<Track>& track)
524 boost::shared_ptr<AudioFileSource> source;
530 push_canvas_cursor (_cursors->wait);
533 for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
538 /* note that we temporarily truncated _id at the colon */
540 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
541 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
545 if (check_sample_rate && (finfo.samplerate != (int) _session->frame_rate())) {
546 vector<string> choices;
549 choices.push_back (_("Cancel entire import"));
550 choices.push_back (_("Don't embed it"));
551 choices.push_back (_("Embed all without questions"));
553 Gtkmm2ext::Choice rate_choice (
555 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
556 short_path (path, 40)),
560 int resx = rate_choice.run ();
563 case 0: /* stop a multi-file import */
566 case 1: /* don't embed this one */
569 case 2: /* do it, and the rest without asking */
570 check_sample_rate = false;
579 choices.push_back (_("Cancel"));
580 choices.push_back (_("Embed it anyway"));
582 Gtkmm2ext::Choice rate_choice (
584 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
588 int resx = rate_choice.run ();
591 case 0: /* don't import */
603 for (int n = 0; n < finfo.channels; ++n) {
607 /* check if we have this thing embedded already */
609 boost::shared_ptr<Source> s;
611 if ((s = _session->audio_source_by_path_and_channel (path, n)) == 0) {
613 source = boost::dynamic_pointer_cast<AudioFileSource> (
614 SourceFactory::createExternal (DataType::AUDIO, *_session,
616 (mode == ImportAsTapeTrack
617 ? Source::Destructive
621 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
624 sources.push_back(source);
627 catch (failed_constructor& err) {
628 error << string_compose(_("could not open %1"), path) << endmsg;
632 gtk_main_iteration();
636 if (sources.empty()) {
641 ret = add_sources (paths, sources, pos, disposition, mode, target_regions, target_tracks, track, true);
644 pop_canvas_cursor ();
649 Editor::add_sources (vector<string> paths, SourceList& sources, framepos_t& pos, ImportDisposition disposition, ImportMode mode,
650 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool /*add_channel_suffix*/)
652 vector<boost::shared_ptr<Region> > regions;
654 uint32_t input_chan = 0;
655 uint32_t output_chan = 0;
657 vector<string> track_names;
659 use_timestamp = (pos == -1);
661 // kludge (for MIDI we're abusing "channel" for "track" here)
662 if (SMFSource::safe_midi_file_extension (paths.front())) {
666 if (target_regions == 1) {
668 /* take all the sources we have and package them up as a region */
670 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
672 /* we checked in import_sndfiles() that there were not too many */
674 while (RegionFactory::region_by_name (region_name)) {
675 region_name = bump_name_once (region_name, '.');
680 plist.add (ARDOUR::Properties::start, 0);
681 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
682 plist.add (ARDOUR::Properties::name, region_name);
683 plist.add (ARDOUR::Properties::layer, 0);
684 plist.add (ARDOUR::Properties::whole_file, true);
685 plist.add (ARDOUR::Properties::external, true);
687 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
689 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
690 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
693 regions.push_back (r);
695 /* if we're creating a new track, name it after the cleaned-up
696 * and "merged" region name.
699 track_names.push_back (region_name);
701 } else if (target_regions == -1 || target_regions > 1) {
703 /* take each source and create a region for each one */
706 SourceList::iterator x;
709 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
712 just_one.push_back (*x);
714 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (*x);
716 if (sources.size() > 1 && disposition == ImportDistinctChannels) {
718 /* generate a per-channel region name so that things work as
725 region_name = basename_nosuffix (fs->path());
727 region_name = (*x)->name();
730 if (sources.size() == 2) {
736 } else if (sources.size() > 2) {
737 region_name += string_compose ("-%1", n+1);
740 track_names.push_back (region_name);
744 region_name = region_name_from_path (fs->path(), false, false, sources.size(), n);
746 region_name = (*x)->name();
749 track_names.push_back (PBD::basename_nosuffix (paths[n]));
754 /* Fudge region length to ensure it is non-zero; make it 1 beat at 120bpm
755 for want of a better idea. It can't be too small, otherwise if this
756 is a MIDI region the conversion from frames -> beats -> frames will
757 round it back down to 0 again.
759 framecnt_t len = (*x)->length (pos);
761 len = (60.0 / 120.0) * _session->frame_rate ();
764 plist.add (ARDOUR::Properties::start, 0);
765 plist.add (ARDOUR::Properties::length, len);
766 plist.add (ARDOUR::Properties::name, region_name);
767 plist.add (ARDOUR::Properties::layer, 0);
768 plist.add (ARDOUR::Properties::whole_file, true);
769 plist.add (ARDOUR::Properties::external, true);
771 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
773 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
774 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
777 regions.push_back (r);
781 if (target_regions == 1) {
782 input_chan = regions.front()->n_channels();
784 if (target_tracks == 1) {
785 input_chan = regions.size();
791 if (Config->get_output_auto_connect() & AutoConnectMaster) {
792 output_chan = (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan);
794 output_chan = input_chan;
800 begin_reversible_command (Operations::insert_file);
802 /* we only use tracks names when importing to new tracks, but we
803 * require that one is defined for every region, just to keep
806 assert (regions.size() == track_names.size());
808 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
809 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
814 /* get timestamp for this region */
816 const boost::shared_ptr<Source> s (ar->sources().front());
817 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
821 if (as->natural_position() != 0) {
822 pos = as->natural_position();
823 } else if (target_tracks == 1) {
824 /* hmm, no timestamp available, put it after the previous region
827 pos = get_preferred_edit_position ();
832 pos = get_preferred_edit_position ();
835 /* should really get first position in MIDI file, but for now, use edit position*/
836 pos = get_preferred_edit_position ();
840 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track, track_names[n]);
842 rlen = (*r)->length();
844 if (target_tracks != 1) {
847 if (!use_timestamp || !ar) {
848 /* line each one up right after the other */
849 pos += (*r)->length();
854 commit_reversible_command ();
856 /* setup peak file building in another thread */
858 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
859 SourceFactory::setup_peakfile (*x, true);
866 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, framepos_t& pos,
867 ImportMode mode, boost::shared_ptr<Track>& existing_track, const string& new_track_name)
869 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
870 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
874 /* relax, its been done */
879 if (!existing_track) {
882 existing_track = get_nth_selected_audio_track (0);
884 existing_track = get_nth_selected_midi_track (0);
887 if (!existing_track) {
892 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
893 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
894 playlist->clear_changes ();
895 playlist->add_region (copy, pos);
896 if (Config->get_edit_mode() == Ripple)
897 playlist->ripple (pos, copy->length(), copy);
899 _session->add_command (new StatefulDiffCommand (playlist));
905 if (!existing_track) {
907 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
913 existing_track = at.front();
915 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (ChanCount (DataType::MIDI, 1),
916 ChanCount (DataType::MIDI, 1),
917 boost::shared_ptr<PluginInfo>(),
924 existing_track = mt.front();
927 if (!new_track_name.empty()) {
928 existing_track->set_name (new_track_name);
930 existing_track->set_name (region->name());
934 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
935 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
936 playlist->clear_changes ();
937 playlist->add_region (copy, pos);
938 _session->add_command (new StatefulDiffCommand (playlist));
942 case ImportAsTapeTrack:
948 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Destructive));
950 boost::shared_ptr<Playlist> playlist = at.front()->playlist();
951 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
952 playlist->clear_changes ();
953 playlist->add_region (copy, pos);
954 _session->add_command (new StatefulDiffCommand (playlist));
964 Editor::_import_thread (void *arg)
966 SessionEvent::create_per_thread_pool ("import events", 64);
968 Editor *ed = (Editor *) arg;
969 return ed->import_thread ();
973 Editor::import_thread ()
975 _session->import_files (import_status);