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 (*this, _("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 (*this, _("Add Existing Media"), _session, audio_track_cnt, midi_track_cnt, true);
129 sfbrowser->reset (audio_track_cnt, midi_track_cnt);
132 sfbrowser->show_all ();
140 int response = sfbrowser->run ();
144 // leave the dialog open
152 // cancel from the browser - we are done
159 vector<string> upaths = sfbrowser->get_paths ();
160 for (vector<string>::iterator x = upaths.begin(); x != upaths.end(); ++x) {
161 paths.push_back (*x);
164 ImportPosition pos = sfbrowser->get_position ();
165 ImportMode mode = sfbrowser->get_mode ();
166 ImportDisposition chns = sfbrowser->get_channel_disposition ();
170 case ImportAtEditPoint:
171 where = get_preferred_edit_position ();
173 case ImportAtTimestamp:
176 case ImportAtPlayhead:
177 where = playhead_cursor->current_frame;
180 where = _session->current_start_frame();
184 SrcQuality quality = sfbrowser->get_src_quality();
187 if (sfbrowser->copy_files_btn.get_active()) {
188 do_import (paths, chns, mode, quality, where);
190 do_embed (paths, chns, mode, where);
193 if (response == RESPONSE_APPLY) {
194 sfbrowser->clear_selection ();
198 } while (keepRunning);
202 Editor::session_import_dialog ()
204 SessionImportDialog dialog (_session);
205 ensure_float (dialog);
209 typedef std::map<PBD::ID,boost::shared_ptr<ARDOUR::Source> > SourceMap;
212 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_files()
215 * true = show "Update", "Import" and "Skip"
216 * false = show "Import", and "Cancel"
219 * 0 To update an existing source of the same name
220 * 1 To import/embed the file normally (make sure the new name will be unique)
221 * 2 If the user wants to skip this file
224 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
226 string wave_name (Glib::path_get_basename(path));
228 SourceMap all_sources = _session->get_sources();
229 bool already_exists = false;
232 if ((existing = _session->count_sources_by_origin (path)) > 0) {
233 already_exists = true;
238 if (already_exists) {
240 if (all_or_nothing) {
241 // updating is still disabled
242 //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);
243 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);
245 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);
248 MessageDialog dialog(message, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
250 if (all_or_nothing) {
252 //dialog.add_button("Update", 0);
253 dialog.add_button("Import", 1);
254 dialog.add_button("Skip", 2);
256 dialog.add_button("Import", 1);
257 dialog.add_button("Cancel", 2);
260 //dialog.add_button("Skip all", 4); // All or rest?
264 function = dialog.run ();
272 boost::shared_ptr<AudioTrack>
273 Editor::get_nth_selected_audio_track (int nth) const
275 AudioTimeAxisView* atv;
276 TrackSelection::iterator x;
278 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
280 atv = dynamic_cast<AudioTimeAxisView*>(*x);
284 } else if (atv->is_audio_track()) {
289 if (x == selection->tracks.end()) {
290 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
292 atv = dynamic_cast<AudioTimeAxisView*>(*x);
295 if (!atv || !atv->is_audio_track()) {
296 return boost::shared_ptr<AudioTrack>();
299 return atv->audio_track();
302 boost::shared_ptr<MidiTrack>
303 Editor::get_nth_selected_midi_track (int nth) const
305 MidiTimeAxisView* mtv;
306 TrackSelection::iterator x;
308 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
310 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
314 } else if (mtv->is_midi_track()) {
319 if (x == selection->tracks.end()) {
320 mtv = dynamic_cast<MidiTimeAxisView*>(selection->tracks.back());
322 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
325 if (!mtv || !mtv->is_midi_track()) {
326 return boost::shared_ptr<MidiTrack>();
329 return mtv->midi_track();
333 Editor::do_import (vector<string> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, framepos_t& pos)
335 boost::shared_ptr<Track> track;
336 vector<string> to_import;
338 bool use_timestamp = (pos == -1);
340 current_interthread_info = &import_status;
341 import_status.current = 1;
342 import_status.total = paths.size ();
343 import_status.all_done = false;
345 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
349 if (chns == Editing::ImportMergeFiles) {
351 /* create 1 region from all paths, add to 1 track,
356 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
357 int check = check_whether_and_how_to_import(*a, false);
368 ok = (import_sndfiles (paths, mode, quality, pos, 1, 1, track, false) == 0);
373 bool replace = false;
375 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
377 const int check = check_whether_and_how_to_import (*a, true);
384 fatal << "Updating existing sources should be disabled!" << endmsg;
391 fatal << "Illegal return " << check << " from check_whether_and_how_to_import()!" << endmsg;
395 /* have to reset this for every file we handle */
404 case Editing::ImportDistinctFiles:
407 to_import.push_back (*a);
409 if (mode == Editing::ImportToTrack) {
410 track = get_nth_selected_audio_track (nth++);
413 ok = (import_sndfiles (to_import, mode, quality, pos, 1, -1, track, replace) == 0);
416 case Editing::ImportDistinctChannels:
419 to_import.push_back (*a);
421 ok = (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, replace) == 0);
424 case Editing::ImportSerializeFiles:
427 to_import.push_back (*a);
429 ok = (import_sndfiles (to_import, mode, quality, pos, 1, 1, track, replace) == 0);
432 case Editing::ImportMergeFiles:
433 // Not entered, handled in earlier if() branch
440 _session->save_state ("");
443 import_status.all_done = true;
447 Editor::do_embed (vector<string> paths, ImportDisposition chns, ImportMode mode, framepos_t& pos)
449 boost::shared_ptr<Track> track;
450 bool check_sample_rate = true;
452 vector<string> to_embed;
453 bool multi = paths.size() > 1;
455 bool use_timestamp = (pos == -1);
458 case Editing::ImportDistinctFiles:
459 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
461 /* have to reset this for every file we handle */
467 to_embed.push_back (*a);
469 if (mode == Editing::ImportToTrack) {
470 track = get_nth_selected_audio_track (nth++);
473 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
479 case Editing::ImportDistinctChannels:
480 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
482 /* have to reset this for every file we handle */
488 to_embed.push_back (*a);
490 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
496 case Editing::ImportMergeFiles:
497 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
502 case Editing::ImportSerializeFiles:
503 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
505 /* have to reset this for every file we handle */
511 to_embed.push_back (*a);
513 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
524 _session->save_state ("");
529 Editor::import_sndfiles (vector<string> paths, ImportMode mode, SrcQuality quality, framepos_t& pos,
530 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool replace)
532 import_status.paths = paths;
533 import_status.done = false;
534 import_status.cancel = false;
535 import_status.freeze = false;
536 import_status.quality = quality;
537 import_status.replace_existing_source = replace;
539 import_status.mode = mode;
540 import_status.pos = pos;
541 import_status.target_tracks = target_tracks;
542 import_status.target_regions = target_regions;
543 import_status.track = track;
544 import_status.replace = replace;
546 set_canvas_cursor (_cursors->wait);
549 /* start import thread for this spec. this will ultimately call Session::import_files()
550 which, if successful, will add the files as regions to the region list. its up to us
551 (the GUI) to direct additional steps after that.
554 pthread_create_and_store ("import", &import_status.thread, _import_thread, this);
555 pthread_detach (import_status.thread);
557 while (!import_status.done && !import_status.cancel) {
558 gtk_main_iteration ();
561 import_status.done = true;
565 if (!import_status.cancel && !import_status.sources.empty()) {
566 result = add_sources (
568 import_status.sources,
571 import_status.target_regions,
572 import_status.target_tracks,
576 /* update position from results */
578 pos = import_status.pos;
581 set_canvas_cursor (current_canvas_cursor);
586 Editor::embed_sndfiles (vector<string> paths, bool multifile,
587 bool& check_sample_rate, ImportMode mode, framepos_t& pos, int target_regions, int target_tracks,
588 boost::shared_ptr<Track>& track)
590 boost::shared_ptr<AudioFileSource> source;
596 set_canvas_cursor (_cursors->wait);
599 for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
604 /* note that we temporarily truncated _id at the colon */
606 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
607 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
611 if (check_sample_rate && (finfo.samplerate != (int) _session->frame_rate())) {
612 vector<string> choices;
615 choices.push_back (_("Cancel entire import"));
616 choices.push_back (_("Don't embed it"));
617 choices.push_back (_("Embed all without questions"));
619 Gtkmm2ext::Choice rate_choice (
621 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
622 short_path (path, 40)),
626 int resx = rate_choice.run ();
629 case 0: /* stop a multi-file import */
632 case 1: /* don't embed this one */
635 case 2: /* do it, and the rest without asking */
636 check_sample_rate = false;
645 choices.push_back (_("Cancel"));
646 choices.push_back (_("Embed it anyway"));
648 Gtkmm2ext::Choice rate_choice (
650 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
654 int resx = rate_choice.run ();
657 case 0: /* don't import */
669 set_canvas_cursor (_cursors->wait);
671 for (int n = 0; n < finfo.channels; ++n) {
674 /* check if we have this thing embedded already */
676 boost::shared_ptr<Source> s;
678 if ((s = _session->source_by_path_and_channel (path, n)) == 0) {
680 source = boost::dynamic_pointer_cast<AudioFileSource> (
681 SourceFactory::createReadable (DataType::AUDIO, *_session,
683 (mode == ImportAsTapeTrack
684 ? Source::Destructive
688 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
691 sources.push_back(source);
694 catch (failed_constructor& err) {
695 error << string_compose(_("could not open %1"), path) << endmsg;
699 ARDOUR_UI::instance()->flush_pending ();
703 if (sources.empty()) {
707 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
710 set_canvas_cursor (current_canvas_cursor);
715 Editor::add_sources (vector<string> paths, SourceList& sources, framepos_t& pos, ImportMode mode,
716 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool /*add_channel_suffix*/)
718 vector<boost::shared_ptr<Region> > regions;
720 uint32_t input_chan = 0;
721 uint32_t output_chan = 0;
724 use_timestamp = (pos == -1);
726 // kludge (for MIDI we're abusing "channel" for "track" here)
727 if (SMFSource::safe_midi_file_extension (paths.front())) {
731 if (target_regions == 1) {
733 /* take all the sources we have and package them up as a region */
735 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
737 /* we checked in import_sndfiles() that there were not too many */
739 while (RegionFactory::region_by_name (region_name)) {
740 region_name = bump_name_once (region_name, '.');
745 plist.add (ARDOUR::Properties::start, 0);
746 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
747 plist.add (ARDOUR::Properties::name, region_name);
748 plist.add (ARDOUR::Properties::layer, 0);
749 plist.add (ARDOUR::Properties::whole_file, true);
750 plist.add (ARDOUR::Properties::external, true);
752 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
754 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
755 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
758 regions.push_back (r);
761 } else if (target_regions == -1 || target_regions > 1) {
763 /* take each source and create a region for each one */
766 SourceList::iterator x;
769 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
772 just_one.push_back (*x);
774 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (*x);
777 region_name = region_name_from_path (fs->path(), false, false, sources.size(), n);
779 region_name = (*x)->name();
784 /* Fudge region length to ensure it is non-zero; make it 1 beat at 120bpm
785 for want of a better idea. It can't be too small, otherwise if this
786 is a MIDI region the conversion from frames -> beats -> frames will
787 round it back down to 0 again.
789 framecnt_t len = (*x)->length (pos);
791 len = (60.0 / 120.0) * _session->frame_rate ();
794 plist.add (ARDOUR::Properties::start, 0);
795 plist.add (ARDOUR::Properties::length, len);
796 plist.add (ARDOUR::Properties::name, region_name);
797 plist.add (ARDOUR::Properties::layer, 0);
798 plist.add (ARDOUR::Properties::whole_file, true);
799 plist.add (ARDOUR::Properties::external, true);
801 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
803 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
804 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
807 regions.push_back (r);
811 if (target_regions == 1) {
812 input_chan = regions.front()->n_channels();
814 if (target_tracks == 1) {
815 input_chan = regions.size();
821 if (Config->get_output_auto_connect() & AutoConnectMaster) {
822 output_chan = (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan);
824 output_chan = input_chan;
830 begin_reversible_command (Operations::insert_file);
832 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
833 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
838 /* get timestamp for this region */
840 const boost::shared_ptr<Source> s (ar->sources().front());
841 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
845 if (as->natural_position() != 0) {
846 pos = as->natural_position();
847 } else if (target_tracks == 1) {
848 /* hmm, no timestamp available, put it after the previous region
851 pos = get_preferred_edit_position ();
856 pos = get_preferred_edit_position ();
859 /* should really get first position in MIDI file, but for now, use edit position*/
860 pos = get_preferred_edit_position ();
865 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
867 rlen = (*r)->length();
869 if (target_tracks != 1) {
872 if (!use_timestamp || !ar) {
873 /* line each one up right after the other */
874 pos += (*r)->length();
879 commit_reversible_command ();
881 /* setup peak file building in another thread */
883 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
884 SourceFactory::setup_peakfile (*x, true);
891 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, framepos_t& pos,
892 ImportMode mode, boost::shared_ptr<Track>& existing_track)
894 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
895 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
899 /* relax, its been done */
904 if (!existing_track) {
907 existing_track = get_nth_selected_audio_track (0);
909 existing_track = get_nth_selected_midi_track (0);
912 if (!existing_track) {
917 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
918 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
919 playlist->clear_changes ();
920 playlist->add_region (copy, pos);
921 _session->add_command (new StatefulDiffCommand (playlist));
927 if (!existing_track) {
929 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
935 existing_track = at.front();
937 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (ChanCount (DataType::MIDI, 1),
938 ChanCount (DataType::MIDI, 1),
939 boost::shared_ptr<PluginInfo>(),
946 existing_track = mt.front();
949 existing_track->set_name (region->name());
952 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
953 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
954 playlist->clear_changes ();
955 playlist->add_region (copy, pos);
956 _session->add_command (new StatefulDiffCommand (playlist));
960 case ImportAsTapeTrack:
966 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Destructive));
968 boost::shared_ptr<Playlist> playlist = at.front()->playlist();
969 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
970 playlist->clear_changes ();
971 playlist->add_region (copy, pos);
972 _session->add_command (new StatefulDiffCommand (playlist));
982 Editor::_import_thread (void *arg)
984 SessionEvent::create_per_thread_pool ("import events", 64);
986 Editor *ed = (Editor *) arg;
987 return ed->import_thread ();
991 Editor::import_thread ()
993 _session->import_files (import_status);
994 pthread_exit_pbd (0);