2 Copyright (C) 2001 Paul Davis
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.
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.
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.
20 /* Note: public Editor methods are documented in public_editor.h */
26 #include <gtkmm/messagedialog.h>
28 #include "pbd/gstdio_compat.h"
30 #include "pbd/pthread_utils.h"
32 #include "ardour/audio_track.h"
33 #include "ardour/audiofilesource.h"
34 #include "ardour/audioplaylist.h"
35 #include "ardour/audioregion.h"
36 #include "ardour/chan_count.h"
37 #include "ardour/midi_region.h"
38 #include "ardour/session.h"
39 #include "ardour/session_directory.h"
40 #include "ardour/source_factory.h"
41 #include "ardour/types.h"
43 #include "audio_region_view.h"
44 #include "audio_time_axis.h"
46 #include "export_dialog.h"
47 #include "midi_export_dialog.h"
48 #include "midi_region_view.h"
49 #include "public_editor.h"
50 #include "selection.h"
51 #include "time_axis_view.h"
57 using namespace ARDOUR;
62 Editor::export_audio ()
64 ExportDialog dialog (*this, _("Export"), ExportProfileManager::RegularExport);
65 dialog.set_session (_session);
70 Editor::stem_export ()
72 StemExportDialog dialog (*this);
73 dialog.set_session (_session);
78 Editor::export_selection ()
80 ExportSelectionDialog dialog (*this);
81 dialog.set_session (_session);
86 Editor::export_range ()
90 if ((marker = reinterpret_cast<ArdourMarker *> (marker_menu_item->get_data ("marker"))) == 0) {
91 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
92 abort(); /*NOTREACHED*/
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);
106 Editor::process_midi_export_dialog (MidiExportDialog& dialog, boost::shared_ptr<MidiRegion> midi_region)
108 string path = dialog.get_path ();
110 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
111 bool overwrite = ARDOUR_UI_UTILS::overwrite_file_dialog (dialog,
112 _("Confirm MIDI File Overwrite"),
113 _("A file with the same name already exists. Do you want to overwrite it?"));
119 /* force ::g_unlink because the backend code will
120 go wrong if it tries to open an existing
123 ::g_unlink (path.c_str());
126 return midi_region->do_export (path);
129 /** Export the first selected region */
131 Editor::export_region ()
133 if (selection->regions.empty()) {
137 boost::shared_ptr<Region> r = selection->regions.front()->region();
138 boost::shared_ptr<AudioRegion> audio_region = boost::dynamic_pointer_cast<AudioRegion>(r);
139 boost::shared_ptr<MidiRegion> midi_region = boost::dynamic_pointer_cast<MidiRegion>(r);
143 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
144 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
146 ExportRegionDialog dialog (*this, *(audio_region.get()), track);
147 dialog.set_session (_session);
150 } else if (midi_region) {
152 MidiExportDialog dialog (*this, midi_region);
153 dialog.set_session (_session);
155 bool finished = false;
157 switch (dialog.run ()) {
158 case Gtk::RESPONSE_ACCEPT:
159 finished = process_midi_export_dialog (dialog, midi_region);
169 Editor::write_region_selection (RegionSelection& regions)
171 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
172 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
174 if (write_region ("", arv->audio_region()) == false)
178 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
180 warning << "MIDI region export not implemented" << endmsg;
188 Editor::bounce_region_selection (bool with_processing)
190 /* no need to check for bounceable() because this operation never puts
191 * its results back in the playlist (only in the region list).
194 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
196 boost::shared_ptr<Region> region ((*i)->region());
197 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
198 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (rtv->route());
202 boost::shared_ptr<Region> r;
204 if (with_processing) {
205 r = track->bounce_range (region->position(), region->position() + region->length(), itt, track->main_outs(), false);
207 r = track->bounce_range (region->position(), region->position() + region->length(), itt, boost::shared_ptr<Processor>(), false);
213 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
215 boost::shared_ptr<AudioFileSource> fs;
216 const framepos_t chunk_size = 4096;
218 Sample buf[chunk_size];
219 gain_t gain_buffer[chunk_size];
223 vector<boost::shared_ptr<AudioFileSource> > sources;
226 const string sound_directory = _session->session_directory().sound_path();
228 nchans = region->n_channels();
230 /* don't do duplicate of the entire source if that's what is going on here */
232 if (region->start() == 0 && region->length() == region->source_length(0)) {
233 /* XXX should link(2) to create a new inode with "path" */
237 if (path.length() == 0) {
239 for (uint32_t n=0; n < nchans; ++n) {
241 for (cnt = 0; cnt < 999999; ++cnt) {
243 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
244 legalize_for_path(region->name()).c_str(), cnt);
247 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
248 legalize_for_path(region->name()).c_str(), cnt, n);
253 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
259 error << "" << endmsg;
266 fs = boost::dynamic_pointer_cast<AudioFileSource> (
267 SourceFactory::createWritable (DataType::AUDIO, *_session,
269 false, _session->frame_rate()));
272 catch (failed_constructor& err) {
276 sources.push_back (fs);
280 /* TODO: make filesources based on passed path */
284 to_read = region->length();
285 pos = region->position();
288 framepos_t this_time;
290 this_time = min (to_read, chunk_size);
292 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
296 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
300 if (fs->write (buf, this_time) != this_time) {
301 error << "" << endmsg;
306 to_read -= this_time;
313 now = localtime (&tnow);
315 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
316 (*src)->update_header (0, *now, tnow);
317 (*src)->mark_immutable ();
324 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
325 (*i)->mark_for_remove ();
332 Editor::write_audio_selection (TimeSelection& ts)
336 if (selection->tracks.empty()) {
340 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
342 AudioTimeAxisView* atv;
344 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
348 if (atv->is_audio_track()) {
350 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
352 if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
363 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
365 boost::shared_ptr<AudioFileSource> fs;
366 const framepos_t chunk_size = 4096;
368 Sample buf[chunk_size];
369 gain_t gain_buffer[chunk_size];
374 vector<boost::shared_ptr<AudioFileSource> > sources;
376 const string sound_directory = _session->session_directory().sound_path();
378 uint32_t channels = count.n_audio();
380 for (uint32_t n=0; n < channels; ++n) {
382 for (cnt = 0; cnt < 999999; ++cnt) {
384 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
385 legalize_for_path(playlist.name()).c_str(), cnt);
388 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
389 legalize_for_path(playlist.name()).c_str(), cnt, n);
392 if (!Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
398 error << "" << endmsg;
405 fs = boost::dynamic_pointer_cast<AudioFileSource> (
406 SourceFactory::createWritable (DataType::AUDIO, *_session,
408 false, _session->frame_rate()));
411 catch (failed_constructor& err) {
415 sources.push_back (fs);
420 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
422 nframes = (*i).length();
426 framepos_t this_time;
428 this_time = min (nframes, chunk_size);
430 for (uint32_t n=0; n < channels; ++n) {
434 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
438 if (fs->write (buf, this_time) != this_time) {
443 nframes -= this_time;
447 list<AudioRange>::iterator tmp = i;
450 if (tmp != range.end()) {
452 /* fill gaps with silence */
454 nframes = (*tmp).start - (*i).end;
458 framepos_t this_time = min (nframes, chunk_size);
459 memset (buf, 0, sizeof (Sample) * this_time);
461 for (uint32_t n=0; n < channels; ++n) {
464 if (fs->write (buf, this_time) != this_time) {
469 nframes -= this_time;
479 now = localtime (&tnow);
481 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
482 (*s)->update_header (0, *now, tnow);
483 (*s)->mark_immutable ();
484 // do we need to ref it again?
490 /* unref created files */
492 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
493 (*i)->mark_for_remove ();
500 Editor::write_selection ()
502 if (!selection->time.empty()) {
503 write_audio_selection (selection->time);
504 } else if (!selection->regions.empty()) {
505 write_region_selection (selection->regions);