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 */
25 #include <gtkmm/messagedialog.h>
27 #include "export_dialog.h"
29 #include "public_editor.h"
30 #include "selection.h"
31 #include "time_axis_view.h"
32 #include "audio_time_axis.h"
33 #include "audio_region_view.h"
34 #include "midi_region_view.h"
36 #include "pbd/pthread_utils.h"
37 #include "ardour/types.h"
38 #include "ardour/audio_track.h"
39 #include "ardour/audiofilesource.h"
40 #include "ardour/audio_diskstream.h"
41 #include "ardour/audioregion.h"
42 #include "ardour/audioplaylist.h"
43 #include "ardour/chan_count.h"
44 #include "ardour/session_directory.h"
45 #include "ardour/source_factory.h"
46 #include "ardour/audiofilesource.h"
51 using namespace ARDOUR;
56 Editor::export_audio ()
58 ExportDialog dialog (*this);
59 dialog.set_session (session);
64 Editor::export_selection ()
66 ExportSelectionDialog dialog (*this);
67 dialog.set_session (session);
72 Editor::export_range ()
76 if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
77 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
84 if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
85 ExportRangeDialog dialog (*this, l->id().to_s());
86 dialog.set_session (session);
91 /** Export the first selected region */
93 Editor::export_region ()
95 if (selection->regions.empty()) {
100 boost::shared_ptr<Region> r = selection->regions.front()->region();
101 AudioRegion & region (dynamic_cast<AudioRegion &> (*r));
103 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
104 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
106 ExportRegionDialog dialog (*this, region, track);
107 dialog.set_session (session);
110 } catch (std::bad_cast & e) {
111 error << "Exporting Region failed!" << endmsg;
117 Editor::write_region_selection (RegionSelection& regions)
119 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
120 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
122 if (write_region ("", arv->audio_region()) == false)
126 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
128 warning << "MIDI region export not implemented" << endmsg;
136 Editor::bounce_region_selection ()
138 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
140 boost::shared_ptr<Region> region ((*i)->region());
141 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
142 Track* track = dynamic_cast<Track*>(rtv->route().get());
150 boost::shared_ptr<Region> r = track->bounce_range (region->position(), region->position() + region->length(), itt);
151 cerr << "Result of bounce of "
152 << region->name() << " len = " << region->length()
154 << r->name() << " len = " << r->length()
160 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
162 boost::shared_ptr<AudioFileSource> fs;
163 const nframes64_t chunk_size = 4096;
165 Sample buf[chunk_size];
166 gain_t gain_buffer[chunk_size];
170 vector<boost::shared_ptr<AudioFileSource> > sources;
173 const string sound_directory = session->session_directory().sound_path().to_string();
175 nchans = region->n_channels();
177 /* don't do duplicate of the entire source if that's what is going on here */
179 if (region->start() == 0 && region->length() == region->source_length(0)) {
180 /* XXX should link(2) to create a new inode with "path" */
184 if (path.length() == 0) {
186 for (uint32_t n=0; n < nchans; ++n) {
188 for (cnt = 0; cnt < 999999; ++cnt) {
190 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
191 legalize_for_path(region->name()).c_str(), cnt);
194 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
195 legalize_for_path(region->name()).c_str(), cnt, n);
200 if (::access (path.c_str(), F_OK) != 0) {
206 error << "" << endmsg;
213 fs = boost::dynamic_pointer_cast<AudioFileSource> (
214 SourceFactory::createWritable (DataType::AUDIO, *session,
216 false, session->frame_rate()));
219 catch (failed_constructor& err) {
223 sources.push_back (fs);
227 /* TODO: make filesources based on passed path */
231 to_read = region->length();
232 pos = region->position();
235 nframes64_t this_time;
237 this_time = min (to_read, chunk_size);
239 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
243 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
247 if (fs->write (buf, this_time) != this_time) {
248 error << "" << endmsg;
253 to_read -= this_time;
260 now = localtime (&tnow);
262 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
263 (*src)->update_header (0, *now, tnow);
264 (*src)->mark_immutable ();
271 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
272 (*i)->mark_for_remove ();
279 Editor::write_audio_selection (TimeSelection& ts)
283 if (selection->tracks.empty()) {
287 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
289 AudioTimeAxisView* atv;
291 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
295 if (atv->is_audio_track()) {
297 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->get_diskstream()->playlist());
299 if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
310 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
312 boost::shared_ptr<AudioFileSource> fs;
313 const nframes64_t chunk_size = 4096;
315 Sample buf[chunk_size];
316 gain_t gain_buffer[chunk_size];
321 vector<boost::shared_ptr<AudioFileSource> > sources;
323 const string sound_directory = session->session_directory().sound_path().to_string();
325 uint32_t channels = count.n_audio();
327 for (uint32_t n=0; n < channels; ++n) {
329 for (cnt = 0; cnt < 999999; ++cnt) {
331 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
332 legalize_for_path(playlist.name()).c_str(), cnt);
335 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
336 legalize_for_path(playlist.name()).c_str(), cnt, n);
339 if (::access (s, F_OK) != 0) {
345 error << "" << endmsg;
352 fs = boost::dynamic_pointer_cast<AudioFileSource> (
353 SourceFactory::createWritable (DataType::AUDIO, *session,
355 false, session->frame_rate()));
358 catch (failed_constructor& err) {
362 sources.push_back (fs);
367 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
369 nframes = (*i).length();
373 nframes64_t this_time;
375 this_time = min (nframes, chunk_size);
377 for (uint32_t n=0; n < channels; ++n) {
381 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
385 if (fs->write (buf, this_time) != this_time) {
390 nframes -= this_time;
394 list<AudioRange>::iterator tmp = i;
397 if (tmp != range.end()) {
399 /* fill gaps with silence */
401 nframes = (*tmp).start - (*i).end;
405 nframes64_t this_time = min (nframes, chunk_size);
406 memset (buf, 0, sizeof (Sample) * this_time);
408 for (uint32_t n=0; n < channels; ++n) {
411 if (fs->write (buf, this_time) != this_time) {
416 nframes -= this_time;
426 now = localtime (&tnow);
428 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
429 (*s)->update_header (0, *now, tnow);
430 (*s)->mark_immutable ();
431 // do we need to ref it again?
437 /* unref created files */
439 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
440 (*i)->mark_for_remove ();
447 Editor::write_selection ()
449 if (!selection->time.empty()) {
450 write_audio_selection (selection->time);
451 } else if (!selection->regions.empty()) {
452 write_region_selection (selection->regions);