Remove dead assignment in Editor::export_audio()
[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         return midi_region->do_export (path);
129 }
130
131 /** Export the first selected region */
132 void
133 Editor::export_region ()
134 {
135         if (selection->regions.empty()) {
136                 return;
137         }
138
139         boost::shared_ptr<Region> r = selection->regions.front()->region();
140         boost::shared_ptr<AudioRegion> audio_region = boost::dynamic_pointer_cast<AudioRegion>(r);
141         boost::shared_ptr<MidiRegion> midi_region = boost::dynamic_pointer_cast<MidiRegion>(r);
142
143         if (audio_region) {
144
145                 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
146                 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
147
148                 ExportRegionDialog dialog (*this, *(audio_region.get()), track);
149                 dialog.set_session (_session);
150                 dialog.run ();
151
152         } else if (midi_region) {
153
154                 MidiExportDialog dialog (*this, midi_region);
155                 dialog.set_session (_session);
156
157                 bool finished = false;
158                 while (!finished) {
159                         switch (dialog.run ()) {
160                         case Gtk::RESPONSE_ACCEPT:
161                                 finished = process_midi_export_dialog (dialog, midi_region);
162                                 break;
163                         default:
164                                 return;
165                         }
166                 }
167         }
168 }
169
170 int
171 Editor::write_region_selection (RegionSelection& regions)
172 {
173         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
174                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
175                 if (arv) {
176                         if (write_region ("", arv->audio_region()) == false)
177                                 return -1;
178                 }
179
180                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
181                 if (mrv) {
182                         warning << "MIDI region export not implemented" << endmsg;
183                 }
184         }
185
186         return 0;
187 }
188
189 void
190 Editor::bounce_region_selection (bool with_processing)
191 {
192         /* no need to check for bounceable() because this operation never puts
193          * its results back in the playlist (only in the region list).
194          */
195
196         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
197
198                 boost::shared_ptr<Region> region ((*i)->region());
199                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
200                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (rtv->route());
201
202                 InterThreadInfo itt;
203
204                 boost::shared_ptr<Region> r;
205
206                 if (with_processing) {
207                         r = track->bounce_range (region->position(), region->position() + region->length(), itt, track->main_outs(), false);
208                 } else {
209                         r = track->bounce_range (region->position(), region->position() + region->length(), itt, boost::shared_ptr<Processor>(), false);
210                 }
211         }
212 }
213
214 bool
215 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
216 {
217         boost::shared_ptr<AudioFileSource> fs;
218         const framepos_t chunk_size = 4096;
219         framepos_t to_read;
220         Sample buf[chunk_size];
221         gain_t gain_buffer[chunk_size];
222         framepos_t pos;
223         char s[PATH_MAX+1];
224         uint32_t cnt;
225         vector<boost::shared_ptr<AudioFileSource> > sources;
226         uint32_t nchans;
227
228         const string sound_directory = _session->session_directory().sound_path();
229
230         nchans = region->n_channels();
231
232         /* don't do duplicate of the entire source if that's what is going on here */
233
234         if (region->start() == 0 && region->length() == region->source_length(0)) {
235                 /* XXX should link(2) to create a new inode with "path" */
236                 return true;
237         }
238
239         if (path.length() == 0) {
240
241                 for (uint32_t n=0; n < nchans; ++n) {
242
243                         for (cnt = 0; cnt < 999999; ++cnt) {
244                                 if (nchans == 1) {
245                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
246                                                   legalize_for_path(region->name()).c_str(), cnt);
247                                 }
248                                 else {
249                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
250                                                   legalize_for_path(region->name()).c_str(), cnt, n);
251                                 }
252
253                                 path = s;
254
255                                 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
256                                         break;
257                                 }
258                         }
259
260                         if (cnt == 999999) {
261                                 error << "" << endmsg;
262                                 goto error_out;
263                         }
264
265
266
267                         try {
268                                 fs = boost::dynamic_pointer_cast<AudioFileSource> (
269                                         SourceFactory::createWritable (DataType::AUDIO, *_session,
270                                                                        path, true,
271                                                                        false, _session->frame_rate()));
272                         }
273
274                         catch (failed_constructor& err) {
275                                 goto error_out;
276                         }
277
278                         sources.push_back (fs);
279                 }
280         }
281         else {
282                 /* TODO: make filesources based on passed path */
283
284         }
285
286         to_read = region->length();
287         pos = region->position();
288
289         while (to_read) {
290                 framepos_t this_time;
291
292                 this_time = min (to_read, chunk_size);
293
294                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
295
296                         fs = (*src);
297
298                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
299                                 break;
300                         }
301
302                         if (fs->write (buf, this_time) != this_time) {
303                                 error << "" << endmsg;
304                                 goto error_out;
305                         }
306                 }
307
308                 to_read -= this_time;
309                 pos += this_time;
310         }
311
312         time_t tnow;
313         struct tm* now;
314         time (&tnow);
315         now = localtime (&tnow);
316
317         for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
318                 (*src)->update_header (0, *now, tnow);
319                 (*src)->mark_immutable ();
320         }
321
322         return true;
323
324 error_out:
325
326         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
327                 (*i)->mark_for_remove ();
328         }
329
330         return 0;
331 }
332
333 int
334 Editor::write_audio_selection (TimeSelection& ts)
335 {
336         int ret = 0;
337
338         if (selection->tracks.empty()) {
339                 return 0;
340         }
341
342         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
343
344                 AudioTimeAxisView* atv;
345
346                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
347                         continue;
348                 }
349
350                 if (atv->is_audio_track()) {
351
352                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
353
354                         if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
355                                 ret = -1;
356                                 break;
357                         }
358                 }
359         }
360
361         return ret;
362 }
363
364 bool
365 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
366 {
367         boost::shared_ptr<AudioFileSource> fs;
368         const framepos_t chunk_size = 4096;
369         framepos_t nframes;
370         Sample buf[chunk_size];
371         gain_t gain_buffer[chunk_size];
372         framepos_t pos;
373         char s[PATH_MAX+1];
374         uint32_t cnt;
375         string path;
376         vector<boost::shared_ptr<AudioFileSource> > sources;
377
378         const string sound_directory = _session->session_directory().sound_path();
379
380         uint32_t channels = count.n_audio();
381
382         for (uint32_t n=0; n < channels; ++n) {
383
384                 for (cnt = 0; cnt < 999999; ++cnt) {
385                         if (channels == 1) {
386                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
387                                           legalize_for_path(playlist.name()).c_str(), cnt);
388                         }
389                         else {
390                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
391                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
392                         }
393
394                         if (!Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
395                                 break;
396                         }
397                 }
398
399                 if (cnt == 999999) {
400                         error << "" << endmsg;
401                         goto error_out;
402                 }
403
404                 path = s;
405
406                 try {
407                         fs = boost::dynamic_pointer_cast<AudioFileSource> (
408                                 SourceFactory::createWritable (DataType::AUDIO, *_session,
409                                                                path, true,
410                                                                false, _session->frame_rate()));
411                 }
412
413                 catch (failed_constructor& err) {
414                         goto error_out;
415                 }
416
417                 sources.push_back (fs);
418
419         }
420
421
422         for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
423
424                 nframes = (*i).length();
425                 pos = (*i).start;
426
427                 while (nframes) {
428                         framepos_t this_time;
429
430                         this_time = min (nframes, chunk_size);
431
432                         for (uint32_t n=0; n < channels; ++n) {
433
434                                 fs = sources[n];
435
436                                 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
437                                         break;
438                                 }
439
440                                 if (fs->write (buf, this_time) != this_time) {
441                                         goto error_out;
442                                 }
443                         }
444
445                         nframes -= this_time;
446                         pos += this_time;
447                 }
448
449                 list<AudioRange>::iterator tmp = i;
450                 ++tmp;
451
452                 if (tmp != range.end()) {
453
454                         /* fill gaps with silence */
455
456                         nframes = (*tmp).start - (*i).end;
457
458                         while (nframes) {
459
460                                 framepos_t this_time = min (nframes, chunk_size);
461                                 memset (buf, 0, sizeof (Sample) * this_time);
462
463                                 for (uint32_t n=0; n < channels; ++n) {
464
465                                         fs = sources[n];
466                                         if (fs->write (buf, this_time) != this_time) {
467                                                 goto error_out;
468                                         }
469                                 }
470
471                                 nframes -= this_time;
472                         }
473                 }
474
475                 i = tmp;
476         }
477
478         time_t tnow;
479         struct tm* now;
480         time (&tnow);
481         now = localtime (&tnow);
482
483         for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
484                 (*s)->update_header (0, *now, tnow);
485                 (*s)->mark_immutable ();
486                 // do we need to ref it again?
487         }
488
489         return true;
490
491 error_out:
492         /* unref created files */
493
494         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
495                 (*i)->mark_for_remove ();
496         }
497
498         return false;
499 }
500
501 void
502 Editor::write_selection ()
503 {
504         if (!selection->time.empty()) {
505                 write_audio_selection (selection->time);
506         } else if (!selection->regions.empty()) {
507                 write_region_selection (selection->regions);
508         }
509 }