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 set_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 set_canvas_cursor (_cursors->wait);
606 for (int n = 0; n < finfo.channels; ++n) {
610 /* check if we have this thing embedded already */
612 boost::shared_ptr<Source> s;
614 if ((s = _session->source_by_path_and_channel (path, n)) == 0) {
616 source = boost::dynamic_pointer_cast<AudioFileSource> (
617 SourceFactory::createExternal (DataType::AUDIO, *_session,
619 (mode == ImportAsTapeTrack
620 ? Source::Destructive
624 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
627 sources.push_back(source);
630 catch (failed_constructor& err) {
631 error << string_compose(_("could not open %1"), path) << endmsg;
635 gtk_main_iteration();
639 if (sources.empty()) {
644 ret = add_sources (paths, sources, pos, disposition, mode, target_regions, target_tracks, track, true);
647 set_canvas_cursor (current_canvas_cursor);
652 Editor::add_sources (vector<string> paths, SourceList& sources, framepos_t& pos, ImportDisposition disposition, ImportMode mode,
653 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool /*add_channel_suffix*/)
655 vector<boost::shared_ptr<Region> > regions;
657 uint32_t input_chan = 0;
658 uint32_t output_chan = 0;
661 use_timestamp = (pos == -1);
663 // kludge (for MIDI we're abusing "channel" for "track" here)
664 if (SMFSource::safe_midi_file_extension (paths.front())) {
668 if (target_regions == 1) {
670 /* take all the sources we have and package them up as a region */
672 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
674 /* we checked in import_sndfiles() that there were not too many */
676 while (RegionFactory::region_by_name (region_name)) {
677 region_name = bump_name_once (region_name, '.');
682 plist.add (ARDOUR::Properties::start, 0);
683 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
684 plist.add (ARDOUR::Properties::name, region_name);
685 plist.add (ARDOUR::Properties::layer, 0);
686 plist.add (ARDOUR::Properties::whole_file, true);
687 plist.add (ARDOUR::Properties::external, true);
689 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
691 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
692 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
695 regions.push_back (r);
698 } else if (target_regions == -1 || target_regions > 1) {
700 /* take each source and create a region for each one */
703 SourceList::iterator x;
706 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
709 just_one.push_back (*x);
711 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (*x);
713 if (sources.size() > 1 && disposition == ImportDistinctChannels) {
715 /* generate a per-channel region name so that things work as
722 region_name = basename_nosuffix (fs->path());
724 region_name = (*x)->name();
727 switch (sources.size()) {
728 /* zero and one channel handled
729 by previous if() condition
739 region_name += (char) '-';
740 region_name += (char) ('1' + n);
746 region_name = region_name_from_path (fs->path(), false, false, sources.size(), n);
748 region_name = (*x)->name();
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 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
803 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
808 /* get timestamp for this region */
810 const boost::shared_ptr<Source> s (ar->sources().front());
811 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
815 if (as->natural_position() != 0) {
816 pos = as->natural_position();
817 } else if (target_tracks == 1) {
818 /* hmm, no timestamp available, put it after the previous region
821 pos = get_preferred_edit_position ();
826 pos = get_preferred_edit_position ();
829 /* should really get first position in MIDI file, but for now, use edit position*/
830 pos = get_preferred_edit_position ();
835 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
837 rlen = (*r)->length();
839 if (target_tracks != 1) {
842 if (!use_timestamp || !ar) {
843 /* line each one up right after the other */
844 pos += (*r)->length();
849 commit_reversible_command ();
851 /* setup peak file building in another thread */
853 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
854 SourceFactory::setup_peakfile (*x, true);
861 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, framepos_t& pos,
862 ImportMode mode, boost::shared_ptr<Track>& existing_track)
864 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
865 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
869 /* relax, its been done */
874 if (!existing_track) {
877 existing_track = get_nth_selected_audio_track (0);
879 existing_track = get_nth_selected_midi_track (0);
882 if (!existing_track) {
887 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
888 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
889 playlist->clear_changes ();
890 playlist->add_region (copy, pos);
891 _session->add_command (new StatefulDiffCommand (playlist));
897 if (!existing_track) {
899 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
905 existing_track = at.front();
907 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (ChanCount (DataType::MIDI, 1),
908 ChanCount (DataType::MIDI, 1),
909 boost::shared_ptr<PluginInfo>(),
916 existing_track = mt.front();
919 existing_track->set_name (region->name());
922 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
923 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
924 playlist->clear_changes ();
925 playlist->add_region (copy, pos);
926 _session->add_command (new StatefulDiffCommand (playlist));
930 case ImportAsTapeTrack:
936 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Destructive));
938 boost::shared_ptr<Playlist> playlist = at.front()->playlist();
939 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
940 playlist->clear_changes ();
941 playlist->add_region (copy, pos);
942 _session->add_command (new StatefulDiffCommand (playlist));
952 Editor::_import_thread (void *arg)
954 SessionEvent::create_per_thread_pool ("import events", 64);
956 Editor *ed = (Editor *) arg;
957 return ed->import_thread ();
961 Editor::import_thread ()
963 _session->import_files (import_status);