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"
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, 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);
139 ensure_float (dialog);
143 typedef std::map<PBD::ID,boost::shared_ptr<ARDOUR::Source> > SourceMap;
146 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_files()
149 * true = show "Update", "Import" and "Skip"
150 * false = show "Import", and "Cancel"
153 * 0 To update an existing source of the same name
154 * 1 To import/embed the file normally (make sure the new name will be unique)
155 * 2 If the user wants to skip this file
158 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
160 string wave_name (Glib::path_get_basename(path));
162 bool already_exists = false;
165 if ((existing = _session->count_sources_by_origin (path)) > 0) {
166 already_exists = true;
171 if (already_exists) {
173 if (all_or_nothing) {
174 // updating is still disabled
175 //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);
176 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);
178 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);
181 MessageDialog dialog(message, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
183 if (all_or_nothing) {
185 //dialog.add_button("Update", 0);
186 dialog.add_button("Import", 1);
187 dialog.add_button("Skip", 2);
189 dialog.add_button("Import", 1);
190 dialog.add_button("Cancel", 2);
193 //dialog.add_button("Skip all", 4); // All or rest?
197 function = dialog.run ();
205 boost::shared_ptr<AudioTrack>
206 Editor::get_nth_selected_audio_track (int nth) const
208 AudioTimeAxisView* atv;
209 TrackSelection::iterator x;
211 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
213 atv = dynamic_cast<AudioTimeAxisView*>(*x);
217 } else if (atv->is_audio_track()) {
222 if (x == selection->tracks.end()) {
223 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
225 atv = dynamic_cast<AudioTimeAxisView*>(*x);
228 if (!atv || !atv->is_audio_track()) {
229 return boost::shared_ptr<AudioTrack>();
232 return atv->audio_track();
235 boost::shared_ptr<MidiTrack>
236 Editor::get_nth_selected_midi_track (int nth) const
238 MidiTimeAxisView* mtv;
239 TrackSelection::iterator x;
241 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
243 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
247 } else if (mtv->is_midi_track()) {
252 if (x == selection->tracks.end()) {
253 mtv = dynamic_cast<MidiTimeAxisView*>(selection->tracks.back());
255 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
258 if (!mtv || !mtv->is_midi_track()) {
259 return boost::shared_ptr<MidiTrack>();
262 return mtv->midi_track();
266 Editor::do_import (vector<string> paths, ImportDisposition disposition, ImportMode mode, SrcQuality quality, framepos_t& pos)
268 boost::shared_ptr<Track> track;
269 vector<string> to_import;
271 bool use_timestamp = (pos == -1);
273 current_interthread_info = &import_status;
274 import_status.current = 1;
275 import_status.total = paths.size ();
276 import_status.all_done = false;
278 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
282 if (disposition == Editing::ImportMergeFiles) {
284 /* create 1 region from all paths, add to 1 track,
289 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
290 int check = check_whether_and_how_to_import(*a, false);
301 ok = (import_sndfiles (paths, disposition, mode, quality, pos, 1, 1, track, false) == 0);
306 bool replace = false;
308 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
310 const int check = check_whether_and_how_to_import (*a, true);
317 fatal << "Updating existing sources should be disabled!" << endmsg;
324 fatal << "Illegal return " << check << " from check_whether_and_how_to_import()!" << endmsg;
328 /* have to reset this for every file we handle */
336 switch (disposition) {
337 case Editing::ImportDistinctFiles:
340 to_import.push_back (*a);
342 if (mode == Editing::ImportToTrack) {
343 track = get_nth_selected_audio_track (nth++);
346 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, -1, track, replace) == 0);
349 case Editing::ImportDistinctChannels:
352 to_import.push_back (*a);
354 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, -1, -1, track, replace) == 0);
357 case Editing::ImportSerializeFiles:
360 to_import.push_back (*a);
362 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, 1, track, replace) == 0);
365 case Editing::ImportMergeFiles:
366 // Not entered, handled in earlier if() branch
373 _session->save_state ("");
376 import_status.all_done = true;
380 Editor::do_embed (vector<string> paths, ImportDisposition import_as, ImportMode mode, framepos_t& pos)
382 boost::shared_ptr<Track> track;
383 bool check_sample_rate = true;
385 vector<string> to_embed;
386 bool multi = paths.size() > 1;
388 bool use_timestamp = (pos == -1);
391 case Editing::ImportDistinctFiles:
392 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
394 /* have to reset this for every file we handle */
400 to_embed.push_back (*a);
402 if (mode == Editing::ImportToTrack) {
403 track = get_nth_selected_audio_track (nth++);
406 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, 1, -1, track) < -1) {
412 case Editing::ImportDistinctChannels:
413 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
415 /* have to reset this for every file we handle */
421 to_embed.push_back (*a);
423 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, -1, -1, track) < -1) {
429 case Editing::ImportMergeFiles:
430 if (embed_sndfiles (paths, multi, check_sample_rate, import_as, mode, pos, 1, 1, track) < -1) {
435 case Editing::ImportSerializeFiles:
436 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
438 /* have to reset this for every file we handle */
444 to_embed.push_back (*a);
446 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, 1, 1, track) < -1) {
457 _session->save_state ("");
462 Editor::import_sndfiles (vector<string> paths, ImportDisposition disposition, ImportMode mode, SrcQuality quality, framepos_t& pos,
463 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool replace)
465 import_status.paths = paths;
466 import_status.done = false;
467 import_status.cancel = false;
468 import_status.freeze = false;
469 import_status.quality = quality;
470 import_status.replace_existing_source = replace;
472 import_status.mode = mode;
473 import_status.pos = pos;
474 import_status.target_tracks = target_tracks;
475 import_status.target_regions = target_regions;
476 import_status.track = track;
477 import_status.replace = replace;
479 set_canvas_cursor (_cursors->wait);
482 /* start import thread for this spec. this will ultimately call Session::import_files()
483 which, if successful, will add the files as regions to the region list. its up to us
484 (the GUI) to direct additional steps after that.
487 pthread_create_and_store ("import", &import_status.thread, _import_thread, this);
488 pthread_detach (import_status.thread);
490 while (!import_status.done && !import_status.cancel) {
491 gtk_main_iteration ();
494 import_status.done = true;
498 if (!import_status.cancel && !import_status.sources.empty()) {
499 result = add_sources (
501 import_status.sources,
505 import_status.target_regions,
506 import_status.target_tracks,
510 /* update position from results */
512 pos = import_status.pos;
515 set_canvas_cursor (current_canvas_cursor);
520 Editor::embed_sndfiles (vector<string> paths, bool multifile,
521 bool& check_sample_rate, ImportDisposition disposition, ImportMode mode,
522 framepos_t& pos, int target_regions, int target_tracks,
523 boost::shared_ptr<Track>& track)
525 boost::shared_ptr<AudioFileSource> source;
531 push_canvas_cursor (_cursors->wait);
534 for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
539 /* note that we temporarily truncated _id at the colon */
541 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
542 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
546 if (check_sample_rate && (finfo.samplerate != (int) _session->frame_rate())) {
547 vector<string> choices;
550 choices.push_back (_("Cancel entire import"));
551 choices.push_back (_("Don't embed it"));
552 choices.push_back (_("Embed all without questions"));
554 Gtkmm2ext::Choice rate_choice (
556 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
557 short_path (path, 40)),
561 int resx = rate_choice.run ();
564 case 0: /* stop a multi-file import */
567 case 1: /* don't embed this one */
570 case 2: /* do it, and the rest without asking */
571 check_sample_rate = false;
580 choices.push_back (_("Cancel"));
581 choices.push_back (_("Embed it anyway"));
583 Gtkmm2ext::Choice rate_choice (
585 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
589 int resx = rate_choice.run ();
592 case 0: /* don't import */
604 for (int n = 0; n < finfo.channels; ++n) {
608 /* check if we have this thing embedded already */
610 boost::shared_ptr<Source> s;
612 if ((s = _session->audio_source_by_path_and_channel (path, n)) == 0) {
614 source = boost::dynamic_pointer_cast<AudioFileSource> (
615 SourceFactory::createExternal (DataType::AUDIO, *_session,
617 (mode == ImportAsTapeTrack
618 ? Source::Destructive
622 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
625 sources.push_back(source);
628 catch (failed_constructor& err) {
629 error << string_compose(_("could not open %1"), path) << endmsg;
633 gtk_main_iteration();
637 if (sources.empty()) {
642 ret = add_sources (paths, sources, pos, disposition, mode, target_regions, target_tracks, track, true);
645 pop_canvas_cursor ();
650 Editor::add_sources (vector<string> paths, SourceList& sources, framepos_t& pos, ImportDisposition disposition, ImportMode mode,
651 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool /*add_channel_suffix*/)
653 vector<boost::shared_ptr<Region> > regions;
655 uint32_t input_chan = 0;
656 uint32_t output_chan = 0;
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);
696 } else if (target_regions == -1 || target_regions > 1) {
698 /* take each source and create a region for each one */
701 SourceList::iterator x;
704 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
707 just_one.push_back (*x);
709 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (*x);
711 if (sources.size() > 1 && disposition == ImportDistinctChannels) {
713 /* generate a per-channel region name so that things work as
720 region_name = basename_nosuffix (fs->path());
722 region_name = (*x)->name();
725 switch (sources.size()) {
726 /* zero and one channel handled
727 by previous if() condition
737 region_name += (char) '-';
738 region_name += (char) ('1' + n);
744 region_name = region_name_from_path (fs->path(), false, false, sources.size(), n);
746 region_name = (*x)->name();
752 /* Fudge region length to ensure it is non-zero; make it 1 beat at 120bpm
753 for want of a better idea. It can't be too small, otherwise if this
754 is a MIDI region the conversion from frames -> beats -> frames will
755 round it back down to 0 again.
757 framecnt_t len = (*x)->length (pos);
759 len = (60.0 / 120.0) * _session->frame_rate ();
762 plist.add (ARDOUR::Properties::start, 0);
763 plist.add (ARDOUR::Properties::length, len);
764 plist.add (ARDOUR::Properties::name, region_name);
765 plist.add (ARDOUR::Properties::layer, 0);
766 plist.add (ARDOUR::Properties::whole_file, true);
767 plist.add (ARDOUR::Properties::external, true);
769 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
771 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
772 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
775 regions.push_back (r);
779 if (target_regions == 1) {
780 input_chan = regions.front()->n_channels();
782 if (target_tracks == 1) {
783 input_chan = regions.size();
789 if (Config->get_output_auto_connect() & AutoConnectMaster) {
790 output_chan = (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan);
792 output_chan = input_chan;
798 begin_reversible_command (Operations::insert_file);
800 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
801 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
806 /* get timestamp for this region */
808 const boost::shared_ptr<Source> s (ar->sources().front());
809 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
813 if (as->natural_position() != 0) {
814 pos = as->natural_position();
815 } else if (target_tracks == 1) {
816 /* hmm, no timestamp available, put it after the previous region
819 pos = get_preferred_edit_position ();
824 pos = get_preferred_edit_position ();
827 /* should really get first position in MIDI file, but for now, use edit position*/
828 pos = get_preferred_edit_position ();
833 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
835 rlen = (*r)->length();
837 if (target_tracks != 1) {
840 if (!use_timestamp || !ar) {
841 /* line each one up right after the other */
842 pos += (*r)->length();
847 commit_reversible_command ();
849 /* setup peak file building in another thread */
851 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
852 SourceFactory::setup_peakfile (*x, true);
859 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, framepos_t& pos,
860 ImportMode mode, boost::shared_ptr<Track>& existing_track)
862 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
863 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
867 /* relax, its been done */
872 if (!existing_track) {
875 existing_track = get_nth_selected_audio_track (0);
877 existing_track = get_nth_selected_midi_track (0);
880 if (!existing_track) {
885 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
886 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
887 playlist->clear_changes ();
888 playlist->add_region (copy, pos);
889 _session->add_command (new StatefulDiffCommand (playlist));
895 if (!existing_track) {
897 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
903 existing_track = at.front();
905 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (ChanCount (DataType::MIDI, 1),
906 ChanCount (DataType::MIDI, 1),
907 boost::shared_ptr<PluginInfo>(),
914 existing_track = mt.front();
917 existing_track->set_name (region->name());
920 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
921 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
922 playlist->clear_changes ();
923 playlist->add_region (copy, pos);
924 _session->add_command (new StatefulDiffCommand (playlist));
928 case ImportAsTapeTrack:
934 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Destructive));
936 boost::shared_ptr<Playlist> playlist = at.front()->playlist();
937 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
938 playlist->clear_changes ();
939 playlist->add_region (copy, pos);
940 _session->add_command (new StatefulDiffCommand (playlist));
950 Editor::_import_thread (void *arg)
952 SessionEvent::create_per_thread_pool ("import events", 64);
954 Editor *ed = (Editor *) arg;
955 return ed->import_thread ();
959 Editor::import_thread ()
961 _session->import_files (import_status);