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.
21 #include <pbd/pthread_utils.h>
22 #include <pbd/basename.h>
24 #include <gtkmm2ext/choice.h>
26 #include <ardour/session.h>
27 #include <ardour/audioplaylist.h>
28 #include <ardour/audioregion.h>
29 #include <ardour/diskstream.h>
30 #include <ardour/filesource.h>
31 #include <ardour/externalsource.h>
32 #include <ardour/utils.h>
33 #include <ardour/audio_track.h>
34 #include <ardour/audioplaylist.h>
36 #include "ardour_ui.h"
40 #include "audio_time_axis.h"
45 using namespace ARDOUR;
48 using namespace Editing;
50 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
53 Editor::add_external_audio_action (ImportMode mode)
55 jack_nframes_t& pos = edit_cursor->current_frame;
56 AudioTrack* track = 0;
58 if (!selection->tracks.empty()) {
59 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
61 track = atv->audio_track();
65 bring_in_external_audio (mode, track, pos, false);
69 Editor::bring_in_external_audio (ImportMode mode, AudioTrack* track, jack_nframes_t& pos, bool prompt)
72 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
77 SoundFileOmega sfdb (_("Add existing audio to session"));
78 sfdb.set_session (session);
82 case SoundFileOmega::ResponseImport:
83 do_import (sfdb.get_paths(), sfdb.get_split(), mode, track, pos, prompt);
86 case SoundFileOmega::ResponseEmbed:
87 do_embed (sfdb.get_paths(), sfdb.get_split(), mode, track, pos, prompt);
96 Editor::do_import (vector<Glib::ustring> paths, bool split, ImportMode mode, AudioTrack* track, jack_nframes_t& pos, bool prompt)
98 /* SFDB sets "multichan" to true to indicate "split channels"
99 so reverse the setting to match the way libardour
103 import_status.multichan = !split;
105 if (interthread_progress_window == 0) {
106 build_interthread_progress_window ();
109 /* for each path that was selected, import it and then potentially create a new track
110 containing the new region as the sole contents.
113 for (vector<Glib::ustring>::iterator i = paths.begin(); i != paths.end(); ++i ) {
114 import_sndfile (*i, mode, track, pos);
117 interthread_progress_window->hide_all ();
121 Editor::do_embed (vector<Glib::ustring> paths, bool split, ImportMode mode, AudioTrack* track, jack_nframes_t& pos, bool prompt)
123 bool multiple_files = paths.size() > 1;
124 bool check_sample_rate = true;
125 vector<Glib::ustring>::iterator i;
127 for (i = paths.begin(); i != paths.end(); ++i) {
128 int ret = embed_sndfile (*i, split, multiple_files, check_sample_rate, mode, track, pos, prompt);
135 if (i == paths.end()) {
136 session->save_state ("");
141 Editor::import_sndfile (Glib::ustring path, ImportMode mode, AudioTrack* track, jack_nframes_t& pos)
143 interthread_progress_window->set_title (string_compose (_("ardour: importing %1"), path));
144 interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
145 interthread_progress_window->show_all ();
146 interthread_progress_bar.set_fraction (0.0f);
147 interthread_cancel_label.set_text (_("Cancel Import"));
148 current_interthread_info = &import_status;
150 import_status.pathname = path;
151 import_status.done = false;
152 import_status.cancel = false;
153 import_status.freeze = false;
154 import_status.done = 0.0;
156 interthread_progress_connection = Glib::signal_timeout().connect
157 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
159 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
160 ARDOUR_UI::instance()->flush_pending ();
162 /* start import thread for this path. this will ultimately call Session::import_audiofile()
163 and if successful will add the file as a region to the session region list.
166 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
167 pthread_detach (import_status.thread);
169 while (!(import_status.done || import_status.cancel)) {
170 gtk_main_iteration ();
173 import_status.done = true;
174 interthread_progress_connection.disconnect ();
176 /* import thread finished - see if we should build a new track */
178 if (!import_status.new_regions.empty()) {
179 AudioRegion& region (*import_status.new_regions.front());
180 finish_bringing_in_audio (region, region.n_channels(), region.n_channels(), track, pos, mode);
183 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
188 Editor::embed_sndfile (Glib::ustring path, bool split, bool multiple_files, bool& check_sample_rate, ImportMode mode,
189 AudioTrack* track, jack_nframes_t& pos, bool prompt)
191 ExternalSource *source = 0; /* keep g++ quiet */
192 AudioRegion::SourceList sources;
199 uint32_t output_chan;
201 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
202 ARDOUR_UI::instance()->flush_pending ();
204 /* lets see if we can link it into the session */
206 linked_path = session->sound_dir();
207 linked_path += PBD::basename (path);
209 if (link (path.c_str(), linked_path.c_str()) == 0) {
211 /* there are many reasons why link(2) might have failed.
212 but if it succeeds, we now have a link in the
213 session sound dir that will protect against
214 unlinking of the original path. nice.
220 /* note that we temporarily truncated _id at the colon */
224 if (!ExternalSource::get_soundfile_info (path, finfo, error_msg)) {
225 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), selection, error_msg ) << endmsg;
229 if (check_sample_rate && (finfo.samplerate != (int) session->frame_rate())) {
230 vector<string> choices;
232 if (multiple_files) {
233 choices.push_back (_("Cancel entire import"));
234 choices.push_back (_("Don't embed it"));
235 choices.push_back (_("Embed all without questions"));
237 choices.push_back (_("Cancel"));
240 choices.push_back (_("Embed it anyway"));
242 Gtkmm2ext::Choice rate_choice (
243 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
246 switch (rate_choice.run()) {
247 case 0: /* stop a multi-file import */
248 case 1: /* don't import this one */
250 case 2: /* do it, and the rest without asking */
251 check_sample_rate = false;
260 track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
261 ARDOUR_UI::instance()->flush_pending ();
263 /* make the proper number of channels in the region */
265 for (int n = 0; n < finfo.channels; ++n)
268 idspec += string_compose(":%1", n);
271 source = ExternalSource::create (idspec.c_str());
272 sources.push_back(source);
275 catch (failed_constructor& err) {
276 error << string_compose(_("could not open %1"), path) << endmsg;
280 ARDOUR_UI::instance()->flush_pending ();
283 if (sources.empty()) {
287 region_name = PBD::basename_nosuffix (path);
290 region = new AudioRegion (sources, 0, sources[0]->length(), region_name, 0,
291 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
293 input_chan = finfo.channels;
295 if (session->get_output_auto_connect() & Session::AutoConnectMaster) {
296 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
298 output_chan = input_chan;
301 finish_bringing_in_audio (*region, input_chan, output_chan, track, pos, mode);
304 track_canvas.get_window()->set_cursor (*current_canvas_cursor);
309 Editor::finish_bringing_in_audio (AudioRegion& region, uint32_t in_chans, uint32_t out_chans, AudioTrack* track, jack_nframes_t& pos, ImportMode mode)
313 /* relax, its been done */
318 Playlist* playlist = track->disk_stream().playlist();
320 AudioRegion* copy = new AudioRegion (region);
321 begin_reversible_command (_("insert sndfile"));
322 session->add_undo (playlist->get_memento());
323 playlist->add_region (*copy, pos);
324 session->add_redo_no_execute (playlist->get_memento());
325 commit_reversible_command ();
327 pos += region.length();
332 AudioTrack* at = session->new_audio_track (in_chans, out_chans);
333 AudioRegion* copy = new AudioRegion (region);
334 at->disk_stream().playlist()->add_region (*copy, pos);
342 Editor::_import_thread (void *arg)
344 PBD::ThreadCreated (pthread_self(), X_("Import"));
346 Editor *ed = (Editor *) arg;
347 return ed->import_thread ();
351 Editor::import_thread ()
353 session->import_audiofile (import_status);
354 pthread_exit_pbd (0);
360 Editor::import_progress_timeout (void *arg)
362 interthread_progress_label.set_text (import_status.doing_what);
364 if (import_status.freeze) {
365 interthread_cancel_button.set_sensitive(false);
367 interthread_cancel_button.set_sensitive(true);
370 if (import_status.doing_what == "building peak files") {
371 interthread_progress_bar.pulse ();
374 interthread_progress_bar.set_fraction (import_status.progress);
377 return !(import_status.done || import_status.cancel);