most of the 2.X->3.0 commit (up to rev 4299) except for gtk2_ardour/editor_canvas...
[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 #include <sigc++/bind.h>
21
22 #include <pbd/error.h>
23 #include <glibmm/thread.h>
24
25 #include <ardour/export_failed.h>
26 #include <ardour/export_file_io.h>
27 #include <ardour/export_utilities.h>
28 #include <ardour/export_handler.h>
29 #include <ardour/export_status.h>
30 #include <ardour/timestamps.h>
31 #include <ardour/ardour.h>
32 #include <ardour/session.h>
33 #include <ardour/audioengine.h>
34 #include <ardour/audio_diskstream.h>
35 #include <ardour/panner.h>
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace ARDOUR;
41 using namespace PBD;
42
43 boost::shared_ptr<ExportHandler>
44 Session::get_export_handler ()
45 {
46         if (!export_handler) {
47                 export_handler.reset (new ExportHandler (*this));
48         }
49         
50         return export_handler;
51 }
52
53 boost::shared_ptr<ExportStatus>
54 Session::get_export_status ()
55 {
56         if (!export_status) {
57                 export_status.reset (new ExportStatus ());
58         }
59         
60         return export_status;
61 }
62
63
64 int
65 Session::pre_export ()
66 {
67         get_export_status (); // Init export_status
68
69         wait_till_butler_finished ();
70
71         /* take everyone out of awrite to avoid disasters */
72
73         {
74                 boost::shared_ptr<RouteList> r = routes.reader ();
75
76                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
77                         (*i)->protect_automation ();
78                 }
79         }
80
81         /* make sure we are actually rolling */
82
83         if (get_record_enabled()) {
84                 disable_record (false);
85         }
86
87         /* no slaving */
88
89         post_export_slave = Config->get_slave_source ();
90         post_export_position = _transport_frame;
91
92         Config->set_slave_source (None);
93         
94         _exporting = true;
95         export_status->running = true;
96         export_status->Aborting.connect (sigc::hide_return (sigc::mem_fun (*this, &Session::stop_audio_export)));
97         export_status->Finished.connect (sigc::hide_return (sigc::mem_fun (*this, &Session::finalize_audio_export)));
98
99         return 0;
100 }
101
102 int
103 Session::start_audio_export (nframes_t position, bool realtime)
104 {
105         if (!_exporting) {
106                 pre_export ();
107         }
108
109         /* get everyone to the right position */
110
111         {
112                 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
113
114                 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
115                         if ((*i)-> seek (position, true)) {
116                                 error << string_compose (_("%1: cannot seek to %2 for export"),
117                                                   (*i)->name(), position)
118                                       << endmsg;
119                                 return -1;
120                         }
121                 }
122         }
123
124         /* we just did the core part of a locate() call above, but
125            for the sake of any GUI, put the _transport_frame in
126            the right place too.
127         */
128
129         _transport_frame = position;
130         
131         _exporting_realtime = realtime;
132         export_status->stop = false;
133
134         /* get transport ready. note how this is calling butler functions
135            from a non-butler thread. we waited for the butler to stop
136            what it was doing earlier in Session::pre_export() and nothing
137            since then has re-awakened it.
138          */
139
140         set_transport_speed (1.0, false);
141         butler_transport_work ();
142         g_atomic_int_set (&butler_should_do_transport_work, 0);
143         post_transport ();
144
145         /* we are ready to go ... */
146         
147         if (!_engine.connected()) {
148                 return -1;
149         }
150
151         if (realtime) {
152                 last_process_function = process_function;
153                 process_function = &Session::process_export;
154         } else {
155                 export_freewheel_connection = _engine.Freewheel.connect (sigc::mem_fun (*this, &Session::process_export_fw));
156                 return _engine.freewheel (true);
157         }
158
159         return 0;
160 }
161
162 void
163 Session::process_export (nframes_t nframes)
164 {
165         try {
166
167                 if (export_status->stop) {
168                         stop_audio_export ();
169                         return;
170                 }
171         
172                 if (!_exporting_realtime) {
173                         /* make sure we've caught up with disk i/o, since
174                         we're running faster than realtime c/o JACK.
175                         */
176                         
177                         wait_till_butler_finished ();
178                 }
179         
180                 /* do the usual stuff */
181         
182                 process_without_events (nframes);
183         
184                 /* handle export */
185         
186                 ProcessExport (nframes);
187
188         } catch (ExportFailed e) {
189                 export_status->abort (true);
190         }
191 }
192
193 int
194 Session::process_export_fw (nframes_t nframes)
195 {
196         process_export (nframes);       
197         return 0;
198 }
199
200 int
201 Session::stop_audio_export ()
202 {
203         if (_exporting_realtime) {
204                 process_function = last_process_function;
205         } else {
206                 export_freewheel_connection.disconnect();
207         }
208
209         /* can't use stop_transport() here because we need
210            an immediate halt and don't require all the declick
211            stuff that stop_transport() implements.
212         */
213
214         realtime_stop (true);
215         schedule_butler_transport_work ();
216
217         if (!export_status->aborted()) {
218                 ExportReadFinished ();
219         } else {
220                 finalize_audio_export ();
221         }
222         
223         return 0;
224
225 }
226
227 void
228 Session::finalize_audio_export ()
229 {
230         _exporting = false;
231         export_status->running = false;
232
233         if (!_exporting_realtime) {
234                 _engine.freewheel (false);
235                 _exporting_realtime = false;
236         }
237
238         /* Clean up */
239         
240         ProcessExport.clear();
241         ExportReadFinished.clear();
242         export_freewheel_connection.disconnect();
243         export_handler.reset();
244         export_status.reset();
245
246         /* restart slaving */
247
248         if (post_export_slave != None) {
249                 Config->set_slave_source (post_export_slave);
250         } else {
251                 locate (post_export_position, false, false, false);
252         }
253 }