make ARDOUR2_UI_RC work again
[ardour.git] / gtk2_ardour / editor_export_audio.cc
1 /*
2     Copyright (C) 2001 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 /* Note: public Editor methods are documented in public_editor.h */
21
22 #include <unistd.h>
23 #include <climits>
24
25 #include <gtkmm/messagedialog.h>
26
27 #include "export_session_dialog.h"
28 #include "export_region_dialog.h"
29 #include "export_range_markers_dialog.h"
30 #include "editor.h"
31 #include "public_editor.h"
32 #include "selection.h"
33 #include "time_axis_view.h"
34 #include "audio_time_axis.h"
35 #include "audio_region_view.h"
36
37 #include <pbd/pthread_utils.h>
38 #include <ardour/types.h>
39 #include <ardour/export.h>
40 #include <ardour/audio_track.h>
41 #include <ardour/audiofilesource.h>
42 #include <ardour/audio_diskstream.h>
43 #include <ardour/audioregion.h>
44 #include <ardour/audioplaylist.h>
45 #include <ardour/chan_count.h>
46 #include <ardour/source_factory.h>
47 #include <ardour/audiofilesource.h>
48
49 #include "i18n.h"
50
51 using namespace std;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Gtk;
55
56 void
57 Editor::export_session()
58 {
59         if (session) {
60                 export_range (session->current_start_frame(), session->current_end_frame());
61         }
62 }
63
64 void
65 Editor::export_selection ()
66 {
67         if (session) {
68                 if (selection->time.empty()) {
69                         MessageDialog message (*this, _("There is no selection to export.\n\nSelect a selection using the range mouse mode"));
70                         message.run ();
71                         return;
72                 }
73
74                 export_range (selection->time.front().start, selection->time.front().end);
75         }
76 }
77
78 void
79 Editor::export_range (nframes_t start, nframes_t end)
80 {
81         if (session) {
82                 if (export_dialog == 0) {
83                         export_dialog = new ExportSessionDialog (*this);
84                 }
85                 
86                 export_dialog->connect_to_session (session);
87                 export_dialog->set_range (start, end);
88                 export_dialog->start_export();
89         }
90 }       
91
92 /** Export the first selected region */
93 void
94 Editor::export_region ()
95 {
96         if (selection->regions.empty()) {
97                 return;
98         }
99
100         boost::shared_ptr<Region> r = selection->regions.front()->region();
101         
102         ExportDialog* dialog = new ExportRegionDialog (*this, r);
103                 
104         dialog->connect_to_session (session);
105         dialog->set_range (r->first_frame(), r->last_frame());
106         dialog->start_export();
107 }
108
109 void
110 Editor::export_range_markers ()
111 {
112         if (session) {
113
114                 if (session->locations()->num_range_markers() == 0) {
115                         MessageDialog message (*this, _("There are no ranges to export.\n\nCreate 1 or more ranges by dragging the mouse in the range bar"));
116                         message.run ();
117                         return;
118                 }
119                 
120
121                 if (export_range_markers_dialog == 0) {
122                         export_range_markers_dialog = new ExportRangeMarkersDialog(*this);
123                 }
124                 
125                 export_range_markers_dialog->connect_to_session (session);
126                 export_range_markers_dialog->start_export();
127         }
128 }       
129
130 int
131 Editor::write_region_selection (RegionSelection& regions)
132 {
133         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
134                 // FIXME
135                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
136                 if (arv)
137                         if (write_region ("", arv->audio_region()) == false)
138                                 return -1;
139         }
140
141         return 0;
142 }
143
144 void
145 Editor::bounce_region_selection ()
146 {
147         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
148                 
149                 boost::shared_ptr<Region> region ((*i)->region());
150                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
151                 Track* track = dynamic_cast<Track*>(rtv->route().get());
152
153                 InterThreadInfo itt;
154
155                 itt.done = false;
156                 itt.cancel = false;
157                 itt.progress = 0.0f;
158
159                 track->bounce_range (region->position(), region->position() + region->length(), itt);
160         }
161 }
162
163 bool
164 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
165 {
166         boost::shared_ptr<AudioFileSource> fs;
167         const nframes_t chunk_size = 4096;
168         nframes_t to_read;
169         Sample buf[chunk_size];
170         gain_t gain_buffer[chunk_size];
171         nframes_t pos;
172         char s[PATH_MAX+1];
173         uint32_t cnt;
174         vector<boost::shared_ptr<AudioFileSource> > sources;
175         uint32_t nchans;
176         
177         nchans = region->n_channels();
178         
179         /* don't do duplicate of the entire source if that's what is going on here */
180
181         if (region->start() == 0 && region->length() == region->source()->length()) {
182                 /* XXX should link(2) to create a new inode with "path" */
183                 return true;
184         }
185
186         if (path.length() == 0) {
187
188                 for (uint32_t n=0; n < nchans; ++n) {
189                         
190                         for (cnt = 0; cnt < 999999; ++cnt) {
191                                 if (nchans == 1) {
192                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
193                                                   legalize_for_path(region->name()).c_str(), cnt);
194                                 }
195                                 else {
196                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
197                                                   legalize_for_path(region->name()).c_str(), cnt, n);
198                                 }
199
200                                 path = s;
201                                 
202                                 if (::access (path.c_str(), F_OK) != 0) {
203                                         break;
204                                 }
205                         }
206                         
207                         if (cnt == 999999) {
208                                 error << "" << endmsg;
209                                 goto error_out;
210                         }
211                         
212                 
213                         
214                         try {
215                                 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *session, path, false, session->frame_rate()));
216                         }
217                         
218                         catch (failed_constructor& err) {
219                                 goto error_out;
220                         }
221
222                         sources.push_back (fs);
223                 }
224         }
225         else {
226                 /* TODO: make filesources based on passed path */
227
228         }
229         
230         to_read = region->length();
231         pos = region->position();
232
233         while (to_read) {
234                 nframes_t this_time;
235
236                 this_time = min (to_read, chunk_size);
237
238                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
239                         
240                         fs = (*src);
241
242                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
243                                 break;
244                         }
245                         
246                         if (fs->write (buf, this_time) != this_time) {
247                                 error << "" << endmsg;
248                                 goto error_out;
249                         }
250                 }
251
252                 to_read -= this_time;
253                 pos += this_time;
254         }
255
256         time_t tnow;
257         struct tm* now;
258         time (&tnow);
259         now = localtime (&tnow);
260         
261         for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
262                 (*src)->update_header (0, *now, tnow);
263                 (*src)->mark_immutable ();
264         }
265
266         return true;
267
268 error_out:
269
270         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
271                 (*i)->mark_for_remove ();
272         }
273
274         return 0;
275 }
276
277 int
278 Editor::write_audio_selection (TimeSelection& ts)
279 {
280         int ret = 0;
281
282         if (selection->tracks.empty()) {
283                 return 0;
284         }
285
286         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
287
288                 AudioTimeAxisView* atv;
289
290                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
291                         continue;
292                 }
293
294                 if (atv->is_audio_track()) {
295
296                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->get_diskstream()->playlist());
297                         
298                         if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
299                                 ret = -1;
300                                 break;
301                         }
302                 }
303         }
304
305         return ret;
306 }
307
308 bool
309 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
310 {
311         boost::shared_ptr<AudioFileSource> fs;
312         const nframes_t chunk_size = 4096;
313         nframes_t nframes;
314         Sample buf[chunk_size];
315         gain_t gain_buffer[chunk_size];
316         nframes_t pos;
317         char s[PATH_MAX+1];
318         uint32_t cnt;
319         string path;
320         vector<boost::shared_ptr<AudioFileSource> > sources;
321
322         uint32_t channels = count.n_audio();
323
324         for (uint32_t n=0; n < channels; ++n) {
325                 
326                 for (cnt = 0; cnt < 999999; ++cnt) {
327                         if (channels == 1) {
328                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
329                                           legalize_for_path(playlist.name()).c_str(), cnt);
330                         }
331                         else {
332                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
333                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
334                         }
335                         
336                         if (::access (s, F_OK) != 0) {
337                                 break;
338                         }
339                 }
340                 
341                 if (cnt == 999999) {
342                         error << "" << endmsg;
343                         goto error_out;
344                 }
345
346                 path = s;
347                 
348                 try {
349                         fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *session, path, false, session->frame_rate()));
350                 }
351                 
352                 catch (failed_constructor& err) {
353                         goto error_out;
354                 }
355                 
356                 sources.push_back (fs);
357
358         }
359         
360
361         for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
362         
363                 nframes = (*i).length();
364                 pos = (*i).start;
365                 
366                 while (nframes) {
367                         nframes_t this_time;
368                         
369                         this_time = min (nframes, chunk_size);
370
371                         for (uint32_t n=0; n < channels; ++n) {
372
373                                 fs = sources[n];
374                                 
375                                 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
376                                         break;
377                                 }
378                                 
379                                 if (fs->write (buf, this_time) != this_time) {
380                                         goto error_out;
381                                 }
382                         }
383                         
384                         nframes -= this_time;
385                         pos += this_time;
386                 }
387                 
388                 list<AudioRange>::iterator tmp = i;
389                 ++tmp;
390
391                 if (tmp != range.end()) {
392                         
393                         /* fill gaps with silence */
394                         
395                         nframes = (*tmp).start - (*i).end;
396
397                         while (nframes) {
398
399                                 nframes_t this_time = min (nframes, chunk_size);
400                                 memset (buf, 0, sizeof (Sample) * this_time);
401
402                                 for (uint32_t n=0; n < channels; ++n) {
403
404                                         fs = sources[n];
405                                         if (fs->write (buf, this_time) != this_time) {
406                                                 goto error_out;
407                                         }
408                                 }
409
410                                 nframes -= this_time;
411                         }
412                 }
413
414                 i = tmp;
415         }
416
417         time_t tnow;
418         struct tm* now;
419         time (&tnow);
420         now = localtime (&tnow);
421
422         for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
423                 (*s)->update_header (0, *now, tnow);
424                 (*s)->mark_immutable ();
425                 // do we need to ref it again?
426         }
427         
428         return true;
429
430 error_out:
431         /* unref created files */
432
433         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
434                 (*i)->mark_for_remove ();
435         }
436
437         return false;
438 }
439
440 void
441 Editor::write_selection ()
442 {
443         if (!selection->time.empty()) {
444                 write_audio_selection (selection->time);
445         } else if (!selection->regions.empty()) {
446                 write_region_selection (selection->regions);
447         }
448 }