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 "export_dialog.h"
32 #include "public_editor.h"
33 #include "selection.h"
34 #include "time_axis_view.h"
35 #include "audio_time_axis.h"
36 #include "audio_region_view.h"
37 #include "midi_region_view.h"
39 #include "pbd/pthread_utils.h"
40 #include "ardour/types.h"
41 #include "ardour/audio_track.h"
42 #include "ardour/audiofilesource.h"
43 #include "ardour/audio_diskstream.h"
44 #include "ardour/audioregion.h"
45 #include "ardour/audioplaylist.h"
46 #include "ardour/chan_count.h"
47 #include "ardour/session_directory.h"
48 #include "ardour/source_factory.h"
49 #include "ardour/audiofilesource.h"
50 #include "ardour/session.h"
55 using namespace ARDOUR;
60 Editor::export_audio ()
62 ExportDialog dialog (*this, _("Export"));
63 dialog.set_session (_session);
68 Editor::export_selection ()
70 ExportSelectionDialog dialog (*this);
71 dialog.set_session (_session);
76 Editor::export_range ()
80 if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
81 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
88 if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
89 ExportRangeDialog dialog (*this, l->id().to_s());
90 dialog.set_session (_session);
95 /** Export the first selected region */
97 Editor::export_region ()
99 if (selection->regions.empty()) {
104 boost::shared_ptr<Region> r = selection->regions.front()->region();
105 AudioRegion & region (dynamic_cast<AudioRegion &> (*r));
107 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
108 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
110 ExportRegionDialog dialog (*this, region, track);
111 dialog.set_session (_session);
114 } catch (std::bad_cast & e) {
115 error << "Exporting Region failed!" << endmsg;
121 Editor::write_region_selection (RegionSelection& regions)
123 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
124 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
126 if (write_region ("", arv->audio_region()) == false)
130 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
132 warning << "MIDI region export not implemented" << endmsg;
140 Editor::bounce_region_selection ()
142 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
144 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&(*i)->get_time_axis_view());
145 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (rtv->route());
147 if (!track->bounceable()) {
149 _("One or more of the selected regions' tracks cannot be bounced because it has more outputs than inputs. "
150 "You can fix this by increasing the number of inputs on that track.")
152 d.set_title (_("Cannot bounce"));
158 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
160 boost::shared_ptr<Region> region ((*i)->region());
161 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
162 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (rtv->route());
166 boost::shared_ptr<Region> r = track->bounce_range (region->position(), region->position() + region->length(), itt);
167 cerr << "Result of bounce of "
168 << region->name() << " len = " << region->length()
170 << r->name() << " len = " << r->length()
176 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
178 boost::shared_ptr<AudioFileSource> fs;
179 const framepos_t chunk_size = 4096;
181 Sample buf[chunk_size];
182 gain_t gain_buffer[chunk_size];
186 vector<boost::shared_ptr<AudioFileSource> > sources;
189 const string sound_directory = _session->session_directory().sound_path().to_string();
191 nchans = region->n_channels();
193 /* don't do duplicate of the entire source if that's what is going on here */
195 if (region->start() == 0 && region->length() == region->source_length(0)) {
196 /* XXX should link(2) to create a new inode with "path" */
200 if (path.length() == 0) {
202 for (uint32_t n=0; n < nchans; ++n) {
204 for (cnt = 0; cnt < 999999; ++cnt) {
206 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
207 legalize_for_path(region->name()).c_str(), cnt);
210 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
211 legalize_for_path(region->name()).c_str(), cnt, n);
216 if (::access (path.c_str(), F_OK) != 0) {
222 error << "" << endmsg;
229 fs = boost::dynamic_pointer_cast<AudioFileSource> (
230 SourceFactory::createWritable (DataType::AUDIO, *_session,
231 path, string(), true,
232 false, _session->frame_rate()));
235 catch (failed_constructor& err) {
239 sources.push_back (fs);
243 /* TODO: make filesources based on passed path */
247 to_read = region->length();
248 pos = region->position();
251 framepos_t this_time;
253 this_time = min (to_read, chunk_size);
255 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
259 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
263 if (fs->write (buf, this_time) != this_time) {
264 error << "" << endmsg;
269 to_read -= this_time;
276 now = localtime (&tnow);
278 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
279 (*src)->update_header (0, *now, tnow);
280 (*src)->mark_immutable ();
287 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
288 (*i)->mark_for_remove ();
295 Editor::write_audio_selection (TimeSelection& ts)
299 if (selection->tracks.empty()) {
303 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
305 AudioTimeAxisView* atv;
307 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
311 if (atv->is_audio_track()) {
313 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
315 if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
326 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
328 boost::shared_ptr<AudioFileSource> fs;
329 const framepos_t chunk_size = 4096;
331 Sample buf[chunk_size];
332 gain_t gain_buffer[chunk_size];
337 vector<boost::shared_ptr<AudioFileSource> > sources;
339 const string sound_directory = _session->session_directory().sound_path().to_string();
341 uint32_t channels = count.n_audio();
343 for (uint32_t n=0; n < channels; ++n) {
345 for (cnt = 0; cnt < 999999; ++cnt) {
347 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
348 legalize_for_path(playlist.name()).c_str(), cnt);
351 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
352 legalize_for_path(playlist.name()).c_str(), cnt, n);
355 if (::access (s, F_OK) != 0) {
361 error << "" << endmsg;
368 fs = boost::dynamic_pointer_cast<AudioFileSource> (
369 SourceFactory::createWritable (DataType::AUDIO, *_session,
370 path, string(), true,
371 false, _session->frame_rate()));
374 catch (failed_constructor& err) {
378 sources.push_back (fs);
383 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
385 nframes = (*i).length();
389 framepos_t this_time;
391 this_time = min (nframes, chunk_size);
393 for (uint32_t n=0; n < channels; ++n) {
397 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
401 if (fs->write (buf, this_time) != this_time) {
406 nframes -= this_time;
410 list<AudioRange>::iterator tmp = i;
413 if (tmp != range.end()) {
415 /* fill gaps with silence */
417 nframes = (*tmp).start - (*i).end;
421 framepos_t this_time = min (nframes, chunk_size);
422 memset (buf, 0, sizeof (Sample) * this_time);
424 for (uint32_t n=0; n < channels; ++n) {
427 if (fs->write (buf, this_time) != this_time) {
432 nframes -= this_time;
442 now = localtime (&tnow);
444 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
445 (*s)->update_header (0, *now, tnow);
446 (*s)->mark_immutable ();
447 // do we need to ref it again?
453 /* unref created files */
455 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
456 (*i)->mark_for_remove ();
463 Editor::write_selection ()
465 if (!selection->time.empty()) {
466 write_audio_selection (selection->time);
467 } else if (!selection->regions.empty()) {
468 write_region_selection (selection->regions);