2 * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
4 * Copyright (C) 2008-2013 Sakari Bergen <sakari.bergen@beatwaves.net>
5 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2015-2018 Robin Gareus <robin@gareus.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "pbd/error.h"
25 #include <glibmm/threads.h>
27 #include <midi++/mmc.h>
29 #include "ardour/audioengine.h"
30 #include "ardour/butler.h"
31 #include "ardour/export_handler.h"
32 #include "ardour/export_status.h"
33 #include "ardour/process_thread.h"
34 #include "ardour/session.h"
35 #include "ardour/track.h"
40 using namespace ARDOUR;
43 boost::shared_ptr<ExportHandler>
44 Session::get_export_handler ()
46 if (!export_handler) {
47 export_handler.reset (new ExportHandler (*this));
50 return export_handler;
53 boost::shared_ptr<ExportStatus>
54 Session::get_export_status ()
57 export_status.reset (new ExportStatus ());
65 Session::pre_export ()
67 get_export_status (); // Init export_status
69 /* take everyone out of awrite to avoid disasters */
72 boost::shared_ptr<RouteList> r = routes.reader ();
74 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
75 (*i)->protect_automation ();
79 /* prepare transport */
81 realtime_stop (true, true);
83 if (get_record_enabled()) {
84 disable_record (false);
91 post_export_sync = config.get_external_sync ();
92 post_export_position = _transport_sample;
94 config.set_external_sync (false);
97 export_status->set_running (true);
98 export_status->Finished.connect_same_thread (*this, boost::bind (&Session::finalize_audio_export, this));
100 /* disable MMC output early */
102 _pre_export_mmc_enabled = _mmc->send_enabled ();
103 _mmc->enable_send (false);
108 /** Called for each range that is being exported */
110 Session::start_audio_export (samplepos_t position, bool realtime, bool region_export)
116 _realtime_export = realtime;
117 _region_export = region_export;
123 _export_preroll = nominal_sample_rate ();
125 _export_preroll = Config->get_export_preroll() * nominal_sample_rate ();
128 if (_export_preroll == 0) {
129 // must be > 0 so that transport is started in sync.
133 /* We're about to call Track::seek, so the butler must have finished everything
134 up otherwise it could be doing do_refill in its thread while we are doing
138 _butler->wait_until_finished ();
140 /* get everyone to the right position */
143 boost::shared_ptr<RouteList> rl = routes.reader();
145 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
146 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
147 if (tr && tr->seek (position, true)) {
148 error << string_compose (_("%1: cannot seek to %2 for export"),
149 (*i)->name(), position)
156 /* we just did the core part of a locate() call above, but
157 for the sake of any GUI, put the _transport_sample in
161 _transport_sample = position;
163 if (!region_export) {
164 _remaining_latency_preroll = worst_latency_preroll ();
166 _remaining_latency_preroll = 0;
168 export_status->stop = false;
170 /* get transport ready. note how this is calling butler functions
171 from a non-butler thread. we waited for the butler to stop
172 what it was doing earlier in Session::pre_export() and nothing
173 since then has re-awakened it.
176 /* we are ready to go ... */
178 if (!_engine.running()) {
182 _engine.Freewheel.connect_same_thread (export_freewheel_connection, boost::bind (&Session::process_export_fw, this, _1));
184 if (_realtime_export) {
185 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
186 _export_rolling = true;
187 process_function = &Session::process_export_fw;
190 _export_rolling = true;
191 return _engine.freewheel (true);
196 Session::process_export (pframes_t nframes)
198 if (_export_rolling && export_status->stop) {
199 stop_audio_export ();
202 /* for Region Raw or Fades, we can skip this
203 * RegionExportChannelFactory::update_buffers() does not care
204 * about anything done here
206 if (!_region_export) {
207 if (_export_rolling) {
208 if (!_realtime_export) {
209 /* make sure we've caught up with disk i/o, since
210 * we're running faster than realtime c/o JACK.
212 _butler->wait_until_finished ();
215 /* do the usual stuff */
217 process_without_events (nframes);
219 } else if (_realtime_export) {
220 fail_roll (nframes); // somehow we need to silence _ALL_ output buffers
225 /* handle export - XXX what about error handling? */
227 ProcessExport (nframes);
229 } catch (std::exception & e) {
230 error << string_compose (_("Export ended unexpectedly: %1"), e.what()) << endmsg;
231 export_status->abort (true);
236 Session::process_export_fw (pframes_t nframes)
238 const bool need_buffers = _engine.freewheeling ();
239 if (_export_preroll > 0) {
242 _engine.main_thread()->get_buffers ();
246 _engine.main_thread()->drop_buffers ();
249 _export_preroll -= std::min ((samplepos_t)nframes, _export_preroll);
251 if (_export_preroll > 0) {
252 // clear out buffers (reverb tails etc).
256 set_transport_speed (1.0, 0, false);
257 butler_transport_work ();
258 g_atomic_int_set (&_butler->should_do_transport_work, 0);
264 if (_remaining_latency_preroll > 0) {
265 samplepos_t remain = std::min ((samplepos_t)nframes, _remaining_latency_preroll);
268 _engine.main_thread()->get_buffers ();
271 process_without_events (remain);
274 _engine.main_thread()->drop_buffers ();
277 _remaining_latency_preroll -= remain;
278 _transport_sample -= remain;
284 _engine.split_cycle (remain);
288 _engine.main_thread()->get_buffers ();
290 process_export (nframes);
292 _engine.main_thread()->drop_buffers ();
299 Session::stop_audio_export ()
301 /* can't use stop_transport() here because we need
302 an immediate halt and don't require all the declick
303 stuff that stop_transport() implements.
306 realtime_stop (true, true);
307 _export_rolling = false;
308 _butler->schedule_transport_work ();
314 Session::finalize_audio_export ()
318 if (_export_rolling) {
319 stop_audio_export ();
324 if (_realtime_export) {
325 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
326 process_function = &Session::process_with_events;
328 _engine.freewheel (false);
329 export_freewheel_connection.disconnect();
331 _mmc->enable_send (_pre_export_mmc_enabled);
333 /* maybe write CUE/TOC */
335 export_handler.reset();
336 export_status.reset();
338 /* restart slaving */
340 if (post_export_sync) {
341 config.set_external_sync (true);
343 locate (post_export_position, false, false, false, false, false);