restore ability to create TOC and CUE files during export. this is an option in a...
[ardour.git] / libs / ardour / session_export.cc
1 /*
2     Copyright (C) 1999-2008 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20
21 #include "pbd/error.h"
22 #include <glibmm/thread.h>
23
24 #include "ardour/audioengine.h"
25 #include "ardour/butler.h"
26 #include "ardour/export_failed.h"
27 #include "ardour/export_handler.h"
28 #include "ardour/export_status.h"
29 #include "ardour/session.h"
30 #include "ardour/track.h"
31 #include "ardour/process_thread.h"
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 boost::shared_ptr<ExportHandler>
40 Session::get_export_handler ()
41 {
42         if (!export_handler) {
43                 export_handler.reset (new ExportHandler (*this));
44         }
45
46         return export_handler;
47 }
48
49 boost::shared_ptr<ExportStatus>
50 Session::get_export_status ()
51 {
52         if (!export_status) {
53                 export_status.reset (new ExportStatus ());
54         }
55
56         return export_status;
57 }
58
59
60 int
61 Session::pre_export ()
62 {
63         get_export_status (); // Init export_status
64
65         /* take everyone out of awrite to avoid disasters */
66
67         {
68                 boost::shared_ptr<RouteList> r = routes.reader ();
69
70                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
71                         (*i)->protect_automation ();
72                 }
73         }
74
75         /* make sure we are actually rolling */
76
77         if (get_record_enabled()) {
78                 disable_record (false);
79         }
80
81         /* no slaving */
82
83         post_export_sync = config.get_external_sync ();
84         post_export_position = _transport_frame;
85
86         config.set_external_sync (false);
87
88         _exporting = true;
89         export_status->running = true;
90         export_status->Aborting.connect_same_thread (*this, boost::bind (&Session::stop_audio_export, this));
91         export_status->Finished.connect_same_thread (*this, boost::bind (&Session::finalize_audio_export, this));
92
93         return 0;
94 }
95
96 /** Called for each range that is being exported */
97 int
98 Session::start_audio_export (framepos_t position, bool /* realtime */)
99 {
100         if (!_exporting) {
101                 pre_export ();
102         }
103
104         /* We're about to call Track::seek, so the butler must have finished everything
105            up otherwise it could be doing do_refill in its thread while we are doing
106            it here.
107         */
108         
109         _butler->wait_until_finished ();
110
111         /* get everyone to the right position */
112
113         {
114                 boost::shared_ptr<RouteList> rl = routes.reader();
115
116                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
117                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
118                         if (tr && tr->seek (position, true)) {
119                                 error << string_compose (_("%1: cannot seek to %2 for export"),
120                                                   (*i)->name(), position)
121                                       << endmsg;
122                                 return -1;
123                         }
124                 }
125         }
126
127         /* we just did the core part of a locate() call above, but
128            for the sake of any GUI, put the _transport_frame in
129            the right place too.
130         */
131
132         _transport_frame = position;
133         export_status->stop = false;
134
135         /* get transport ready. note how this is calling butler functions
136            from a non-butler thread. we waited for the butler to stop
137            what it was doing earlier in Session::pre_export() and nothing
138            since then has re-awakened it.
139          */
140
141         set_transport_speed (1.0, false);
142         butler_transport_work ();
143         g_atomic_int_set (&_butler->should_do_transport_work, 0);
144         post_transport ();
145
146         /* we are ready to go ... */
147
148         if (!_engine.connected()) {
149                 return -1;
150         }
151
152         _engine.Freewheel.connect_same_thread (export_freewheel_connection, boost::bind (&Session::process_export_fw, this, _1));
153         _export_rolling = true;
154         return _engine.freewheel (true);
155 }
156
157 void
158 Session::process_export (pframes_t nframes)
159 {
160         if (_export_rolling && export_status->stop) {
161                 stop_audio_export ();
162         }
163
164         if (_export_rolling) {
165                 /* make sure we've caught up with disk i/o, since
166                 we're running faster than realtime c/o JACK.
167                 */
168                 _butler->wait_until_finished ();
169
170                 /* do the usual stuff */
171
172                 process_without_events (nframes);
173         }
174
175         try {
176                 /* handle export - XXX what about error handling? */
177
178                 ProcessExport (nframes);
179
180         } catch (std::exception & e) {
181                 error << string_compose (_("Export ended unexpectedly: %1"), e.what()) << endmsg;
182                 export_status->abort (true);
183         }
184 }
185
186 int
187 Session::process_export_fw (pframes_t nframes)
188 {
189         _engine.main_thread()->get_buffers ();
190         process_export (nframes);
191         _engine.main_thread()->drop_buffers ();
192         return 0;
193 }
194
195 int
196 Session::stop_audio_export ()
197 {
198         /* can't use stop_transport() here because we need
199            an immediate halt and don't require all the declick
200            stuff that stop_transport() implements.
201         */
202
203         realtime_stop (true, true);
204         _export_rolling = false;
205         _butler->schedule_transport_work ();
206
207         if (export_status->aborted()) {
208                 finalize_audio_export ();
209         }
210
211         return 0;
212
213 }
214
215 void
216 Session::finalize_audio_export ()
217 {
218         _exporting = false;
219         _export_rolling = false;
220
221         /* Clean up */
222
223         _engine.freewheel (false);
224         export_freewheel_connection.disconnect();
225         
226         /* maybe write CUE/TOC */
227
228         export_handler.reset();
229         export_status.reset();
230
231         /* restart slaving */
232
233         if (post_export_sync) {
234                 config.set_external_sync (true);
235         } else {
236                 locate (post_export_position, false, false, false, false, false);
237         }
238 }