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