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 "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"
59 using namespace ARDOUR;
64 Editor::export_audio ()
66 ExportDialog dialog (*this, _("Export"), ExportProfileManager::RegularExport);
67 dialog.set_session (_session);
72 Editor::stem_export ()
74 StemExportDialog dialog (*this);
75 dialog.set_session (_session);
80 Editor::export_selection ()
82 ExportSelectionDialog dialog (*this);
83 dialog.set_session (_session);
88 Editor::export_range ()
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*/
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);
108 Editor::process_midi_export_dialog (MidiExportDialog& dialog, boost::shared_ptr<MidiRegion> midi_region)
110 string path = dialog.get_path ();
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?"));
121 /* force ::g_unlink because the backend code will
122 go wrong if it tries to open an existing
125 ::g_unlink (path.c_str());
128 return midi_region->do_export (path);
131 /** Export the first selected region */
133 Editor::export_region ()
135 if (selection->regions.empty()) {
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);
145 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
146 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
148 ExportRegionDialog dialog (*this, *(audio_region.get()), track);
149 dialog.set_session (_session);
152 } else if (midi_region) {
154 MidiExportDialog dialog (*this, midi_region);
155 dialog.set_session (_session);
157 bool finished = false;
159 switch (dialog.run ()) {
160 case Gtk::RESPONSE_ACCEPT:
161 finished = process_midi_export_dialog (dialog, midi_region);
172 Editor::write_region_selection (RegionSelection& regions)
174 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
175 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
177 if (write_region ("", arv->audio_region()) == false)
181 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
183 warning << "MIDI region export not implemented" << endmsg;
191 Editor::bounce_region_selection (bool with_processing)
193 /* no need to check for bounceable() because this operation never puts
194 * its results back in the playlist (only in the region list).
197 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
199 boost::shared_ptr<Region> region ((*i)->region());
200 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
201 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (rtv->route());
205 boost::shared_ptr<Region> r;
207 if (with_processing) {
208 r = track->bounce_range (region->position(), region->position() + region->length(), itt, track->main_outs(), false);
210 r = track->bounce_range (region->position(), region->position() + region->length(), itt, boost::shared_ptr<Processor>(), false);
216 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
218 boost::shared_ptr<AudioFileSource> fs;
219 const framepos_t chunk_size = 4096;
221 Sample buf[chunk_size];
222 gain_t gain_buffer[chunk_size];
226 vector<boost::shared_ptr<AudioFileSource> > sources;
229 const string sound_directory = _session->session_directory().sound_path();
231 nchans = region->n_channels();
233 /* don't do duplicate of the entire source if that's what is going on here */
235 if (region->start() == 0 && region->length() == region->source_length(0)) {
236 /* XXX should link(2) to create a new inode with "path" */
240 if (path.length() == 0) {
242 for (uint32_t n=0; n < nchans; ++n) {
244 for (cnt = 0; cnt < 999999; ++cnt) {
246 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
247 legalize_for_path(region->name()).c_str(), cnt);
250 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
251 legalize_for_path(region->name()).c_str(), cnt, n);
256 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
262 error << "" << endmsg;
269 fs = boost::dynamic_pointer_cast<AudioFileSource> (
270 SourceFactory::createWritable (DataType::AUDIO, *_session,
272 false, _session->frame_rate()));
275 catch (failed_constructor& err) {
279 sources.push_back (fs);
283 /* TODO: make filesources based on passed path */
287 to_read = region->length();
288 pos = region->position();
291 framepos_t this_time;
293 this_time = min (to_read, chunk_size);
295 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
299 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
303 if (fs->write (buf, this_time) != this_time) {
304 error << "" << endmsg;
309 to_read -= this_time;
316 now = localtime (&tnow);
318 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
319 (*src)->update_header (0, *now, tnow);
320 (*src)->mark_immutable ();
327 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
328 (*i)->mark_for_remove ();
335 Editor::write_audio_selection (TimeSelection& ts)
339 if (selection->tracks.empty()) {
343 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
345 AudioTimeAxisView* atv;
347 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
351 if (atv->is_audio_track()) {
353 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
355 if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
366 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
368 boost::shared_ptr<AudioFileSource> fs;
369 const framepos_t chunk_size = 4096;
371 Sample buf[chunk_size];
372 gain_t gain_buffer[chunk_size];
377 vector<boost::shared_ptr<AudioFileSource> > sources;
379 const string sound_directory = _session->session_directory().sound_path();
381 uint32_t channels = count.n_audio();
383 for (uint32_t n=0; n < channels; ++n) {
385 for (cnt = 0; cnt < 999999; ++cnt) {
387 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
388 legalize_for_path(playlist.name()).c_str(), cnt);
391 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
392 legalize_for_path(playlist.name()).c_str(), cnt, n);
395 if (!Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
401 error << "" << endmsg;
408 fs = boost::dynamic_pointer_cast<AudioFileSource> (
409 SourceFactory::createWritable (DataType::AUDIO, *_session,
411 false, _session->frame_rate()));
414 catch (failed_constructor& err) {
418 sources.push_back (fs);
423 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
425 nframes = (*i).length();
429 framepos_t this_time;
431 this_time = min (nframes, chunk_size);
433 for (uint32_t n=0; n < channels; ++n) {
437 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
441 if (fs->write (buf, this_time) != this_time) {
446 nframes -= this_time;
450 list<AudioRange>::iterator tmp = i;
453 if (tmp != range.end()) {
455 /* fill gaps with silence */
457 nframes = (*tmp).start - (*i).end;
461 framepos_t this_time = min (nframes, chunk_size);
462 memset (buf, 0, sizeof (Sample) * this_time);
464 for (uint32_t n=0; n < channels; ++n) {
467 if (fs->write (buf, this_time) != this_time) {
472 nframes -= this_time;
482 now = localtime (&tnow);
484 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
485 (*s)->update_header (0, *now, tnow);
486 (*s)->mark_immutable ();
487 // do we need to ref it again?
493 /* unref created files */
495 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
496 (*i)->mark_for_remove ();
503 Editor::write_selection ()
505 if (!selection->time.empty()) {
506 write_audio_selection (selection->time);
507 } else if (!selection->regions.empty()) {
508 write_region_selection (selection->regions);