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>
28 #include <pbd/pthread_utils.h>
29 #include <pbd/basename.h>
30 #include <pbd/shortpath.h>
32 #include <gtkmm2ext/choice.h>
33 #include <gtkmm2ext/window_title.h>
35 #include <ardour/session.h>
36 #include <ardour/audioplaylist.h>
37 #include <ardour/audioregion.h>
38 #include <ardour/audio_diskstream.h>
39 #include <ardour/utils.h>
40 #include <ardour/audio_track.h>
41 #include <ardour/audioplaylist.h>
42 #include <ardour/audiofilesource.h>
43 #include <ardour/region_factory.h>
44 #include <ardour/source_factory.h>
45 #include <ardour/session.h>
46 #include <pbd/memento_command.h>
48 #include "ardour_ui.h"
52 #include "audio_time_axis.h"
58 using namespace ARDOUR;
62 using namespace Gtkmm2ext;
63 using namespace Editing;
66 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
69 Editor::add_external_audio_action (ImportMode mode_hint)
72 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
78 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, 0, true, mode_hint);
80 sfbrowser->set_mode (mode_hint);
83 external_audio_dialog ();
87 Editor::external_audio_dialog ()
89 vector<Glib::ustring> paths;
93 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
100 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
101 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
105 } else if (atv->is_audio_track()) {
110 if (sfbrowser == 0) {
111 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, track_cnt, true);
113 sfbrowser->reset (track_cnt);
116 sfbrowser->show_all ();
124 int response = sfbrowser->run ();
128 // leave the dialog open
136 // cancel from the browser - we are done
143 paths = sfbrowser->get_paths ();
145 ImportPosition pos = sfbrowser->get_position ();
146 ImportMode mode = sfbrowser->get_mode ();
147 ImportDisposition chns = sfbrowser->get_channel_disposition ();
151 case ImportAtEditPoint:
152 where = get_preferred_edit_position ();
154 case ImportAtTimestamp:
157 case ImportAtPlayhead:
158 where = playhead_cursor->current_frame;
161 where = session->current_start_frame();
165 SrcQuality quality = sfbrowser->get_src_quality();
168 if (sfbrowser->copy_files_btn.get_active()) {
169 do_import (paths, chns, mode, quality, where);
171 do_embed (paths, chns, mode, where);
174 if (response == RESPONSE_APPLY) {
175 sfbrowser->clear_selection ();
179 } while (keepRunning);
182 typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
185 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_audiofiles()
188 * true = show "Update", "Import" and "Skip"
189 * false = show "Import", and "Cancel"
192 * 0 To update an existing source of the same name
193 * 1 To import/embed the file normally (make sure the new name will be unique)
194 * 2 If the user wants to skip this file
197 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
199 string wave_name (Glib::path_get_basename(path));
201 AudioSourceList all_sources = session->get_audio_sources();
202 bool wave_name_exists = false;
204 for (AudioSourceList::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
205 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(i->second);
207 string tmp (Glib::path_get_basename (afs->path()));
209 if (tmp == wave_name) {
210 wave_name_exists = true;
218 if (wave_name_exists) {
220 if (all_or_nothing) {
221 // updating is still disabled
222 //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);
223 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);
225 message = _("Lorem ipsum. Do you want to skidaddle?");
228 MessageDialog dialog(message, false,Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
230 if (all_or_nothing) {
232 //dialog.add_button("Update", 0);
233 dialog.add_button("Import", 1);
234 dialog.add_button("Skip", 2);
236 dialog.add_button("Import", 1);
237 dialog.add_button("Cancel", 2);
241 //dialog.add_button("Skip all", 4); // All or rest?
245 function = dialog.run ();
253 boost::shared_ptr<AudioTrack>
254 Editor::get_nth_selected_audio_track (int nth) const
256 AudioTimeAxisView* atv;
257 TrackSelection::iterator x;
259 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
261 atv = dynamic_cast<AudioTimeAxisView*>(*x);
265 } else if (atv->is_audio_track()) {
270 if (x == selection->tracks.end()) {
271 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
273 atv = dynamic_cast<AudioTimeAxisView*>(*x);
276 if (!atv || !atv->is_audio_track()) {
277 return boost::shared_ptr<AudioTrack>();
280 return atv->audio_track();
284 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
286 boost::shared_ptr<AudioTrack> track;
287 vector<ustring> to_import;
291 if (interthread_progress_window == 0) {
292 build_interthread_progress_window ();
296 if (chns == Editing::ImportMergeFiles) {
297 /* create 1 region from all paths, add to 1 track,
301 for (vector<ustring>::iterator a = paths.begin(); a != paths.end() && ok; ++a) {
302 int check = check_whether_and_how_to_import(*a, false);
310 if (import_sndfiles (paths, mode, quality, pos, 1, 1, track, false)) {
318 for (vector<ustring>::iterator a = paths.begin(); a != paths.end() && ok; ++a) {
320 int check = check_whether_and_how_to_import(*a, true);
328 fatal << "Updating existing sources should be disabled!" << endl;
330 } else if (check == 1) {
336 case Editing::ImportDistinctFiles:
339 to_import.push_back (*a);
341 if (mode == Editing::ImportToTrack) {
342 track = get_nth_selected_audio_track (nth++);
345 if (import_sndfiles (to_import, mode, quality, pos, 1, -1, track, replace)) {
351 case Editing::ImportDistinctChannels:
354 to_import.push_back (*a);
356 if (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, replace)) {
362 case Editing::ImportSerializeFiles:
365 to_import.push_back (*a);
367 /* create 1 region from this path, add to 1 track,
368 reuse "track" across paths
371 if (import_sndfiles (to_import, mode, quality, pos, 1, 1, track, replace)) {
377 case Editing::ImportMergeFiles:
384 session->save_state ("");
387 interthread_progress_window->hide_all ();
392 Editor::idle_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
394 _do_embed (paths, chns, mode, pos);
399 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
402 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, chns, mode, pos));
404 _do_embed (paths, chns, mode, pos);
409 Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
411 boost::shared_ptr<AudioTrack> track;
412 bool check_sample_rate = true;
414 vector<ustring> to_embed;
415 bool multi = paths.size() > 1;
419 case Editing::ImportDistinctFiles:
420 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
423 to_embed.push_back (*a);
425 if (mode == Editing::ImportToTrack) {
426 track = get_nth_selected_audio_track (nth++);
429 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
435 case Editing::ImportDistinctChannels:
436 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
439 to_embed.push_back (*a);
441 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
447 case Editing::ImportMergeFiles:
448 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
453 case Editing::ImportSerializeFiles:
454 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
457 to_embed.push_back (*a);
459 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
470 session->save_state ("");
475 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos,
476 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool replace)
478 WindowTitle title = string_compose (_("importing %1"), paths.front());
480 interthread_progress_window->set_title (title.get_string());
481 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
482 interthread_progress_window->show_all ();
483 interthread_progress_bar.set_fraction (0.0f);
484 interthread_cancel_label.set_text (_("Cancel Import"));
485 current_interthread_info = &import_status;
487 import_status.paths = paths;
488 import_status.done = false;
489 import_status.cancel = false;
490 import_status.freeze = false;
491 import_status.done = 0.0;
492 import_status.quality = quality;
493 import_status.replace_existing_source = replace;
495 interthread_progress_connection = Glib::signal_timeout().connect
496 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
498 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
499 ARDOUR_UI::instance()->flush_pending ();
501 /* start import thread for this spec. this will ultimately call Session::import_audiofile()
502 and if successful will add the file(s) as a region to the session region list.
505 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
506 pthread_detach (import_status.thread);
508 while (!(import_status.done || import_status.cancel)) {
509 gtk_main_iteration ();
512 interthread_progress_window->hide ();
514 import_status.done = true;
515 interthread_progress_connection.disconnect ();
517 /* import thread finished - see if we should build a new track */
519 boost::shared_ptr<AudioRegion> r;
521 if (import_status.cancel || import_status.sources.empty()) {
525 if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track, false) == 0) {
526 session->save_state ("");
530 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
535 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
536 bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
537 boost::shared_ptr<AudioTrack>& track)
539 boost::shared_ptr<AudioFileSource> source;
545 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
546 ARDOUR_UI::instance()->flush_pending ();
548 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
552 /* lets see if we can link it into the session */
554 linked_path = session->sound_dir();
556 linked_path += Glib::path_get_basename (path);
558 if (link (path.c_str(), linked_path.c_str()) == 0) {
560 /* there are many reasons why link(2) might have failed.
561 but if it succeeds, we now have a link in the
562 session sound dir that will protect against
563 unlinking of the original path. nice.
570 /* one possible reason is that its already linked */
572 if (errno == EEXIST) {
575 if (stat (linked_path.c_str(), &sb) == 0) {
576 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
583 /* note that we temporarily truncated _id at the colon */
587 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
588 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
592 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
593 vector<string> choices;
596 choices.push_back (_("Cancel entire import"));
597 choices.push_back (_("Don't embed it"));
598 choices.push_back (_("Embed all without questions"));
600 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)),
605 int resx = rate_choice.run ();
608 case 0: /* stop a multi-file import */
611 case 1: /* don't embed this one */
614 case 2: /* do it, and the rest without asking */
615 check_sample_rate = false;
624 choices.push_back (_("Cancel"));
625 choices.push_back (_("Embed it anyway"));
627 Gtkmm2ext::Choice rate_choice (
628 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
631 int resx = rate_choice.run ();
634 case 0: /* don't import */
646 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
648 for (int n = 0; n < finfo.channels; ++n) {
651 /* check if we have this thing embedded already */
653 boost::shared_ptr<Source> s;
655 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
656 source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable
658 (mode == ImportAsTapeTrack ?
659 AudioFileSource::Destructive :
660 AudioFileSource::Flag (0)),
663 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
666 sources.push_back(source);
669 catch (failed_constructor& err) {
670 error << string_compose(_("could not open %1"), path) << endmsg;
674 ARDOUR_UI::instance()->flush_pending ();
678 if (sources.empty()) {
682 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
685 track_canvas->get_window()->set_cursor (*current_canvas_cursor);
690 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode,
691 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool add_channel_suffix)
693 vector<boost::shared_ptr<AudioRegion> > regions;
695 uint32_t input_chan = 0;
696 uint32_t output_chan = 0;
698 if (pos == -1) { // "use timestamp"
699 if (sources[0]->natural_position() != 0) {
700 pos = sources[0]->natural_position();
702 pos = get_preferred_edit_position ();
706 if (target_regions == 1) {
708 /* take all the sources we have and package them up as a region */
710 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
712 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
713 (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
714 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
716 } else if (target_regions == -1) {
718 /* take each source and create a region for each one */
721 SourceList::iterator x;
724 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
727 just_one.push_back (*x);
729 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*x);
731 region_name = region_name_from_path (afs->path(), false, true, sources.size(), n);
733 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
734 (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
735 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
740 if (target_regions == 1) {
741 input_chan = regions.front()->n_channels();
743 if (target_tracks == 1) {
744 input_chan = regions.size();
750 if (Config->get_output_auto_connect() & AutoConnectMaster) {
751 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
753 output_chan = input_chan;
758 for (vector<boost::shared_ptr<AudioRegion> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
760 finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track);
762 if (target_tracks != 1) {
765 pos += (*r)->length();
769 /* setup peak file building in another thread */
771 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
772 SourceFactory::setup_peakfile (*x, true);
779 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos,
780 ImportMode mode, boost::shared_ptr<AudioTrack>& existing_track)
784 /* relax, its been done */
789 if (!existing_track) {
791 existing_track = get_nth_selected_audio_track (0);
793 if (!existing_track) {
798 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
799 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
800 begin_reversible_command (_("insert sndfile"));
801 XMLNode &before = playlist->get_state();
802 playlist->add_region (copy, pos);
803 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
804 commit_reversible_command ();
810 if (!existing_track) {
811 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
817 existing_track = at.front();
818 existing_track->set_name (region->name(), this);
821 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
822 existing_track->diskstream()->playlist()->add_region (copy, pos);
827 case ImportAsTapeTrack:
829 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
831 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
832 at.front()->set_name (basename_nosuffix (copy->name()), this);
833 at.front()->diskstream()->playlist()->add_region (copy, pos);
843 Editor::_import_thread (void *arg)
845 PBD::ThreadCreated (pthread_self(), X_("Import"));
847 Editor *ed = (Editor *) arg;
848 return ed->import_thread ();
852 Editor::import_thread ()
854 session->import_audiofiles (import_status);
855 pthread_exit_pbd (0);
861 Editor::import_progress_timeout (void *arg)
863 interthread_progress_label.set_text (import_status.doing_what);
865 if (import_status.freeze) {
866 interthread_cancel_button.set_sensitive(false);
868 interthread_cancel_button.set_sensitive(true);
871 if (import_status.doing_what == "building peak files") {
872 interthread_progress_bar.pulse ();
875 interthread_progress_bar.set_fraction (import_status.progress);
878 return !(import_status.done || import_status.cancel);