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/filesource.h>
41 #include <ardour/diskstream.h>
42 #include <ardour/audioregion.h>
43 #include <ardour/audioplaylist.h>
48 using namespace ARDOUR;
52 Editor::export_session()
55 export_range (0, session->current_end_frame());
60 Editor::export_selection ()
63 if (selection->time.empty()) {
64 MessageDialog message (*this, _("There is no selection to export.\n\nSelect a selection using the range mouse mode"));
69 export_range (selection->time.front().start, selection->time.front().end);
74 Editor::export_range (jack_nframes_t start, jack_nframes_t end)
77 if (export_dialog == 0) {
78 export_dialog = new ExportSessionDialog (*this);
81 export_dialog->connect_to_session (session);
82 export_dialog->set_range (start, end);
83 export_dialog->start_export();
88 Editor::export_region ()
90 if (clicked_regionview == 0) {
94 ExportDialog* dialog = new ExportRegionDialog (*this, &clicked_regionview->region);
96 dialog->connect_to_session (session);
98 clicked_regionview->region.first_frame(),
99 clicked_regionview->region.last_frame());
100 dialog->start_export();
104 Editor::export_range_markers ()
108 if (session->locations()->num_range_markers() == 0) {
109 MessageDialog message (*this, _("There are no ranges to export.\n\nCreate 1 or more ranges by dragging the mouse in the range bar"));
115 if (export_range_markers_dialog == 0) {
116 export_range_markers_dialog = new ExportRangeMarkersDialog(*this);
119 export_range_markers_dialog->connect_to_session (session);
120 export_range_markers_dialog->start_export();
125 Editor::write_region_selection (AudioRegionSelection& regions)
127 for (AudioRegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
128 if (write_region ("", (*i)->region) == false) {
136 Editor::bounce_region_selection ()
138 for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
140 AudioRegion& region ((*i)->region);
141 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(&(*i)->get_time_axis_view());
142 AudioTrack* track = dynamic_cast<AudioTrack*>(&(atv->route()));
150 track->bounce_range (region.position(), region.position() + region.length(), itt);
155 Editor::write_region (string path, AudioRegion& region)
158 const jack_nframes_t chunk_size = 4096;
159 jack_nframes_t to_read;
160 Sample buf[chunk_size];
161 gain_t gain_buffer[chunk_size];
162 char workbuf[chunk_size *4];
166 vector<FileSource *> sources;
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()) {
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", session->sound_dir().c_str(),
185 legalize_for_path(region.name()).c_str(), cnt);
188 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().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 = new FileSource (path, session->frame_rate());
210 catch (failed_constructor& err) {
214 sources.push_back (fs);
218 /* TODO: make filesources based on passed path */
222 to_read = region.length();
223 pos = region.position();
226 jack_nframes_t this_time;
228 this_time = min (to_read, chunk_size);
230 for (vector<FileSource *>::iterator src=sources.begin(); src != sources.end(); ++src) {
234 if (region.read_at (buf, buf, gain_buffer, workbuf, pos, this_time) != this_time) {
238 if (fs->write (buf, this_time, workbuf) != this_time) {
239 error << "" << endmsg;
244 to_read -= this_time;
251 now = localtime (&tnow);
253 for (vector<FileSource *>::iterator src = sources.begin(); src != sources.end(); ++src) {
254 (*src)->update_header (0, *now, tnow);
261 for (vector<FileSource*>::iterator i = sources.begin(); i != sources.end(); ++i) {
262 (*i)->mark_for_remove ();
270 Editor::write_audio_selection (TimeSelection& ts)
274 if (selection->tracks.empty()) {
278 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
280 AudioTimeAxisView* atv;
282 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
286 if (atv->is_audio_track()) {
288 Playlist* playlist = atv->get_diskstream()->playlist();
290 if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
301 Editor::write_audio_range (Playlist& playlist, uint32_t channels, list<AudioRange>& range)
304 const jack_nframes_t chunk_size = 4096;
305 jack_nframes_t nframes;
306 Sample buf[chunk_size];
307 gain_t gain_buffer[chunk_size];
308 char workbuf[chunk_size*4];
313 vector<FileSource *> sources;
315 for (uint32_t n=0; n < channels; ++n) {
317 for (cnt = 0; cnt < 999999; ++cnt) {
319 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
320 legalize_for_path(playlist.name()).c_str(), cnt);
323 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
324 legalize_for_path(playlist.name()).c_str(), cnt, n);
327 if (::access (s, F_OK) != 0) {
333 error << "" << endmsg;
340 fs = new FileSource (path, session->frame_rate());
343 catch (failed_constructor& err) {
347 sources.push_back (fs);
352 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
354 nframes = (*i).length();
358 jack_nframes_t this_time;
360 this_time = min (nframes, chunk_size);
362 for (uint32_t n=0; n < channels; ++n) {
366 if (playlist.read (buf, buf, gain_buffer, workbuf, pos, this_time, n) != this_time) {
370 if (fs->write (buf, this_time, workbuf) != this_time) {
375 nframes -= this_time;
379 list<AudioRange>::iterator tmp = i;
382 if (tmp != range.end()) {
384 /* fill gaps with silence */
386 nframes = (*tmp).start - (*i).end;
390 jack_nframes_t this_time = min (nframes, chunk_size);
391 memset (buf, 0, sizeof (Sample) * this_time);
393 for (uint32_t n=0; n < channels; ++n) {
396 if (fs->write (buf, this_time, workbuf) != this_time) {
401 nframes -= this_time;
411 now = localtime (&tnow);
413 for (uint32_t n=0; n < channels; ++n) {
414 sources[n]->update_header (0, *now, tnow);
415 // do we need to ref it again?
421 /* unref created files */
423 for (vector<FileSource*>::iterator i = sources.begin(); i != sources.end(); ++i) {
424 (*i)->mark_for_remove ();
432 Editor::write_selection ()
434 if (!selection->time.empty()) {
435 write_audio_selection (selection->time);
436 } else if (!selection->audio_regions.empty()) {
437 write_region_selection (selection->audio_regions);