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