remove all lines to avoid recompiles after commits
[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 */
19
20 #include <unistd.h>
21 #include <climits>
22
23 #include <gtkmm/messagedialog.h>
24
25 #include "export_session_dialog.h"
26 #include "export_region_dialog.h"
27 #include "export_range_markers_dialog.h"
28 #include "editor.h"
29 #include "public_editor.h"
30 #include "selection.h"
31 #include "time_axis_view.h"
32 #include "audio_time_axis.h"
33 #include "audio_region_view.h"
34
35 #include <pbd/pthread_utils.h>
36 #include <ardour/types.h>
37 #include <ardour/export.h>
38 #include <ardour/audio_track.h>
39 #include <ardour/audiofilesource.h>
40 #include <ardour/audio_diskstream.h>
41 #include <ardour/audioregion.h>
42 #include <ardour/audioplaylist.h>
43 #include <ardour/source_factory.h>
44 #include <ardour/audiofilesource.h>
45
46 #include "i18n.h"
47
48 using namespace std;
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace Gtk;
52
53 void
54 Editor::export_session()
55 {
56         if (session) {
57                 export_range (session->current_start_frame(), session->current_end_frame());
58         }
59 }
60
61 void
62 Editor::export_selection ()
63 {
64         if (session) {
65                 if (selection->time.empty()) {
66                         MessageDialog message (*this, _("There is no selection to export.\n\nSelect a selection using the range mouse mode"));
67                         message.run ();
68                         return;
69                 }
70
71                 export_range (selection->time.front().start, selection->time.front().end);
72         }
73 }
74
75 void
76 Editor::export_range (nframes_t start, nframes_t end)
77 {
78         if (session) {
79                 if (export_dialog == 0) {
80                         export_dialog = new ExportSessionDialog (*this);
81                 }
82                 
83                 export_dialog->connect_to_session (session);
84                 export_dialog->set_range (start, end);
85                 export_dialog->start_export();
86         }
87 }       
88
89 void
90 Editor::export_region ()
91 {
92         if (clicked_regionview == 0) {
93                 return;
94         }
95
96         ExportDialog* dialog = new ExportRegionDialog (*this, clicked_regionview->region());
97                 
98         dialog->connect_to_session (session);
99         dialog->set_range (
100                 clicked_regionview->region()->first_frame(), 
101                 clicked_regionview->region()->last_frame());
102         dialog->start_export();
103 }
104
105 void
106 Editor::export_range_markers ()
107 {
108         if (session) {
109
110                 if (session->locations()->num_range_markers() == 0) {
111                         MessageDialog message (*this, _("There are no ranges to export.\n\nCreate 1 or more ranges by dragging the mouse in the range bar"));
112                         message.run ();
113                         return;
114                 }
115                 
116
117                 if (export_range_markers_dialog == 0) {
118                         export_range_markers_dialog = new ExportRangeMarkersDialog(*this);
119                 }
120                 
121                 export_range_markers_dialog->connect_to_session (session);
122                 export_range_markers_dialog->start_export();
123         }
124 }       
125
126 int
127 Editor::write_region_selection (RegionSelection& regions)
128 {
129         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
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                 boost::shared_ptr<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, boost::shared_ptr<AudioRegion> region)
160 {
161         boost::shared_ptr<AudioFileSource> fs;
162         const nframes_t chunk_size = 4096;
163         nframes_t to_read;
164         Sample buf[chunk_size];
165         gain_t gain_buffer[chunk_size];
166         nframes_t pos;
167         char s[PATH_MAX+1];
168         uint32_t cnt;
169         vector<boost::shared_ptr<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 = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (*session, path, false, session->frame_rate()));
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                 nframes_t this_time;
230
231                 this_time = min (to_read, chunk_size);
232
233                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
234                         
235                         fs = (*src);
236
237                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
238                                 break;
239                         }
240                         
241                         if (fs->write (buf, this_time) != 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<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
257                 (*src)->update_header (0, *now, tnow);
258                 (*src)->mark_immutable ();
259         }
260
261         return true;
262
263 error_out:
264
265         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
266                 (*i)->mark_for_remove ();
267         }
268
269         return 0;
270 }
271
272 int
273 Editor::write_audio_selection (TimeSelection& ts)
274 {
275         int ret = 0;
276
277         if (selection->tracks.empty()) {
278                 return 0;
279         }
280
281         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
282
283                 AudioTimeAxisView* atv;
284
285                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
286                         continue;
287                 }
288
289                 if (atv->is_audio_track()) {
290
291                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->get_diskstream()->playlist());
292                         
293                         if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
294                                 ret = -1;
295                                 break;
296                         }
297                 }
298         }
299
300         return ret;
301 }
302
303 bool
304 Editor::write_audio_range (AudioPlaylist& playlist, uint32_t channels, list<AudioRange>& range)
305 {
306         boost::shared_ptr<AudioFileSource> fs;
307         const nframes_t chunk_size = 4096;
308         nframes_t nframes;
309         Sample buf[chunk_size];
310         gain_t gain_buffer[chunk_size];
311         nframes_t pos;
312         char s[PATH_MAX+1];
313         uint32_t cnt;
314         string path;
315         vector<boost::shared_ptr<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 = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (*session, path, false, session->frame_rate()));
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                         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, pos, this_time, n) != this_time) {
369                                         break;
370                                 }
371                                 
372                                 if (fs->write (buf, this_time) != 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                                 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) != 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 (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
416                 (*s)->update_header (0, *now, tnow);
417                 (*s)->mark_immutable ();
418                 // do we need to ref it again?
419         }
420         
421         return true;
422
423 error_out:
424         /* unref created files */
425
426         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
427                 (*i)->mark_for_remove ();
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->regions.empty()) {
439                 write_region_selection (selection->regions);
440         }
441 }