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 ();
199 } while (keepRunning);
203 Editor::session_import_dialog ()
205 SessionImportDialog dialog (_session);
206 ensure_float (dialog);
210 typedef std::map<PBD::ID,boost::shared_ptr<ARDOUR::Source> > SourceMap;
213 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_files()
216 * true = show "Update", "Import" and "Skip"
217 * false = show "Import", and "Cancel"
220 * 0 To update an existing source of the same name
221 * 1 To import/embed the file normally (make sure the new name will be unique)
222 * 2 If the user wants to skip this file
225 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
227 string wave_name (Glib::path_get_basename(path));
229 SourceMap all_sources = _session->get_sources();
230 bool already_exists = false;
233 if ((existing = _session->count_sources_by_origin (path)) > 0) {
234 already_exists = true;
239 if (already_exists) {
241 if (all_or_nothing) {
242 // updating is still disabled
243 //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);
244 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);
246 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);
249 MessageDialog dialog(message, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
251 if (all_or_nothing) {
253 //dialog.add_button("Update", 0);
254 dialog.add_button("Import", 1);
255 dialog.add_button("Skip", 2);
257 dialog.add_button("Import", 1);
258 dialog.add_button("Cancel", 2);
261 //dialog.add_button("Skip all", 4); // All or rest?
265 function = dialog.run ();
273 boost::shared_ptr<AudioTrack>
274 Editor::get_nth_selected_audio_track (int nth) const
276 AudioTimeAxisView* atv;
277 TrackSelection::iterator x;
279 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
281 atv = dynamic_cast<AudioTimeAxisView*>(*x);
285 } else if (atv->is_audio_track()) {
290 if (x == selection->tracks.end()) {
291 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
293 atv = dynamic_cast<AudioTimeAxisView*>(*x);
296 if (!atv || !atv->is_audio_track()) {
297 return boost::shared_ptr<AudioTrack>();
300 return atv->audio_track();
303 boost::shared_ptr<MidiTrack>
304 Editor::get_nth_selected_midi_track (int nth) const
306 MidiTimeAxisView* mtv;
307 TrackSelection::iterator x;
309 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
311 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
315 } else if (mtv->is_midi_track()) {
320 if (x == selection->tracks.end()) {
321 mtv = dynamic_cast<MidiTimeAxisView*>(selection->tracks.back());
323 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
326 if (!mtv || !mtv->is_midi_track()) {
327 return boost::shared_ptr<MidiTrack>();
330 return mtv->midi_track();
334 Editor::do_import (vector<string> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, framepos_t& pos)
336 boost::shared_ptr<Track> track;
337 vector<string> to_import;
339 bool use_timestamp = (pos == -1);
341 current_interthread_info = &import_status;
342 import_status.current = 1;
343 import_status.total = paths.size ();
344 import_status.all_done = false;
346 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
350 if (chns == Editing::ImportMergeFiles) {
352 /* create 1 region from all paths, add to 1 track,
357 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
358 int check = check_whether_and_how_to_import(*a, false);
369 ok = (import_sndfiles (paths, mode, quality, pos, 1, 1, track, false) == 0);
374 bool replace = false;
376 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
378 const int check = check_whether_and_how_to_import (*a, true);
385 fatal << "Updating existing sources should be disabled!" << endmsg;
392 fatal << "Illegal return " << check << " from check_whether_and_how_to_import()!" << endmsg;
396 /* have to reset this for every file we handle */
405 case Editing::ImportDistinctFiles:
408 to_import.push_back (*a);
410 if (mode == Editing::ImportToTrack) {
411 track = get_nth_selected_audio_track (nth++);
414 ok = (import_sndfiles (to_import, mode, quality, pos, 1, -1, track, replace) == 0);
417 case Editing::ImportDistinctChannels:
420 to_import.push_back (*a);
422 ok = (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, replace) == 0);
425 case Editing::ImportSerializeFiles:
428 to_import.push_back (*a);
430 ok = (import_sndfiles (to_import, mode, quality, pos, 1, 1, track, replace) == 0);
433 case Editing::ImportMergeFiles:
434 // Not entered, handled in earlier if() branch
441 _session->save_state ("");
444 import_status.all_done = true;
448 Editor::do_embed (vector<string> paths, ImportDisposition chns, ImportMode mode, framepos_t& pos)
450 boost::shared_ptr<Track> track;
451 bool check_sample_rate = true;
453 vector<string> to_embed;
454 bool multi = paths.size() > 1;
456 bool use_timestamp = (pos == -1);
459 case Editing::ImportDistinctFiles:
460 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
462 /* have to reset this for every file we handle */
468 to_embed.push_back (*a);
470 if (mode == Editing::ImportToTrack) {
471 track = get_nth_selected_audio_track (nth++);
474 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
480 case Editing::ImportDistinctChannels:
481 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
483 /* have to reset this for every file we handle */
489 to_embed.push_back (*a);
491 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
497 case Editing::ImportMergeFiles:
498 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
503 case Editing::ImportSerializeFiles:
504 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
506 /* have to reset this for every file we handle */
512 to_embed.push_back (*a);
514 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
525 _session->save_state ("");
530 Editor::import_sndfiles (vector<string> paths, ImportMode mode, SrcQuality quality, framepos_t& pos,
531 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool replace)
533 import_status.paths = paths;
534 import_status.done = false;
535 import_status.cancel = false;
536 import_status.freeze = false;
537 import_status.quality = quality;
538 import_status.replace_existing_source = replace;
540 import_status.mode = mode;
541 import_status.pos = pos;
542 import_status.target_tracks = target_tracks;
543 import_status.target_regions = target_regions;
544 import_status.track = track;
545 import_status.replace = replace;
547 set_canvas_cursor (_cursors->wait);
550 /* start import thread for this spec. this will ultimately call Session::import_files()
551 which, if successful, will add the files as regions to the region list. its up to us
552 (the GUI) to direct additional steps after that.
555 pthread_create_and_store ("import", &import_status.thread, _import_thread, this);
556 pthread_detach (import_status.thread);
558 while (!import_status.done && !import_status.cancel) {
559 gtk_main_iteration ();
562 import_status.done = true;
566 if (!import_status.cancel && !import_status.sources.empty()) {
567 result = add_sources (
569 import_status.sources,
572 import_status.target_regions,
573 import_status.target_tracks,
577 /* update position from results */
579 pos = import_status.pos;
582 set_canvas_cursor (current_canvas_cursor);
587 Editor::embed_sndfiles (vector<string> paths, bool multifile,
588 bool& check_sample_rate, ImportMode mode, framepos_t& pos, int target_regions, int target_tracks,
589 boost::shared_ptr<Track>& track)
591 boost::shared_ptr<AudioFileSource> source;
597 set_canvas_cursor (_cursors->wait);
600 for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
605 /* note that we temporarily truncated _id at the colon */
607 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
608 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
612 if (check_sample_rate && (finfo.samplerate != (int) _session->frame_rate())) {
613 vector<string> choices;
616 choices.push_back (_("Cancel entire import"));
617 choices.push_back (_("Don't embed it"));
618 choices.push_back (_("Embed all without questions"));
620 Gtkmm2ext::Choice rate_choice (
622 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
623 short_path (path, 40)),
627 int resx = rate_choice.run ();
630 case 0: /* stop a multi-file import */
633 case 1: /* don't embed this one */
636 case 2: /* do it, and the rest without asking */
637 check_sample_rate = false;
646 choices.push_back (_("Cancel"));
647 choices.push_back (_("Embed it anyway"));
649 Gtkmm2ext::Choice rate_choice (
651 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
655 int resx = rate_choice.run ();
658 case 0: /* don't import */
670 set_canvas_cursor (_cursors->wait);
672 for (int n = 0; n < finfo.channels; ++n) {
675 /* check if we have this thing embedded already */
677 boost::shared_ptr<Source> s;
679 if ((s = _session->source_by_path_and_channel (path, n)) == 0) {
681 source = boost::dynamic_pointer_cast<AudioFileSource> (
682 SourceFactory::createReadable (DataType::AUDIO, *_session,
684 (mode == ImportAsTapeTrack
685 ? Source::Destructive
689 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
692 sources.push_back(source);
695 catch (failed_constructor& err) {
696 error << string_compose(_("could not open %1"), path) << endmsg;
700 ARDOUR_UI::instance()->flush_pending ();
704 if (sources.empty()) {
708 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
711 set_canvas_cursor (current_canvas_cursor);
716 Editor::add_sources (vector<string> paths, SourceList& sources, framepos_t& pos, ImportMode mode,
717 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool /*add_channel_suffix*/)
719 vector<boost::shared_ptr<Region> > regions;
721 uint32_t input_chan = 0;
722 uint32_t output_chan = 0;
725 use_timestamp = (pos == -1);
727 // kludge (for MIDI we're abusing "channel" for "track" here)
728 if (SMFSource::safe_midi_file_extension (paths.front())) {
732 if (target_regions == 1) {
734 /* take all the sources we have and package them up as a region */
736 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
738 /* we checked in import_sndfiles() that there were not too many */
740 while (RegionFactory::region_by_name (region_name)) {
741 region_name = bump_name_once (region_name, '.');
746 plist.add (ARDOUR::Properties::start, 0);
747 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
748 plist.add (ARDOUR::Properties::name, region_name);
749 plist.add (ARDOUR::Properties::layer, 0);
750 plist.add (ARDOUR::Properties::whole_file, true);
751 plist.add (ARDOUR::Properties::external, true);
753 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
755 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
756 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
759 regions.push_back (r);
762 } else if (target_regions == -1 || target_regions > 1) {
764 /* take each source and create a region for each one */
767 SourceList::iterator x;
770 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
773 just_one.push_back (*x);
775 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (*x);
778 region_name = region_name_from_path (fs->path(), false, false, sources.size(), n);
780 region_name = (*x)->name();
785 /* Fudge region length to ensure it is non-zero; make it 1 beat at 120bpm
786 for want of a better idea. It can't be too small, otherwise if this
787 is a MIDI region the conversion from frames -> beats -> frames will
788 round it back down to 0 again.
790 framecnt_t len = (*x)->length (pos);
792 len = (60.0 / 120.0) * _session->frame_rate ();
795 plist.add (ARDOUR::Properties::start, 0);
796 plist.add (ARDOUR::Properties::length, len);
797 plist.add (ARDOUR::Properties::name, region_name);
798 plist.add (ARDOUR::Properties::layer, 0);
799 plist.add (ARDOUR::Properties::whole_file, true);
800 plist.add (ARDOUR::Properties::external, true);
802 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
804 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
805 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
808 regions.push_back (r);
812 if (target_regions == 1) {
813 input_chan = regions.front()->n_channels();
815 if (target_tracks == 1) {
816 input_chan = regions.size();
822 if (Config->get_output_auto_connect() & AutoConnectMaster) {
823 output_chan = (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan);
825 output_chan = input_chan;
831 begin_reversible_command (Operations::insert_file);
833 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
834 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
839 /* get timestamp for this region */
841 const boost::shared_ptr<Source> s (ar->sources().front());
842 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
846 if (as->natural_position() != 0) {
847 pos = as->natural_position();
848 } else if (target_tracks == 1) {
849 /* hmm, no timestamp available, put it after the previous region
852 pos = get_preferred_edit_position ();
857 pos = get_preferred_edit_position ();
860 /* should really get first position in MIDI file, but for now, use edit position*/
861 pos = get_preferred_edit_position ();
866 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
868 rlen = (*r)->length();
870 if (target_tracks != 1) {
873 if (!use_timestamp || !ar) {
874 /* line each one up right after the other */
875 pos += (*r)->length();
880 commit_reversible_command ();
882 /* setup peak file building in another thread */
884 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
885 SourceFactory::setup_peakfile (*x, true);
892 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, framepos_t& pos,
893 ImportMode mode, boost::shared_ptr<Track>& existing_track)
895 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
896 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
900 /* relax, its been done */
905 if (!existing_track) {
908 existing_track = get_nth_selected_audio_track (0);
910 existing_track = get_nth_selected_midi_track (0);
913 if (!existing_track) {
918 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
919 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
920 playlist->clear_changes ();
921 playlist->add_region (copy, pos);
922 _session->add_command (new StatefulDiffCommand (playlist));
928 if (!existing_track) {
930 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
936 existing_track = at.front();
938 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (ChanCount (DataType::MIDI, 1),
939 ChanCount (DataType::MIDI, 1),
940 boost::shared_ptr<PluginInfo>(),
947 existing_track = mt.front();
950 existing_track->set_name (region->name());
953 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
954 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
955 playlist->clear_changes ();
956 playlist->add_region (copy, pos);
957 _session->add_command (new StatefulDiffCommand (playlist));
961 case ImportAsTapeTrack:
967 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Destructive));
969 boost::shared_ptr<Playlist> playlist = at.front()->playlist();
970 boost::shared_ptr<Region> copy (RegionFactory::create (region, true));
971 playlist->clear_changes ();
972 playlist->add_region (copy, pos);
973 _session->add_command (new StatefulDiffCommand (playlist));
983 Editor::_import_thread (void *arg)
985 SessionEvent::create_per_thread_pool ("import events", 64);
987 Editor *ed = (Editor *) arg;
988 return ed->import_thread ();
992 Editor::import_thread ()
994 _session->import_files (import_status);
995 pthread_exit_pbd (0);