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 */
22 #define __STDC_FORMAT_MACROS 1
27 #include <gtkmm/messagedialog.h>
29 #include "export_dialog.h"
31 #include "public_editor.h"
32 #include "selection.h"
33 #include "time_axis_view.h"
34 #include "audio_time_axis.h"
35 #include "audio_region_view.h"
36 #include "midi_region_view.h"
38 #include "pbd/pthread_utils.h"
39 #include "ardour/types.h"
40 #include "ardour/audio_track.h"
41 #include "ardour/audiofilesource.h"
42 #include "ardour/audio_diskstream.h"
43 #include "ardour/audioregion.h"
44 #include "ardour/audioplaylist.h"
45 #include "ardour/chan_count.h"
46 #include "ardour/session_directory.h"
47 #include "ardour/source_factory.h"
48 #include "ardour/audiofilesource.h"
49 #include "ardour/session.h"
54 using namespace ARDOUR;
59 Editor::export_audio ()
61 ExportDialog dialog (*this);
62 dialog.set_session (_session);
67 Editor::export_selection ()
69 ExportSelectionDialog dialog (*this);
70 dialog.set_session (_session);
75 Editor::export_range ()
79 if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
80 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
87 if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
88 ExportRangeDialog dialog (*this, l->id().to_s());
89 dialog.set_session (_session);
94 /** Export the first selected region */
96 Editor::export_region ()
98 if (selection->regions.empty()) {
103 boost::shared_ptr<Region> r = selection->regions.front()->region();
104 AudioRegion & region (dynamic_cast<AudioRegion &> (*r));
106 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
107 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
109 ExportRegionDialog dialog (*this, region, track);
110 dialog.set_session (_session);
113 } catch (std::bad_cast & e) {
114 error << "Exporting Region failed!" << endmsg;
120 Editor::write_region_selection (RegionSelection& regions)
122 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
123 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
125 if (write_region ("", arv->audio_region()) == false)
129 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
131 warning << "MIDI region export not implemented" << endmsg;
139 Editor::bounce_region_selection ()
141 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
143 boost::shared_ptr<Region> region ((*i)->region());
144 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
145 Track* track = dynamic_cast<Track*>(rtv->route().get());
153 boost::shared_ptr<Region> r = track->bounce_range (region->position(), region->position() + region->length(), itt);
154 cerr << "Result of bounce of "
155 << region->name() << " len = " << region->length()
157 << r->name() << " len = " << r->length()
163 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
165 boost::shared_ptr<AudioFileSource> fs;
166 const nframes64_t chunk_size = 4096;
168 Sample buf[chunk_size];
169 gain_t gain_buffer[chunk_size];
173 vector<boost::shared_ptr<AudioFileSource> > sources;
176 const string sound_directory = _session->session_directory().sound_path().to_string();
178 nchans = region->n_channels();
180 /* don't do duplicate of the entire source if that's what is going on here */
182 if (region->start() == 0 && region->length() == region->source_length(0)) {
183 /* XXX should link(2) to create a new inode with "path" */
187 if (path.length() == 0) {
189 for (uint32_t n=0; n < nchans; ++n) {
191 for (cnt = 0; cnt < 999999; ++cnt) {
193 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
194 legalize_for_path(region->name()).c_str(), cnt);
197 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
198 legalize_for_path(region->name()).c_str(), cnt, n);
203 if (::access (path.c_str(), F_OK) != 0) {
209 error << "" << endmsg;
216 fs = boost::dynamic_pointer_cast<AudioFileSource> (
217 SourceFactory::createWritable (DataType::AUDIO, *_session,
219 false, _session->frame_rate()));
222 catch (failed_constructor& err) {
226 sources.push_back (fs);
230 /* TODO: make filesources based on passed path */
234 to_read = region->length();
235 pos = region->position();
238 nframes64_t this_time;
240 this_time = min (to_read, chunk_size);
242 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
246 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
250 if (fs->write (buf, this_time) != this_time) {
251 error << "" << endmsg;
256 to_read -= this_time;
263 now = localtime (&tnow);
265 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
266 (*src)->update_header (0, *now, tnow);
267 (*src)->mark_immutable ();
274 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
275 (*i)->mark_for_remove ();
282 Editor::write_audio_selection (TimeSelection& ts)
286 if (selection->tracks.empty()) {
290 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
292 AudioTimeAxisView* atv;
294 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
298 if (atv->is_audio_track()) {
300 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->get_diskstream()->playlist());
302 if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
313 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
315 boost::shared_ptr<AudioFileSource> fs;
316 const nframes64_t chunk_size = 4096;
318 Sample buf[chunk_size];
319 gain_t gain_buffer[chunk_size];
324 vector<boost::shared_ptr<AudioFileSource> > sources;
326 const string sound_directory = _session->session_directory().sound_path().to_string();
328 uint32_t channels = count.n_audio();
330 for (uint32_t n=0; n < channels; ++n) {
332 for (cnt = 0; cnt < 999999; ++cnt) {
334 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
335 legalize_for_path(playlist.name()).c_str(), cnt);
338 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
339 legalize_for_path(playlist.name()).c_str(), cnt, n);
342 if (::access (s, F_OK) != 0) {
348 error << "" << endmsg;
355 fs = boost::dynamic_pointer_cast<AudioFileSource> (
356 SourceFactory::createWritable (DataType::AUDIO, *_session,
358 false, _session->frame_rate()));
361 catch (failed_constructor& err) {
365 sources.push_back (fs);
370 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
372 nframes = (*i).length();
376 nframes64_t this_time;
378 this_time = min (nframes, chunk_size);
380 for (uint32_t n=0; n < channels; ++n) {
384 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
388 if (fs->write (buf, this_time) != this_time) {
393 nframes -= this_time;
397 list<AudioRange>::iterator tmp = i;
400 if (tmp != range.end()) {
402 /* fill gaps with silence */
404 nframes = (*tmp).start - (*i).end;
408 nframes64_t this_time = min (nframes, chunk_size);
409 memset (buf, 0, sizeof (Sample) * this_time);
411 for (uint32_t n=0; n < channels; ++n) {
414 if (fs->write (buf, this_time) != this_time) {
419 nframes -= this_time;
429 now = localtime (&tnow);
431 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
432 (*s)->update_header (0, *now, tnow);
433 (*s)->mark_immutable ();
434 // do we need to ref it again?
440 /* unref created files */
442 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
443 (*i)->mark_for_remove ();
450 Editor::write_selection ()
452 if (!selection->time.empty()) {
453 write_audio_selection (selection->time);
454 } else if (!selection->regions.empty()) {
455 write_region_selection (selection->regions);