Merged with trunk R1612.
[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/chan_count.h>
44 #include <ardour/source_factory.h>
45 #include <ardour/audiofilesource.h>
46
47 #include "i18n.h"
48
49 using namespace std;
50 using namespace ARDOUR;
51 using namespace PBD;
52 using namespace Gtk;
53
54 void
55 Editor::export_session()
56 {
57         if (session) {
58                 export_range (session->current_start_frame(), session->current_end_frame());
59         }
60 }
61
62 void
63 Editor::export_selection ()
64 {
65         if (session) {
66                 if (selection->time.empty()) {
67                         MessageDialog message (*this, _("There is no selection to export.\n\nSelect a selection using the range mouse mode"));
68                         message.run ();
69                         return;
70                 }
71
72                 export_range (selection->time.front().start, selection->time.front().end);
73         }
74 }
75
76 void
77 Editor::export_range (nframes_t start, nframes_t end)
78 {
79         if (session) {
80                 if (export_dialog == 0) {
81                         export_dialog = new ExportSessionDialog (*this);
82                 }
83                 
84                 export_dialog->connect_to_session (session);
85                 export_dialog->set_range (start, end);
86                 export_dialog->start_export();
87         }
88 }       
89
90 void
91 Editor::export_region ()
92 {
93         if (clicked_regionview == 0) {
94                 return;
95         }
96
97         ExportDialog* dialog = new ExportRegionDialog (*this, clicked_regionview->region());
98                 
99         dialog->connect_to_session (session);
100         dialog->set_range (
101                 clicked_regionview->region()->first_frame(), 
102                 clicked_regionview->region()->last_frame());
103         dialog->start_export();
104 }
105
106 void
107 Editor::export_range_markers ()
108 {
109         if (session) {
110
111                 if (session->locations()->num_range_markers() == 0) {
112                         MessageDialog message (*this, _("There are no ranges to export.\n\nCreate 1 or more ranges by dragging the mouse in the range bar"));
113                         message.run ();
114                         return;
115                 }
116                 
117
118                 if (export_range_markers_dialog == 0) {
119                         export_range_markers_dialog = new ExportRangeMarkersDialog(*this);
120                 }
121                 
122                 export_range_markers_dialog->connect_to_session (session);
123                 export_range_markers_dialog->start_export();
124         }
125 }       
126
127 int
128 Editor::write_region_selection (RegionSelection& regions)
129 {
130         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
131                 // FIXME
132                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
133                 if (arv)
134                         if (write_region ("", arv->audio_region()) == false)
135                                 return -1;
136         }
137
138         return 0;
139 }
140
141 void
142 Editor::bounce_region_selection ()
143 {
144         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
145                 
146                 boost::shared_ptr<Region> region ((*i)->region());
147                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
148                 Track* track = dynamic_cast<Track*>(rtv->route().get());
149
150                 InterThreadInfo itt;
151
152                 itt.done = false;
153                 itt.cancel = false;
154                 itt.progress = 0.0f;
155
156                 track->bounce_range (region->position(), region->position() + region->length(), itt);
157         }
158 }
159
160 bool
161 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
162 {
163         boost::shared_ptr<AudioFileSource> fs;
164         const nframes_t chunk_size = 4096;
165         nframes_t to_read;
166         Sample buf[chunk_size];
167         gain_t gain_buffer[chunk_size];
168         nframes_t pos;
169         char s[PATH_MAX+1];
170         uint32_t cnt;
171         vector<boost::shared_ptr<AudioFileSource> > sources;
172         uint32_t nchans;
173         
174         nchans = region->n_channels();
175         
176         /* don't do duplicate of the entire source if that's what is going on here */
177
178         if (region->start() == 0 && region->length() == region->source()->length()) {
179                 /* XXX should link(2) to create a new inode with "path" */
180                 return true;
181         }
182
183         if (path.length() == 0) {
184
185                 for (uint32_t n=0; n < nchans; ++n) {
186                         
187                         for (cnt = 0; cnt < 999999; ++cnt) {
188                                 if (nchans == 1) {
189                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
190                                                   legalize_for_path(region->name()).c_str(), cnt);
191                                 }
192                                 else {
193                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
194                                                   legalize_for_path(region->name()).c_str(), cnt, n);
195                                 }
196
197                                 path = s;
198                                 
199                                 if (::access (path.c_str(), F_OK) != 0) {
200                                         break;
201                                 }
202                         }
203                         
204                         if (cnt == 999999) {
205                                 error << "" << endmsg;
206                                 goto error_out;
207                         }
208                         
209                 
210                         
211                         try {
212                                 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *session, path, false, session->frame_rate()));
213                         }
214                         
215                         catch (failed_constructor& err) {
216                                 goto error_out;
217                         }
218
219                         sources.push_back (fs);
220                 }
221         }
222         else {
223                 /* TODO: make filesources based on passed path */
224
225         }
226         
227         to_read = region->length();
228         pos = region->position();
229
230         while (to_read) {
231                 nframes_t this_time;
232
233                 this_time = min (to_read, chunk_size);
234
235                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
236                         
237                         fs = (*src);
238
239                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
240                                 break;
241                         }
242                         
243                         if (fs->write (buf, this_time) != this_time) {
244                                 error << "" << endmsg;
245                                 goto error_out;
246                         }
247                 }
248
249                 to_read -= this_time;
250                 pos += this_time;
251         }
252
253         time_t tnow;
254         struct tm* now;
255         time (&tnow);
256         now = localtime (&tnow);
257         
258         for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
259                 (*src)->update_header (0, *now, tnow);
260                 (*src)->mark_immutable ();
261         }
262
263         return true;
264
265 error_out:
266
267         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
268                 (*i)->mark_for_remove ();
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                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_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, const ChanCount& count, list<AudioRange>& range)
307 {
308         boost::shared_ptr<AudioFileSource> fs;
309         const nframes_t chunk_size = 4096;
310         nframes_t nframes;
311         Sample buf[chunk_size];
312         gain_t gain_buffer[chunk_size];
313         nframes_t pos;
314         char s[PATH_MAX+1];
315         uint32_t cnt;
316         string path;
317         vector<boost::shared_ptr<AudioFileSource> > sources;
318
319         uint32_t channels = count.get(DataType::AUDIO);
320
321         for (uint32_t n=0; n < channels; ++n) {
322                 
323                 for (cnt = 0; cnt < 999999; ++cnt) {
324                         if (channels == 1) {
325                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
326                                           legalize_for_path(playlist.name()).c_str(), cnt);
327                         }
328                         else {
329                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
330                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
331                         }
332                         
333                         if (::access (s, F_OK) != 0) {
334                                 break;
335                         }
336                 }
337                 
338                 if (cnt == 999999) {
339                         error << "" << endmsg;
340                         goto error_out;
341                 }
342
343                 path = s;
344                 
345                 try {
346                         fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *session, path, false, session->frame_rate()));
347                 }
348                 
349                 catch (failed_constructor& err) {
350                         goto error_out;
351                 }
352                 
353                 sources.push_back (fs);
354
355         }
356         
357
358         for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
359         
360                 nframes = (*i).length();
361                 pos = (*i).start;
362                 
363                 while (nframes) {
364                         nframes_t this_time;
365                         
366                         this_time = min (nframes, chunk_size);
367
368                         for (uint32_t n=0; n < channels; ++n) {
369
370                                 fs = sources[n];
371                                 
372                                 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
373                                         break;
374                                 }
375                                 
376                                 if (fs->write (buf, this_time) != this_time) {
377                                         goto error_out;
378                                 }
379                         }
380                         
381                         nframes -= this_time;
382                         pos += this_time;
383                 }
384                 
385                 list<AudioRange>::iterator tmp = i;
386                 ++tmp;
387
388                 if (tmp != range.end()) {
389                         
390                         /* fill gaps with silence */
391                         
392                         nframes = (*tmp).start - (*i).end;
393
394                         while (nframes) {
395
396                                 nframes_t this_time = min (nframes, chunk_size);
397                                 memset (buf, 0, sizeof (Sample) * this_time);
398
399                                 for (uint32_t n=0; n < channels; ++n) {
400
401                                         fs = sources[n];
402                                         if (fs->write (buf, this_time) != this_time) {
403                                                 goto error_out;
404                                         }
405                                 }
406
407                                 nframes -= this_time;
408                         }
409                 }
410
411                 i = tmp;
412         }
413
414         time_t tnow;
415         struct tm* now;
416         time (&tnow);
417         now = localtime (&tnow);
418
419         for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
420                 (*s)->update_header (0, *now, tnow);
421                 (*s)->mark_immutable ();
422                 // do we need to ref it again?
423         }
424         
425         return true;
426
427 error_out:
428         /* unref created files */
429
430         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
431                 (*i)->mark_for_remove ();
432         }
433
434         return false;
435 }
436
437 void
438 Editor::write_selection ()
439 {
440         if (!selection->time.empty()) {
441                 write_audio_selection (selection->time);
442         } else if (!selection->regions.empty()) {
443                 write_region_selection (selection->regions);
444         }
445 }