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 "export_dialog.h"
30 #include "public_editor.h"
31 #include "selection.h"
32 #include "time_axis_view.h"
33 #include "audio_time_axis.h"
34 #include "audio_region_view.h"
35 #include "midi_region_view.h"
37 #include "pbd/pthread_utils.h"
38 #include "ardour/types.h"
39 #include "ardour/audio_track.h"
40 #include "ardour/audiofilesource.h"
41 #include "ardour/audio_diskstream.h"
42 #include "ardour/audioregion.h"
43 #include "ardour/audioplaylist.h"
44 #include "ardour/chan_count.h"
45 #include "ardour/session_directory.h"
46 #include "ardour/source_factory.h"
47 #include "ardour/audiofilesource.h"
48 #include "ardour/session.h"
53 using namespace ARDOUR;
58 Editor::export_audio ()
60 ExportDialog dialog (*this, _("Export"));
61 dialog.set_session (_session);
66 Editor::export_selection ()
68 ExportSelectionDialog dialog (*this);
69 dialog.set_session (_session);
74 Editor::export_range ()
78 if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
79 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
86 if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
87 ExportRangeDialog dialog (*this, l->id().to_s());
88 dialog.set_session (_session);
93 /** Export the first selected region */
95 Editor::export_region ()
97 if (selection->regions.empty()) {
102 boost::shared_ptr<Region> r = selection->regions.front()->region();
103 AudioRegion & region (dynamic_cast<AudioRegion &> (*r));
105 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
106 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
108 ExportRegionDialog dialog (*this, region, track);
109 dialog.set_session (_session);
112 } catch (std::bad_cast & e) {
113 error << "Exporting Region failed!" << endmsg;
119 Editor::write_region_selection (RegionSelection& regions)
121 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
122 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
124 if (write_region ("", arv->audio_region()) == false)
128 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
130 warning << "MIDI region export not implemented" << endmsg;
138 Editor::bounce_region_selection ()
140 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
142 boost::shared_ptr<Region> region ((*i)->region());
143 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
144 Track* track = dynamic_cast<Track*>(rtv->route().get());
148 boost::shared_ptr<Region> r = track->bounce_range (region->position(), region->position() + region->length(), itt);
149 cerr << "Result of bounce of "
150 << region->name() << " len = " << region->length()
152 << r->name() << " len = " << r->length()
158 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
160 boost::shared_ptr<AudioFileSource> fs;
161 const framepos_t chunk_size = 4096;
163 Sample buf[chunk_size];
164 gain_t gain_buffer[chunk_size];
168 vector<boost::shared_ptr<AudioFileSource> > sources;
171 const string sound_directory = _session->session_directory().sound_path().to_string();
173 nchans = region->n_channels();
175 /* don't do duplicate of the entire source if that's what is going on here */
177 if (region->start() == 0 && region->length() == region->source_length(0)) {
178 /* XXX should link(2) to create a new inode with "path" */
182 if (path.length() == 0) {
184 for (uint32_t n=0; n < nchans; ++n) {
186 for (cnt = 0; cnt < 999999; ++cnt) {
188 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
189 legalize_for_path(region->name()).c_str(), cnt);
192 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
193 legalize_for_path(region->name()).c_str(), cnt, n);
198 if (::access (path.c_str(), F_OK) != 0) {
204 error << "" << endmsg;
211 fs = boost::dynamic_pointer_cast<AudioFileSource> (
212 SourceFactory::createWritable (DataType::AUDIO, *_session,
213 path, string(), true,
214 false, _session->frame_rate()));
217 catch (failed_constructor& err) {
221 sources.push_back (fs);
225 /* TODO: make filesources based on passed path */
229 to_read = region->length();
230 pos = region->position();
233 framepos_t this_time;
235 this_time = min (to_read, chunk_size);
237 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
241 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
245 if (fs->write (buf, this_time) != this_time) {
246 error << "" << endmsg;
251 to_read -= this_time;
258 now = localtime (&tnow);
260 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
261 (*src)->update_header (0, *now, tnow);
262 (*src)->mark_immutable ();
269 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
270 (*i)->mark_for_remove ();
277 Editor::write_audio_selection (TimeSelection& ts)
281 if (selection->tracks.empty()) {
285 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
287 AudioTimeAxisView* atv;
289 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
293 if (atv->is_audio_track()) {
295 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
297 if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
308 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
310 boost::shared_ptr<AudioFileSource> fs;
311 const framepos_t chunk_size = 4096;
313 Sample buf[chunk_size];
314 gain_t gain_buffer[chunk_size];
319 vector<boost::shared_ptr<AudioFileSource> > sources;
321 const string sound_directory = _session->session_directory().sound_path().to_string();
323 uint32_t channels = count.n_audio();
325 for (uint32_t n=0; n < channels; ++n) {
327 for (cnt = 0; cnt < 999999; ++cnt) {
329 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
330 legalize_for_path(playlist.name()).c_str(), cnt);
333 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
334 legalize_for_path(playlist.name()).c_str(), cnt, n);
337 if (::access (s, F_OK) != 0) {
343 error << "" << endmsg;
350 fs = boost::dynamic_pointer_cast<AudioFileSource> (
351 SourceFactory::createWritable (DataType::AUDIO, *_session,
352 path, string(), true,
353 false, _session->frame_rate()));
356 catch (failed_constructor& err) {
360 sources.push_back (fs);
365 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
367 nframes = (*i).length();
371 framepos_t this_time;
373 this_time = min (nframes, chunk_size);
375 for (uint32_t n=0; n < channels; ++n) {
379 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
383 if (fs->write (buf, this_time) != this_time) {
388 nframes -= this_time;
392 list<AudioRange>::iterator tmp = i;
395 if (tmp != range.end()) {
397 /* fill gaps with silence */
399 nframes = (*tmp).start - (*i).end;
403 framepos_t this_time = min (nframes, chunk_size);
404 memset (buf, 0, sizeof (Sample) * this_time);
406 for (uint32_t n=0; n < channels; ++n) {
409 if (fs->write (buf, this_time) != this_time) {
414 nframes -= this_time;
424 now = localtime (&tnow);
426 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
427 (*s)->update_header (0, *now, tnow);
428 (*s)->mark_immutable ();
429 // do we need to ref it again?
435 /* unref created files */
437 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
438 (*i)->mark_for_remove ();
445 Editor::write_selection ()
447 if (!selection->time.empty()) {
448 write_audio_selection (selection->time);
449 } else if (!selection->regions.empty()) {
450 write_region_selection (selection->regions);