Move Diskstream ownership to Track, so that Session no longer holds lists of Diskstre...
[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 #define __STDC_FORMAT_MACROS 1
23 #include <inttypes.h>
24 #include <unistd.h>
25 #include <climits>
26
27 #include <gtkmm/messagedialog.h>
28
29 #include "export_dialog.h"
30 #include "editor.h"
31 #include "public_editor.h"
32 #include "selection.h"
33 #include "time_axis_view.h"
34 #include "audio_time_axis.h"
35 #include "audio_region_view.h"
36 #include "midi_region_view.h"
37
38 #include "pbd/pthread_utils.h"
39 #include "ardour/types.h"
40 #include "ardour/audio_track.h"
41 #include "ardour/audiofilesource.h"
42 #include "ardour/audio_diskstream.h"
43 #include "ardour/audioregion.h"
44 #include "ardour/audioplaylist.h"
45 #include "ardour/chan_count.h"
46 #include "ardour/session_directory.h"
47 #include "ardour/source_factory.h"
48 #include "ardour/audiofilesource.h"
49 #include "ardour/session.h"
50
51 #include "i18n.h"
52
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace Gtk;
57
58 void
59 Editor::export_audio ()
60 {
61         ExportDialog dialog (*this);
62         dialog.set_session (_session);
63         dialog.run();
64 }
65
66 void
67 Editor::export_selection ()
68 {
69         ExportSelectionDialog dialog (*this);
70         dialog.set_session (_session);
71         dialog.run();
72 }
73
74 void
75 Editor::export_range ()
76 {
77         Marker* marker;
78
79         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
80                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
81                 /*NOTREACHED*/
82         }
83
84         Location* l;
85         bool is_start;
86
87         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
88                 ExportRangeDialog dialog (*this, l->id().to_s());
89                 dialog.set_session (_session);
90                 dialog.run();
91         }
92 }
93
94 /** Export the first selected region */
95 void
96 Editor::export_region ()
97 {
98         if (selection->regions.empty()) {
99                 return;
100         }
101
102         try {
103                 boost::shared_ptr<Region> r = selection->regions.front()->region();
104                 AudioRegion & region (dynamic_cast<AudioRegion &> (*r));
105
106                 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
107                 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
108
109                 ExportRegionDialog dialog (*this, region, track);
110                 dialog.set_session (_session);
111                 dialog.run();
112
113         } catch (std::bad_cast & e) {
114                 error << "Exporting Region failed!" << endmsg;
115                 return;
116         }
117 }
118
119 int
120 Editor::write_region_selection (RegionSelection& regions)
121 {
122         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
123                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
124                 if (arv) {
125                         if (write_region ("", arv->audio_region()) == false)
126                                 return -1;
127                 }
128
129                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
130                 if (mrv) {
131                         warning << "MIDI region export not implemented" << endmsg;
132                 }
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                 boost::shared_ptr<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                 boost::shared_ptr<Region> r = track->bounce_range (region->position(), region->position() + region->length(), itt);
154                 cerr << "Result of bounce of "
155                      << region->name() << " len = " << region->length()
156                      << " was "
157                      << r->name() << " len = " << r->length()
158                      << endl;
159         }
160 }
161
162 bool
163 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
164 {
165         boost::shared_ptr<AudioFileSource> fs;
166         const nframes64_t chunk_size = 4096;
167         nframes64_t to_read;
168         Sample buf[chunk_size];
169         gain_t gain_buffer[chunk_size];
170         nframes64_t pos;
171         char s[PATH_MAX+1];
172         uint32_t cnt;
173         vector<boost::shared_ptr<AudioFileSource> > sources;
174         uint32_t nchans;
175
176         const string sound_directory = _session->session_directory().sound_path().to_string();
177
178         nchans = region->n_channels();
179
180         /* don't do duplicate of the entire source if that's what is going on here */
181
182         if (region->start() == 0 && region->length() == region->source_length(0)) {
183                 /* XXX should link(2) to create a new inode with "path" */
184                 return true;
185         }
186
187         if (path.length() == 0) {
188
189                 for (uint32_t n=0; n < nchans; ++n) {
190
191                         for (cnt = 0; cnt < 999999; ++cnt) {
192                                 if (nchans == 1) {
193                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
194                                                   legalize_for_path(region->name()).c_str(), cnt);
195                                 }
196                                 else {
197                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
198                                                   legalize_for_path(region->name()).c_str(), cnt, n);
199                                 }
200
201                                 path = s;
202
203                                 if (::access (path.c_str(), F_OK) != 0) {
204                                         break;
205                                 }
206                         }
207
208                         if (cnt == 999999) {
209                                 error << "" << endmsg;
210                                 goto error_out;
211                         }
212
213
214
215                         try {
216                                 fs = boost::dynamic_pointer_cast<AudioFileSource> (
217                                                 SourceFactory::createWritable (DataType::AUDIO, *_session,
218                                                                 path, true,
219                                                                 false, _session->frame_rate()));
220                         }
221
222                         catch (failed_constructor& err) {
223                                 goto error_out;
224                         }
225
226                         sources.push_back (fs);
227                 }
228         }
229         else {
230                 /* TODO: make filesources based on passed path */
231
232         }
233
234         to_read = region->length();
235         pos = region->position();
236
237         while (to_read) {
238                 nframes64_t this_time;
239
240                 this_time = min (to_read, chunk_size);
241
242                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
243
244                         fs = (*src);
245
246                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
247                                 break;
248                         }
249
250                         if (fs->write (buf, this_time) != this_time) {
251                                 error << "" << endmsg;
252                                 goto error_out;
253                         }
254                 }
255
256                 to_read -= this_time;
257                 pos += this_time;
258         }
259
260         time_t tnow;
261         struct tm* now;
262         time (&tnow);
263         now = localtime (&tnow);
264
265         for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
266                 (*src)->update_header (0, *now, tnow);
267                 (*src)->mark_immutable ();
268         }
269
270         return true;
271
272 error_out:
273
274         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
275                 (*i)->mark_for_remove ();
276         }
277
278         return 0;
279 }
280
281 int
282 Editor::write_audio_selection (TimeSelection& ts)
283 {
284         int ret = 0;
285
286         if (selection->tracks.empty()) {
287                 return 0;
288         }
289
290         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
291
292                 AudioTimeAxisView* atv;
293
294                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
295                         continue;
296                 }
297
298                 if (atv->is_audio_track()) {
299
300                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
301
302                         if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
303                                 ret = -1;
304                                 break;
305                         }
306                 }
307         }
308
309         return ret;
310 }
311
312 bool
313 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
314 {
315         boost::shared_ptr<AudioFileSource> fs;
316         const nframes64_t chunk_size = 4096;
317         nframes64_t nframes;
318         Sample buf[chunk_size];
319         gain_t gain_buffer[chunk_size];
320         nframes64_t pos;
321         char s[PATH_MAX+1];
322         uint32_t cnt;
323         string path;
324         vector<boost::shared_ptr<AudioFileSource> > sources;
325
326         const string sound_directory = _session->session_directory().sound_path().to_string();
327
328         uint32_t channels = count.n_audio();
329
330         for (uint32_t n=0; n < channels; ++n) {
331
332                 for (cnt = 0; cnt < 999999; ++cnt) {
333                         if (channels == 1) {
334                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
335                                           legalize_for_path(playlist.name()).c_str(), cnt);
336                         }
337                         else {
338                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
339                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
340                         }
341
342                         if (::access (s, F_OK) != 0) {
343                                 break;
344                         }
345                 }
346
347                 if (cnt == 999999) {
348                         error << "" << endmsg;
349                         goto error_out;
350                 }
351
352                 path = s;
353
354                 try {
355                         fs = boost::dynamic_pointer_cast<AudioFileSource> (
356                                         SourceFactory::createWritable (DataType::AUDIO, *_session,
357                                                         path, true,
358                                                         false, _session->frame_rate()));
359                 }
360
361                 catch (failed_constructor& err) {
362                         goto error_out;
363                 }
364
365                 sources.push_back (fs);
366
367         }
368
369
370         for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
371
372                 nframes = (*i).length();
373                 pos = (*i).start;
374
375                 while (nframes) {
376                         nframes64_t this_time;
377
378                         this_time = min (nframes, chunk_size);
379
380                         for (uint32_t n=0; n < channels; ++n) {
381
382                                 fs = sources[n];
383
384                                 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
385                                         break;
386                                 }
387
388                                 if (fs->write (buf, this_time) != this_time) {
389                                         goto error_out;
390                                 }
391                         }
392
393                         nframes -= this_time;
394                         pos += this_time;
395                 }
396
397                 list<AudioRange>::iterator tmp = i;
398                 ++tmp;
399
400                 if (tmp != range.end()) {
401
402                         /* fill gaps with silence */
403
404                         nframes = (*tmp).start - (*i).end;
405
406                         while (nframes) {
407
408                                 nframes64_t this_time = min (nframes, chunk_size);
409                                 memset (buf, 0, sizeof (Sample) * this_time);
410
411                                 for (uint32_t n=0; n < channels; ++n) {
412
413                                         fs = sources[n];
414                                         if (fs->write (buf, this_time) != this_time) {
415                                                 goto error_out;
416                                         }
417                                 }
418
419                                 nframes -= this_time;
420                         }
421                 }
422
423                 i = tmp;
424         }
425
426         time_t tnow;
427         struct tm* now;
428         time (&tnow);
429         now = localtime (&tnow);
430
431         for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
432                 (*s)->update_header (0, *now, tnow);
433                 (*s)->mark_immutable ();
434                 // do we need to ref it again?
435         }
436
437         return true;
438
439 error_out:
440         /* unref created files */
441
442         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
443                 (*i)->mark_for_remove ();
444         }
445
446         return false;
447 }
448
449 void
450 Editor::write_selection ()
451 {
452         if (!selection->time.empty()) {
453                 write_audio_selection (selection->time);
454         } else if (!selection->regions.empty()) {
455                 write_region_selection (selection->regions);
456         }
457 }