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.
24 #include <gtkmm/messagedialog.h>
26 #include "export_session_dialog.h"
27 #include "export_region_dialog.h"
28 #include "export_range_markers_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"
36 #include <pbd/pthread_utils.h>
37 #include <ardour/types.h>
38 #include <ardour/export.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/source_factory.h>
46 #include <ardour/audiofilesource.h>
51 using namespace ARDOUR;
56 Editor::export_session()
59 export_range (session->current_start_frame(), session->current_end_frame());
64 Editor::export_selection ()
67 if (selection->time.empty()) {
68 MessageDialog message (*this, _("There is no selection to export.\n\nSelect a selection using the range mouse mode"));
73 export_range (selection->time.front().start, selection->time.front().end);
78 Editor::export_range (nframes_t start, nframes_t end)
81 if (export_dialog == 0) {
82 export_dialog = new ExportSessionDialog (*this);
85 export_dialog->connect_to_session (session);
86 export_dialog->set_range (start, end);
87 export_dialog->start_export();
92 Editor::export_region ()
94 if (clicked_regionview == 0) {
98 ExportDialog* dialog = new ExportRegionDialog (*this, clicked_regionview->region());
100 dialog->connect_to_session (session);
102 clicked_regionview->region()->first_frame(),
103 clicked_regionview->region()->last_frame());
104 dialog->start_export();
108 Editor::export_range_markers ()
112 if (session->locations()->num_range_markers() == 0) {
113 MessageDialog message (*this, _("There are no ranges to export.\n\nCreate 1 or more ranges by dragging the mouse in the range bar"));
119 if (export_range_markers_dialog == 0) {
120 export_range_markers_dialog = new ExportRangeMarkersDialog(*this);
123 export_range_markers_dialog->connect_to_session (session);
124 export_range_markers_dialog->start_export();
129 Editor::write_region_selection (RegionSelection& regions)
131 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
133 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
135 if (write_region ("", arv->audio_region()) == false)
143 Editor::bounce_region_selection ()
145 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
147 boost::shared_ptr<Region> region ((*i)->region());
148 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
149 Track* track = dynamic_cast<Track*>(rtv->route().get());
157 track->bounce_range (region->position(), region->position() + region->length(), itt);
162 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
164 boost::shared_ptr<AudioFileSource> fs;
165 const nframes_t chunk_size = 4096;
167 Sample buf[chunk_size];
168 gain_t gain_buffer[chunk_size];
172 vector<boost::shared_ptr<AudioFileSource> > sources;
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()) {
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", session->sound_dir().c_str(),
191 legalize_for_path(region->name()).c_str(), cnt);
194 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().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> (SourceFactory::createReadable (DataType::AUDIO, *session, path, AudioFileSource::Flag (0)));
216 catch (failed_constructor& err) {
220 sources.push_back (fs);
224 /* TODO: make filesources based on passed path */
228 to_read = region->length();
229 pos = region->position();
234 this_time = min (to_read, chunk_size);
236 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
240 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
244 if (fs->write (buf, this_time) != this_time) {
245 error << "" << endmsg;
250 to_read -= this_time;
257 now = localtime (&tnow);
259 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
260 (*src)->update_header (0, *now, tnow);
267 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
268 (*i)->mark_for_remove ();
275 Editor::write_audio_selection (TimeSelection& ts)
279 if (selection->tracks.empty()) {
283 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
285 AudioTimeAxisView* atv;
287 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
291 if (atv->is_audio_track()) {
293 AudioPlaylist* playlist = dynamic_cast<AudioPlaylist*>(atv->get_diskstream()->playlist());
295 if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
306 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
308 boost::shared_ptr<AudioFileSource> fs;
309 const nframes_t chunk_size = 4096;
311 Sample buf[chunk_size];
312 gain_t gain_buffer[chunk_size];
317 vector<boost::shared_ptr<AudioFileSource> > sources;
319 uint32_t channels = count.get(DataType::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", session->sound_dir().c_str(),
326 legalize_for_path(playlist.name()).c_str(), cnt);
329 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().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> (SourceFactory::createReadable (DataType::AUDIO, *session, path, AudioFileSource::Flag (0)));
349 catch (failed_constructor& err) {
353 sources.push_back (fs);
358 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
360 nframes = (*i).length();
366 this_time = min (nframes, chunk_size);
368 for (uint32_t n=0; n < channels; ++n) {
372 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
376 if (fs->write (buf, this_time) != this_time) {
381 nframes -= this_time;
385 list<AudioRange>::iterator tmp = i;
388 if (tmp != range.end()) {
390 /* fill gaps with silence */
392 nframes = (*tmp).start - (*i).end;
396 nframes_t this_time = min (nframes, chunk_size);
397 memset (buf, 0, sizeof (Sample) * this_time);
399 for (uint32_t n=0; n < channels; ++n) {
402 if (fs->write (buf, this_time) != this_time) {
407 nframes -= this_time;
417 now = localtime (&tnow);
419 for (uint32_t n=0; n < channels; ++n) {
420 sources[n]->update_header (0, *now, tnow);
421 // do we need to ref it again?
427 /* unref created files */
429 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
430 (*i)->mark_for_remove ();
437 Editor::write_selection ()
439 if (!selection->time.empty()) {
440 write_audio_selection (selection->time);
441 } else if (!selection->regions.empty()) {
442 write_region_selection (selection->regions);