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 <glib/gstdio.h>
30 #include "gtkmm2ext/choice.h"
32 #include "pbd/pthread_utils.h"
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"
45 #include "audio_region_view.h"
46 #include "audio_time_axis.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"
58 using namespace ARDOUR;
63 Editor::export_audio ()
65 ExportDialog dialog (*this, _("Export"), ExportProfileManager::RegularExport);
66 dialog.set_session (_session);
71 Editor::stem_export ()
73 StemExportDialog dialog (*this);
74 dialog.set_session (_session);
79 Editor::export_selection ()
81 ExportSelectionDialog dialog (*this);
82 dialog.set_session (_session);
87 Editor::export_range ()
91 if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
92 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
99 if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
100 ExportRangeDialog dialog (*this, l->id().to_s());
101 dialog.set_session (_session);
106 /** Export the first selected region */
108 Editor::export_region ()
110 if (selection->regions.empty()) {
114 boost::shared_ptr<Region> r = selection->regions.front()->region();
115 boost::shared_ptr<AudioRegion> audio_region = boost::dynamic_pointer_cast<AudioRegion>(r);
116 boost::shared_ptr<MidiRegion> midi_region = boost::dynamic_pointer_cast<MidiRegion>(r);
120 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
121 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
123 ExportRegionDialog dialog (*this, *(audio_region.get()), track);
124 dialog.set_session (_session);
127 } else if (midi_region) {
129 MidiExportDialog dialog (*this, midi_region);
130 dialog.set_session (_session);
131 int ret = dialog.run ();
133 case Gtk::RESPONSE_ACCEPT:
141 string path = dialog.get_path ();
143 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
145 MessageDialog checker (_("File Exists!"),
147 Gtk::MESSAGE_WARNING,
150 checker.set_title (_("File Exists!"));
152 checker.add_button (Stock::CANCEL, RESPONSE_CANCEL);
153 checker.add_button (_("Overwrite Existing File"), RESPONSE_ACCEPT);
154 checker.set_default_response (RESPONSE_CANCEL);
156 checker.set_wmclass (X_("midi_export_file_exists"), PROGRAM_NAME);
157 checker.set_position (Gtk::WIN_POS_MOUSE);
159 ret = checker.run ();
162 case Gtk::RESPONSE_ACCEPT:
163 /* force ::g_unlink because the backend code will
164 go wrong if it tries to open an existing
167 ::g_unlink (path.c_str());
175 (void) midi_region->clone (path);
180 Editor::write_region_selection (RegionSelection& regions)
182 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
183 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
185 if (write_region ("", arv->audio_region()) == false)
189 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
191 warning << "MIDI region export not implemented" << endmsg;
199 Editor::bounce_region_selection (bool with_processing)
201 /* no need to check for bounceable() because this operation never puts
202 * its results back in the playlist (only in the region list).
205 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
207 boost::shared_ptr<Region> region ((*i)->region());
208 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
209 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (rtv->route());
213 boost::shared_ptr<Region> r;
215 if (with_processing) {
216 r = track->bounce_range (region->position(), region->position() + region->length(), itt, track->main_outs(), false);
218 r = track->bounce_range (region->position(), region->position() + region->length(), itt, boost::shared_ptr<Processor>(), false);
224 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
226 boost::shared_ptr<AudioFileSource> fs;
227 const framepos_t chunk_size = 4096;
229 Sample buf[chunk_size];
230 gain_t gain_buffer[chunk_size];
234 vector<boost::shared_ptr<AudioFileSource> > sources;
237 const string sound_directory = _session->session_directory().sound_path();
239 nchans = region->n_channels();
241 /* don't do duplicate of the entire source if that's what is going on here */
243 if (region->start() == 0 && region->length() == region->source_length(0)) {
244 /* XXX should link(2) to create a new inode with "path" */
248 if (path.length() == 0) {
250 for (uint32_t n=0; n < nchans; ++n) {
252 for (cnt = 0; cnt < 999999; ++cnt) {
254 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
255 legalize_for_path(region->name()).c_str(), cnt);
258 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
259 legalize_for_path(region->name()).c_str(), cnt, n);
264 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
270 error << "" << endmsg;
277 fs = boost::dynamic_pointer_cast<AudioFileSource> (
278 SourceFactory::createWritable (DataType::AUDIO, *_session,
280 false, _session->frame_rate()));
283 catch (failed_constructor& err) {
287 sources.push_back (fs);
291 /* TODO: make filesources based on passed path */
295 to_read = region->length();
296 pos = region->position();
299 framepos_t this_time;
301 this_time = min (to_read, chunk_size);
303 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
307 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
311 if (fs->write (buf, this_time) != this_time) {
312 error << "" << endmsg;
317 to_read -= this_time;
324 now = localtime (&tnow);
326 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
327 (*src)->update_header (0, *now, tnow);
328 (*src)->mark_immutable ();
335 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
336 (*i)->mark_for_remove ();
343 Editor::write_audio_selection (TimeSelection& ts)
347 if (selection->tracks.empty()) {
351 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
353 AudioTimeAxisView* atv;
355 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
359 if (atv->is_audio_track()) {
361 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
363 if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
374 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
376 boost::shared_ptr<AudioFileSource> fs;
377 const framepos_t chunk_size = 4096;
379 Sample buf[chunk_size];
380 gain_t gain_buffer[chunk_size];
385 vector<boost::shared_ptr<AudioFileSource> > sources;
387 const string sound_directory = _session->session_directory().sound_path();
389 uint32_t channels = count.n_audio();
391 for (uint32_t n=0; n < channels; ++n) {
393 for (cnt = 0; cnt < 999999; ++cnt) {
395 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
396 legalize_for_path(playlist.name()).c_str(), cnt);
399 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
400 legalize_for_path(playlist.name()).c_str(), cnt, n);
403 if (!Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
409 error << "" << endmsg;
416 fs = boost::dynamic_pointer_cast<AudioFileSource> (
417 SourceFactory::createWritable (DataType::AUDIO, *_session,
419 false, _session->frame_rate()));
422 catch (failed_constructor& err) {
426 sources.push_back (fs);
431 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
433 nframes = (*i).length();
437 framepos_t this_time;
439 this_time = min (nframes, chunk_size);
441 for (uint32_t n=0; n < channels; ++n) {
445 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
449 if (fs->write (buf, this_time) != this_time) {
454 nframes -= this_time;
458 list<AudioRange>::iterator tmp = i;
461 if (tmp != range.end()) {
463 /* fill gaps with silence */
465 nframes = (*tmp).start - (*i).end;
469 framepos_t this_time = min (nframes, chunk_size);
470 memset (buf, 0, sizeof (Sample) * this_time);
472 for (uint32_t n=0; n < channels; ++n) {
475 if (fs->write (buf, this_time) != this_time) {
480 nframes -= this_time;
490 now = localtime (&tnow);
492 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
493 (*s)->update_header (0, *now, tnow);
494 (*s)->mark_immutable ();
495 // do we need to ref it again?
501 /* unref created files */
503 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
504 (*i)->mark_for_remove ();
511 Editor::write_selection ()
513 if (!selection->time.empty()) {
514 write_audio_selection (selection->time);
515 } else if (!selection->regions.empty()) {
516 write_region_selection (selection->regions);