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