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