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"
58 #include "session_import_dialog.h"
60 #include "gui_thread.h"
65 using namespace ARDOUR;
69 using namespace Gtkmm2ext;
70 using namespace Editing;
73 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
76 Editor::add_external_audio_action (ImportMode mode_hint)
79 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
85 sfbrowser = new SoundFileOmega (*this, _("Add existing media"), session, 0, true, mode_hint);
87 sfbrowser->set_mode (mode_hint);
90 external_audio_dialog ();
94 Editor::external_audio_dialog ()
96 vector<Glib::ustring> paths;
100 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);
112 } else if (atv->is_audio_track()) {
117 if (sfbrowser == 0) {
118 sfbrowser = new SoundFileOmega (*this, _("Add existing media"), session, track_cnt, true);
120 sfbrowser->reset (track_cnt);
123 sfbrowser->show_all ();
131 int response = sfbrowser->run ();
135 // leave the dialog open
143 // cancel from the browser - we are done
150 paths = sfbrowser->get_paths ();
152 ImportPosition pos = sfbrowser->get_position ();
153 ImportMode mode = sfbrowser->get_mode ();
154 ImportDisposition chns = sfbrowser->get_channel_disposition ();
158 case ImportAtEditPoint:
159 where = get_preferred_edit_position ();
161 case ImportAtTimestamp:
164 case ImportAtPlayhead:
165 where = playhead_cursor->current_frame;
168 where = session->current_start_frame();
172 SrcQuality quality = sfbrowser->get_src_quality();
175 if (sfbrowser->copy_files_btn.get_active()) {
176 do_import (paths, chns, mode, quality, where);
178 do_embed (paths, chns, mode, where);
181 if (response == RESPONSE_APPLY) {
182 sfbrowser->clear_selection ();
186 } while (keepRunning);
190 Editor::session_import_dialog ()
192 SessionImportDialog dialog (*session);
193 ensure_float (dialog);
197 typedef std::map<PBD::ID,boost::shared_ptr<ARDOUR::Source> > SourceMap;
200 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_audiofiles()
203 * true = show "Update", "Import" and "Skip"
204 * false = show "Import", and "Cancel"
207 * 0 To update an existing source of the same name
208 * 1 To import/embed the file normally (make sure the new name will be unique)
209 * 2 If the user wants to skip this file
212 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
214 string wave_name (Glib::path_get_basename(path));
216 SourceMap all_sources = session->get_sources();
217 bool wave_name_exists = false;
219 for (SourceMap::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
220 string tmp (Glib::path_get_basename (i->second->path()));
221 if (tmp == wave_name) {
222 wave_name_exists = true;
229 if (wave_name_exists) {
231 if (all_or_nothing) {
232 // updating is still disabled
233 //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);
234 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);
236 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);
239 MessageDialog dialog(message, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
241 if (all_or_nothing) {
243 //dialog.add_button("Update", 0);
244 dialog.add_button("Import", 1);
245 dialog.add_button("Skip", 2);
247 dialog.add_button("Import", 1);
248 dialog.add_button("Cancel", 2);
251 //dialog.add_button("Skip all", 4); // All or rest?
255 function = dialog.run ();
263 boost::shared_ptr<AudioTrack>
264 Editor::get_nth_selected_audio_track (int nth) const
266 AudioTimeAxisView* atv;
267 TrackSelection::iterator x;
269 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
271 atv = dynamic_cast<AudioTimeAxisView*>(*x);
275 } else if (atv->is_audio_track()) {
280 if (x == selection->tracks.end()) {
281 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
283 atv = dynamic_cast<AudioTimeAxisView*>(*x);
286 if (!atv || !atv->is_audio_track()) {
287 return boost::shared_ptr<AudioTrack>();
290 return atv->audio_track();
293 boost::shared_ptr<MidiTrack>
294 Editor::get_nth_selected_midi_track (int nth) const
296 MidiTimeAxisView* mtv;
297 TrackSelection::iterator x;
299 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
301 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
305 } else if (mtv->is_midi_track()) {
310 if (x == selection->tracks.end()) {
311 mtv = dynamic_cast<MidiTimeAxisView*>(selection->tracks.back());
313 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
316 if (!mtv || !mtv->is_midi_track()) {
317 return boost::shared_ptr<MidiTrack>();
320 return mtv->midi_track();
324 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
326 boost::shared_ptr<Track> track;
327 vector<ustring> to_import;
330 if (interthread_progress_window == 0) {
331 build_interthread_progress_window ();
334 if (chns == Editing::ImportMergeFiles) {
336 /* create 1 region from all paths, add to 1 track,
341 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
342 int check = check_whether_and_how_to_import(*a, false);
350 import_sndfiles (paths, mode, quality, pos, 1, 1, track, false, paths.size());
355 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
357 int const check = check_whether_and_how_to_import (*a, true);
360 to_import.push_back (*a);
367 case Editing::ImportDistinctFiles:
369 if (mode == Editing::ImportToTrack) {
370 track = get_nth_selected_audio_track (nth++);
373 ok = (import_sndfiles (to_import, mode, quality, pos, 1, -1, track, false, to_import.size()) == 0);
376 case Editing::ImportDistinctChannels:
377 ok = (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, false, to_import.size()) == 0);
380 case Editing::ImportSerializeFiles:
381 ok = (import_sndfiles (to_import, mode, quality, pos, 1, 1, track, false, to_import.size()) == 0);
384 case Editing::ImportMergeFiles:
385 // Not entered, handled in earlier if() branch
390 interthread_progress_window->hide_all ();
394 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
396 boost::shared_ptr<Track> track;
397 bool check_sample_rate = true;
399 vector<ustring> to_embed;
400 bool multi = paths.size() > 1;
404 case Editing::ImportDistinctFiles:
405 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
408 to_embed.push_back (*a);
410 if (mode == Editing::ImportToTrack) {
411 track = get_nth_selected_audio_track (nth++);
414 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
420 case Editing::ImportDistinctChannels:
421 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
424 to_embed.push_back (*a);
426 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
432 case Editing::ImportMergeFiles:
433 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
438 case Editing::ImportSerializeFiles:
439 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
442 to_embed.push_back (*a);
444 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
455 session->save_state ("");
460 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos,
461 int target_regions, int target_tracks, boost::shared_ptr<Track> track, bool replace, uint32_t total)
463 WindowTitle title = string_compose (_("importing %1"), paths.front());
465 interthread_progress_window->set_title (title.get_string());
466 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
467 interthread_progress_bar.set_fraction (0.0f);
468 interthread_cancel_label.set_text (_("Cancel Import"));
469 current_interthread_info = &import_status;
471 import_status.paths = paths;
472 import_status.done = false;
473 import_status.cancel = false;
474 import_status.freeze = false;
475 import_status.done = 0.0;
476 import_status.quality = quality;
477 import_status.replace_existing_source = replace;
478 import_status.total = total;
480 import_status.mode = mode;
481 import_status.pos = pos;
482 import_status.target_tracks = target_tracks;
483 import_status.target_regions = target_regions;
484 import_status.track = track;
485 import_status.replace = replace;
486 interthread_progress_connection = Glib::signal_timeout().connect
487 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 500);
489 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
492 /* start import thread for this spec. this will ultimately call Session::import_audiofiles()
493 which, if successful, will add the files as regions to the region list. its up to us
494 (the GUI) to direct additional steps after that.
497 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
498 pthread_detach (import_status.thread);
500 while (!import_status.done && !import_status.cancel) {
501 gtk_main_iteration ();
504 interthread_progress_window->hide ();
505 import_status.done = true;
506 interthread_progress_connection.disconnect ();
508 if (!import_status.cancel && !import_status.sources.empty()) {
509 if (add_sources (import_status.paths,
510 import_status.sources,
513 import_status.target_regions,
514 import_status.target_tracks,
515 import_status.track, false) == 0) {
516 session->save_state ("");
519 /* update position from results */
521 pos = import_status.pos;
525 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
530 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
531 bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
532 boost::shared_ptr<Track>& track)
534 boost::shared_ptr<AudioFileSource> source;
539 Glib::ustring path_to_use;
541 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
544 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
548 /* lets see if we can link it into the session */
550 sys::path tmp = session->session_directory().sound_path() / Glib::path_get_basename(path);
551 linked_path = tmp.to_string();
553 path_to_use = linked_path;
555 if (link (path.c_str(), linked_path.c_str()) == 0) {
557 /* there are many reasons why link(2) might have failed.
558 but if it succeeds, we now have a link in the
559 session sound dir that will protect against
560 unlinking of the original path. nice.
564 path_to_use = Glib::path_get_basename (path);
568 /* one possible reason is that its already linked */
570 if (errno == EEXIST) {
573 if (stat (linked_path.c_str(), &sb) == 0) {
574 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
576 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 (
600 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
601 short_path (path, 40)),
604 int resx = rate_choice.run ();
607 case 0: /* stop a multi-file import */
610 case 1: /* don't embed this one */
613 case 2: /* do it, and the rest without asking */
614 check_sample_rate = false;
623 choices.push_back (_("Cancel"));
624 choices.push_back (_("Embed it anyway"));
626 Gtkmm2ext::Choice rate_choice (
627 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
630 int resx = rate_choice.run ();
633 case 0: /* don't import */
645 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
647 for (int n = 0; n < finfo.channels; ++n) {
650 /* check if we have this thing embedded already */
652 boost::shared_ptr<Source> s;
654 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
656 source = boost::dynamic_pointer_cast<AudioFileSource> (
657 SourceFactory::createReadable (DataType::AUDIO, *session,
658 path_to_use, false, n,
659 (mode == ImportAsTapeTrack
660 ? Source::Destructive
664 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
667 sources.push_back(source);
670 catch (failed_constructor& err) {
671 error << string_compose(_("could not open %1"), path) << endmsg;
675 ARDOUR_UI::instance()->flush_pending ();
679 if (sources.empty()) {
683 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
686 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
691 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode,
692 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool /*add_channel_suffix*/)
694 vector<boost::shared_ptr<Region> > regions;
696 uint32_t input_chan = 0;
697 uint32_t output_chan = 0;
700 use_timestamp = (pos == -1);
703 if (sources[0]->natural_position() != 0) {
704 pos = sources[0]->natural_position();
706 pos = get_preferred_edit_position ();
710 // kludge (for MIDI we're abusing "channel" for "track" here)
711 if (paths.front().rfind(".mid") != Glib::ustring::npos)
714 if (target_regions == 1) {
716 /* take all the sources we have and package them up as a region */
718 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
720 boost::shared_ptr<Region> r = RegionFactory::create (sources, 0, sources[0]->length(pos), region_name, 0,
721 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
723 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
724 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
727 regions.push_back (r);
730 } else if (target_regions == -1 || target_regions > 1) {
732 /* take each source and create a region for each one */
735 SourceList::iterator x;
738 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
741 just_one.push_back (*x);
743 region_name = region_name_from_path ((*x)->path(), false, false, sources.size(), n);
745 boost::shared_ptr<Region> r = RegionFactory::create (just_one, 0, (*x)->length(pos), region_name, 0,
746 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
748 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
749 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
752 regions.push_back (r);
756 if (target_regions == 1) {
757 input_chan = regions.front()->n_channels();
759 if (target_tracks == 1) {
760 input_chan = regions.size();
766 if (Config->get_output_auto_connect() & AutoConnectMaster) {
767 output_chan = (session->master_out() ? session->master_out()->n_inputs().n_audio() : input_chan);
769 output_chan = input_chan;
774 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
776 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
778 if (target_tracks != 1) {
781 pos += (*r)->length();
785 /* setup peak file building in another thread */
787 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
788 SourceFactory::setup_peakfile (*x, true);
795 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos,
796 ImportMode mode, boost::shared_ptr<Track>& existing_track)
798 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
799 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
803 /* relax, its been done */
808 if (!existing_track) {
811 existing_track = get_nth_selected_audio_track (0);
813 existing_track = get_nth_selected_midi_track (0);
816 if (!existing_track) {
821 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
822 boost::shared_ptr<Region> copy (RegionFactory::create (region));
823 begin_reversible_command (_("insert file"));
824 XMLNode &before = playlist->get_state();
825 playlist->add_region (copy, pos);
826 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
827 commit_reversible_command ();
833 if (!existing_track) {
835 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
841 existing_track = at.front();
843 list<boost::shared_ptr<MidiTrack> > mt (session->new_midi_track (Normal, 0, 1));
849 existing_track = mt.front();
852 existing_track->set_name (region->name());
855 boost::shared_ptr<Region> copy (RegionFactory::create (region));
856 existing_track->diskstream()->playlist()->add_region (copy, pos);
861 case ImportAsTapeTrack:
866 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
868 boost::shared_ptr<Region> copy (RegionFactory::create (region));
869 at.front()->set_name (basename_nosuffix (copy->name()));
870 at.front()->diskstream()->playlist()->add_region (copy, pos);
880 Editor::_import_thread (void *arg)
882 PBD::notify_gui_about_thread_creation (pthread_self(), X_("Import"));
884 Editor *ed = (Editor *) arg;
885 return ed->import_thread ();
889 Editor::import_thread ()
891 session->import_audiofiles (import_status);
892 pthread_exit_pbd (0);
898 Editor::import_progress_timeout (void */*arg*/)
902 if (!interthread_progress_window->is_visible()) {
903 interthread_progress_window->show_all ();
907 interthread_progress_label.set_text (import_status.doing_what);
909 if (import_status.freeze) {
910 interthread_cancel_button.set_sensitive(false);
912 interthread_cancel_button.set_sensitive(true);
915 if (import_status.doing_what == "building peak files") {
916 interthread_progress_bar.pulse ();
919 float val = import_status.progress;
920 interthread_progress_bar.set_fraction (min (max (0.0f, val), 1.0f));
925 /* the window is now visible, speed up the updates */
927 interthread_progress_connection.disconnect ();
928 interthread_progress_connection = Glib::signal_timeout().connect
929 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
932 return !(import_status.done || import_status.cancel);