fix import/embed of multichannel audiofiles, as per #1433
[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 #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                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
132                 if (arv)
133                         if (write_region ("", arv->audio_region()) == false)
134                                 return -1;
135         }
136
137         return 0;
138 }
139
140 void
141 Editor::bounce_region_selection ()
142 {
143         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
144                 
145                 boost::shared_ptr<Region> region ((*i)->region());
146                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
147                 Track* track = dynamic_cast<Track*>(rtv->route().get());
148
149                 InterThreadInfo itt;
150
151                 itt.done = false;
152                 itt.cancel = false;
153                 itt.progress = 0.0f;
154
155                 track->bounce_range (region->position(), region->position() + region->length(), itt);
156         }
157 }
158
159 bool
160 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
161 {
162         boost::shared_ptr<AudioFileSource> fs;
163         const nframes_t chunk_size = 4096;
164         nframes_t to_read;
165         Sample buf[chunk_size];
166         gain_t gain_buffer[chunk_size];
167         nframes_t pos;
168         char s[PATH_MAX+1];
169         uint32_t cnt;
170         vector<boost::shared_ptr<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 = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (*session, path, false, session->frame_rate()));
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                 nframes_t this_time;
231
232                 this_time = min (to_read, chunk_size);
233
234                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
235                         
236                         fs = (*src);
237
238                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
239                                 break;
240                         }
241                         
242                         if (fs->write (buf, this_time) != 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<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
258                 (*src)->update_header (0, *now, tnow);
259                 (*src)->mark_immutable ();
260         }
261
262         return true;
263
264 error_out:
265
266         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
267                 (*i)->mark_for_remove ();
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                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_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         boost::shared_ptr<AudioFileSource> fs;
308         const nframes_t chunk_size = 4096;
309         nframes_t nframes;
310         Sample buf[chunk_size];
311         gain_t gain_buffer[chunk_size];
312         nframes_t pos;
313         char s[PATH_MAX+1];
314         uint32_t cnt;
315         string path;
316         vector<boost::shared_ptr<AudioFileSource> > sources;
317
318         for (uint32_t n=0; n < channels; ++n) {
319                 
320                 for (cnt = 0; cnt < 999999; ++cnt) {
321                         if (channels == 1) {
322                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
323                                           legalize_for_path(playlist.name()).c_str(), cnt);
324                         }
325                         else {
326                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
327                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
328                         }
329                         
330                         if (::access (s, F_OK) != 0) {
331                                 break;
332                         }
333                 }
334                 
335                 if (cnt == 999999) {
336                         error << "" << endmsg;
337                         goto error_out;
338                 }
339
340                 path = s;
341                 
342                 try {
343                         fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (*session, path, false, session->frame_rate()));
344                 }
345                 
346                 catch (failed_constructor& err) {
347                         goto error_out;
348                 }
349                 
350                 sources.push_back (fs);
351
352         }
353         
354
355         for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
356         
357                 nframes = (*i).length();
358                 pos = (*i).start;
359                 
360                 while (nframes) {
361                         nframes_t this_time;
362                         
363                         this_time = min (nframes, chunk_size);
364
365                         for (uint32_t n=0; n < channels; ++n) {
366
367                                 fs = sources[n];
368                                 
369                                 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
370                                         break;
371                                 }
372                                 
373                                 if (fs->write (buf, this_time) != this_time) {
374                                         goto error_out;
375                                 }
376                         }
377                         
378                         nframes -= this_time;
379                         pos += this_time;
380                 }
381                 
382                 list<AudioRange>::iterator tmp = i;
383                 ++tmp;
384
385                 if (tmp != range.end()) {
386                         
387                         /* fill gaps with silence */
388                         
389                         nframes = (*tmp).start - (*i).end;
390
391                         while (nframes) {
392
393                                 nframes_t this_time = min (nframes, chunk_size);
394                                 memset (buf, 0, sizeof (Sample) * this_time);
395
396                                 for (uint32_t n=0; n < channels; ++n) {
397
398                                         fs = sources[n];
399                                         if (fs->write (buf, this_time) != this_time) {
400                                                 goto error_out;
401                                         }
402                                 }
403
404                                 nframes -= this_time;
405                         }
406                 }
407
408                 i = tmp;
409         }
410
411         time_t tnow;
412         struct tm* now;
413         time (&tnow);
414         now = localtime (&tnow);
415
416         for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
417                 (*s)->update_header (0, *now, tnow);
418                 (*s)->mark_immutable ();
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<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
428                 (*i)->mark_for_remove ();
429         }
430
431         return false;
432 }
433
434 void
435 Editor::write_selection ()
436 {
437         if (!selection->time.empty()) {
438                 write_audio_selection (selection->time);
439         } else if (!selection->regions.empty()) {
440                 write_region_selection (selection->regions);
441         }
442 }