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 = track->bounce_range (region->position(), region->position() + region->length(), itt, with_processing);
169 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
171 boost::shared_ptr<AudioFileSource> fs;
172 const framepos_t chunk_size = 4096;
174 Sample buf[chunk_size];
175 gain_t gain_buffer[chunk_size];
179 vector<boost::shared_ptr<AudioFileSource> > sources;
182 const string sound_directory = _session->session_directory().sound_path().to_string();
184 nchans = region->n_channels();
186 /* don't do duplicate of the entire source if that's what is going on here */
188 if (region->start() == 0 && region->length() == region->source_length(0)) {
189 /* XXX should link(2) to create a new inode with "path" */
193 if (path.length() == 0) {
195 for (uint32_t n=0; n < nchans; ++n) {
197 for (cnt = 0; cnt < 999999; ++cnt) {
199 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
200 legalize_for_path(region->name()).c_str(), cnt);
203 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
204 legalize_for_path(region->name()).c_str(), cnt, n);
209 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
215 error << "" << endmsg;
222 fs = boost::dynamic_pointer_cast<AudioFileSource> (
223 SourceFactory::createWritable (DataType::AUDIO, *_session,
224 path, string(), true,
225 false, _session->frame_rate()));
228 catch (failed_constructor& err) {
232 sources.push_back (fs);
236 /* TODO: make filesources based on passed path */
240 to_read = region->length();
241 pos = region->position();
244 framepos_t this_time;
246 this_time = min (to_read, chunk_size);
248 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
252 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
256 if (fs->write (buf, this_time) != this_time) {
257 error << "" << endmsg;
262 to_read -= this_time;
269 now = localtime (&tnow);
271 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
272 (*src)->update_header (0, *now, tnow);
273 (*src)->mark_immutable ();
280 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
281 (*i)->mark_for_remove ();
288 Editor::write_audio_selection (TimeSelection& ts)
292 if (selection->tracks.empty()) {
296 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
298 AudioTimeAxisView* atv;
300 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
304 if (atv->is_audio_track()) {
306 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
308 if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
319 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
321 boost::shared_ptr<AudioFileSource> fs;
322 const framepos_t chunk_size = 4096;
324 Sample buf[chunk_size];
325 gain_t gain_buffer[chunk_size];
330 vector<boost::shared_ptr<AudioFileSource> > sources;
332 const string sound_directory = _session->session_directory().sound_path().to_string();
334 uint32_t channels = count.n_audio();
336 for (uint32_t n=0; n < channels; ++n) {
338 for (cnt = 0; cnt < 999999; ++cnt) {
340 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
341 legalize_for_path(playlist.name()).c_str(), cnt);
344 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
345 legalize_for_path(playlist.name()).c_str(), cnt, n);
348 if (!Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
354 error << "" << endmsg;
361 fs = boost::dynamic_pointer_cast<AudioFileSource> (
362 SourceFactory::createWritable (DataType::AUDIO, *_session,
363 path, string(), true,
364 false, _session->frame_rate()));
367 catch (failed_constructor& err) {
371 sources.push_back (fs);
376 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
378 nframes = (*i).length();
382 framepos_t this_time;
384 this_time = min (nframes, chunk_size);
386 for (uint32_t n=0; n < channels; ++n) {
390 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
394 if (fs->write (buf, this_time) != this_time) {
399 nframes -= this_time;
403 list<AudioRange>::iterator tmp = i;
406 if (tmp != range.end()) {
408 /* fill gaps with silence */
410 nframes = (*tmp).start - (*i).end;
414 framepos_t this_time = min (nframes, chunk_size);
415 memset (buf, 0, sizeof (Sample) * this_time);
417 for (uint32_t n=0; n < channels; ++n) {
420 if (fs->write (buf, this_time) != this_time) {
425 nframes -= this_time;
435 now = localtime (&tnow);
437 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
438 (*s)->update_header (0, *now, tnow);
439 (*s)->mark_immutable ();
440 // do we need to ref it again?
446 /* unref created files */
448 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
449 (*i)->mark_for_remove ();
456 Editor::write_selection ()
458 if (!selection->time.empty()) {
459 write_audio_selection (selection->time);
460 } else if (!selection->regions.empty()) {
461 write_region_selection (selection->regions);