One fix.
[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/threads.h>
23
24 #include <midi++/mmc.h>
25
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"
33
34 #include "pbd/i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 boost::shared_ptr<ExportHandler>
41 Session::get_export_handler ()
42 {
43         if (!export_handler) {
44                 export_handler.reset (new ExportHandler (*this));
45         }
46
47         return export_handler;
48 }
49
50 boost::shared_ptr<ExportStatus>
51 Session::get_export_status ()
52 {
53         if (!export_status) {
54                 export_status.reset (new ExportStatus ());
55         }
56
57         return export_status;
58 }
59
60
61 int
62 Session::pre_export ()
63 {
64         get_export_status (); // Init export_status
65
66         /* take everyone out of awrite to avoid disasters */
67
68         {
69                 boost::shared_ptr<RouteList> r = routes.reader ();
70
71                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
72                         (*i)->protect_automation ();
73                 }
74         }
75
76         /* prepare transport */
77
78         realtime_stop (true, true);
79
80         if (get_record_enabled()) {
81                 disable_record (false);
82         }
83
84         unset_play_loop ();
85
86         /* no slaving */
87
88         post_export_sync = config.get_external_sync ();
89         post_export_position = _transport_frame;
90
91         config.set_external_sync (false);
92
93         _exporting = true;
94         export_status->set_running (true);
95         export_status->Finished.connect_same_thread (*this, boost::bind (&Session::finalize_audio_export, this));
96
97         /* disable MMC output early */
98
99         _pre_export_mmc_enabled = _mmc->send_enabled ();
100         _mmc->enable_send (false);
101
102         return 0;
103 }
104
105 /** Called for each range that is being exported */
106 int
107 Session::start_audio_export (framepos_t position, bool realtime, bool region_export, bool comensate_master_latency)
108 {
109         if (!_exporting) {
110                 pre_export ();
111         }
112
113         _realtime_export = realtime;
114         _region_export = region_export;
115
116         if (region_export) {
117                 _export_preroll = 0;
118         }
119         else if (realtime) {
120                 _export_preroll = nominal_frame_rate ();
121         } else {
122                 _export_preroll = Config->get_export_preroll() * nominal_frame_rate ();
123         }
124
125         if (_export_preroll == 0) {
126                 // must be > 0 so that transport is started in sync.
127                 _export_preroll = 1;
128         }
129
130         /* "worst_track_latency" is the correct value for stem-exports
131          * see to Route::add_export_point(),
132          *
133          * For master-bus export, we also need to add the master's latency.
134          * (or actually longest-total-session-latency - worst-track-latency)
135          * to align the export to 00:00:00:00.
136          *
137          * We must not use worst_playback_latency because that
138          * includes external (hardware) latencies and would overcompensate
139          * during file-export.
140          *
141          * (this is all still very [w]hacky. Individual Bus and Track outputs
142          * are not aligned but one can select them in the PortExportChannelSelector)
143          */
144         _export_latency = worst_track_latency ();
145
146         boost::shared_ptr<Route> master = master_out ();
147         if (master && comensate_master_latency) {
148                 _export_latency += master->signal_latency ();
149         }
150
151         if (region_export) {
152                 _export_latency = 0;
153         }
154
155         /* We're about to call Track::seek, so the butler must have finished everything
156            up otherwise it could be doing do_refill in its thread while we are doing
157            it here.
158         */
159
160         _butler->wait_until_finished ();
161
162         /* get everyone to the right position */
163
164         {
165                 boost::shared_ptr<RouteList> rl = routes.reader();
166
167                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
168                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
169                         if (tr && tr->seek (position, true)) {
170                                 error << string_compose (_("%1: cannot seek to %2 for export"),
171                                                   (*i)->name(), position)
172                                       << endmsg;
173                                 return -1;
174                         }
175                 }
176         }
177
178         /* we just did the core part of a locate() call above, but
179            for the sake of any GUI, put the _transport_frame in
180            the right place too.
181         */
182
183         _transport_frame = position;
184         export_status->stop = false;
185
186         /* get transport ready. note how this is calling butler functions
187            from a non-butler thread. we waited for the butler to stop
188            what it was doing earlier in Session::pre_export() and nothing
189            since then has re-awakened it.
190          */
191
192         /* we are ready to go ... */
193
194         if (!_engine.connected()) {
195                 return -1;
196         }
197
198         _engine.Freewheel.connect_same_thread (export_freewheel_connection, boost::bind (&Session::process_export_fw, this, _1));
199
200         if (_realtime_export) {
201                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
202                 _export_rolling = true;
203                 process_function = &Session::process_export_fw;
204                 return 0;
205         } else {
206                 _export_rolling = true;
207                 return _engine.freewheel (true);
208         }
209 }
210
211 void
212 Session::process_export (pframes_t nframes)
213 {
214         if (_export_rolling && export_status->stop) {
215                 stop_audio_export ();
216         }
217
218         /* for Region Raw or Fades, we can skip this
219          * RegionExportChannelFactory::update_buffers() does not care
220          * about anything done here
221          */
222         if (!_region_export) {
223                 if (_export_rolling) {
224                         if (!_realtime_export)  {
225                                 /* make sure we've caught up with disk i/o, since
226                                  * we're running faster than realtime c/o JACK.
227                                  */
228                                 _butler->wait_until_finished ();
229                         }
230
231                         /* do the usual stuff */
232
233                         process_without_events (nframes);
234
235                 } else if (_realtime_export) {
236                         fail_roll (nframes); // somehow we need to silence _ALL_ output buffers
237                 }
238         }
239
240         try {
241                 /* handle export - XXX what about error handling? */
242
243                 ProcessExport (nframes);
244
245         } catch (std::exception & e) {
246                 error << string_compose (_("Export ended unexpectedly: %1"), e.what()) << endmsg;
247                 export_status->abort (true);
248         }
249 }
250
251 void
252 Session::process_export_fw (pframes_t nframes)
253 {
254         const bool need_buffers = _engine.freewheeling ();
255         if (_export_preroll > 0) {
256
257                 if (need_buffers) {
258                         _engine.main_thread()->get_buffers ();
259                 }
260                 fail_roll (nframes);
261                 if (need_buffers) {
262                         _engine.main_thread()->drop_buffers ();
263                 }
264
265                 _export_preroll -= std::min ((framepos_t)nframes, _export_preroll);
266
267                 if (_export_preroll > 0) {
268                         // clear out buffers (reverb tails etc).
269                         return;
270                 }
271
272                 set_transport_speed (1.0, 0, false);
273                 butler_transport_work ();
274                 g_atomic_int_set (&_butler->should_do_transport_work, 0);
275                 post_transport ();
276
277                 return;
278         }
279
280         if (_export_latency > 0) {
281                 framepos_t remain = std::min ((framepos_t)nframes, _export_latency);
282
283                 if (need_buffers) {
284                         _engine.main_thread()->get_buffers ();
285                 }
286                 process_without_events (remain);
287                 if (need_buffers) {
288                         _engine.main_thread()->drop_buffers ();
289                 }
290
291                 _export_latency -= remain;
292                 nframes -= remain;
293
294                 if (nframes == 0) {
295                         return;
296                 }
297         }
298
299         if (need_buffers) {
300                 _engine.main_thread()->get_buffers ();
301         }
302         process_export (nframes);
303         if (need_buffers) {
304                 _engine.main_thread()->drop_buffers ();
305         }
306
307         return;
308 }
309
310 int
311 Session::stop_audio_export ()
312 {
313         /* can't use stop_transport() here because we need
314            an immediate halt and don't require all the declick
315            stuff that stop_transport() implements.
316         */
317
318         realtime_stop (true, true);
319         _export_rolling = false;
320         _butler->schedule_transport_work ();
321
322         return 0;
323 }
324
325 void
326 Session::finalize_audio_export ()
327 {
328         _exporting = false;
329
330         if (_export_rolling) {
331                 stop_audio_export ();
332         }
333
334         /* Clean up */
335
336         if (_realtime_export) {
337                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
338                 process_function = &Session::process_with_events;
339         }
340         _engine.freewheel (false);
341         export_freewheel_connection.disconnect();
342
343         _mmc->enable_send (_pre_export_mmc_enabled);
344
345         /* maybe write CUE/TOC */
346
347         export_handler.reset();
348         export_status.reset();
349
350         /* restart slaving */
351
352         if (post_export_sync) {
353                 config.set_external_sync (true);
354         } else {
355                 locate (post_export_position, false, false, false, false, false);
356         }
357 }