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_diskstream.h"
33 #include "ardour/audio_track.h"
34 #include "ardour/audiofilesource.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_region_view.h"
50 #include "public_editor.h"
51 #include "selection.h"
52 #include "time_axis_view.h"
57 using namespace ARDOUR;
62 Editor::export_audio ()
64 ExportDialog dialog (*this, _("Export"), X_("ExportProfile"));
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<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
91 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
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);
105 /** Export the first selected region */
107 Editor::export_region ()
109 if (selection->regions.empty()) {
114 boost::shared_ptr<Region> r = selection->regions.front()->region();
115 AudioRegion & region (dynamic_cast<AudioRegion &> (*r));
117 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
118 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
120 ExportRegionDialog dialog (*this, region, track);
121 dialog.set_session (_session);
124 } catch (std::bad_cast & e) {
125 error << "Exporting Region failed!" << endmsg;
131 Editor::write_region_selection (RegionSelection& regions)
133 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
134 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
136 if (write_region ("", arv->audio_region()) == false)
140 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
142 warning << "MIDI region export not implemented" << endmsg;
150 Editor::bounce_region_selection (bool with_processing)
152 /* no need to check for bounceable() because this operation never puts
153 * its results back in the playlist (only in the region list).
156 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
158 boost::shared_ptr<Region> region ((*i)->region());
159 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
160 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (rtv->route());
164 boost::shared_ptr<Region> r;
166 if (with_processing) {
167 r = track->bounce_range (region->position(), region->position() + region->length(), itt, track->main_outs(), false);
169 r = track->bounce_range (region->position(), region->position() + region->length(), itt, boost::shared_ptr<Processor>(), false);
175 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
177 boost::shared_ptr<AudioFileSource> fs;
178 const framepos_t chunk_size = 4096;
180 Sample buf[chunk_size];
181 gain_t gain_buffer[chunk_size];
185 vector<boost::shared_ptr<AudioFileSource> > sources;
188 const string sound_directory = _session->session_directory().sound_path().to_string();
190 nchans = region->n_channels();
192 /* don't do duplicate of the entire source if that's what is going on here */
194 if (region->start() == 0 && region->length() == region->source_length(0)) {
195 /* XXX should link(2) to create a new inode with "path" */
199 if (path.length() == 0) {
201 for (uint32_t n=0; n < nchans; ++n) {
203 for (cnt = 0; cnt < 999999; ++cnt) {
205 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
206 legalize_for_path(region->name()).c_str(), cnt);
209 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
210 legalize_for_path(region->name()).c_str(), cnt, n);
215 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
221 error << "" << endmsg;
228 fs = boost::dynamic_pointer_cast<AudioFileSource> (
229 SourceFactory::createWritable (DataType::AUDIO, *_session,
230 path, string(), true,
231 false, _session->frame_rate()));
234 catch (failed_constructor& err) {
238 sources.push_back (fs);
242 /* TODO: make filesources based on passed path */
246 to_read = region->length();
247 pos = region->position();
250 framepos_t this_time;
252 this_time = min (to_read, chunk_size);
254 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
258 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
262 if (fs->write (buf, this_time) != this_time) {
263 error << "" << endmsg;
268 to_read -= this_time;
275 now = localtime (&tnow);
277 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
278 (*src)->update_header (0, *now, tnow);
279 (*src)->mark_immutable ();
286 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
287 (*i)->mark_for_remove ();
294 Editor::write_audio_selection (TimeSelection& ts)
298 if (selection->tracks.empty()) {
302 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
304 AudioTimeAxisView* atv;
306 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
310 if (atv->is_audio_track()) {
312 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
314 if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
325 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
327 boost::shared_ptr<AudioFileSource> fs;
328 const framepos_t chunk_size = 4096;
330 Sample buf[chunk_size];
331 gain_t gain_buffer[chunk_size];
336 vector<boost::shared_ptr<AudioFileSource> > sources;
338 const string sound_directory = _session->session_directory().sound_path().to_string();
340 uint32_t channels = count.n_audio();
342 for (uint32_t n=0; n < channels; ++n) {
344 for (cnt = 0; cnt < 999999; ++cnt) {
346 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
347 legalize_for_path(playlist.name()).c_str(), cnt);
350 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
351 legalize_for_path(playlist.name()).c_str(), cnt, n);
354 if (!Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
360 error << "" << endmsg;
367 fs = boost::dynamic_pointer_cast<AudioFileSource> (
368 SourceFactory::createWritable (DataType::AUDIO, *_session,
369 path, string(), true,
370 false, _session->frame_rate()));
373 catch (failed_constructor& err) {
377 sources.push_back (fs);
382 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
384 nframes = (*i).length();
388 framepos_t this_time;
390 this_time = min (nframes, chunk_size);
392 for (uint32_t n=0; n < channels; ++n) {
396 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
400 if (fs->write (buf, this_time) != this_time) {
405 nframes -= this_time;
409 list<AudioRange>::iterator tmp = i;
412 if (tmp != range.end()) {
414 /* fill gaps with silence */
416 nframes = (*tmp).start - (*i).end;
420 framepos_t this_time = min (nframes, chunk_size);
421 memset (buf, 0, sizeof (Sample) * this_time);
423 for (uint32_t n=0; n < channels; ++n) {
426 if (fs->write (buf, this_time) != this_time) {
431 nframes -= this_time;
441 now = localtime (&tnow);
443 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
444 (*s)->update_header (0, *now, tnow);
445 (*s)->mark_immutable ();
446 // do we need to ref it again?
452 /* unref created files */
454 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
455 (*i)->mark_for_remove ();
462 Editor::write_selection ()
464 if (!selection->time.empty()) {
465 write_audio_selection (selection->time);
466 } else if (!selection->regions.empty()) {
467 write_region_selection (selection->regions);