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;
79 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
86 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
87 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
91 } else if (atv->is_audio_track()) {
97 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, track_cnt);
99 sfbrowser->reset (track_cnt);
102 sfbrowser->show_all ();
105 int response = sfbrowser->run ();
109 // leave the dialog open
117 // cancel from the browser - we are done
124 paths = sfbrowser->get_paths ();
126 ImportPosition pos = sfbrowser->get_position ();
127 ImportMode mode = sfbrowser->get_mode ();
128 ImportDisposition chns = sfbrowser->get_channel_disposition ();
132 case ImportAtEditCursor:
133 where = edit_cursor->current_frame;
135 case ImportAtTimestamp:
138 case ImportAtPlayhead:
139 where = playhead_cursor->current_frame;
142 where = session->current_start_frame();
146 SrcQuality quality = sfbrowser->get_src_quality();
148 if (sfbrowser->copy_files_btn.get_active()) {
149 do_import (paths, chns, mode, quality, where);
151 do_embed (paths, chns, mode, where);
154 if (response == RESPONSE_APPLY) {
155 sfbrowser->clear_selection ();
160 boost::shared_ptr<AudioTrack>
161 Editor::get_nth_selected_audio_track (int nth) const
163 AudioTimeAxisView* atv;
164 TrackSelection::iterator x;
166 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
168 atv = dynamic_cast<AudioTimeAxisView*>(*x);
172 } else if (atv->is_audio_track()) {
177 if (x == selection->tracks.end()) {
178 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
180 atv = dynamic_cast<AudioTimeAxisView*>(*x);
183 if (!atv || !atv->is_audio_track()) {
184 return boost::shared_ptr<AudioTrack>();
187 return atv->audio_track();
191 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
193 boost::shared_ptr<AudioTrack> track;
194 vector<ustring> to_import;
198 if (interthread_progress_window == 0) {
199 build_interthread_progress_window ();
203 case Editing::ImportDistinctFiles:
204 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
207 to_import.push_back (*a);
209 if (mode == Editing::ImportToTrack) {
210 track = get_nth_selected_audio_track (nth++);
213 if (import_sndfiles (to_import, mode, quality, pos, 1, -1, track)) {
220 case Editing::ImportDistinctChannels:
221 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
224 to_import.push_back (*a);
226 if (import_sndfiles (to_import, mode, quality, pos, -1, -1, track)) {
233 case Editing::ImportMergeFiles:
234 /* create 1 region from all paths, add to 1 track,
237 if (import_sndfiles (paths, mode, quality, pos, 1, 1, track)) {
242 case Editing::ImportSerializeFiles:
243 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
246 to_import.push_back (*a);
248 /* create 1 region from this path, add to 1 track,
249 reuse "track" across paths
252 if (import_sndfiles (to_import, mode, quality, pos, 1, 1, track)) {
264 session->save_state ("");
267 interthread_progress_window->hide_all ();
271 Editor::idle_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
273 _do_embed (paths, chns, mode, pos);
278 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
281 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, chns, mode, pos));
283 _do_embed (paths, chns, mode, pos);
288 Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
290 boost::shared_ptr<AudioTrack> track;
291 bool check_sample_rate = true;
293 vector<ustring> to_embed;
294 bool multi = paths.size() > 1;
298 case Editing::ImportDistinctFiles:
299 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
302 to_embed.push_back (*a);
304 if (mode == Editing::ImportToTrack) {
305 track = get_nth_selected_audio_track (nth++);
308 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
314 case Editing::ImportDistinctChannels:
315 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
318 to_embed.push_back (*a);
320 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
326 case Editing::ImportMergeFiles:
327 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
332 case Editing::ImportSerializeFiles:
333 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
336 to_embed.push_back (*a);
338 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
349 session->save_state ("");
354 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos,
355 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track)
357 WindowTitle title = string_compose (_("importing %1"), paths.front());
359 interthread_progress_window->set_title (title.get_string());
360 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
361 interthread_progress_window->show_all ();
362 interthread_progress_bar.set_fraction (0.0f);
363 interthread_cancel_label.set_text (_("Cancel Import"));
364 current_interthread_info = &import_status;
366 import_status.paths = paths;
367 import_status.done = false;
368 import_status.cancel = false;
369 import_status.freeze = false;
370 import_status.done = 0.0;
371 import_status.quality = quality;
373 interthread_progress_connection = Glib::signal_timeout().connect
374 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
376 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
377 ARDOUR_UI::instance()->flush_pending ();
379 /* start import thread for this spec. this will ultimately call Session::import_audiofile()
380 and if successful will add the file(s) as a region to the session region list.
383 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
384 pthread_detach (import_status.thread);
386 while (!(import_status.done || import_status.cancel)) {
387 gtk_main_iteration ();
390 interthread_progress_window->hide ();
392 import_status.done = true;
393 interthread_progress_connection.disconnect ();
395 /* import thread finished - see if we should build a new track */
397 boost::shared_ptr<AudioRegion> r;
399 if (import_status.cancel || import_status.sources.empty()) {
403 if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track, false) == 0) {
404 session->save_state ("");
408 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
413 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
414 bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
415 boost::shared_ptr<AudioTrack>& track)
417 boost::shared_ptr<AudioFileSource> source;
423 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
424 ARDOUR_UI::instance()->flush_pending ();
426 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
430 /* lets see if we can link it into the session */
432 linked_path = session->sound_dir();
434 linked_path += Glib::path_get_basename (path);
436 if (link (path.c_str(), linked_path.c_str()) == 0) {
438 /* there are many reasons why link(2) might have failed.
439 but if it succeeds, we now have a link in the
440 session sound dir that will protect against
441 unlinking of the original path. nice.
448 /* one possible reason is that its already linked */
450 if (errno == EEXIST) {
453 if (stat (linked_path.c_str(), &sb) == 0) {
454 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
461 /* note that we temporarily truncated _id at the colon */
465 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
466 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
470 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
471 vector<string> choices;
474 choices.push_back (_("Cancel entire import"));
475 choices.push_back (_("Don't embed it"));
476 choices.push_back (_("Embed all without questions"));
478 Gtkmm2ext::Choice rate_choice (
479 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
480 short_path (path, 40)),
483 int resx = rate_choice.run ();
486 case 0: /* stop a multi-file import */
489 case 1: /* don't embed this one */
492 case 2: /* do it, and the rest without asking */
493 check_sample_rate = false;
502 choices.push_back (_("Cancel"));
503 choices.push_back (_("Embed it anyway"));
505 Gtkmm2ext::Choice rate_choice (
506 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
509 int resx = rate_choice.run ();
512 case 0: /* don't import */
524 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
526 for (int n = 0; n < finfo.channels; ++n) {
529 /* check if we have this thing embedded already */
531 boost::shared_ptr<Source> s;
533 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
534 source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable
536 (mode == ImportAsTapeTrack ?
537 AudioFileSource::Destructive :
538 AudioFileSource::Flag (0)),
541 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
544 sources.push_back(source);
547 catch (failed_constructor& err) {
548 error << string_compose(_("could not open %1"), path) << endmsg;
552 ARDOUR_UI::instance()->flush_pending ();
556 if (sources.empty()) {
560 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
563 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
568 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode,
569 int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool add_channel_suffix)
571 vector<boost::shared_ptr<AudioRegion> > regions;
573 uint32_t input_chan = 0;
574 uint32_t output_chan = 0;
576 if (pos == -1) { // "use timestamp"
577 if (sources[0]->natural_position() != 0) {
578 pos = sources[0]->natural_position();
580 // XXX is this the best alternative ?
581 pos = edit_cursor->current_frame;
585 if (target_regions == 1) {
587 /* take all the sources we have and package them up as a region */
589 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
591 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
592 (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
593 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
595 } else if (target_regions == -1) {
597 /* take each source and create a region for each one */
600 SourceList::iterator x;
603 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
606 just_one.push_back (*x);
608 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*x);
610 region_name = region_name_from_path (afs->path(), false, true, sources.size(), n);
612 regions.push_back (boost::dynamic_pointer_cast<AudioRegion>
613 (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
614 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
619 if (target_regions == 1) {
620 input_chan = regions.front()->n_channels();
622 if (target_tracks == 1) {
623 input_chan = regions.size();
629 if (Config->get_output_auto_connect() & AutoConnectMaster) {
630 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
632 output_chan = input_chan;
637 for (vector<boost::shared_ptr<AudioRegion> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
639 finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track);
641 if (target_tracks != 1) {
644 pos += (*r)->length();
648 /* setup peak file building in another thread */
650 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
651 SourceFactory::setup_peakfile (*x, true);
658 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos,
659 ImportMode mode, boost::shared_ptr<AudioTrack>& existing_track)
663 /* relax, its been done */
668 if (!existing_track) {
670 existing_track = get_nth_selected_audio_track (0);
672 if (!existing_track) {
677 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
678 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
679 begin_reversible_command (_("insert sndfile"));
680 XMLNode &before = playlist->get_state();
681 playlist->add_region (copy, pos);
682 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
683 commit_reversible_command ();
689 if (!existing_track) {
690 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
696 existing_track = at.front();
697 existing_track->set_name (region->name(), this);
700 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
701 existing_track->diskstream()->playlist()->add_region (copy, pos);
706 case ImportAsTapeTrack:
708 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
710 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
711 at.front()->set_name (basename_nosuffix (copy->name()), this);
712 at.front()->diskstream()->playlist()->add_region (copy, pos);
722 Editor::_import_thread (void *arg)
724 PBD::ThreadCreated (pthread_self(), X_("Import"));
726 Editor *ed = (Editor *) arg;
727 return ed->import_thread ();
731 Editor::import_thread ()
733 session->import_audiofile (import_status);
734 pthread_exit_pbd (0);
740 Editor::import_progress_timeout (void *arg)
742 interthread_progress_label.set_text (import_status.doing_what);
744 if (import_status.freeze) {
745 interthread_cancel_button.set_sensitive(false);
747 interthread_cancel_button.set_sensitive(true);
750 if (import_status.doing_what == "building peak files") {
751 interthread_progress_bar.pulse ();
754 interthread_progress_bar.set_fraction (import_status.progress);
757 return !(import_status.done || import_status.cancel);