2 Copyright (C) 1999-2008 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/error.h"
22 #include <glibmm/threads.h>
24 #include <midi++/mmc.h>
26 #include "ardour/audioengine.h"
27 #include "ardour/butler.h"
28 #include "ardour/export_handler.h"
29 #include "ardour/export_status.h"
30 #include "ardour/process_thread.h"
31 #include "ardour/session.h"
32 #include "ardour/track.h"
37 using namespace ARDOUR;
40 boost::shared_ptr<ExportHandler>
41 Session::get_export_handler ()
43 if (!export_handler) {
44 export_handler.reset (new ExportHandler (*this));
47 return export_handler;
50 boost::shared_ptr<ExportStatus>
51 Session::get_export_status ()
54 export_status.reset (new ExportStatus ());
62 Session::pre_export ()
64 get_export_status (); // Init export_status
66 /* take everyone out of awrite to avoid disasters */
69 boost::shared_ptr<RouteList> r = routes.reader ();
71 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
72 (*i)->protect_automation ();
76 /* prepare transport */
78 realtime_stop (true, true);
80 if (get_record_enabled()) {
81 disable_record (false);
88 post_export_sync = config.get_external_sync ();
89 post_export_position = _transport_frame;
91 config.set_external_sync (false);
94 export_status->set_running (true);
95 export_status->Finished.connect_same_thread (*this, boost::bind (&Session::finalize_audio_export, this));
97 /* disable MMC output early */
99 _pre_export_mmc_enabled = _mmc->send_enabled ();
100 _mmc->enable_send (false);
105 /** Called for each range that is being exported */
107 Session::start_audio_export (framepos_t position, bool realtime, bool region_export)
113 _realtime_export = realtime;
114 _region_export = region_export;
120 _export_preroll = nominal_frame_rate ();
122 _export_preroll = Config->get_export_preroll() * nominal_frame_rate ();
125 if (_export_preroll == 0) {
126 // must be > 0 so that transport is started in sync.
130 /* "worst_track_latency" is the correct value for stem-exports
131 * see to Route::add_export_point(),
133 * for master-bus export, we'd need to add the master's latency.
134 * or actually longest-total-session-latency.
136 * We can't use worst_playback_latency because that includes
137 * includes external latencies and would overcompensate.
139 _export_latency = worst_track_latency ();
145 /* We're about to call Track::seek, so the butler must have finished everything
146 up otherwise it could be doing do_refill in its thread while we are doing
150 _butler->wait_until_finished ();
152 /* get everyone to the right position */
155 boost::shared_ptr<RouteList> rl = routes.reader();
157 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
158 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
159 if (tr && tr->seek (position, true)) {
160 error << string_compose (_("%1: cannot seek to %2 for export"),
161 (*i)->name(), position)
168 /* we just did the core part of a locate() call above, but
169 for the sake of any GUI, put the _transport_frame in
173 _transport_frame = position;
174 export_status->stop = false;
176 /* get transport ready. note how this is calling butler functions
177 from a non-butler thread. we waited for the butler to stop
178 what it was doing earlier in Session::pre_export() and nothing
179 since then has re-awakened it.
182 /* we are ready to go ... */
184 if (!_engine.connected()) {
188 _engine.Freewheel.connect_same_thread (export_freewheel_connection, boost::bind (&Session::process_export_fw, this, _1));
190 if (_realtime_export) {
191 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
192 _export_rolling = true;
193 process_function = &Session::process_export_fw;
196 _export_rolling = true;
197 return _engine.freewheel (true);
202 Session::process_export (pframes_t nframes)
204 if (_export_rolling && export_status->stop) {
205 stop_audio_export ();
208 /* for Region Raw or Fades, we can skip this
209 * RegionExportChannelFactory::update_buffers() does not care
210 * about anything done here
212 if (!_region_export) {
213 if (_export_rolling) {
214 if (!_realtime_export) {
215 /* make sure we've caught up with disk i/o, since
216 * we're running faster than realtime c/o JACK.
218 _butler->wait_until_finished ();
221 /* do the usual stuff */
223 process_without_events (nframes);
225 } else if (_realtime_export) {
226 fail_roll (nframes); // somehow we need to silence _ALL_ output buffers
231 /* handle export - XXX what about error handling? */
233 ProcessExport (nframes);
235 } catch (std::exception & e) {
236 error << string_compose (_("Export ended unexpectedly: %1"), e.what()) << endmsg;
237 export_status->abort (true);
242 Session::process_export_fw (pframes_t nframes)
244 const bool need_buffers = _engine.freewheeling ();
245 if (_export_preroll > 0) {
248 _engine.main_thread()->get_buffers ();
252 _engine.main_thread()->drop_buffers ();
255 _export_preroll -= std::min ((framepos_t)nframes, _export_preroll);
257 if (_export_preroll > 0) {
258 // clear out buffers (reverb tails etc).
262 set_transport_speed (1.0, 0, false);
263 butler_transport_work ();
264 g_atomic_int_set (&_butler->should_do_transport_work, 0);
270 if (_export_latency > 0) {
271 framepos_t remain = std::min ((framepos_t)nframes, _export_latency);
274 _engine.main_thread()->get_buffers ();
276 process_without_events (remain);
278 _engine.main_thread()->drop_buffers ();
281 _export_latency -= remain;
290 _engine.main_thread()->get_buffers ();
292 process_export (nframes);
294 _engine.main_thread()->drop_buffers ();
301 Session::stop_audio_export ()
303 /* can't use stop_transport() here because we need
304 an immediate halt and don't require all the declick
305 stuff that stop_transport() implements.
308 realtime_stop (true, true);
309 _export_rolling = false;
310 _butler->schedule_transport_work ();
316 Session::finalize_audio_export ()
320 if (_export_rolling) {
321 stop_audio_export ();
326 if (_realtime_export) {
327 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
328 process_function = &Session::process_with_events;
330 _engine.freewheel (false);
331 export_freewheel_connection.disconnect();
333 _mmc->enable_send (_pre_export_mmc_enabled);
335 /* maybe write CUE/TOC */
337 export_handler.reset();
338 export_status.reset();
340 /* restart slaving */
342 if (post_export_sync) {
343 config.set_external_sync (true);
345 locate (post_export_position, false, false, false, false, false);