2 * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
5 * Copyright (C) 2007-2010 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2007-2017 Tim Mayberry <mojofunk@gmail.com>
7 * Copyright (C) 2008-2012 Sakari Bergen <sakari.bergen@beatwaves.net>
8 * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
9 * Copyright (C) 2015 André Nusser <andre.nusser@googlemail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 /* Note: public Editor methods are documented in public_editor.h */
32 #include <gtkmm/messagedialog.h>
34 #include "pbd/gstdio_compat.h"
36 #include "pbd/pthread_utils.h"
38 #include "ardour/audio_track.h"
39 #include "ardour/audiofilesource.h"
40 #include "ardour/audioplaylist.h"
41 #include "ardour/audioregion.h"
42 #include "ardour/chan_count.h"
43 #include "ardour/midi_region.h"
44 #include "ardour/session.h"
45 #include "ardour/session_directory.h"
46 #include "ardour/source_factory.h"
47 #include "ardour/types.h"
49 #include "audio_region_view.h"
50 #include "audio_time_axis.h"
52 #include "export_dialog.h"
53 #include "midi_export_dialog.h"
54 #include "midi_region_view.h"
55 #include "public_editor.h"
56 #include "selection.h"
57 #include "time_axis_view.h"
63 using namespace ARDOUR;
68 Editor::export_audio ()
70 ExportDialog dialog (*this, _("Export"), ExportProfileManager::RegularExport);
71 dialog.set_session (_session);
76 Editor::stem_export ()
78 StemExportDialog dialog (*this);
79 dialog.set_session (_session);
84 Editor::export_selection ()
86 ExportSelectionDialog dialog (*this);
87 dialog.set_session (_session);
92 Editor::export_range ()
96 if ((marker = reinterpret_cast<ArdourMarker *> (marker_menu_item->get_data ("marker"))) == 0) {
97 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
98 abort(); /*NOTREACHED*/
104 if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
105 ExportRangeDialog dialog (*this, l->id().to_s());
106 dialog.set_session (_session);
112 Editor::process_midi_export_dialog (MidiExportDialog& dialog, boost::shared_ptr<MidiRegion> midi_region)
114 string path = dialog.get_path ();
116 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
117 bool overwrite = ARDOUR_UI_UTILS::overwrite_file_dialog (dialog,
118 _("Confirm MIDI File Overwrite"),
119 _("A file with the same name already exists. Do you want to overwrite it?"));
125 /* force ::g_unlink because the backend code will
126 go wrong if it tries to open an existing
129 ::g_unlink (path.c_str());
132 return midi_region->do_export (path);
135 /** Export the first selected region */
137 Editor::export_region ()
139 if (selection->regions.empty()) {
143 boost::shared_ptr<Region> r = selection->regions.front()->region();
144 boost::shared_ptr<AudioRegion> audio_region = boost::dynamic_pointer_cast<AudioRegion>(r);
145 boost::shared_ptr<MidiRegion> midi_region = boost::dynamic_pointer_cast<MidiRegion>(r);
149 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
150 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
152 ExportRegionDialog dialog (*this, *(audio_region.get()), track);
153 dialog.set_session (_session);
156 } else if (midi_region) {
158 MidiExportDialog dialog (*this, midi_region);
159 dialog.set_session (_session);
161 bool finished = false;
163 switch (dialog.run ()) {
164 case Gtk::RESPONSE_ACCEPT:
165 finished = process_midi_export_dialog (dialog, midi_region);
175 Editor::write_region_selection (RegionSelection& regions)
177 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
178 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
180 if (write_region ("", arv->audio_region()) == false)
184 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
186 warning << "MIDI region export not implemented" << endmsg;
194 Editor::bounce_region_selection (bool with_processing)
196 /* no need to check for bounceable() because this operation never puts
197 * its results back in the playlist (only in the region list).
200 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
202 boost::shared_ptr<Region> region ((*i)->region());
203 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
204 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (rtv->route());
208 boost::shared_ptr<Region> r;
210 if (with_processing) {
211 r = track->bounce_range (region->position(), region->position() + region->length(), itt, track->main_outs(), false);
213 r = track->bounce_range (region->position(), region->position() + region->length(), itt, boost::shared_ptr<Processor>(), false);
219 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
221 boost::shared_ptr<AudioFileSource> fs;
222 const samplepos_t chunk_size = 4096;
224 Sample buf[chunk_size];
225 gain_t gain_buffer[chunk_size];
229 vector<boost::shared_ptr<AudioFileSource> > sources;
232 const string sound_directory = _session->session_directory().sound_path();
234 nchans = region->n_channels();
236 /* don't do duplicate of the entire source if that's what is going on here */
238 if (region->start() == 0 && region->length() == region->source_length(0)) {
239 /* XXX should link(2) to create a new inode with "path" */
243 if (path.length() == 0) {
245 for (uint32_t n=0; n < nchans; ++n) {
247 for (cnt = 0; cnt < 999999; ++cnt) {
249 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
250 legalize_for_path(region->name()).c_str(), cnt);
253 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
254 legalize_for_path(region->name()).c_str(), cnt, n);
259 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
265 error << "" << endmsg;
272 fs = boost::dynamic_pointer_cast<AudioFileSource> (
273 SourceFactory::createWritable (DataType::AUDIO, *_session,
275 false, _session->sample_rate()));
278 catch (failed_constructor& err) {
282 sources.push_back (fs);
286 /* TODO: make filesources based on passed path */
290 to_read = region->length();
291 pos = region->position();
294 samplepos_t this_time;
296 this_time = min (to_read, chunk_size);
298 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
302 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
306 if (fs->write (buf, this_time) != this_time) {
307 error << "" << endmsg;
312 to_read -= this_time;
319 now = localtime (&tnow);
321 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
322 (*src)->update_header (0, *now, tnow);
323 (*src)->mark_immutable ();
330 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
331 (*i)->mark_for_remove ();
338 Editor::write_audio_selection (TimeSelection& ts)
342 if (selection->tracks.empty()) {
346 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
348 AudioTimeAxisView* atv;
350 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
354 if (atv->is_audio_track()) {
356 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
358 if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
369 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
371 boost::shared_ptr<AudioFileSource> fs;
372 const samplepos_t chunk_size = 4096;
374 Sample buf[chunk_size];
375 gain_t gain_buffer[chunk_size];
380 vector<boost::shared_ptr<AudioFileSource> > sources;
382 const string sound_directory = _session->session_directory().sound_path();
384 uint32_t channels = count.n_audio();
386 for (uint32_t n=0; n < channels; ++n) {
388 for (cnt = 0; cnt < 999999; ++cnt) {
390 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
391 legalize_for_path(playlist.name()).c_str(), cnt);
394 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
395 legalize_for_path(playlist.name()).c_str(), cnt, n);
398 if (!Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
404 error << "" << endmsg;
411 fs = boost::dynamic_pointer_cast<AudioFileSource> (
412 SourceFactory::createWritable (DataType::AUDIO, *_session,
414 false, _session->sample_rate()));
417 catch (failed_constructor& err) {
421 sources.push_back (fs);
426 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
428 nframes = (*i).length();
432 samplepos_t this_time;
434 this_time = min (nframes, chunk_size);
436 for (uint32_t n=0; n < channels; ++n) {
440 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
444 if (fs->write (buf, this_time) != this_time) {
449 nframes -= this_time;
453 list<AudioRange>::iterator tmp = i;
456 if (tmp != range.end()) {
458 /* fill gaps with silence */
460 nframes = (*tmp).start - (*i).end;
464 samplepos_t this_time = min (nframes, chunk_size);
465 memset (buf, 0, sizeof (Sample) * this_time);
467 for (uint32_t n=0; n < channels; ++n) {
470 if (fs->write (buf, this_time) != this_time) {
475 nframes -= this_time;
485 now = localtime (&tnow);
487 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
488 (*s)->update_header (0, *now, tnow);
489 (*s)->mark_immutable ();
490 // do we need to ref it again?
496 /* unref created files */
498 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
499 (*i)->mark_for_remove ();
506 Editor::write_selection ()
508 if (!selection->time.empty()) {
509 write_audio_selection (selection->time);
510 } else if (!selection->regions.empty()) {
511 write_region_selection (selection->regions);