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 "gtkmm2ext/choice.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"
56 using namespace ARDOUR;
61 Editor::export_audio ()
63 ExportDialog dialog (*this, _("Export"), ExportProfileManager::RegularExport);
64 dialog.set_session (_session);
69 Editor::stem_export ()
71 StemExportDialog dialog (*this);
72 dialog.set_session (_session);
77 Editor::export_selection ()
79 ExportSelectionDialog dialog (*this);
80 dialog.set_session (_session);
85 Editor::export_range ()
89 if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
90 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
97 if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
98 ExportRangeDialog dialog (*this, l->id().to_s());
99 dialog.set_session (_session);
104 /** Export the first selected region */
106 Editor::export_region ()
108 if (selection->regions.empty()) {
112 boost::shared_ptr<Region> r = selection->regions.front()->region();
113 boost::shared_ptr<AudioRegion> audio_region = boost::dynamic_pointer_cast<AudioRegion>(r);
114 boost::shared_ptr<MidiRegion> midi_region = boost::dynamic_pointer_cast<MidiRegion>(r);
118 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
119 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
121 ExportRegionDialog dialog (*this, *(audio_region.get()), track);
122 dialog.set_session (_session);
125 } else if (midi_region) {
127 MidiExportDialog dialog (*this, midi_region);
128 dialog.set_session (_session);
129 int ret = dialog.run ();
131 case Gtk::RESPONSE_ACCEPT:
139 string path = dialog.get_path ();
141 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
143 MessageDialog checker (_("File Exists!"),
145 Gtk::MESSAGE_WARNING,
148 checker.set_title (_("File Exists!"));
150 checker.add_button (Stock::CANCEL, RESPONSE_CANCEL);
151 checker.add_button (_("Overwrite Existing File"), RESPONSE_ACCEPT);
152 checker.set_default_response (RESPONSE_CANCEL);
154 checker.set_wmclass (X_("midi_export_file_exists"), PROGRAM_NAME);
155 checker.set_position (Gtk::WIN_POS_MOUSE);
157 ret = checker.run ();
160 case Gtk::RESPONSE_ACCEPT:
161 /* force unlink because the backend code will
162 go wrong if it tries to open an existing
165 ::unlink (path.c_str());
173 (void) midi_region->clone (path);
178 Editor::write_region_selection (RegionSelection& regions)
180 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
181 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
183 if (write_region ("", arv->audio_region()) == false)
187 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
189 warning << "MIDI region export not implemented" << endmsg;
197 Editor::bounce_region_selection (bool with_processing)
199 /* no need to check for bounceable() because this operation never puts
200 * its results back in the playlist (only in the region list).
203 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
205 boost::shared_ptr<Region> region ((*i)->region());
206 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
207 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (rtv->route());
211 boost::shared_ptr<Region> r;
213 if (with_processing) {
214 r = track->bounce_range (region->position(), region->position() + region->length(), itt, track->main_outs(), false);
216 r = track->bounce_range (region->position(), region->position() + region->length(), itt, boost::shared_ptr<Processor>(), false);
222 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
224 boost::shared_ptr<AudioFileSource> fs;
225 const framepos_t chunk_size = 4096;
227 Sample buf[chunk_size];
228 gain_t gain_buffer[chunk_size];
232 vector<boost::shared_ptr<AudioFileSource> > sources;
235 const string sound_directory = _session->session_directory().sound_path();
237 nchans = region->n_channels();
239 /* don't do duplicate of the entire source if that's what is going on here */
241 if (region->start() == 0 && region->length() == region->source_length(0)) {
242 /* XXX should link(2) to create a new inode with "path" */
246 if (path.length() == 0) {
248 for (uint32_t n=0; n < nchans; ++n) {
250 for (cnt = 0; cnt < 999999; ++cnt) {
252 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
253 legalize_for_path(region->name()).c_str(), cnt);
256 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
257 legalize_for_path(region->name()).c_str(), cnt, n);
262 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
268 error << "" << endmsg;
275 fs = boost::dynamic_pointer_cast<AudioFileSource> (
276 SourceFactory::createWritable (DataType::AUDIO, *_session,
277 path, string(), true,
278 false, _session->frame_rate()));
281 catch (failed_constructor& err) {
285 sources.push_back (fs);
289 /* TODO: make filesources based on passed path */
293 to_read = region->length();
294 pos = region->position();
297 framepos_t this_time;
299 this_time = min (to_read, chunk_size);
301 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
305 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
309 if (fs->write (buf, this_time) != this_time) {
310 error << "" << endmsg;
315 to_read -= this_time;
322 now = localtime (&tnow);
324 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
325 (*src)->update_header (0, *now, tnow);
326 (*src)->mark_immutable ();
333 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
334 (*i)->mark_for_remove ();
341 Editor::write_audio_selection (TimeSelection& ts)
345 if (selection->tracks.empty()) {
349 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
351 AudioTimeAxisView* atv;
353 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
357 if (atv->is_audio_track()) {
359 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
361 if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
372 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
374 boost::shared_ptr<AudioFileSource> fs;
375 const framepos_t chunk_size = 4096;
377 Sample buf[chunk_size];
378 gain_t gain_buffer[chunk_size];
383 vector<boost::shared_ptr<AudioFileSource> > sources;
385 const string sound_directory = _session->session_directory().sound_path();
387 uint32_t channels = count.n_audio();
389 for (uint32_t n=0; n < channels; ++n) {
391 for (cnt = 0; cnt < 999999; ++cnt) {
393 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
394 legalize_for_path(playlist.name()).c_str(), cnt);
397 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
398 legalize_for_path(playlist.name()).c_str(), cnt, n);
401 if (!Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
407 error << "" << endmsg;
414 fs = boost::dynamic_pointer_cast<AudioFileSource> (
415 SourceFactory::createWritable (DataType::AUDIO, *_session,
416 path, string(), true,
417 false, _session->frame_rate()));
420 catch (failed_constructor& err) {
424 sources.push_back (fs);
429 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
431 nframes = (*i).length();
435 framepos_t this_time;
437 this_time = min (nframes, chunk_size);
439 for (uint32_t n=0; n < channels; ++n) {
443 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
447 if (fs->write (buf, this_time) != this_time) {
452 nframes -= this_time;
456 list<AudioRange>::iterator tmp = i;
459 if (tmp != range.end()) {
461 /* fill gaps with silence */
463 nframes = (*tmp).start - (*i).end;
467 framepos_t this_time = min (nframes, chunk_size);
468 memset (buf, 0, sizeof (Sample) * this_time);
470 for (uint32_t n=0; n < channels; ++n) {
473 if (fs->write (buf, this_time) != this_time) {
478 nframes -= this_time;
488 now = localtime (&tnow);
490 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
491 (*s)->update_header (0, *now, tnow);
492 (*s)->mark_immutable ();
493 // do we need to ref it again?
499 /* unref created files */
501 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
502 (*i)->mark_for_remove ();
509 Editor::write_selection ()
511 if (!selection->time.empty()) {
512 write_audio_selection (selection->time);
513 } else if (!selection->regions.empty()) {
514 write_region_selection (selection->regions);