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/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"
58 #include "session_import_dialog.h"
60 #include "gui_thread.h"
65 using namespace ARDOUR;
68 using namespace Gtkmm2ext;
69 using namespace Editing;
72 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
75 Editor::add_external_audio_action (ImportMode mode_hint)
78 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
84 sfbrowser = new SoundFileOmega (*this, _("Add Existing Media"), _session, 0, true, mode_hint);
86 sfbrowser->set_mode (mode_hint);
89 external_audio_dialog ();
93 Editor::external_audio_dialog ()
95 vector<Glib::ustring> paths;
99 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
106 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
107 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
111 } else if (atv->is_audio_track()) {
116 if (sfbrowser == 0) {
117 sfbrowser = new SoundFileOmega (*this, _("Add Existing Media"), _session, track_cnt, true);
119 sfbrowser->reset (track_cnt);
122 sfbrowser->show_all ();
130 int response = sfbrowser->run ();
134 // leave the dialog open
142 // cancel from the browser - we are done
149 paths = sfbrowser->get_paths ();
151 ImportPosition pos = sfbrowser->get_position ();
152 ImportMode mode = sfbrowser->get_mode ();
153 ImportDisposition chns = sfbrowser->get_channel_disposition ();
157 case ImportAtEditPoint:
158 where = get_preferred_edit_position ();
160 case ImportAtTimestamp:
163 case ImportAtPlayhead:
164 where = playhead_cursor->current_frame;
167 where = _session->current_start_frame();
171 SrcQuality quality = sfbrowser->get_src_quality();
174 if (sfbrowser->copy_files_btn.get_active()) {
175 do_import (paths, chns, mode, quality, where);
177 do_embed (paths, chns, mode, where);
180 if (response == RESPONSE_APPLY) {
181 sfbrowser->clear_selection ();
185 } while (keepRunning);
189 Editor::session_import_dialog ()
191 SessionImportDialog dialog (_session);
192 ensure_float (dialog);
196 typedef std::map<PBD::ID,boost::shared_ptr<ARDOUR::Source> > SourceMap;
199 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_audiofiles()
202 * true = show "Update", "Import" and "Skip"
203 * false = show "Import", and "Cancel"
206 * 0 To update an existing source of the same name
207 * 1 To import/embed the file normally (make sure the new name will be unique)
208 * 2 If the user wants to skip this file
211 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
213 string wave_name (Glib::path_get_basename(path));
215 SourceMap all_sources = _session->get_sources();
216 bool wave_name_exists = false;
218 for (SourceMap::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
219 string tmp (Glib::path_get_basename (i->second->path()));
220 if (tmp == wave_name) {
221 wave_name_exists = true;
228 if (wave_name_exists) {
230 if (all_or_nothing) {
231 // updating is still disabled
232 //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);
233 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);
235 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);
238 MessageDialog dialog(message, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
240 if (all_or_nothing) {
242 //dialog.add_button("Update", 0);
243 dialog.add_button("Import", 1);
244 dialog.add_button("Skip", 2);
246 dialog.add_button("Import", 1);
247 dialog.add_button("Cancel", 2);
250 //dialog.add_button("Skip all", 4); // All or rest?
254 function = dialog.run ();
262 boost::shared_ptr<AudioTrack>
263 Editor::get_nth_selected_audio_track (int nth) const
265 AudioTimeAxisView* atv;
266 TrackSelection::iterator x;
268 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
270 atv = dynamic_cast<AudioTimeAxisView*>(*x);
274 } else if (atv->is_audio_track()) {
279 if (x == selection->tracks.end()) {
280 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
282 atv = dynamic_cast<AudioTimeAxisView*>(*x);
285 if (!atv || !atv->is_audio_track()) {
286 return boost::shared_ptr<AudioTrack>();
289 return atv->audio_track();
292 boost::shared_ptr<MidiTrack>
293 Editor::get_nth_selected_midi_track (int nth) const
295 MidiTimeAxisView* mtv;
296 TrackSelection::iterator x;
298 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
300 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
304 } else if (mtv->is_midi_track()) {
309 if (x == selection->tracks.end()) {
310 mtv = dynamic_cast<MidiTimeAxisView*>(selection->tracks.back());
312 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
315 if (!mtv || !mtv->is_midi_track()) {
316 return boost::shared_ptr<MidiTrack>();
319 return mtv->midi_track();
323 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
325 boost::shared_ptr<Track> track;
326 vector<ustring> to_import;
329 if (interthread_progress_window == 0) {
330 build_interthread_progress_window ();
333 if (chns == Editing::ImportMergeFiles) {
335 /* create 1 region from all paths, add to 1 track,
340 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
341 int check = check_whether_and_how_to_import(*a, false);
349 import_sndfiles (paths, mode, quality, pos, 1, 1, track, false, paths.size());
354 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
356 int const check = check_whether_and_how_to_import (*a, true);
359 to_import.push_back (*a);
366 case Editing::ImportDistinctFiles:
368 if (mode == Editing::ImportToTrack) {
369 track = get_nth_selected_audio_track (nth++);
372 ok = (import_sndfiles (to_import, mode, quality, pos, 1, -1, track, false, to_import.size()) == 0);
375 case Editing::ImportDistinctChannels:
376 ok = (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, false, to_import.size()) == 0);
379 case Editing::ImportSerializeFiles:
380 ok = (import_sndfiles (to_import, mode, quality, pos, 1, 1, track, false, to_import.size()) == 0);
383 case Editing::ImportMergeFiles:
384 // Not entered, handled in earlier if() branch
389 interthread_progress_window->hide_all ();
393 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
395 boost::shared_ptr<Track> track;
396 bool check_sample_rate = true;
398 vector<ustring> to_embed;
399 bool multi = paths.size() > 1;
403 case Editing::ImportDistinctFiles:
404 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
407 to_embed.push_back (*a);
409 if (mode == Editing::ImportToTrack) {
410 track = get_nth_selected_audio_track (nth++);
413 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
419 case Editing::ImportDistinctChannels:
420 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
423 to_embed.push_back (*a);
425 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
431 case Editing::ImportMergeFiles:
432 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
437 case Editing::ImportSerializeFiles:
438 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
441 to_embed.push_back (*a);
443 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
454 _session->save_state ("");
459 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos,
460 int target_regions, int target_tracks, boost::shared_ptr<Track> track, bool replace, uint32_t total)
462 interthread_progress_window->set_title (string_compose (_("Importing %1"), paths.front()));
463 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
464 interthread_progress_bar.set_fraction (0.0f);
465 interthread_cancel_label.set_text (_("Cancel Import"));
466 current_interthread_info = &import_status;
468 import_status.paths = paths;
469 import_status.done = false;
470 import_status.cancel = false;
471 import_status.freeze = false;
472 import_status.done = 0.0;
473 import_status.quality = quality;
474 import_status.replace_existing_source = replace;
475 import_status.total = total;
477 import_status.mode = mode;
478 import_status.pos = pos;
479 import_status.target_tracks = target_tracks;
480 import_status.target_regions = target_regions;
481 import_status.track = track;
482 import_status.replace = replace;
483 interthread_progress_connection = Glib::signal_timeout().connect
484 (sigc::bind (sigc::mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 500);
486 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
489 /* start import thread for this spec. this will ultimately call Session::import_audiofiles()
490 which, if successful, will add the files as regions to the region list. its up to us
491 (the GUI) to direct additional steps after that.
494 pthread_create_and_store ("import", &import_status.thread, _import_thread, this);
495 pthread_detach (import_status.thread);
497 while (!import_status.done && !import_status.cancel) {
498 gtk_main_iteration ();
501 interthread_progress_window->hide ();
502 import_status.done = true;
503 interthread_progress_connection.disconnect ();
505 if (!import_status.cancel && !import_status.sources.empty()) {
506 if (add_sources (import_status.paths,
507 import_status.sources,
510 import_status.target_regions,
511 import_status.target_tracks,
512 import_status.track, false) == 0) {
513 _session->save_state ("");
516 /* update position from results */
518 pos = import_status.pos;
522 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
527 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
528 bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
529 boost::shared_ptr<Track>& track)
531 boost::shared_ptr<AudioFileSource> source;
536 Glib::ustring path_to_use;
538 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
541 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
545 if (Config->get_try_link_for_embed()) {
547 /* lets see if we can link it into the session */
549 sys::path tmp = _session->session_directory().sound_path() / Glib::path_get_basename(path);
550 linked_path = tmp.to_string();
552 path_to_use = linked_path;
554 if (link (path.c_str(), linked_path.c_str()) == 0) {
556 /* there are many reasons why link(2) might have failed.
557 but if it succeeds, we now have a link in the
558 session sound dir that will protect against
559 unlinking of the original path. nice.
563 path_to_use = Glib::path_get_basename (path);
567 /* one possible reason is that its already linked */
569 if (errno == EEXIST) {
572 if (stat (linked_path.c_str(), &sb) == 0) {
573 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
575 path_to_use = Glib::path_get_basename (path);
582 /* note that we temporarily truncated _id at the colon */
586 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
587 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
591 if (check_sample_rate && (finfo.samplerate != (int) _session->frame_rate())) {
592 vector<string> choices;
595 choices.push_back (_("Cancel entire import"));
596 choices.push_back (_("Don't embed it"));
597 choices.push_back (_("Embed all without questions"));
599 Gtkmm2ext::Choice rate_choice (
601 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
602 short_path (path, 40)),
606 int resx = rate_choice.run ();
609 case 0: /* stop a multi-file import */
612 case 1: /* don't embed this one */
615 case 2: /* do it, and the rest without asking */
616 check_sample_rate = false;
625 choices.push_back (_("Cancel"));
626 choices.push_back (_("Embed it anyway"));
628 Gtkmm2ext::Choice rate_choice (
630 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
634 int resx = rate_choice.run ();
637 case 0: /* don't import */
649 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
651 for (int n = 0; n < finfo.channels; ++n) {
654 /* check if we have this thing embedded already */
656 boost::shared_ptr<Source> s;
658 if ((s = _session->source_by_path_and_channel (path, n)) == 0) {
660 source = boost::dynamic_pointer_cast<AudioFileSource> (
661 SourceFactory::createReadable (DataType::AUDIO, *_session,
663 (mode == ImportAsTapeTrack
664 ? Source::Destructive
668 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
671 sources.push_back(source);
674 catch (failed_constructor& err) {
675 error << string_compose(_("could not open %1"), path) << endmsg;
679 ARDOUR_UI::instance()->flush_pending ();
683 if (sources.empty()) {
687 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
690 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
695 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode,
696 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool /*add_channel_suffix*/)
698 vector<boost::shared_ptr<Region> > regions;
700 uint32_t input_chan = 0;
701 uint32_t output_chan = 0;
704 use_timestamp = (pos == -1);
707 if (sources[0]->natural_position() != 0) {
708 pos = sources[0]->natural_position();
710 pos = get_preferred_edit_position ();
714 // kludge (for MIDI we're abusing "channel" for "track" here)
715 if (paths.front().rfind(".mid") != Glib::ustring::npos)
718 if (target_regions == 1) {
720 /* take all the sources we have and package them up as a region */
722 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
726 plist.add (ARDOUR::Properties::start, 0);
727 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
728 plist.add (ARDOUR::Properties::name, region_name);
729 plist.add (ARDOUR::Properties::layer, 0);
730 plist.add (ARDOUR::Properties::whole_file, true);
731 plist.add (ARDOUR::Properties::external, true);
733 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
735 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
736 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
739 regions.push_back (r);
742 } else if (target_regions == -1 || target_regions > 1) {
744 /* take each source and create a region for each one */
747 SourceList::iterator x;
750 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
753 just_one.push_back (*x);
755 region_name = region_name_from_path ((*x)->path(), false, false, sources.size(), n);
759 plist.add (ARDOUR::Properties::start, 0);
760 plist.add (ARDOUR::Properties::length, (*x)->length (pos));
761 plist.add (ARDOUR::Properties::name, region_name);
762 plist.add (ARDOUR::Properties::layer, 0);
763 plist.add (ARDOUR::Properties::whole_file, true);
764 plist.add (ARDOUR::Properties::external, true);
766 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
768 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
769 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
772 regions.push_back (r);
776 if (target_regions == 1) {
777 input_chan = regions.front()->n_channels();
779 if (target_tracks == 1) {
780 input_chan = regions.size();
786 if (Config->get_output_auto_connect() & AutoConnectMaster) {
787 output_chan = (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan);
789 output_chan = input_chan;
794 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
796 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
798 if (target_tracks != 1) {
801 pos += (*r)->length();
805 /* setup peak file building in another thread */
807 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
808 SourceFactory::setup_peakfile (*x, true);
815 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos,
816 ImportMode mode, boost::shared_ptr<Track>& existing_track)
818 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
819 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
823 /* relax, its been done */
828 if (!existing_track) {
831 existing_track = get_nth_selected_audio_track (0);
833 existing_track = get_nth_selected_midi_track (0);
836 if (!existing_track) {
841 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
842 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
843 begin_reversible_command (_("insert file"));
844 playlist->clear_history ();
845 playlist->add_region (copy, pos);
846 _session->add_command (new StatefulDiffCommand (playlist));
847 commit_reversible_command ();
853 if (!existing_track) {
855 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
861 existing_track = at.front();
863 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (Normal, 0, 1));
869 existing_track = mt.front();
872 existing_track->set_name (region->name());
875 boost::shared_ptr<Region> copy (RegionFactory::create (region));
876 existing_track->diskstream()->playlist()->add_region (copy, pos);
881 case ImportAsTapeTrack:
886 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Destructive));
888 boost::shared_ptr<Region> copy (RegionFactory::create (region));
889 at.front()->set_name (basename_nosuffix (copy->name()));
890 at.front()->diskstream()->playlist()->add_region (copy, pos);
900 Editor::_import_thread (void *arg)
902 SessionEvent::create_per_thread_pool ("import events", 64);
904 Editor *ed = (Editor *) arg;
905 return ed->import_thread ();
909 Editor::import_thread ()
911 _session->import_audiofiles (import_status);
912 pthread_exit_pbd (0);
918 Editor::import_progress_timeout (void */*arg*/)
922 if (!interthread_progress_window->is_visible()) {
923 interthread_progress_window->show_all ();
927 interthread_progress_label.set_text (import_status.doing_what);
929 if (import_status.freeze) {
930 interthread_cancel_button.set_sensitive(false);
932 interthread_cancel_button.set_sensitive(true);
935 if (import_status.doing_what == "building peak files") {
936 interthread_progress_bar.pulse ();
939 float val = import_status.progress;
940 interthread_progress_bar.set_fraction (min (max (0.0f, val), 1.0f));
945 /* the window is now visible, speed up the updates */
947 interthread_progress_connection.disconnect ();
948 interthread_progress_connection = Glib::signal_timeout().connect
949 (sigc::bind (sigc::mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
952 return !(import_status.done || import_status.cancel);