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/thread.h>
24 #include "ardour/audio_diskstream.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/butler.h"
27 #include "ardour/export_failed.h"
28 #include "ardour/export_handler.h"
29 #include "ardour/export_status.h"
30 #include "ardour/route.h"
31 #include "ardour/session.h"
36 using namespace ARDOUR;
39 boost::shared_ptr<ExportHandler>
40 Session::get_export_handler ()
42 if (!export_handler) {
43 export_handler.reset (new ExportHandler (*this));
46 return export_handler;
49 boost::shared_ptr<ExportStatus>
50 Session::get_export_status ()
53 export_status.reset (new ExportStatus ());
61 Session::pre_export ()
63 get_export_status (); // Init export_status
65 _butler->wait_until_finished ();
67 /* take everyone out of awrite to avoid disasters */
70 boost::shared_ptr<RouteList> r = routes.reader ();
72 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
73 (*i)->protect_automation ();
77 /* make sure we are actually rolling */
79 if (get_record_enabled()) {
80 disable_record (false);
85 post_export_sync = config.get_external_sync ();
86 post_export_position = _transport_frame;
88 config.set_external_sync (false);
91 export_status->running = true;
92 export_status->Aborting.connect_same_thread (*this, boost::bind (&Session::stop_audio_export, this));
93 export_status->Finished.connect_same_thread (*this, boost::bind (&Session::finalize_audio_export, this));
99 Session::start_audio_export (nframes_t position, bool realtime)
105 /* get everyone to the right position */
108 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
110 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
111 if ((*i)-> seek (position, true)) {
112 error << string_compose (_("%1: cannot seek to %2 for export"),
113 (*i)->name(), position)
120 /* we just did the core part of a locate() call above, but
121 for the sake of any GUI, put the _transport_frame in
125 _transport_frame = position;
126 export_status->stop = false;
128 /* get transport ready. note how this is calling butler functions
129 from a non-butler thread. we waited for the butler to stop
130 what it was doing earlier in Session::pre_export() and nothing
131 since then has re-awakened it.
134 set_transport_speed (1.0, false);
135 butler_transport_work ();
136 g_atomic_int_set (&_butler->should_do_transport_work, 0);
139 /* we are ready to go ... */
141 if (!_engine.connected()) {
145 _engine.Freewheel.connect_same_thread (export_freewheel_connection, boost::bind (&Session::process_export_fw, this, _1));
146 _export_rolling = true;
147 return _engine.freewheel (true);
151 Session::process_export (nframes_t nframes)
153 if (_export_rolling && export_status->stop) {
154 stop_audio_export ();
157 if (_export_rolling) {
158 /* make sure we've caught up with disk i/o, since
159 we're running faster than realtime c/o JACK.
161 _butler->wait_until_finished ();
163 /* do the usual stuff */
165 process_without_events (nframes);
169 /* handle export - XXX what about error handling? */
171 ProcessExport (nframes);
173 } catch (std::exception & e) {
174 std::cout << e.what() << std::endl;
175 export_status->abort (true);
180 Session::process_export_fw (nframes_t nframes)
182 process_export (nframes);
187 Session::stop_audio_export ()
189 /* can't use stop_transport() here because we need
190 an immediate halt and don't require all the declick
191 stuff that stop_transport() implements.
194 realtime_stop (true, true);
195 _export_rolling = false;
196 _butler->schedule_transport_work ();
198 if (export_status->aborted()) {
199 finalize_audio_export ();
207 Session::finalize_audio_export ()
210 _export_rolling = false;
214 _engine.freewheel (false);
215 export_freewheel_connection.disconnect();
216 export_handler.reset();
217 export_status.reset();
219 /* restart slaving */
221 if (post_export_sync) {
222 config.set_external_sync (true);
224 locate (post_export_position, false, false, false);