7e26ba37b2021a6ba7a2cd765a94631ac2ff71d7
[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 "audio_region_view.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 (RegionSelection& regions)
127 {
128         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
129                 // FIXME
130                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
131                 if (arv)
132                         if (write_region ("", arv->audio_region()) == false)
133                                 return -1;
134         }
135
136         return 0;
137 }
138
139 void
140 Editor::bounce_region_selection ()
141 {
142         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
143                 
144                 Region& region ((*i)->region());
145                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
146                 Track* track = dynamic_cast<Track*>(rtv->route().get());
147
148                 InterThreadInfo itt;
149
150                 itt.done = false;
151                 itt.cancel = false;
152                 itt.progress = 0.0f;
153
154                 track->bounce_range (region.position(), region.position() + region.length(), itt);
155         }
156 }
157
158 bool
159 Editor::write_region (string path, AudioRegion& region)
160 {
161         AudioFileSource* fs;
162         const jack_nframes_t chunk_size = 4096;
163         jack_nframes_t to_read;
164         Sample buf[chunk_size];
165         gain_t gain_buffer[chunk_size];
166         char   workbuf[chunk_size *4];
167         jack_nframes_t pos;
168         char s[PATH_MAX+1];
169         uint32_t cnt;
170         vector<AudioFileSource *> sources;
171         uint32_t nchans;
172         
173         nchans = region.n_channels();
174         
175         /* don't do duplicate of the entire source if that's what is going on here */
176
177         if (region.start() == 0 && region.length() == region.source().length()) {
178                 /* XXX should link(2) to create a new inode with "path" */
179                 return true;
180         }
181
182         if (path.length() == 0) {
183
184                 for (uint32_t n=0; n < nchans; ++n) {
185                         
186                         for (cnt = 0; cnt < 999999; ++cnt) {
187                                 if (nchans == 1) {
188                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
189                                                   legalize_for_path(region.name()).c_str(), cnt);
190                                 }
191                                 else {
192                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
193                                                   legalize_for_path(region.name()).c_str(), cnt, n);
194                                 }
195
196                                 path = s;
197                                 
198                                 if (::access (path.c_str(), F_OK) != 0) {
199                                         break;
200                                 }
201                         }
202                         
203                         if (cnt == 999999) {
204                                 error << "" << endmsg;
205                                 goto error_out;
206                         }
207                         
208                 
209                         
210                         try {
211                                 fs = AudioFileSource::create (path);
212                         }
213                         
214                         catch (failed_constructor& err) {
215                                 goto error_out;
216                         }
217
218                         sources.push_back (fs);
219                 }
220         }
221         else {
222                 /* TODO: make filesources based on passed path */
223
224         }
225         
226         to_read = region.length();
227         pos = region.position();
228
229         while (to_read) {
230                 jack_nframes_t this_time;
231
232                 this_time = min (to_read, chunk_size);
233
234                 for (vector<AudioFileSource *>::iterator src=sources.begin(); src != sources.end(); ++src) {
235                         
236                         fs = (*src);
237
238                         if (region.read_at (buf, buf, gain_buffer, workbuf, pos, this_time) != this_time) {
239                                 break;
240                         }
241                         
242                         if (fs->write (buf, this_time, workbuf) != this_time) {
243                                 error << "" << endmsg;
244                                 goto error_out;
245                         }
246                 }
247
248                 to_read -= this_time;
249                 pos += this_time;
250         }
251
252         time_t tnow;
253         struct tm* now;
254         time (&tnow);
255         now = localtime (&tnow);
256         
257         for (vector<AudioFileSource *>::iterator src = sources.begin(); src != sources.end(); ++src) {
258                 (*src)->update_header (0, *now, tnow);
259         }
260
261         return true;
262
263 error_out:
264
265         for (vector<AudioFileSource*>::iterator i = sources.begin(); i != sources.end(); ++i) {
266                 
267                 (*i)->mark_for_remove ();
268                 delete (*i);
269         }
270
271         return 0;
272 }
273
274 int
275 Editor::write_audio_selection (TimeSelection& ts)
276 {
277         int ret = 0;
278
279         if (selection->tracks.empty()) {
280                 return 0;
281         }
282
283         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
284
285                 AudioTimeAxisView* atv;
286
287                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
288                         continue;
289                 }
290
291                 if (atv->is_audio_track()) {
292
293                         AudioPlaylist* playlist = dynamic_cast<AudioPlaylist*>(atv->get_diskstream()->playlist());
294                         
295                         if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
296                                 ret = -1;
297                                 break;
298                         }
299                 }
300         }
301
302         return ret;
303 }
304
305 bool
306 Editor::write_audio_range (AudioPlaylist& playlist, uint32_t channels, list<AudioRange>& range)
307 {
308         AudioFileSource* fs;
309         const jack_nframes_t chunk_size = 4096;
310         jack_nframes_t nframes;
311         Sample buf[chunk_size];
312         gain_t gain_buffer[chunk_size];
313         char   workbuf[chunk_size*4];
314         jack_nframes_t pos;
315         char s[PATH_MAX+1];
316         uint32_t cnt;
317         string path;
318         vector<AudioFileSource *> sources;
319
320         for (uint32_t n=0; n < channels; ++n) {
321                 
322                 for (cnt = 0; cnt < 999999; ++cnt) {
323                         if (channels == 1) {
324                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
325                                           legalize_for_path(playlist.name()).c_str(), cnt);
326                         }
327                         else {
328                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
329                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
330                         }
331                         
332                         if (::access (s, F_OK) != 0) {
333                                 break;
334                         }
335                 }
336                 
337                 if (cnt == 999999) {
338                         error << "" << endmsg;
339                         goto error_out;
340                 }
341
342                 path = s;
343                 
344                 try {
345                         fs = AudioFileSource::create (path);
346                 }
347                 
348                 catch (failed_constructor& err) {
349                         goto error_out;
350                 }
351                 
352                 sources.push_back (fs);
353
354         }
355         
356
357         for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
358         
359                 nframes = (*i).length();
360                 pos = (*i).start;
361                 
362                 while (nframes) {
363                         jack_nframes_t this_time;
364                         
365                         this_time = min (nframes, chunk_size);
366
367                         for (uint32_t n=0; n < channels; ++n) {
368
369                                 fs = sources[n];
370                                 
371                                 if (playlist.read (buf, buf, gain_buffer, workbuf, pos, this_time, n) != this_time) {
372                                         break;
373                                 }
374                                 
375                                 if (fs->write (buf, this_time, workbuf) != this_time) {
376                                         goto error_out;
377                                 }
378                         }
379                         
380                         nframes -= this_time;
381                         pos += this_time;
382                 }
383                 
384                 list<AudioRange>::iterator tmp = i;
385                 ++tmp;
386
387                 if (tmp != range.end()) {
388                         
389                         /* fill gaps with silence */
390                         
391                         nframes = (*tmp).start - (*i).end;
392
393                         while (nframes) {
394
395                                 jack_nframes_t this_time = min (nframes, chunk_size);
396                                 memset (buf, 0, sizeof (Sample) * this_time);
397
398                                 for (uint32_t n=0; n < channels; ++n) {
399
400                                         fs = sources[n];
401                                         if (fs->write (buf, this_time, workbuf) != this_time) {
402                                                 goto error_out;
403                                         }
404                                 }
405
406                                 nframes -= this_time;
407                         }
408                 }
409
410                 i = tmp;
411         }
412
413         time_t tnow;
414         struct tm* now;
415         time (&tnow);
416         now = localtime (&tnow);
417
418         for (uint32_t n=0; n < channels; ++n) {
419                 sources[n]->update_header (0, *now, tnow);
420                 // do we need to ref it again?
421         }
422         
423         return true;
424
425 error_out:
426         /* unref created files */
427
428         for (vector<AudioFileSource*>::iterator i = sources.begin(); i != sources.end(); ++i) {
429                 (*i)->mark_for_remove ();
430                 delete *i;
431         }
432
433         return false;
434 }
435
436 void
437 Editor::write_selection ()
438 {
439         if (!selection->time.empty()) {
440                 write_audio_selection (selection->time);
441         } else if (!selection->regions.empty()) {
442                 write_region_selection (selection->regions);
443         }
444 }