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