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)
73 Editor::external_audio_dialog ()
75 vector<Glib::ustring> paths;
78 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
84 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, selection->tracks.size());
86 sfbrowser->reset (selection->tracks.size());
89 sfbrowser->show_all ();
92 int response = sfbrowser->run ();
96 // leave the dialog open
103 // cancel from the browser - we are done
110 paths = sfbrowser->get_paths ();
112 ImportPosition pos = sfbrowser->get_position ();
113 ImportMode mode = sfbrowser->get_mode ();
114 ImportDisposition chns = sfbrowser->get_channel_disposition ();
118 case ImportAtEditCursor:
119 where = edit_cursor->current_frame;
121 case ImportAtTimestamp:
124 case ImportAtPlayhead:
125 where = playhead_cursor->current_frame;
128 where = session->current_start_frame();
132 SrcQuality quality = sfbrowser->get_src_quality();
134 if (sfbrowser->copy_files_btn.get_active()) {
135 do_import (paths, chns, mode, quality, where);
137 do_embed (paths, chns, mode, where);
140 if (response == RESPONSE_APPLY) {
141 sfbrowser->clear_selection ();
146 boost::shared_ptr<AudioTrack>
147 Editor::get_nth_selected_audio_track (int nth) const
149 AudioTimeAxisView* atv;
150 TrackSelection::iterator x;
152 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
153 if (dynamic_cast<AudioTimeAxisView*>(*x)) {
158 if (x == selection->tracks.end()) {
159 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
161 atv = dynamic_cast<AudioTimeAxisView*>(*x);
165 return boost::shared_ptr<AudioTrack>();
168 return atv->audio_track();
172 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
174 boost::shared_ptr<AudioTrack> track;
175 vector<ustring> to_import;
179 if (interthread_progress_window == 0) {
180 build_interthread_progress_window ();
184 case Editing::ImportDistinctFiles:
185 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
188 to_import.push_back (*a);
190 if (mode == Editing::ImportToTrack) {
191 track = get_nth_selected_audio_track (nth++);
194 if (import_sndfiles (to_import, mode, quality, pos, 1, -1, track)) {
201 case Editing::ImportDistinctChannels:
202 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
205 to_import.push_back (*a);
207 if (import_sndfiles (to_import, mode, quality, pos, -1, -1, track)) {
214 case Editing::ImportMergeFiles:
215 /* create 1 region from all paths, add to 1 track,
218 if (import_sndfiles (paths, mode, quality, pos, 1, 1, track)) {
223 case Editing::ImportSerializeFiles:
224 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
227 to_import.push_back (*a);
229 /* create 1 region from this path, add to 1 track,
230 reuse "track" across paths
233 if (import_sndfiles (to_import, mode, quality, pos, 1, 1, track)) {
245 session->save_state ("");
248 interthread_progress_window->hide_all ();
252 Editor::idle_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
254 _do_embed (paths, chns, mode, pos);
259 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
262 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, chns, mode, pos));
264 _do_embed (paths, chns, mode, pos);
269 Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
271 boost::shared_ptr<AudioTrack> track;
272 bool check_sample_rate = true;
274 vector<ustring> to_embed;
275 bool multi = paths.size() > 1;
279 case Editing::ImportDistinctFiles:
280 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
283 to_embed.push_back (*a);
285 if (mode == Editing::ImportToTrack) {
286 track = get_nth_selected_audio_track (nth++);
289 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
295 case Editing::ImportDistinctChannels:
296 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
299 to_embed.push_back (*a);
301 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
307 case Editing::ImportMergeFiles:
308 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
313 case Editing::ImportSerializeFiles:
314 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
317 to_embed.push_back (*a);
319 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
330 session->save_state ("");
335 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos,
336 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track)
338 WindowTitle title = string_compose (_("importing %1"), paths.front());
340 interthread_progress_window->set_title (title.get_string());
341 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
342 interthread_progress_window->show_all ();
343 interthread_progress_bar.set_fraction (0.0f);
344 interthread_cancel_label.set_text (_("Cancel Import"));
345 current_interthread_info = &import_status;
347 import_status.paths = paths;
348 import_status.done = false;
349 import_status.cancel = false;
350 import_status.freeze = false;
351 import_status.done = 0.0;
352 import_status.quality = quality;
354 interthread_progress_connection = Glib::signal_timeout().connect
355 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
357 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
358 ARDOUR_UI::instance()->flush_pending ();
360 /* start import thread for this spec. this will ultimately call Session::import_audiofile()
361 and if successful will add the file(s) as a region to the session region list.
364 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
365 pthread_detach (import_status.thread);
367 while (!(import_status.done || import_status.cancel)) {
368 gtk_main_iteration ();
371 interthread_progress_window->hide ();
373 import_status.done = true;
374 interthread_progress_connection.disconnect ();
376 /* import thread finished - see if we should build a new track */
378 boost::shared_ptr<AudioRegion> r;
380 if (import_status.cancel || import_status.sources.empty()) {
384 if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track, false) == 0) {
385 session->save_state ("");
389 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
394 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
395 bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
396 boost::shared_ptr<AudioTrack>& track)
398 boost::shared_ptr<AudioFileSource> source;
404 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
405 ARDOUR_UI::instance()->flush_pending ();
407 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
411 /* lets see if we can link it into the session */
413 linked_path = session->sound_dir();
415 linked_path += Glib::path_get_basename (path);
417 if (link (path.c_str(), linked_path.c_str()) == 0) {
419 /* there are many reasons why link(2) might have failed.
420 but if it succeeds, we now have a link in the
421 session sound dir that will protect against
422 unlinking of the original path. nice.
429 /* one possible reason is that its already linked */
431 if (errno == EEXIST) {
434 if (stat (linked_path.c_str(), &sb) == 0) {
435 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
442 /* note that we temporarily truncated _id at the colon */
446 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
447 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
451 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
452 vector<string> choices;
455 choices.push_back (_("Cancel entire import"));
456 choices.push_back (_("Don't embed it"));
457 choices.push_back (_("Embed all without questions"));
459 Gtkmm2ext::Choice rate_choice (
460 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
461 short_path (path, 40)),
464 int resx = rate_choice.run ();
467 case 0: /* stop a multi-file import */
470 case 1: /* don't embed this one */
473 case 2: /* do it, and the rest without asking */
474 check_sample_rate = false;
483 choices.push_back (_("Cancel"));
484 choices.push_back (_("Embed it anyway"));
486 Gtkmm2ext::Choice rate_choice (
487 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
490 int resx = rate_choice.run ();
493 case 0: /* don't import */
505 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
507 for (int n = 0; n < finfo.channels; ++n) {
510 /* check if we have this thing embedded already */
512 boost::shared_ptr<Source> s;
514 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
515 source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable
517 (mode == ImportAsTapeTrack ?
518 AudioFileSource::Destructive :
519 AudioFileSource::Flag (0)),
522 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
525 sources.push_back(source);
528 catch (failed_constructor& err) {
529 error << string_compose(_("could not open %1"), path) << endmsg;
533 ARDOUR_UI::instance()->flush_pending ();
537 if (sources.empty()) {
541 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
544 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
549 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode,
550 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool add_channel_suffix)
552 vector<boost::shared_ptr<AudioRegion> > regions;
554 uint32_t input_chan = 0;
555 uint32_t output_chan = 0;
557 if (pos == -1) { // "use timestamp"
558 if (sources[0]->natural_position() != 0) {
559 pos = sources[0]->natural_position();
561 // XXX is this the best alternative ?
562 pos = edit_cursor->current_frame;
566 if (target_regions == 1) {
568 /* take all the sources we have and package them up as a region */
570 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
572 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
573 (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
574 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
576 } else if (target_regions == -1) {
578 /* take each source and create a region for each one */
581 SourceList::iterator x;
584 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
587 just_one.push_back (*x);
589 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*x);
591 region_name = region_name_from_path (afs->path(), false, true, sources.size(), n);
593 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
594 (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
595 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
600 if (target_regions == 1) {
601 input_chan = regions.front()->n_channels();
603 if (target_tracks == 1) {
604 input_chan = regions.size();
610 if (Config->get_output_auto_connect() & AutoConnectMaster) {
611 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
613 output_chan = input_chan;
618 for (vector<boost::shared_ptr<AudioRegion> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
620 finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track);
622 if (target_tracks != 1) {
625 pos += (*r)->length();
629 /* setup peak file building in another thread */
631 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
632 SourceFactory::setup_peakfile (*x, true);
639 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos,
640 ImportMode mode, boost::shared_ptr<AudioTrack>& existing_track)
644 /* relax, its been done */
649 if (!existing_track) {
651 if (selection->tracks.empty()) {
655 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
661 existing_track = atv->audio_track();
664 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
665 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
666 begin_reversible_command (_("insert sndfile"));
667 XMLNode &before = playlist->get_state();
668 playlist->add_region (copy, pos);
669 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
670 commit_reversible_command ();
676 if (!existing_track) {
677 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
683 existing_track = at.front();
684 existing_track->set_name (region->name(), this);
687 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
688 existing_track->diskstream()->playlist()->add_region (copy, pos);
693 case ImportAsTapeTrack:
695 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
697 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
698 at.front()->set_name (basename_nosuffix (copy->name()), this);
699 at.front()->diskstream()->playlist()->add_region (copy, pos);
709 Editor::_import_thread (void *arg)
711 PBD::ThreadCreated (pthread_self(), X_("Import"));
713 Editor *ed = (Editor *) arg;
714 return ed->import_thread ();
718 Editor::import_thread ()
720 session->import_audiofile (import_status);
721 pthread_exit_pbd (0);
727 Editor::import_progress_timeout (void *arg)
729 interthread_progress_label.set_text (import_status.doing_what);
731 if (import_status.freeze) {
732 interthread_cancel_button.set_sensitive(false);
734 interthread_cancel_button.set_sensitive(true);
737 if (import_status.doing_what == "building peak files") {
738 interthread_progress_bar.pulse ();
741 interthread_progress_bar.set_fraction (import_status.progress);
744 return !(import_status.done || import_status.cancel);