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 "regionview.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>
48 using namespace ARDOUR;
53 Editor::export_session()
56 export_range (0, session->current_end_frame());
61 Editor::export_selection ()
64 if (selection->time.empty()) {
65 MessageDialog message (*this, _("There is no selection to export.\n\nSelect a selection using the range mouse mode"));
70 export_range (selection->time.front().start, selection->time.front().end);
75 Editor::export_range (jack_nframes_t start, jack_nframes_t end)
78 if (export_dialog == 0) {
79 export_dialog = new ExportSessionDialog (*this);
82 export_dialog->connect_to_session (session);
83 export_dialog->set_range (start, end);
84 export_dialog->start_export();
89 Editor::export_region ()
91 if (clicked_regionview == 0) {
95 ExportDialog* dialog = new ExportRegionDialog (*this, &clicked_regionview->region);
97 dialog->connect_to_session (session);
99 clicked_regionview->region.first_frame(),
100 clicked_regionview->region.last_frame());
101 dialog->start_export();
105 Editor::export_range_markers ()
109 if (session->locations()->num_range_markers() == 0) {
110 MessageDialog message (*this, _("There are no ranges to export.\n\nCreate 1 or more ranges by dragging the mouse in the range bar"));
116 if (export_range_markers_dialog == 0) {
117 export_range_markers_dialog = new ExportRangeMarkersDialog(*this);
120 export_range_markers_dialog->connect_to_session (session);
121 export_range_markers_dialog->start_export();
126 Editor::write_region_selection (AudioRegionSelection& regions)
128 for (AudioRegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
129 if (write_region ("", (*i)->region) == false) {
137 Editor::bounce_region_selection ()
139 for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
141 AudioRegion& region ((*i)->region);
142 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(&(*i)->get_time_axis_view());
143 AudioTrack* track = dynamic_cast<AudioTrack*>(&(atv->route()));
151 track->bounce_range (region.position(), region.position() + region.length(), itt);
156 Editor::write_region (string path, AudioRegion& region)
159 const jack_nframes_t chunk_size = 4096;
160 jack_nframes_t to_read;
161 Sample buf[chunk_size];
162 gain_t gain_buffer[chunk_size];
163 char workbuf[chunk_size *4];
167 vector<AudioFileSource *> sources;
170 nchans = region.n_channels();
172 /* don't do duplicate of the entire source if that's what is going on here */
174 if (region.start() == 0 && region.length() == region.source().length()) {
175 /* XXX should link(2) to create a new inode with "path" */
179 if (path.length() == 0) {
181 for (uint32_t n=0; n < nchans; ++n) {
183 for (cnt = 0; cnt < 999999; ++cnt) {
185 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
186 legalize_for_path(region.name()).c_str(), cnt);
189 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
190 legalize_for_path(region.name()).c_str(), cnt, n);
195 if (::access (path.c_str(), F_OK) != 0) {
201 error << "" << endmsg;
208 fs = AudioFileSource::create (path);
211 catch (failed_constructor& err) {
215 sources.push_back (fs);
219 /* TODO: make filesources based on passed path */
223 to_read = region.length();
224 pos = region.position();
227 jack_nframes_t this_time;
229 this_time = min (to_read, chunk_size);
231 for (vector<AudioFileSource *>::iterator src=sources.begin(); src != sources.end(); ++src) {
235 if (region.read_at (buf, buf, gain_buffer, workbuf, pos, this_time) != this_time) {
239 if (fs->write (buf, this_time, workbuf) != this_time) {
240 error << "" << endmsg;
245 to_read -= this_time;
252 now = localtime (&tnow);
254 for (vector<AudioFileSource *>::iterator src = sources.begin(); src != sources.end(); ++src) {
255 (*src)->update_header (0, *now, tnow);
262 for (vector<AudioFileSource*>::iterator i = sources.begin(); i != sources.end(); ++i) {
264 (*i)->mark_for_remove ();
272 Editor::write_audio_selection (TimeSelection& ts)
276 if (selection->tracks.empty()) {
280 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
282 AudioTimeAxisView* atv;
284 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
288 if (atv->is_audio_track()) {
290 AudioPlaylist* playlist = dynamic_cast<AudioPlaylist*>(atv->get_diskstream()->playlist());
292 if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
303 Editor::write_audio_range (AudioPlaylist& playlist, uint32_t channels, list<AudioRange>& range)
306 const jack_nframes_t chunk_size = 4096;
307 jack_nframes_t nframes;
308 Sample buf[chunk_size];
309 gain_t gain_buffer[chunk_size];
310 char workbuf[chunk_size*4];
315 vector<AudioFileSource *> sources;
317 for (uint32_t n=0; n < channels; ++n) {
319 for (cnt = 0; cnt < 999999; ++cnt) {
321 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
322 legalize_for_path(playlist.name()).c_str(), cnt);
325 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
326 legalize_for_path(playlist.name()).c_str(), cnt, n);
329 if (::access (s, F_OK) != 0) {
335 error << "" << endmsg;
342 fs = AudioFileSource::create (path);
345 catch (failed_constructor& err) {
349 sources.push_back (fs);
354 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
356 nframes = (*i).length();
360 jack_nframes_t this_time;
362 this_time = min (nframes, chunk_size);
364 for (uint32_t n=0; n < channels; ++n) {
368 if (playlist.read (buf, buf, gain_buffer, workbuf, pos, this_time, n) != this_time) {
372 if (fs->write (buf, this_time, workbuf) != this_time) {
377 nframes -= this_time;
381 list<AudioRange>::iterator tmp = i;
384 if (tmp != range.end()) {
386 /* fill gaps with silence */
388 nframes = (*tmp).start - (*i).end;
392 jack_nframes_t this_time = min (nframes, chunk_size);
393 memset (buf, 0, sizeof (Sample) * this_time);
395 for (uint32_t n=0; n < channels; ++n) {
398 if (fs->write (buf, this_time, workbuf) != this_time) {
403 nframes -= this_time;
413 now = localtime (&tnow);
415 for (uint32_t n=0; n < channels; ++n) {
416 sources[n]->update_header (0, *now, tnow);
417 // do we need to ref it again?
423 /* unref created files */
425 for (vector<AudioFileSource*>::iterator i = sources.begin(); i != sources.end(); ++i) {
426 (*i)->mark_for_remove ();
434 Editor::write_selection ()
436 if (!selection->time.empty()) {
437 write_audio_selection (selection->time);
438 } else if (!selection->audio_regions.empty()) {
439 write_region_selection (selection->audio_regions);