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 chns, 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 (chns == 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, 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 */
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, mode, quality, pos, 1, -1, track, replace) == 0);
350 case Editing::ImportDistinctChannels:
353 to_import.push_back (*a);
355 ok = (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, replace) == 0);
358 case Editing::ImportSerializeFiles:
361 to_import.push_back (*a);
363 ok = (import_sndfiles (to_import, 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 chns, 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, 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, mode, pos, -1, -1, track) < -1) {
430 case Editing::ImportMergeFiles:
431 if (embed_sndfiles (paths, multi, check_sample_rate, 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, mode, pos, 1, 1, track) < -1) {
458 _session->save_state ("");
463 Editor::import_sndfiles (vector<string> paths, 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,
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, ImportMode mode, framepos_t& pos, int target_regions, int target_tracks,
522 boost::shared_ptr<Track>& track)
524 boost::shared_ptr<AudioFileSource> source;
530 set_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 set_canvas_cursor (_cursors->wait);
605 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->source_by_path_and_channel (path, n)) == 0) {
614 source = boost::dynamic_pointer_cast<AudioFileSource> (
615 SourceFactory::createReadable (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 ARDOUR_UI::instance()->flush_pending ();
637 if (sources.empty()) {
641 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
644 set_canvas_cursor (current_canvas_cursor);
649 Editor::add_sources (vector<string> paths, SourceList& sources, framepos_t& pos, 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;
658 use_timestamp = (pos == -1);
660 // kludge (for MIDI we're abusing "channel" for "track" here)
661 if (SMFSource::safe_midi_file_extension (paths.front())) {
665 if (target_regions == 1) {
667 /* take all the sources we have and package them up as a region */
669 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
671 /* we checked in import_sndfiles() that there were not too many */
673 while (RegionFactory::region_by_name (region_name)) {
674 region_name = bump_name_once (region_name, '.');
679 plist.add (ARDOUR::Properties::start, 0);
680 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
681 plist.add (ARDOUR::Properties::name, region_name);
682 plist.add (ARDOUR::Properties::layer, 0);
683 plist.add (ARDOUR::Properties::whole_file, true);
684 plist.add (ARDOUR::Properties::external, true);
686 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
688 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
689 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
692 regions.push_back (r);
695 } else if (target_regions == -1 || target_regions > 1) {
697 /* take each source and create a region for each one */
700 SourceList::iterator x;
703 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
706 just_one.push_back (*x);
708 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (*x);
711 region_name = region_name_from_path (fs->path(), false, false, sources.size(), n);
713 region_name = (*x)->name();
718 /* Fudge region length to ensure it is non-zero; make it 1 beat at 120bpm
719 for want of a better idea. It can't be too small, otherwise if this
720 is a MIDI region the conversion from frames -> beats -> frames will
721 round it back down to 0 again.
723 framecnt_t len = (*x)->length (pos);
725 len = (60.0 / 120.0) * _session->frame_rate ();
728 plist.add (ARDOUR::Properties::start, 0);
729 plist.add (ARDOUR::Properties::length, len);
730 plist.add (ARDOUR::Properties::name, region_name);
731 plist.add (ARDOUR::Properties::layer, 0);
732 plist.add (ARDOUR::Properties::whole_file, true);
733 plist.add (ARDOUR::Properties::external, true);
735 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
737 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
738 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
741 regions.push_back (r);
745 if (target_regions == 1) {
746 input_chan = regions.front()->n_channels();
748 if (target_tracks == 1) {
749 input_chan = regions.size();
755 if (Config->get_output_auto_connect() & AutoConnectMaster) {
756 output_chan = (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan);
758 output_chan = input_chan;
764 begin_reversible_command (Operations::insert_file);
766 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
767 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
772 /* get timestamp for this region */
774 const boost::shared_ptr<Source> s (ar->sources().front());
775 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
779 if (as->natural_position() != 0) {
780 pos = as->natural_position();
781 } else if (target_tracks == 1) {
782 /* hmm, no timestamp available, put it after the previous region
785 pos = get_preferred_edit_position ();
790 pos = get_preferred_edit_position ();
793 /* should really get first position in MIDI file, but for now, use edit position*/
794 pos = get_preferred_edit_position ();
799 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
801 rlen = (*r)->length();
803 if (target_tracks != 1) {
806 if (!use_timestamp || !ar) {
807 /* line each one up right after the other */
808 pos += (*r)->length();
813 commit_reversible_command ();
815 /* setup peak file building in another thread */
817 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
818 SourceFactory::setup_peakfile (*x, true);
825 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, framepos_t& pos,
826 ImportMode mode, boost::shared_ptr<Track>& existing_track)
828 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
829 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
833 /* relax, its been done */
838 if (!existing_track) {
841 existing_track = get_nth_selected_audio_track (0);
843 existing_track = get_nth_selected_midi_track (0);
846 if (!existing_track) {
851 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
852 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
853 playlist->clear_changes ();
854 playlist->add_region (copy, pos);
855 _session->add_command (new StatefulDiffCommand (playlist));
861 if (!existing_track) {
863 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
869 existing_track = at.front();
871 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (ChanCount (DataType::MIDI, 1),
872 ChanCount (DataType::MIDI, 1),
873 boost::shared_ptr<PluginInfo>(),
880 existing_track = mt.front();
883 existing_track->set_name (region->name());
886 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
887 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
888 playlist->clear_changes ();
889 playlist->add_region (copy, pos);
890 _session->add_command (new StatefulDiffCommand (playlist));
894 case ImportAsTapeTrack:
900 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Destructive));
902 boost::shared_ptr<Playlist> playlist = at.front()->playlist();
903 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
904 playlist->clear_changes ();
905 playlist->add_region (copy, pos);
906 _session->add_command (new StatefulDiffCommand (playlist));
916 Editor::_import_thread (void *arg)
918 SessionEvent::create_per_thread_pool ("import events", 64);
920 Editor *ed = (Editor *) arg;
921 return ed->import_thread ();
925 Editor::import_thread ()
927 _session->import_files (import_status);
928 pthread_exit_pbd (0);