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