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>
33 #include <gtkmm2ext/choice.h>
34 #include <gtkmm2ext/window_title.h>
36 #include <ardour/session.h>
37 #include <ardour/session_directory.h>
38 #include <ardour/audioplaylist.h>
39 #include <ardour/audioregion.h>
40 #include <ardour/audio_diskstream.h>
41 #include <ardour/midi_track.h>
42 #include <ardour/midi_region.h>
43 #include <ardour/utils.h>
44 #include <ardour/audio_track.h>
45 #include <ardour/audioplaylist.h>
46 #include <ardour/audiofilesource.h>
47 #include <ardour/region_factory.h>
48 #include <ardour/source_factory.h>
49 #include <ardour/session.h>
50 #include <pbd/memento_command.h>
52 #include "ardour_ui.h"
56 #include "audio_time_axis.h"
57 #include "midi_time_axis.h"
63 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 (0, _("You can't import or embed a file 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 ()
94 vector<Glib::ustring> paths;
98 MessageDialog msg (0, _("You can't import or embed a file until you have a session loaded."));
105 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
106 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
110 } else if (atv->is_audio_track()) {
115 if (sfbrowser == 0) {
116 sfbrowser = new SoundFileOmega (*this, _("Add existing media"), session, track_cnt, true);
118 sfbrowser->reset (track_cnt);
121 sfbrowser->show_all ();
129 int response = sfbrowser->run ();
133 // leave the dialog open
141 // cancel from the browser - we are done
148 paths = sfbrowser->get_paths ();
150 ImportPosition pos = sfbrowser->get_position ();
151 ImportMode mode = sfbrowser->get_mode ();
152 ImportDisposition chns = sfbrowser->get_channel_disposition ();
156 case ImportAtEditPoint:
157 where = get_preferred_edit_position ();
159 case ImportAtTimestamp:
162 case ImportAtPlayhead:
163 where = playhead_cursor->current_frame;
166 where = session->current_start_frame();
170 SrcQuality quality = sfbrowser->get_src_quality();
173 if (sfbrowser->copy_files_btn.get_active()) {
174 do_import (paths, chns, mode, quality, where);
176 do_embed (paths, chns, mode, where);
179 if (response == RESPONSE_APPLY) {
180 sfbrowser->clear_selection ();
184 } while (keepRunning);
187 typedef std::map<PBD::ID,boost::shared_ptr<ARDOUR::Source> > SourceMap;
190 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_audiofiles()
193 * true = show "Update", "Import" and "Skip"
194 * false = show "Import", and "Cancel"
197 * 0 To update an existing source of the same name
198 * 1 To import/embed the file normally (make sure the new name will be unique)
199 * 2 If the user wants to skip this file
202 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
204 string wave_name (Glib::path_get_basename(path));
206 SourceMap all_sources = session->get_sources();
207 bool wave_name_exists = false;
209 for (SourceMap::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
210 string tmp (Glib::path_get_basename (i->second->path()));
212 if (tmp == wave_name) {
213 wave_name_exists = true;
221 if (wave_name_exists) {
223 if (all_or_nothing) {
224 // updating is still disabled
225 //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);
226 message = string_compose(_("The session already contains a source file named %1. This file will be imported as a new file, please confirm."),wave_name);
228 message = _("Lorem ipsum. Do you want to skidaddle?");
231 MessageDialog dialog(message, false,Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
233 if (all_or_nothing) {
235 //dialog.add_button("Update", 0);
236 dialog.add_button("Import", 1);
237 dialog.add_button("Skip", 2);
239 dialog.add_button("Import", 1);
240 dialog.add_button("Cancel", 2);
244 //dialog.add_button("Skip all", 4); // All or rest?
248 function = dialog.run ();
256 boost::shared_ptr<AudioTrack>
257 Editor::get_nth_selected_audio_track (int nth) const
259 AudioTimeAxisView* atv;
260 TrackSelection::iterator x;
262 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
264 atv = dynamic_cast<AudioTimeAxisView*>(*x);
268 } else if (atv->is_audio_track()) {
273 if (x == selection->tracks.end()) {
274 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
276 atv = dynamic_cast<AudioTimeAxisView*>(*x);
279 if (!atv || !atv->is_audio_track()) {
280 return boost::shared_ptr<AudioTrack>();
283 return atv->audio_track();
286 boost::shared_ptr<MidiTrack>
287 Editor::get_nth_selected_midi_track (int nth) const
289 MidiTimeAxisView* mtv;
290 TrackSelection::iterator x;
292 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
294 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
298 } else if (mtv->is_midi_track()) {
303 if (x == selection->tracks.end()) {
304 mtv = dynamic_cast<MidiTimeAxisView*>(selection->tracks.back());
306 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
309 if (!mtv || !mtv->is_midi_track()) {
310 return boost::shared_ptr<MidiTrack>();
313 return mtv->midi_track();
317 Editor::idle_do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
319 _do_import (paths, chns, mode, quality, pos);
324 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
327 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_import), paths, chns, mode, quality, pos));
329 _do_import (paths, chns, mode, quality, pos);
334 Editor::_do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
336 boost::shared_ptr<Track> track;
337 vector<ustring> to_import;
341 if (interthread_progress_window == 0) {
342 build_interthread_progress_window ();
345 if (chns == Editing::ImportMergeFiles) {
346 /* create 1 region from all paths, add to 1 track,
350 for (vector<ustring>::iterator a = paths.begin(); a != paths.end() && ok; ++a) {
351 int check = check_whether_and_how_to_import(*a, false);
359 if (import_sndfiles (paths, mode, quality, pos, 1, 1, track, false)) {
365 bool replace = false;
367 for (vector<ustring>::iterator a = paths.begin(); a != paths.end() && ok; ++a) {
369 int check = check_whether_and_how_to_import(*a, true);
377 fatal << "Updating existing sources should be disabled!" << endl;
379 } else if (check == 1) {
385 case Editing::ImportDistinctFiles:
388 to_import.push_back (*a);
390 if (mode == Editing::ImportToTrack) {
391 track = get_nth_selected_audio_track (nth++);
394 if (import_sndfiles (to_import, mode, quality, pos, 1, -1, track, replace)) {
400 case Editing::ImportDistinctChannels:
403 to_import.push_back (*a);
405 if (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, replace)) {
411 case Editing::ImportSerializeFiles:
414 to_import.push_back (*a);
416 /* create 1 region from this path, add to 1 track,
417 reuse "track" across paths
420 if (import_sndfiles (to_import, mode, quality, pos, 1, 1, track, replace)) {
426 case Editing::ImportMergeFiles:
433 session->save_state ("");
436 interthread_progress_window->hide_all ();
441 Editor::idle_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
443 _do_embed (paths, chns, mode, pos);
448 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
451 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, chns, mode, pos));
453 _do_embed (paths, chns, mode, pos);
458 Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
460 boost::shared_ptr<Track> track;
461 bool check_sample_rate = true;
463 vector<ustring> to_embed;
464 bool multi = paths.size() > 1;
468 case Editing::ImportDistinctFiles:
469 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
472 to_embed.push_back (*a);
474 if (mode == Editing::ImportToTrack) {
475 track = get_nth_selected_audio_track (nth++);
478 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
484 case Editing::ImportDistinctChannels:
485 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
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<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
506 to_embed.push_back (*a);
508 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
519 session->save_state ("");
524 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos,
525 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool replace)
527 WindowTitle title = string_compose (_("importing %1"), paths.front());
529 interthread_progress_window->set_title (title.get_string());
530 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
531 interthread_progress_bar.set_fraction (0.0f);
532 interthread_cancel_label.set_text (_("Cancel Import"));
533 current_interthread_info = &import_status;
535 import_status.paths = paths;
536 import_status.done = false;
537 import_status.cancel = false;
538 import_status.freeze = false;
539 import_status.done = 0.0;
540 import_status.quality = quality;
541 import_status.replace_existing_source = replace;
543 interthread_progress_connection = Glib::signal_timeout().connect
544 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 500);
546 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
547 ARDOUR_UI::instance()->flush_pending ();
549 /* start import thread for this spec. this will ultimately call Session::import_audiofiles()
550 and if successful will add the file(s) as a region to the session region list.
553 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
554 pthread_detach (import_status.thread);
556 while (!(import_status.done || import_status.cancel)) {
557 gtk_main_iteration ();
560 interthread_progress_window->hide ();
562 import_status.done = true;
563 interthread_progress_connection.disconnect ();
565 /* import thread finished - see if we should build a new track */
567 boost::shared_ptr<AudioRegion> r;
569 if (import_status.cancel || import_status.sources.empty()) {
573 if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track, false) == 0) {
574 session->save_state ("");
578 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
583 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
584 bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
585 boost::shared_ptr<Track>& track)
587 boost::shared_ptr<AudioFileSource> source;
593 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
594 ARDOUR_UI::instance()->flush_pending ();
596 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
600 /* lets see if we can link it into the session */
602 sys::path tmp = session->session_directory().sound_path() / Glib::path_get_basename(path);
603 linked_path = tmp.to_string();
605 if (link (path.c_str(), linked_path.c_str()) == 0) {
607 /* there are many reasons why link(2) might have failed.
608 but if it succeeds, we now have a link in the
609 session sound dir that will protect against
610 unlinking of the original path. nice.
617 /* one possible reason is that its already linked */
619 if (errno == EEXIST) {
622 if (stat (linked_path.c_str(), &sb) == 0) {
623 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
630 /* note that we temporarily truncated _id at the colon */
634 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
635 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
639 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
640 vector<string> choices;
643 choices.push_back (_("Cancel entire import"));
644 choices.push_back (_("Don't embed it"));
645 choices.push_back (_("Embed all without questions"));
647 Gtkmm2ext::Choice rate_choice (
648 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
649 short_path (path, 40)),
652 int resx = rate_choice.run ();
655 case 0: /* stop a multi-file import */
658 case 1: /* don't embed this one */
661 case 2: /* do it, and the rest without asking */
662 check_sample_rate = false;
671 choices.push_back (_("Cancel"));
672 choices.push_back (_("Embed it anyway"));
674 Gtkmm2ext::Choice rate_choice (
675 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
678 int resx = rate_choice.run ();
681 case 0: /* don't import */
693 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
695 for (int n = 0; n < finfo.channels; ++n) {
698 /* check if we have this thing embedded already */
700 boost::shared_ptr<Source> s;
702 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
704 cerr << "add embed/import source with defer_peaks = true\n";
706 source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable
707 (DataType::AUDIO, *session, path, n,
708 (mode == ImportAsTapeTrack ?
709 AudioFileSource::Destructive :
710 AudioFileSource::Flag (0)),
713 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
716 sources.push_back(source);
719 catch (failed_constructor& err) {
720 error << string_compose(_("could not open %1"), path) << endmsg;
724 ARDOUR_UI::instance()->flush_pending ();
728 if (sources.empty()) {
732 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
735 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
740 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode,
741 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool add_channel_suffix)
743 vector<boost::shared_ptr<Region> > regions;
745 uint32_t input_chan = 0;
746 uint32_t output_chan = 0;
748 if (pos == -1) { // "use timestamp"
749 if (sources[0]->natural_position() != 0) {
750 pos = sources[0]->natural_position();
752 pos = get_preferred_edit_position ();
756 // kludge (for MIDI we're abusing "channel" for "track" here)
757 if (paths.front().rfind(".mid") != Glib::ustring::npos)
760 if (target_regions == 1) {
762 /* take all the sources we have and package them up as a region */
764 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
766 regions.push_back (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
767 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External)));
769 } else if (target_regions == -1) {
771 /* take each source and create a region for each one */
774 SourceList::iterator x;
777 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
780 just_one.push_back (*x);
782 region_name = region_name_from_path ((*x)->path(), false, true, sources.size(), n);
784 cout << "REGION NAME: " << region_name << endl;
785 cout << "SOURCE LENGTH: " << (*x)->length() << endl;
787 regions.push_back (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
788 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External)));
793 if (target_regions == 1) {
794 input_chan = regions.front()->n_channels();
796 if (target_tracks == 1) {
797 input_chan = regions.size();
803 if (Config->get_output_auto_connect() & AutoConnectMaster) {
804 output_chan = (session->master_out() ? session->master_out()->n_inputs().n_audio() : input_chan);
806 output_chan = input_chan;
811 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
813 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
815 if (target_tracks != 1) {
818 pos += (*r)->length();
822 /* setup peak file building in another thread */
824 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
825 SourceFactory::setup_peakfile (*x, true);
832 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos,
833 ImportMode mode, boost::shared_ptr<Track>& existing_track)
835 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
836 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
840 /* relax, its been done */
845 if (!existing_track) {
848 existing_track = get_nth_selected_audio_track (0);
850 existing_track = get_nth_selected_midi_track (0);
853 if (!existing_track) {
858 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
859 boost::shared_ptr<Region> copy (RegionFactory::create (region));
860 begin_reversible_command (_("insert file"));
861 XMLNode &before = playlist->get_state();
862 playlist->add_region (copy, pos);
863 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
864 commit_reversible_command ();
870 if (!existing_track) {
872 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
878 existing_track = at.front();
880 list<boost::shared_ptr<MidiTrack> > mt (session->new_midi_track (Normal, 1));
886 existing_track = mt.front();
889 existing_track->set_name (region->name());
892 boost::shared_ptr<Region> copy (RegionFactory::create (region));
893 existing_track->diskstream()->playlist()->add_region (copy, pos);
898 case ImportAsTapeTrack:
903 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
905 boost::shared_ptr<Region> copy (RegionFactory::create (region));
906 at.front()->set_name (basename_nosuffix (copy->name()));
907 at.front()->diskstream()->playlist()->add_region (copy, pos);
917 Editor::_import_thread (void *arg)
919 PBD::ThreadCreated (pthread_self(), X_("Import"));
921 Editor *ed = (Editor *) arg;
922 return ed->import_thread ();
926 Editor::import_thread ()
928 session->import_audiofiles (import_status);
929 pthread_exit_pbd (0);
935 Editor::import_progress_timeout (void *arg)
939 if (!interthread_progress_window->is_visible()) {
940 interthread_progress_window->show_all ();
944 interthread_progress_label.set_text (import_status.doing_what);
946 if (import_status.freeze) {
947 interthread_cancel_button.set_sensitive(false);
949 interthread_cancel_button.set_sensitive(true);
952 if (import_status.doing_what == "building peak files") {
953 interthread_progress_bar.pulse ();
956 float val = import_status.progress;
957 interthread_progress_bar.set_fraction (min (max (0.0f, val), 1.0f));
962 /* the window is now visible, speed up the updates */
964 interthread_progress_connection.disconnect ();
965 interthread_progress_connection = Glib::signal_timeout().connect
966 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
969 return !(import_status.done || import_status.cancel);