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"
35 #include "pbd/pthread_utils.h"
36 #include "ardour/types.h"
37 #include "ardour/audio_track.h"
38 #include "ardour/audiofilesource.h"
39 #include "ardour/audio_diskstream.h"
40 #include "ardour/audioregion.h"
41 #include "ardour/audioplaylist.h"
42 #include "ardour/chan_count.h"
43 #include "ardour/session_directory.h"
44 #include "ardour/source_factory.h"
45 #include "ardour/audiofilesource.h"
50 using namespace ARDOUR;
55 Editor::export_audio ()
57 ExportDialog dialog (*this);
58 dialog.set_session (session);
63 Editor::export_selection ()
65 ExportSelectionDialog dialog (*this);
66 dialog.set_session (session);
71 Editor::export_range ()
75 if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
76 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
83 if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
84 ExportRangeDialog dialog (*this, l->id().to_s());
85 dialog.set_session (session);
90 /** Export the first selected region */
92 Editor::export_region ()
94 if (selection->regions.empty()) {
99 boost::shared_ptr<Region> r = selection->regions.front()->region();
100 AudioRegion & region (dynamic_cast<AudioRegion &> (*r));
102 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
103 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
105 ExportRegionDialog dialog (*this, region, track);
106 dialog.set_session (session);
109 } catch (std::bad_cast & e) {
110 error << "Exporting Region failed!" << endmsg;
116 Editor::write_region_selection (RegionSelection& regions)
118 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)
130 Editor::bounce_region_selection ()
132 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
134 boost::shared_ptr<Region> region ((*i)->region());
135 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
136 Track* track = dynamic_cast<Track*>(rtv->route().get());
144 boost::shared_ptr<Region> r = track->bounce_range (region->position(), region->position() + region->length(), itt);
145 cerr << "Result of bounce of "
146 << region->name() << " len = " << region->length()
148 << r->name() << " len = " << r->length()
154 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
156 boost::shared_ptr<AudioFileSource> fs;
157 const nframes64_t chunk_size = 4096;
159 Sample buf[chunk_size];
160 gain_t gain_buffer[chunk_size];
164 vector<boost::shared_ptr<AudioFileSource> > sources;
167 const string sound_directory = session->session_directory().sound_path().to_string();
169 nchans = region->n_channels();
171 /* don't do duplicate of the entire source if that's what is going on here */
173 if (region->start() == 0 && region->length() == region->source_length(0)) {
174 /* XXX should link(2) to create a new inode with "path" */
178 if (path.length() == 0) {
180 for (uint32_t n=0; n < nchans; ++n) {
182 for (cnt = 0; cnt < 999999; ++cnt) {
184 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
185 legalize_for_path(region->name()).c_str(), cnt);
188 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
189 legalize_for_path(region->name()).c_str(), cnt, n);
194 if (::access (path.c_str(), F_OK) != 0) {
200 error << "" << endmsg;
207 fs = boost::dynamic_pointer_cast<AudioFileSource> (
208 SourceFactory::createWritable (DataType::AUDIO, *session,
210 false, session->frame_rate()));
213 catch (failed_constructor& err) {
217 sources.push_back (fs);
221 /* TODO: make filesources based on passed path */
225 to_read = region->length();
226 pos = region->position();
229 nframes64_t this_time;
231 this_time = min (to_read, chunk_size);
233 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
237 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
241 if (fs->write (buf, this_time) != this_time) {
242 error << "" << endmsg;
247 to_read -= this_time;
254 now = localtime (&tnow);
256 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
257 (*src)->update_header (0, *now, tnow);
258 (*src)->mark_immutable ();
265 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
266 (*i)->mark_for_remove ();
273 Editor::write_audio_selection (TimeSelection& ts)
277 if (selection->tracks.empty()) {
281 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
283 AudioTimeAxisView* atv;
285 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
289 if (atv->is_audio_track()) {
291 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->get_diskstream()->playlist());
293 if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
304 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
306 boost::shared_ptr<AudioFileSource> fs;
307 const nframes64_t chunk_size = 4096;
309 Sample buf[chunk_size];
310 gain_t gain_buffer[chunk_size];
315 vector<boost::shared_ptr<AudioFileSource> > sources;
317 const string sound_directory = session->session_directory().sound_path().to_string();
319 uint32_t channels = count.n_audio();
321 for (uint32_t n=0; n < channels; ++n) {
323 for (cnt = 0; cnt < 999999; ++cnt) {
325 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
326 legalize_for_path(playlist.name()).c_str(), cnt);
329 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
330 legalize_for_path(playlist.name()).c_str(), cnt, n);
333 if (::access (s, F_OK) != 0) {
339 error << "" << endmsg;
346 fs = boost::dynamic_pointer_cast<AudioFileSource> (
347 SourceFactory::createWritable (DataType::AUDIO, *session,
349 false, session->frame_rate()));
352 catch (failed_constructor& err) {
356 sources.push_back (fs);
361 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
363 nframes = (*i).length();
367 nframes64_t this_time;
369 this_time = min (nframes, chunk_size);
371 for (uint32_t n=0; n < channels; ++n) {
375 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
379 if (fs->write (buf, this_time) != this_time) {
384 nframes -= this_time;
388 list<AudioRange>::iterator tmp = i;
391 if (tmp != range.end()) {
393 /* fill gaps with silence */
395 nframes = (*tmp).start - (*i).end;
399 nframes64_t this_time = min (nframes, chunk_size);
400 memset (buf, 0, sizeof (Sample) * this_time);
402 for (uint32_t n=0; n < channels; ++n) {
405 if (fs->write (buf, this_time) != this_time) {
410 nframes -= this_time;
420 now = localtime (&tnow);
422 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
423 (*s)->update_header (0, *now, tnow);
424 (*s)->mark_immutable ();
425 // do we need to ref it again?
431 /* unref created files */
433 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
434 (*i)->mark_for_remove ();
441 Editor::write_selection ()
443 if (!selection->time.empty()) {
444 write_audio_selection (selection->time);
445 } else if (!selection->regions.empty()) {
446 write_region_selection (selection->regions);