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>
27 #include <pbd/pthread_utils.h>
28 #include <pbd/basename.h>
29 #include <pbd/shortpath.h>
31 #include <gtkmm2ext/choice.h>
32 #include <gtkmm2ext/window_title.h>
34 #include <ardour/session.h>
35 #include <ardour/audioplaylist.h>
36 #include <ardour/audioregion.h>
37 #include <ardour/audio_diskstream.h>
38 #include <ardour/utils.h>
39 #include <ardour/audio_track.h>
40 #include <ardour/audioplaylist.h>
41 #include <ardour/audiofilesource.h>
42 #include <ardour/region_factory.h>
43 #include <ardour/source_factory.h>
44 #include <pbd/memento_command.h>
46 #include "ardour_ui.h"
50 #include "audio_time_axis.h"
56 using namespace ARDOUR;
60 using namespace Gtkmm2ext;
61 using namespace Editing;
64 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
67 Editor::add_external_audio_action (ImportMode mode)
69 nframes_t& pos = edit_cursor->current_frame;
70 AudioTrack* track = 0;
72 if (!selection->tracks.empty()) {
73 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
75 track = atv->audio_track();
79 bring_in_external_audio (mode, track, pos);
84 Editor::external_audio_dialog ()
86 vector<Glib::ustring> paths;
89 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
94 SoundFileBrowser browser (_("Add existing audio"), session);
95 SoundFileOptionsDialog* options = 0;
101 int response = browser.run ();
107 // cancel from the browser - we are done
111 paths = browser.get_paths ();
113 options = new SoundFileOptionsDialog (browser, *session, paths, selection->tracks.size());
114 options->show_all ();
116 response = options->run ();
119 // leave options non-null so that we break out of the loop
122 // back to the browser another try
134 AudioTrack* track = 0;
136 if (!selection->tracks.empty()) {
137 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
139 track = atv->audio_track();
143 if (options->import.get_active()) {
144 do_import (paths, options->split_files.get_active(), options->mode, track, edit_cursor->current_frame);
146 do_embed (paths, options->split_files.get_active(), options->mode, track, edit_cursor->current_frame);
153 Editor::bring_in_external_audio (ImportMode mode, AudioTrack* track, nframes_t& pos)
158 Editor::do_import (vector<ustring> paths, bool split, ImportMode mode, AudioTrack* track, nframes_t& pos)
160 /* SFDB sets "multichan" to true to indicate "split channels"
161 so reverse the setting to match the way libardour
165 import_status.multichan = !split;
167 if (interthread_progress_window == 0) {
168 build_interthread_progress_window ();
171 vector<ustring> to_import;
173 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
176 to_import.push_back (*a);
178 import_sndfile (to_import, mode, track, pos);
181 interthread_progress_window->hide_all ();
185 Editor::idle_do_embed (vector<ustring> paths, bool split, ImportMode mode, AudioTrack* track, nframes_t& pos)
187 _do_embed (paths, split, mode, track, pos);
192 Editor::do_embed (vector<ustring> paths, bool split, ImportMode mode, AudioTrack* track, nframes_t& pos)
195 Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, split, mode, track, pos));
197 _do_embed (paths, split, mode, track, pos);
202 Editor::_do_embed (vector<ustring> paths, bool split, ImportMode mode, AudioTrack* track, nframes_t& pos)
204 bool multiple_files = paths.size() > 1;
205 bool check_sample_rate = true;
206 vector<ustring>::iterator a;
208 for (a = paths.begin(); a != paths.end(); ) {
210 Glib::ustring path = *a;
211 Glib::ustring pair_base;
212 vector<ustring> to_embed;
214 to_embed.push_back (path);
217 if (path_is_paired (path, pair_base)) {
219 ustring::size_type len = pair_base.length();
221 for (vector<Glib::ustring>::iterator b = paths.begin(); b != paths.end(); ) {
223 if (((*b).substr (0, len) == pair_base) && ((*b).length() == path.length())) {
225 to_embed.push_back (*b);
227 /* don't process this one again */
238 if (to_embed.size() > 1) {
240 vector<string> choices;
242 choices.push_back (string_compose (_("Import as a %1 region"),
243 to_embed.size() > 2 ? _("multichannel") : _("stereo")));
244 choices.push_back (_("Import as multiple regions"));
246 Choice chooser (string_compose (_("Paired files detected (%1, %2 ...).\nDo you want to:"),
251 if (chooser.run () == 0) {
253 /* keep them paired */
255 if (embed_sndfile (to_embed, split, multiple_files, check_sample_rate, mode, track, pos) < -1) {
261 /* one thing per file */
265 for (vector<ustring>::iterator x = to_embed.begin(); x != to_embed.end(); ++x) {
270 if (embed_sndfile (foo, split, multiple_files, check_sample_rate, mode, track, pos) < -1) {
278 if (embed_sndfile (to_embed, split, multiple_files, check_sample_rate, mode, track, pos) < -1) {
284 if (a == paths.end()) {
285 session->save_state ("");
290 Editor::import_sndfile (vector<ustring> paths, ImportMode mode, AudioTrack* track, nframes_t& pos)
292 WindowTitle title = string_compose (_("importing %1"), paths.front());
294 interthread_progress_window->set_title (title.get_string());
295 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
296 interthread_progress_window->show_all ();
297 interthread_progress_bar.set_fraction (0.0f);
298 interthread_cancel_label.set_text (_("Cancel Import"));
299 current_interthread_info = &import_status;
301 import_status.paths = paths;
302 import_status.done = false;
303 import_status.cancel = false;
304 import_status.freeze = false;
305 import_status.done = 0.0;
307 interthread_progress_connection = Glib::signal_timeout().connect
308 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
310 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
311 ARDOUR_UI::instance()->flush_pending ();
313 /* start import thread for this spec. this will ultimately call Session::import_audiofile()
314 and if successful will add the file(s) as a region to the session region list.
317 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
318 pthread_detach (import_status.thread);
320 while (!(import_status.done || import_status.cancel)) {
321 gtk_main_iteration ();
324 interthread_progress_window->hide ();
326 import_status.done = true;
327 interthread_progress_connection.disconnect ();
329 /* import thread finished - see if we should build a new track */
331 if (!import_status.new_regions.empty()) {
332 boost::shared_ptr<AudioRegion> region (import_status.new_regions.front());
333 finish_bringing_in_audio (region, region->n_channels(), region->n_channels(), track, pos, mode);
336 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
341 Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_files, bool& check_sample_rate, ImportMode mode,
342 AudioTrack* track, nframes_t& pos)
344 boost::shared_ptr<AudioFileSource> source;
346 boost::shared_ptr<AudioRegion> region;
350 uint32_t input_chan = 0;
351 uint32_t output_chan = 0;
354 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
355 ARDOUR_UI::instance()->flush_pending ();
357 for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
361 /* lets see if we can link it into the session */
363 linked_path = session->sound_dir();
365 linked_path += Glib::path_get_basename (path);
367 if (link (path.c_str(), linked_path.c_str()) == 0) {
369 /* there are many reasons why link(2) might have failed.
370 but if it succeeds, we now have a link in the
371 session sound dir that will protect against
372 unlinking of the original path. nice.
379 /* one possible reason is that its already linked */
381 if (errno == EEXIST) {
384 if (stat (linked_path.c_str(), &sb) == 0) {
385 if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
392 /* note that we temporarily truncated _id at the colon */
396 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
397 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
401 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
402 vector<string> choices;
404 if (multiple_files) {
405 choices.push_back (_("Cancel entire import"));
406 choices.push_back (_("Don't embed it"));
407 choices.push_back (_("Embed all without questions"));
409 Gtkmm2ext::Choice rate_choice (
410 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
411 short_path (path, 40)),
414 int resx = rate_choice.run ();
417 case 0: /* stop a multi-file import */
420 case 1: /* don't embed this one */
423 case 2: /* do it, and the rest without asking */
424 check_sample_rate = false;
433 choices.push_back (_("Cancel"));
434 choices.push_back (_("Embed it anyway"));
436 Gtkmm2ext::Choice rate_choice (
437 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
440 int resx = rate_choice.run ();
443 case 0: /* don't import */
455 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
456 // ARDOUR_UI::instance()->flush_pending ();
458 /* make the proper number of channels in the region */
460 input_chan += finfo.channels;
462 for (int n = 0; n < finfo.channels; ++n)
466 /* check if we have this thing embedded already */
468 boost::shared_ptr<Source> s;
470 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
471 source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable
473 (mode == ImportAsTapeTrack ?
474 AudioFileSource::Destructive :
475 AudioFileSource::Flag (0))));
477 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
480 sources.push_back(source);
483 catch (failed_constructor& err) {
484 error << string_compose(_("could not open %1"), path) << endmsg;
488 ARDOUR_UI::instance()->flush_pending ();
492 if (sources.empty()) {
496 if (sources[0]->natural_position() != 0) {
497 pos = sources[0]->natural_position();
500 region_name = region_name_from_path (paths.front(), (sources.size() > 1));
502 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
503 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External)));
505 if (Config->get_output_auto_connect() & AutoConnectMaster) {
506 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
508 output_chan = input_chan;
511 finish_bringing_in_audio (region, input_chan, output_chan, track, pos, mode);
514 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
519 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, AudioTrack* track, nframes_t& pos, ImportMode mode)
523 /* relax, its been done */
528 boost::shared_ptr<Playlist> playlist = track->diskstream()->playlist();
530 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
531 begin_reversible_command (_("insert sndfile"));
532 XMLNode &before = playlist->get_state();
533 playlist->add_region (copy, pos);
534 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
535 commit_reversible_command ();
537 pos += region->length();
543 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
545 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
546 at.front()->diskstream()->playlist()->add_region (copy, pos);
551 case ImportAsTapeTrack:
553 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
555 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
556 at.front()->diskstream()->playlist()->add_region (copy, pos);
566 Editor::_import_thread (void *arg)
568 PBD::ThreadCreated (pthread_self(), X_("Import"));
570 Editor *ed = (Editor *) arg;
571 return ed->import_thread ();
575 Editor::import_thread ()
577 session->import_audiofile (import_status);
578 pthread_exit_pbd (0);
584 Editor::import_progress_timeout (void *arg)
586 interthread_progress_label.set_text (import_status.doing_what);
588 if (import_status.freeze) {
589 interthread_cancel_button.set_sensitive(false);
591 interthread_cancel_button.set_sensitive(true);
594 if (import_status.doing_what == "building peak files") {
595 interthread_progress_bar.pulse ();
598 interthread_progress_bar.set_fraction (import_status.progress);
601 return !(import_status.done || import_status.cancel);