Fly my pretties!
[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 void
98 Editor::write_a_region ()
99 {
100         if (clicked_regionview == 0) {
101                 return;
102         }
103
104         FileSelection file_selector;
105
106         file_selector.get_selection_entry()->activate.connect (bind (slot (*this, &Editor::finish_sub_event_loop), 1));
107         file_selector.get_cancel_button()-.signal_clicked().connect (bind (slot (*this, &Editor::finish_sub_event_loop), -1));
108         file_selector.get_ok_button()-.signal_clicked().connect (bind (slot (*this, &Editor::finish_sub_event_loop), 1));
109         file_selector.delete_event.connect (bind (slot (*this, &Editor::finish_sub_event_loop_on_delete), -1));
110
111         file_selector.show_all();
112
113         run_sub_event_loop ();
114         
115         if (sub_event_loop_status == 1) {
116                 string path = file_selector.get_filename();
117                 printf ("got region: %s\n", path.c_str());
118                 if (path.length()) {
119                         write_region (path, clicked_regionview->region);
120                 } 
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         FileSource* 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         jack_nframes_t pos;
163         char s[PATH_MAX+1];
164         uint32_t cnt;
165         vector<FileSource *> sources;
166         uint32_t nchans;
167         
168         nchans = region.n_channels();
169         
170         /* don't do duplicate of the entire source if that's what is going on here */
171
172         if (region.start() == 0 && region.length() == region.source().length()) {
173                 /* XXX should link(2) to create a new inode with "path" */
174                 return true;
175         }
176
177         if (path.length() == 0) {
178
179                 for (uint32_t n=0; n < nchans; ++n) {
180                         
181                         for (cnt = 0; cnt < 999999; ++cnt) {
182                                 if (nchans == 1) {
183                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
184                                                   legalize_for_path(region.name()).c_str(), cnt);
185                                 }
186                                 else {
187                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
188                                                   legalize_for_path(region.name()).c_str(), cnt, n);
189                                 }
190
191                                 path = s;
192                                 
193                                 if (::access (path.c_str(), F_OK) != 0) {
194                                         break;
195                                 }
196                         }
197                         
198                         if (cnt == 999999) {
199                                 error << "" << endmsg;
200                                 goto error_out;
201                         }
202                         
203                 
204                         
205                         try {
206                                 fs = new FileSource (path, session->frame_rate());
207                         }
208                         
209                         catch (failed_constructor& err) {
210                                 goto error_out;
211                         }
212
213                         sources.push_back (fs);
214                 }
215         }
216         else {
217                 /* TODO: make filesources based on passed path */
218
219         }
220         
221         to_read = region.length();
222         pos = region.position();
223
224         while (to_read) {
225                 jack_nframes_t this_time;
226
227                 this_time = min (to_read, chunk_size);
228
229                 for (vector<FileSource *>::iterator src=sources.begin(); src != sources.end(); ++src) {
230
231                         fs = (*src);
232                         
233                         if (region.read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
234                                 break;
235                         }
236                         
237                         if (fs->write (buf, this_time) != this_time) {
238                                 error << "" << endmsg;
239                                 goto error_out;
240                         }
241                 }
242
243                 to_read -= this_time;
244                 pos += this_time;
245         }
246
247         time_t tnow;
248         struct tm* now;
249         time (&tnow);
250         now = localtime (&tnow);
251         
252         for (vector<FileSource *>::iterator src = sources.begin(); src != sources.end(); ++src) {
253                 (*src)->update_header (0, *now, tnow);
254         }
255
256         return true;
257
258 error_out:
259
260         for (vector<FileSource*>::iterator i = sources.begin(); i != sources.end(); ++i) {
261                 (*i)->mark_for_remove ();
262                 delete (*i);
263         }
264
265         return 0;
266 }
267
268 int
269 Editor::write_audio_selection (TimeSelection& ts)
270 {
271         int ret = 0;
272
273         if (selection->tracks.empty()) {
274                 return 0;
275         }
276
277         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
278
279                 AudioTimeAxisView* atv;
280
281                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
282                         continue;
283                 }
284
285                 if (atv->is_audio_track()) {
286
287                         Playlist* playlist = atv->get_diskstream()->playlist();
288                         
289                         if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
290                                 ret = -1;
291                                 break;
292                         }
293                 }
294         }
295
296         return ret;
297 }
298
299 bool
300 Editor::write_audio_range (Playlist& playlist, uint32_t channels, list<AudioRange>& range)
301 {
302         FileSource* fs;
303         const jack_nframes_t chunk_size = 4096;
304         jack_nframes_t nframes;
305         Sample buf[chunk_size];
306         gain_t gain_buffer[chunk_size];
307         jack_nframes_t pos;
308         char s[PATH_MAX+1];
309         uint32_t cnt;
310         string path;
311         vector<FileSource *> sources;
312
313         for (uint32_t n=0; n < channels; ++n) {
314                 
315                 for (cnt = 0; cnt < 999999; ++cnt) {
316                         if (channels == 1) {
317                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
318                                           legalize_for_path(playlist.name()).c_str(), cnt);
319                         }
320                         else {
321                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
322                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
323                         }
324                         
325                         if (::access (s, F_OK) != 0) {
326                                 break;
327                         }
328                 }
329                 
330                 if (cnt == 999999) {
331                         error << "" << endmsg;
332                         goto error_out;
333                 }
334
335                 path = s;
336                 
337                 try {
338                         fs = new FileSource (path, session->frame_rate());
339                 }
340                 
341                 catch (failed_constructor& err) {
342                         goto error_out;
343                 }
344                 
345                 sources.push_back (fs);
346
347         }
348         
349
350         for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
351         
352                 nframes = (*i).length();
353                 pos = (*i).start;
354                 
355                 while (nframes) {
356                         jack_nframes_t this_time;
357                         
358                         this_time = min (nframes, chunk_size);
359
360                         for (uint32_t n=0; n < channels; ++n) {
361
362                                 fs = sources[n];
363                                 
364                                 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
365                                         break;
366                                 }
367                                 
368                                 if (fs->write (buf, this_time) != this_time) {
369                                         goto error_out;
370                                 }
371                         }
372                         
373                         nframes -= this_time;
374                         pos += this_time;
375                 }
376                 
377                 list<AudioRange>::iterator tmp = i;
378                 ++tmp;
379
380                 if (tmp != range.end()) {
381                         
382                         /* fill gaps with silence */
383                         
384                         nframes = (*tmp).start - (*i).end;
385
386                         while (nframes) {
387
388                                 jack_nframes_t this_time = min (nframes, chunk_size);
389                                 memset (buf, 0, sizeof (Sample) * this_time);
390
391                                 for (uint32_t n=0; n < channels; ++n) {
392
393                                         fs = sources[n];
394                                         if (fs->write (buf, this_time) != this_time) {
395                                                 goto error_out;
396                                         }
397                                 }
398
399                                 nframes -= this_time;
400                         }
401                 }
402
403                 i = tmp;
404         }
405
406         time_t tnow;
407         struct tm* now;
408         time (&tnow);
409         now = localtime (&tnow);
410
411         for (uint32_t n=0; n < channels; ++n) {
412                 sources[n]->update_header (0, *now, tnow);
413                 // do we need to ref it again?
414         }
415         
416         return true;
417
418 error_out:
419         /* unref created files */
420
421         for (vector<FileSource*>::iterator i = sources.begin(); i != sources.end(); ++i) {
422                 (*i)->mark_for_remove ();
423                 delete *i;
424         }
425
426         return false;
427 }
428
429 void
430 Editor::write_selection ()
431 {
432         if (!selection->time.empty()) {
433                 write_audio_selection (selection->time);
434         } else if (!selection->audio_regions.empty()) {
435                 write_region_selection (selection->audio_regions);
436         }
437 }