Make Bundles work a bit better. A few include optimisations.
[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 /* Note: public Editor methods are documented in public_editor.h */
21
22 #include <unistd.h>
23 #include <climits>
24
25 #include <gtkmm/messagedialog.h>
26
27 #include "export_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/audio_track.h>
38 #include <ardour/audiofilesource.h>
39 #include <ardour/audio_diskstream.h>
40 #include <ardour/audioregion.h>
41 #include <ardour/audioplaylist.h>
42 #include <ardour/chan_count.h>
43 #include <ardour/session_directory.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_audio ()
56 {
57         ExportDialog dialog (*this);
58         dialog.set_session (session);
59         dialog.run();
60 }
61
62 void
63 Editor::export_selection ()
64 {
65         ExportSelectionDialog dialog (*this);
66         dialog.set_session (session);
67         dialog.run();
68 }
69
70 void
71 Editor::export_range ()
72 {
73         Marker* marker;
74
75         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
76                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
77                 /*NOTREACHED*/
78         }
79
80         Location* l;
81         bool is_start;
82
83         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
84                 ExportRangeDialog dialog (*this, l->id().to_s());
85                 dialog.set_session (session);
86                 dialog.run();
87         }
88 }
89
90 /** Export the first selected region */
91 void
92 Editor::export_region ()
93 {
94         if (selection->regions.empty()) {
95                 return;
96         }
97         
98         try {
99                 boost::shared_ptr<Region> r = selection->regions.front()->region();
100                 AudioRegion & region (dynamic_cast<AudioRegion &> (*r));
101                 
102                 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
103                 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
104                 
105                 ExportRegionDialog dialog (*this, region, track);
106                 dialog.set_session (session);
107                 dialog.run();
108                 
109         } catch (std::bad_cast & e) {
110                 error << "Exporting Region failed!" << endmsg;
111                 return;
112         }
113 }
114
115 int
116 Editor::write_region_selection (RegionSelection& regions)
117 {
118         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
119                 // FIXME
120                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
121                 if (arv)
122                         if (write_region ("", arv->audio_region()) == false)
123                                 return -1;
124         }
125
126         return 0;
127 }
128
129 void
130 Editor::bounce_region_selection ()
131 {
132         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
133                 
134                 boost::shared_ptr<Region> region ((*i)->region());
135                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
136                 Track* track = dynamic_cast<Track*>(rtv->route().get());
137
138                 InterThreadInfo itt;
139
140                 itt.done = false;
141                 itt.cancel = false;
142                 itt.progress = 0.0f;
143
144                 boost::shared_ptr<Region> r = track->bounce_range (region->position(), region->position() + region->length(), itt);
145                 cerr << "Result of bounce of "
146                      << region->name() << " len = " << region->length()
147                      << " was "
148                      << r->name() << " len = " << r->length()
149                      << endl;
150         }
151 }
152
153 bool
154 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
155 {
156         boost::shared_ptr<AudioFileSource> fs;
157         const nframes64_t chunk_size = 4096;
158         nframes64_t to_read;
159         Sample buf[chunk_size];
160         gain_t gain_buffer[chunk_size];
161         nframes64_t pos;
162         char s[PATH_MAX+1];
163         uint32_t cnt;
164         vector<boost::shared_ptr<AudioFileSource> > sources;
165         uint32_t nchans;
166
167         const string sound_directory = session->session_directory().sound_path().to_string();
168
169         nchans = region->n_channels();
170         
171         /* don't do duplicate of the entire source if that's what is going on here */
172
173         if (region->start() == 0 && region->length() == region->source()->length()) {
174                 /* XXX should link(2) to create a new inode with "path" */
175                 return true;
176         }
177
178         if (path.length() == 0) {
179
180                 for (uint32_t n=0; n < nchans; ++n) {
181                         
182                         for (cnt = 0; cnt < 999999; ++cnt) {
183                                 if (nchans == 1) {
184                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
185                                                   legalize_for_path(region->name()).c_str(), cnt);
186                                 }
187                                 else {
188                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
189                                                   legalize_for_path(region->name()).c_str(), cnt, n);
190                                 }
191
192                                 path = s;
193                                 
194                                 if (::access (path.c_str(), F_OK) != 0) {
195                                         break;
196                                 }
197                         }
198                         
199                         if (cnt == 999999) {
200                                 error << "" << endmsg;
201                                 goto error_out;
202                         }
203                         
204                 
205                         
206                         try {
207                                 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *session, path, false, session->frame_rate()));
208                         }
209                         
210                         catch (failed_constructor& err) {
211                                 goto error_out;
212                         }
213
214                         sources.push_back (fs);
215                 }
216         }
217         else {
218                 /* TODO: make filesources based on passed path */
219
220         }
221         
222         to_read = region->length();
223         pos = region->position();
224
225         while (to_read) {
226                 nframes64_t this_time;
227
228                 this_time = min (to_read, chunk_size);
229
230                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
231                         
232                         fs = (*src);
233
234                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
235                                 break;
236                         }
237                         
238                         if (fs->write (buf, this_time) != this_time) {
239                                 error << "" << endmsg;
240                                 goto error_out;
241                         }
242                 }
243
244                 to_read -= this_time;
245                 pos += this_time;
246         }
247
248         time_t tnow;
249         struct tm* now;
250         time (&tnow);
251         now = localtime (&tnow);
252         
253         for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
254                 (*src)->update_header (0, *now, tnow);
255                 (*src)->mark_immutable ();
256         }
257
258         return true;
259
260 error_out:
261
262         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
263                 (*i)->mark_for_remove ();
264         }
265
266         return 0;
267 }
268
269 int
270 Editor::write_audio_selection (TimeSelection& ts)
271 {
272         int ret = 0;
273
274         if (selection->tracks.empty()) {
275                 return 0;
276         }
277
278         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
279
280                 AudioTimeAxisView* atv;
281
282                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
283                         continue;
284                 }
285
286                 if (atv->is_audio_track()) {
287
288                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->get_diskstream()->playlist());
289                         
290                         if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
291                                 ret = -1;
292                                 break;
293                         }
294                 }
295         }
296
297         return ret;
298 }
299
300 bool
301 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
302 {
303         boost::shared_ptr<AudioFileSource> fs;
304         const nframes64_t chunk_size = 4096;
305         nframes64_t nframes;
306         Sample buf[chunk_size];
307         gain_t gain_buffer[chunk_size];
308         nframes64_t pos;
309         char s[PATH_MAX+1];
310         uint32_t cnt;
311         string path;
312         vector<boost::shared_ptr<AudioFileSource> > sources;
313
314         const string sound_directory = session->session_directory().sound_path().to_string();
315
316         uint32_t channels = count.n_audio();
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", sound_directory.c_str(),
323                                           legalize_for_path(playlist.name()).c_str(), cnt);
324                         }
325                         else {
326                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.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 (DataType::AUDIO, *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                         nframes64_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                                 nframes64_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 }