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