fix crash when copy'ing latent plugins
[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)
108 {
109         if (!_exporting) {
110                 pre_export ();
111         }
112
113         _realtime_export = realtime;
114
115         if (realtime) {
116                 _export_preroll = nominal_frame_rate ();
117         } else {
118                 _export_preroll = Config->get_export_preroll() * nominal_frame_rate ();
119         }
120
121         if (_export_preroll == 0) {
122                 // must be > 0 so that transport is started in sync.
123                 _export_preroll = 1;
124         }
125
126         /* "worst_track_latency" is the correct value for stem-exports
127          * see to Route::add_export_point(),
128          *
129          * for master-bus export, we'd need to add the master's latency.
130          * or actually longest-total-session-latency.
131          *
132          * We can't use worst_playback_latency because that includes
133          * includes external latencies and would overcompensate.
134          */
135         _export_latency = worst_track_latency ();
136
137         /* We're about to call Track::seek, so the butler must have finished everything
138            up otherwise it could be doing do_refill in its thread while we are doing
139            it here.
140         */
141
142         _butler->wait_until_finished ();
143
144         /* get everyone to the right position */
145
146         {
147                 boost::shared_ptr<RouteList> rl = routes.reader();
148
149                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
150                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
151                         if (tr && tr->seek (position, true)) {
152                                 error << string_compose (_("%1: cannot seek to %2 for export"),
153                                                   (*i)->name(), position)
154                                       << endmsg;
155                                 return -1;
156                         }
157                 }
158         }
159
160         /* we just did the core part of a locate() call above, but
161            for the sake of any GUI, put the _transport_frame in
162            the right place too.
163         */
164
165         _transport_frame = position;
166         export_status->stop = false;
167
168         /* get transport ready. note how this is calling butler functions
169            from a non-butler thread. we waited for the butler to stop
170            what it was doing earlier in Session::pre_export() and nothing
171            since then has re-awakened it.
172          */
173
174         /* we are ready to go ... */
175
176         if (!_engine.connected()) {
177                 return -1;
178         }
179
180         _engine.Freewheel.connect_same_thread (export_freewheel_connection, boost::bind (&Session::process_export_fw, this, _1));
181
182         if (_realtime_export) {
183                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
184                 _export_rolling = true;
185                 process_function = &Session::process_export_fw;
186                 return 0;
187         } else {
188                 _export_rolling = true;
189                 return _engine.freewheel (true);
190         }
191 }
192
193 void
194 Session::process_export (pframes_t nframes)
195 {
196         if (_export_rolling && export_status->stop) {
197                 stop_audio_export ();
198         }
199
200         if (_export_rolling) {
201                 if (!_realtime_export)  {
202                         /* make sure we've caught up with disk i/o, since
203                          * we're running faster than realtime c/o JACK.
204                          */
205                         _butler->wait_until_finished ();
206                 }
207
208                 /* do the usual stuff */
209
210                 process_without_events (nframes);
211         } else if (_realtime_export) {
212                 fail_roll (nframes); // somehow we need to silence _ALL_ output buffers
213         }
214
215         try {
216                 /* handle export - XXX what about error handling? */
217
218                 ProcessExport (nframes);
219
220         } catch (std::exception & e) {
221                 error << string_compose (_("Export ended unexpectedly: %1"), e.what()) << endmsg;
222                 export_status->abort (true);
223         }
224 }
225
226 void
227 Session::process_export_fw (pframes_t nframes)
228 {
229         const bool need_buffers = _engine.freewheeling ();
230         if (_export_preroll > 0) {
231
232                 if (need_buffers) {
233                         _engine.main_thread()->get_buffers ();
234                 }
235                 fail_roll (nframes);
236                 if (need_buffers) {
237                         _engine.main_thread()->drop_buffers ();
238                 }
239
240                 _export_preroll -= std::min ((framepos_t)nframes, _export_preroll);
241
242                 if (_export_preroll > 0) {
243                         // clear out buffers (reverb tails etc).
244                         return;
245                 }
246
247                 set_transport_speed (1.0, 0, false);
248                 butler_transport_work ();
249                 g_atomic_int_set (&_butler->should_do_transport_work, 0);
250                 post_transport ();
251
252                 return;
253         }
254
255         if (_export_latency > 0) {
256                 framepos_t remain = std::min ((framepos_t)nframes, _export_latency);
257
258                 if (need_buffers) {
259                         _engine.main_thread()->get_buffers ();
260                 }
261                 process_without_events (remain);
262                 if (need_buffers) {
263                         _engine.main_thread()->drop_buffers ();
264                 }
265
266                 _export_latency -= remain;
267                 nframes -= remain;
268
269                 if (nframes == 0) {
270                         return;
271                 }
272         }
273
274         if (need_buffers) {
275                 _engine.main_thread()->get_buffers ();
276         }
277         process_export (nframes);
278         if (need_buffers) {
279                 _engine.main_thread()->drop_buffers ();
280         }
281
282         return;
283 }
284
285 int
286 Session::stop_audio_export ()
287 {
288         /* can't use stop_transport() here because we need
289            an immediate halt and don't require all the declick
290            stuff that stop_transport() implements.
291         */
292
293         realtime_stop (true, true);
294         _export_rolling = false;
295         _butler->schedule_transport_work ();
296
297         return 0;
298 }
299
300 void
301 Session::finalize_audio_export ()
302 {
303         _exporting = false;
304
305         if (_export_rolling) {
306                 stop_audio_export ();
307         }
308
309         /* Clean up */
310
311         if (_realtime_export) {
312                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
313                 process_function = &Session::process_with_events;
314         }
315         _engine.freewheel (false);
316         export_freewheel_connection.disconnect();
317
318         _mmc->enable_send (_pre_export_mmc_enabled);
319
320         /* maybe write CUE/TOC */
321
322         export_handler.reset();
323         export_status.reset();
324
325         /* restart slaving */
326
327         if (post_export_sync) {
328                 config.set_external_sync (true);
329         } else {
330                 locate (post_export_position, false, false, false, false, false);
331         }
332 }