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 <pbd/memento_command.h>
47 #include "ardour_ui.h"
51 #include "audio_time_axis.h"
57 using namespace ARDOUR;
61 using namespace Gtkmm2ext;
62 using namespace Editing;
65 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
68 Editor::add_external_audio_action (ImportMode mode_hint)
71 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
77 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, 0, true, mode_hint);
79 sfbrowser->set_mode (mode_hint);
82 external_audio_dialog ();
86 Editor::external_audio_dialog ()
88 vector<Glib::ustring> paths;
92 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
99 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
100 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
104 } else if (atv->is_audio_track()) {
109 if (sfbrowser == 0) {
110 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, track_cnt, true);
112 sfbrowser->reset (track_cnt);
115 sfbrowser->show_all ();
118 int response = sfbrowser->run ();
122 // leave the dialog open
130 // cancel from the browser - we are done
137 paths = sfbrowser->get_paths ();
139 ImportPosition pos = sfbrowser->get_position ();
140 ImportMode mode = sfbrowser->get_mode ();
141 ImportDisposition chns = sfbrowser->get_channel_disposition ();
145 case ImportAtEditPoint:
146 where = get_preferred_edit_position ();
148 case ImportAtTimestamp:
151 case ImportAtPlayhead:
152 where = playhead_cursor->current_frame;
155 where = session->current_start_frame();
163 SrcQuality quality = sfbrowser->get_src_quality();
165 if (sfbrowser->copy_files_btn.get_active()) {
166 do_import (paths, chns, mode, quality, where);
168 do_embed (paths, chns, mode, where);
171 if (response == RESPONSE_APPLY) {
172 sfbrowser->clear_selection ();
177 boost::shared_ptr<AudioTrack>
178 Editor::get_nth_selected_audio_track (int nth) const
180 AudioTimeAxisView* atv;
181 TrackSelection::iterator x;
183 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
185 atv = dynamic_cast<AudioTimeAxisView*>(*x);
189 } else if (atv->is_audio_track()) {
194 if (x == selection->tracks.end()) {
195 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
197 atv = dynamic_cast<AudioTimeAxisView*>(*x);
200 if (!atv || !atv->is_audio_track()) {
201 return boost::shared_ptr<AudioTrack>();
204 return atv->audio_track();
208 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
210 boost::shared_ptr<AudioTrack> track;
211 vector<ustring> to_import;
215 if (interthread_progress_window == 0) {
216 build_interthread_progress_window ();
220 case Editing::ImportDistinctFiles:
221 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
224 to_import.push_back (*a);
226 if (mode == Editing::ImportToTrack) {
227 track = get_nth_selected_audio_track (nth++);
230 if (import_sndfiles (to_import, mode, quality, pos, 1, -1, track)) {
237 case Editing::ImportDistinctChannels:
238 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
241 to_import.push_back (*a);
243 if (import_sndfiles (to_import, mode, quality, pos, -1, -1, track)) {
250 case Editing::ImportMergeFiles:
251 /* create 1 region from all paths, add to 1 track,
254 if (import_sndfiles (paths, mode, quality, pos, 1, 1, track)) {
259 case Editing::ImportSerializeFiles:
260 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
263 to_import.push_back (*a);
265 /* create 1 region from this path, add to 1 track,
266 reuse "track" across paths
269 if (import_sndfiles (to_import, mode, quality, pos, 1, 1, track)) {
281 session->save_state ("");
284 interthread_progress_window->hide_all ();
288 Editor::idle_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
290 _do_embed (paths, chns, mode, pos);
295 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
298 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, chns, mode, pos));
300 _do_embed (paths, chns, mode, pos);
305 Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
307 boost::shared_ptr<AudioTrack> track;
308 bool check_sample_rate = true;
310 vector<ustring> to_embed;
311 bool multi = paths.size() > 1;
315 case Editing::ImportDistinctFiles:
316 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
319 to_embed.push_back (*a);
321 if (mode == Editing::ImportToTrack) {
322 track = get_nth_selected_audio_track (nth++);
325 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
331 case Editing::ImportDistinctChannels:
332 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
335 to_embed.push_back (*a);
337 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
343 case Editing::ImportMergeFiles:
344 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
349 case Editing::ImportSerializeFiles:
350 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
353 to_embed.push_back (*a);
355 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
366 session->save_state ("");
371 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos,
372 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track)
374 WindowTitle title = string_compose (_("importing %1"), paths.front());
376 interthread_progress_window->set_title (title.get_string());
377 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
378 interthread_progress_window->show_all ();
379 interthread_progress_bar.set_fraction (0.0f);
380 interthread_cancel_label.set_text (_("Cancel Import"));
381 current_interthread_info = &import_status;
383 import_status.paths = paths;
384 import_status.done = false;
385 import_status.cancel = false;
386 import_status.freeze = false;
387 import_status.done = 0.0;
388 import_status.quality = quality;
390 interthread_progress_connection = Glib::signal_timeout().connect
391 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
393 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
394 ARDOUR_UI::instance()->flush_pending ();
396 /* start import thread for this spec. this will ultimately call Session::import_audiofile()
397 and if successful will add the file(s) as a region to the session region list.
400 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
401 pthread_detach (import_status.thread);
403 while (!(import_status.done || import_status.cancel)) {
404 gtk_main_iteration ();
407 interthread_progress_window->hide ();
409 import_status.done = true;
410 interthread_progress_connection.disconnect ();
412 /* import thread finished - see if we should build a new track */
414 boost::shared_ptr<AudioRegion> r;
416 if (import_status.cancel || import_status.sources.empty()) {
420 if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track, false) == 0) {
421 session->save_state ("");
425 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
430 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
431 bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
432 boost::shared_ptr<AudioTrack>& track)
434 boost::shared_ptr<AudioFileSource> source;
440 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
441 ARDOUR_UI::instance()->flush_pending ();
443 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
447 /* lets see if we can link it into the session */
449 linked_path = session->sound_dir();
451 linked_path += Glib::path_get_basename (path);
453 if (link (path.c_str(), linked_path.c_str()) == 0) {
455 /* there are many reasons why link(2) might have failed.
456 but if it succeeds, we now have a link in the
457 session sound dir that will protect against
458 unlinking of the original path. nice.
465 /* one possible reason is that its already linked */
467 if (errno == EEXIST) {
470 if (stat (linked_path.c_str(), &sb) == 0) {
471 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
478 /* note that we temporarily truncated _id at the colon */
482 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
483 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
487 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
488 vector<string> choices;
491 choices.push_back (_("Cancel entire import"));
492 choices.push_back (_("Don't embed it"));
493 choices.push_back (_("Embed all without questions"));
495 Gtkmm2ext::Choice rate_choice (
496 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
497 short_path (path, 40)),
500 int resx = rate_choice.run ();
503 case 0: /* stop a multi-file import */
506 case 1: /* don't embed this one */
509 case 2: /* do it, and the rest without asking */
510 check_sample_rate = false;
519 choices.push_back (_("Cancel"));
520 choices.push_back (_("Embed it anyway"));
522 Gtkmm2ext::Choice rate_choice (
523 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
526 int resx = rate_choice.run ();
529 case 0: /* don't import */
541 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
543 for (int n = 0; n < finfo.channels; ++n) {
546 /* check if we have this thing embedded already */
548 boost::shared_ptr<Source> s;
550 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
551 source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable
553 (mode == ImportAsTapeTrack ?
554 AudioFileSource::Destructive :
555 AudioFileSource::Flag (0)),
558 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
561 sources.push_back(source);
564 catch (failed_constructor& err) {
565 error << string_compose(_("could not open %1"), path) << endmsg;
569 ARDOUR_UI::instance()->flush_pending ();
573 if (sources.empty()) {
577 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
580 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
585 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode,
586 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool add_channel_suffix)
588 vector<boost::shared_ptr<AudioRegion> > regions;
590 uint32_t input_chan = 0;
591 uint32_t output_chan = 0;
593 if (pos == -1) { // "use timestamp"
594 if (sources[0]->natural_position() != 0) {
595 pos = sources[0]->natural_position();
597 // XXX is this the best alternative ?
598 pos = get_preferred_edit_position ();
602 if (target_regions == 1) {
604 /* take all the sources we have and package them up as a region */
606 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
608 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
609 (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
610 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
612 } else if (target_regions == -1) {
614 /* take each source and create a region for each one */
617 SourceList::iterator x;
620 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
623 just_one.push_back (*x);
625 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*x);
627 region_name = region_name_from_path (afs->path(), false, true, sources.size(), n);
629 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
630 (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
631 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
636 if (target_regions == 1) {
637 input_chan = regions.front()->n_channels();
639 if (target_tracks == 1) {
640 input_chan = regions.size();
646 if (Config->get_output_auto_connect() & AutoConnectMaster) {
647 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
649 output_chan = input_chan;
654 for (vector<boost::shared_ptr<AudioRegion> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
656 finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track);
658 if (target_tracks != 1) {
661 pos += (*r)->length();
665 /* setup peak file building in another thread */
667 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
668 SourceFactory::setup_peakfile (*x, true);
675 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos,
676 ImportMode mode, boost::shared_ptr<AudioTrack>& existing_track)
680 /* relax, its been done */
685 if (!existing_track) {
687 existing_track = get_nth_selected_audio_track (0);
689 if (!existing_track) {
694 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
695 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
696 begin_reversible_command (_("insert sndfile"));
697 XMLNode &before = playlist->get_state();
698 playlist->add_region (copy, pos);
699 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
700 commit_reversible_command ();
706 if (!existing_track) {
707 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
713 existing_track = at.front();
714 existing_track->set_name (region->name(), this);
717 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
718 existing_track->diskstream()->playlist()->add_region (copy, pos);
723 case ImportAsTapeTrack:
725 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
727 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
728 at.front()->set_name (basename_nosuffix (copy->name()), this);
729 at.front()->diskstream()->playlist()->add_region (copy, pos);
739 Editor::_import_thread (void *arg)
741 PBD::ThreadCreated (pthread_self(), X_("Import"));
743 Editor *ed = (Editor *) arg;
744 return ed->import_thread ();
748 Editor::import_thread ()
750 session->import_audiofile (import_status);
751 pthread_exit_pbd (0);
757 Editor::import_progress_timeout (void *arg)
759 interthread_progress_label.set_text (import_status.doing_what);
761 if (import_status.freeze) {
762 interthread_cancel_button.set_sensitive(false);
764 interthread_cancel_button.set_sensitive(true);
767 if (import_status.doing_what == "building peak files") {
768 interthread_progress_bar.pulse ();
771 interthread_progress_bar.set_fraction (import_status.progress);
774 return !(import_status.done || import_status.cancel);