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 SourceMap all_sources = _session->get_sources();
163 bool already_exists = false;
166 if ((existing = _session->count_sources_by_origin (path)) > 0) {
167 already_exists = true;
172 if (already_exists) {
174 if (all_or_nothing) {
175 // updating is still disabled
176 //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);
177 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);
179 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);
182 MessageDialog dialog(message, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
184 if (all_or_nothing) {
186 //dialog.add_button("Update", 0);
187 dialog.add_button("Import", 1);
188 dialog.add_button("Skip", 2);
190 dialog.add_button("Import", 1);
191 dialog.add_button("Cancel", 2);
194 //dialog.add_button("Skip all", 4); // All or rest?
198 function = dialog.run ();
206 boost::shared_ptr<AudioTrack>
207 Editor::get_nth_selected_audio_track (int nth) const
209 AudioTimeAxisView* atv;
210 TrackSelection::iterator x;
212 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
214 atv = dynamic_cast<AudioTimeAxisView*>(*x);
218 } else if (atv->is_audio_track()) {
223 if (x == selection->tracks.end()) {
224 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
226 atv = dynamic_cast<AudioTimeAxisView*>(*x);
229 if (!atv || !atv->is_audio_track()) {
230 return boost::shared_ptr<AudioTrack>();
233 return atv->audio_track();
236 boost::shared_ptr<MidiTrack>
237 Editor::get_nth_selected_midi_track (int nth) const
239 MidiTimeAxisView* mtv;
240 TrackSelection::iterator x;
242 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
244 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
248 } else if (mtv->is_midi_track()) {
253 if (x == selection->tracks.end()) {
254 mtv = dynamic_cast<MidiTimeAxisView*>(selection->tracks.back());
256 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
259 if (!mtv || !mtv->is_midi_track()) {
260 return boost::shared_ptr<MidiTrack>();
263 return mtv->midi_track();
267 Editor::do_import (vector<string> paths, ImportDisposition disposition, ImportMode mode, SrcQuality quality, framepos_t& pos)
269 boost::shared_ptr<Track> track;
270 vector<string> to_import;
272 bool use_timestamp = (pos == -1);
274 current_interthread_info = &import_status;
275 import_status.current = 1;
276 import_status.total = paths.size ();
277 import_status.all_done = false;
279 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
283 if (disposition == Editing::ImportMergeFiles) {
285 /* create 1 region from all paths, add to 1 track,
290 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
291 int check = check_whether_and_how_to_import(*a, false);
302 ok = (import_sndfiles (paths, disposition, mode, quality, pos, 1, 1, track, false) == 0);
307 bool replace = false;
309 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
311 const int check = check_whether_and_how_to_import (*a, true);
318 fatal << "Updating existing sources should be disabled!" << endmsg;
325 fatal << "Illegal return " << check << " from check_whether_and_how_to_import()!" << endmsg;
329 /* have to reset this for every file we handle */
337 switch (disposition) {
338 case Editing::ImportDistinctFiles:
341 to_import.push_back (*a);
343 if (mode == Editing::ImportToTrack) {
344 track = get_nth_selected_audio_track (nth++);
347 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, -1, track, replace) == 0);
350 case Editing::ImportDistinctChannels:
353 to_import.push_back (*a);
355 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, -1, -1, track, replace) == 0);
358 case Editing::ImportSerializeFiles:
361 to_import.push_back (*a);
363 ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, 1, track, replace) == 0);
366 case Editing::ImportMergeFiles:
367 // Not entered, handled in earlier if() branch
374 _session->save_state ("");
377 import_status.all_done = true;
381 Editor::do_embed (vector<string> paths, ImportDisposition import_as, ImportMode mode, framepos_t& pos)
383 boost::shared_ptr<Track> track;
384 bool check_sample_rate = true;
386 vector<string> to_embed;
387 bool multi = paths.size() > 1;
389 bool use_timestamp = (pos == -1);
392 case Editing::ImportDistinctFiles:
393 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
395 /* have to reset this for every file we handle */
401 to_embed.push_back (*a);
403 if (mode == Editing::ImportToTrack) {
404 track = get_nth_selected_audio_track (nth++);
407 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, 1, -1, track) < -1) {
413 case Editing::ImportDistinctChannels:
414 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
416 /* have to reset this for every file we handle */
422 to_embed.push_back (*a);
424 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, -1, -1, track) < -1) {
430 case Editing::ImportMergeFiles:
431 if (embed_sndfiles (paths, multi, check_sample_rate, import_as, mode, pos, 1, 1, track) < -1) {
436 case Editing::ImportSerializeFiles:
437 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
439 /* have to reset this for every file we handle */
445 to_embed.push_back (*a);
447 if (embed_sndfiles (to_embed, multi, check_sample_rate, import_as, mode, pos, 1, 1, track) < -1) {
458 _session->save_state ("");
463 Editor::import_sndfiles (vector<string> paths, ImportDisposition disposition, ImportMode mode, SrcQuality quality, framepos_t& pos,
464 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool replace)
466 import_status.paths = paths;
467 import_status.done = false;
468 import_status.cancel = false;
469 import_status.freeze = false;
470 import_status.quality = quality;
471 import_status.replace_existing_source = replace;
473 import_status.mode = mode;
474 import_status.pos = pos;
475 import_status.target_tracks = target_tracks;
476 import_status.target_regions = target_regions;
477 import_status.track = track;
478 import_status.replace = replace;
480 set_canvas_cursor (_cursors->wait);
483 /* start import thread for this spec. this will ultimately call Session::import_files()
484 which, if successful, will add the files as regions to the region list. its up to us
485 (the GUI) to direct additional steps after that.
488 pthread_create_and_store ("import", &import_status.thread, _import_thread, this);
489 pthread_detach (import_status.thread);
491 while (!import_status.done && !import_status.cancel) {
492 gtk_main_iteration ();
495 import_status.done = true;
499 if (!import_status.cancel && !import_status.sources.empty()) {
500 result = add_sources (
502 import_status.sources,
506 import_status.target_regions,
507 import_status.target_tracks,
511 /* update position from results */
513 pos = import_status.pos;
516 set_canvas_cursor (current_canvas_cursor);
521 Editor::embed_sndfiles (vector<string> paths, bool multifile,
522 bool& check_sample_rate, ImportDisposition disposition, ImportMode mode,
523 framepos_t& pos, int target_regions, int target_tracks,
524 boost::shared_ptr<Track>& track)
526 boost::shared_ptr<AudioFileSource> source;
532 set_canvas_cursor (_cursors->wait);
535 for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
540 /* note that we temporarily truncated _id at the colon */
542 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
543 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
547 if (check_sample_rate && (finfo.samplerate != (int) _session->frame_rate())) {
548 vector<string> choices;
551 choices.push_back (_("Cancel entire import"));
552 choices.push_back (_("Don't embed it"));
553 choices.push_back (_("Embed all without questions"));
555 Gtkmm2ext::Choice rate_choice (
557 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
558 short_path (path, 40)),
562 int resx = rate_choice.run ();
565 case 0: /* stop a multi-file import */
568 case 1: /* don't embed this one */
571 case 2: /* do it, and the rest without asking */
572 check_sample_rate = false;
581 choices.push_back (_("Cancel"));
582 choices.push_back (_("Embed it anyway"));
584 Gtkmm2ext::Choice rate_choice (
586 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
590 int resx = rate_choice.run ();
593 case 0: /* don't import */
605 set_canvas_cursor (_cursors->wait);
607 for (int n = 0; n < finfo.channels; ++n) {
611 /* check if we have this thing embedded already */
613 boost::shared_ptr<Source> s;
615 if ((s = _session->source_by_path_and_channel (path, n)) == 0) {
617 source = boost::dynamic_pointer_cast<AudioFileSource> (
618 SourceFactory::createExternal (DataType::AUDIO, *_session,
620 (mode == ImportAsTapeTrack
621 ? Source::Destructive
625 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
628 sources.push_back(source);
631 catch (failed_constructor& err) {
632 error << string_compose(_("could not open %1"), path) << endmsg;
636 ARDOUR_UI::instance()->flush_pending ();
640 if (sources.empty()) {
645 ret = add_sources (paths, sources, pos, disposition, mode, target_regions, target_tracks, track, true);
648 set_canvas_cursor (current_canvas_cursor);
653 Editor::add_sources (vector<string> paths, SourceList& sources, framepos_t& pos, ImportDisposition disposition, ImportMode mode,
654 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool /*add_channel_suffix*/)
656 vector<boost::shared_ptr<Region> > regions;
658 uint32_t input_chan = 0;
659 uint32_t output_chan = 0;
662 use_timestamp = (pos == -1);
664 // kludge (for MIDI we're abusing "channel" for "track" here)
665 if (SMFSource::safe_midi_file_extension (paths.front())) {
669 if (target_regions == 1) {
671 /* take all the sources we have and package them up as a region */
673 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
675 /* we checked in import_sndfiles() that there were not too many */
677 while (RegionFactory::region_by_name (region_name)) {
678 region_name = bump_name_once (region_name, '.');
683 plist.add (ARDOUR::Properties::start, 0);
684 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
685 plist.add (ARDOUR::Properties::name, region_name);
686 plist.add (ARDOUR::Properties::layer, 0);
687 plist.add (ARDOUR::Properties::whole_file, true);
688 plist.add (ARDOUR::Properties::external, true);
690 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
692 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
693 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
696 regions.push_back (r);
699 } else if (target_regions == -1 || target_regions > 1) {
701 /* take each source and create a region for each one */
704 SourceList::iterator x;
707 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
710 just_one.push_back (*x);
712 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (*x);
714 if (sources.size() > 1 && disposition == ImportDistinctChannels) {
716 /* generate a per-channel region name so that things work as
723 region_name = basename_nosuffix (fs->path());
725 region_name = (*x)->name();
728 switch (sources.size()) {
729 /* zero and one channel handled
730 by previous if() condition
740 region_name += (char) '-';
741 region_name += (char) ('1' + n);
747 region_name = region_name_from_path (fs->path(), false, false, sources.size(), n);
749 region_name = (*x)->name();
755 /* Fudge region length to ensure it is non-zero; make it 1 beat at 120bpm
756 for want of a better idea. It can't be too small, otherwise if this
757 is a MIDI region the conversion from frames -> beats -> frames will
758 round it back down to 0 again.
760 framecnt_t len = (*x)->length (pos);
762 len = (60.0 / 120.0) * _session->frame_rate ();
765 plist.add (ARDOUR::Properties::start, 0);
766 plist.add (ARDOUR::Properties::length, len);
767 plist.add (ARDOUR::Properties::name, region_name);
768 plist.add (ARDOUR::Properties::layer, 0);
769 plist.add (ARDOUR::Properties::whole_file, true);
770 plist.add (ARDOUR::Properties::external, true);
772 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
774 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
775 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
778 regions.push_back (r);
782 if (target_regions == 1) {
783 input_chan = regions.front()->n_channels();
785 if (target_tracks == 1) {
786 input_chan = regions.size();
792 if (Config->get_output_auto_connect() & AutoConnectMaster) {
793 output_chan = (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan);
795 output_chan = input_chan;
801 begin_reversible_command (Operations::insert_file);
803 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
804 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
809 /* get timestamp for this region */
811 const boost::shared_ptr<Source> s (ar->sources().front());
812 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
816 if (as->natural_position() != 0) {
817 pos = as->natural_position();
818 } else if (target_tracks == 1) {
819 /* hmm, no timestamp available, put it after the previous region
822 pos = get_preferred_edit_position ();
827 pos = get_preferred_edit_position ();
830 /* should really get first position in MIDI file, but for now, use edit position*/
831 pos = get_preferred_edit_position ();
836 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
838 rlen = (*r)->length();
840 if (target_tracks != 1) {
843 if (!use_timestamp || !ar) {
844 /* line each one up right after the other */
845 pos += (*r)->length();
850 commit_reversible_command ();
852 /* setup peak file building in another thread */
854 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
855 SourceFactory::setup_peakfile (*x, true);
862 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, framepos_t& pos,
863 ImportMode mode, boost::shared_ptr<Track>& existing_track)
865 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
866 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
870 /* relax, its been done */
875 if (!existing_track) {
878 existing_track = get_nth_selected_audio_track (0);
880 existing_track = get_nth_selected_midi_track (0);
883 if (!existing_track) {
888 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
889 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
890 playlist->clear_changes ();
891 playlist->add_region (copy, pos);
892 _session->add_command (new StatefulDiffCommand (playlist));
898 if (!existing_track) {
900 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
906 existing_track = at.front();
908 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (ChanCount (DataType::MIDI, 1),
909 ChanCount (DataType::MIDI, 1),
910 boost::shared_ptr<PluginInfo>(),
917 existing_track = mt.front();
920 existing_track->set_name (region->name());
923 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
924 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
925 playlist->clear_changes ();
926 playlist->add_region (copy, pos);
927 _session->add_command (new StatefulDiffCommand (playlist));
931 case ImportAsTapeTrack:
937 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Destructive));
939 boost::shared_ptr<Playlist> playlist = at.front()->playlist();
940 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
941 playlist->clear_changes ();
942 playlist->add_region (copy, pos);
943 _session->add_command (new StatefulDiffCommand (playlist));
953 Editor::_import_thread (void *arg)
955 SessionEvent::create_per_thread_pool ("import events", 64);
957 Editor *ed = (Editor *) arg;
958 return ed->import_thread ();
962 Editor::import_thread ()
964 _session->import_files (import_status);
965 pthread_exit_pbd (0);