bump version
[ardour2.git] / gtk2_ardour / editor_export_audio.cc
blob776832143628bf879b570275f82bcc2c2c88fdb6
1 /*
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 #include <unistd.h>
21 #include <climits>
23 #include <gtkmm/messagedialog.h>
25 #include "export_session_dialog.h"
26 #include "export_region_dialog.h"
27 #include "export_range_markers_dialog.h"
28 #include "editor.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/export.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/source_factory.h>
44 #include <ardour/audiofilesource.h>
46 #include "i18n.h"
48 using namespace std;
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace Gtk;
53 void
54 Editor::export_session()
56 if (session) {
57 export_range (session->current_start_frame(), session->current_end_frame());
61 void
62 Editor::export_selection ()
64 if (session) {
65 if (selection->time.empty()) {
66 MessageDialog message (*this, _("There is no selection to export.\n\nSelect a selection using the range mouse mode"));
67 message.run ();
68 return;
71 export_range (selection->time.front().start, selection->time.front().end);
75 void
76 Editor::export_range (nframes64_t start, nframes64_t end)
78 if (session) {
79 if (export_dialog == 0) {
80 export_dialog = new ExportSessionDialog (*this);
83 export_dialog->connect_to_session (session);
84 export_dialog->set_range (start, end);
85 export_dialog->start_export();
89 void
90 Editor::export_region ()
92 if (clicked_regionview == 0) {
93 return;
96 ExportDialog* dialog = new ExportRegionDialog (*this, clicked_regionview->region());
98 dialog->connect_to_session (session);
99 dialog->set_range (clicked_regionview->region()->first_frame(), clicked_regionview->region()->last_frame());
100 dialog->start_export();
103 void
104 Editor::export_range_markers ()
106 if (session) {
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"));
110 message.run ();
111 return;
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 (RegionSelection& regions)
127 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
128 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
129 if (arv)
130 if (write_region ("", arv->audio_region()) == false)
131 return -1;
134 return 0;
137 void
138 Editor::bounce_region_selection ()
140 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
142 boost::shared_ptr<Region> region ((*i)->region());
143 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
144 Track* track = dynamic_cast<Track*>(rtv->route().get());
146 InterThreadInfo itt;
148 itt.done = false;
149 itt.cancel = false;
150 itt.progress = 0.0f;
152 boost::shared_ptr<Region> r = track->bounce_range (region->position(), region->position() + region->length(), itt);
153 cerr << "Result of bounce of "
154 << region->name() << " len = " << region->length()
155 << " was "
156 << r->name() << " len = " << r->length()
157 << endl;
161 bool
162 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
164 boost::shared_ptr<AudioFileSource> fs;
165 const nframes64_t chunk_size = 4096;
166 nframes64_t to_read;
167 Sample buf[chunk_size];
168 gain_t gain_buffer[chunk_size];
169 nframes64_t pos;
170 char s[PATH_MAX+1];
171 uint32_t cnt;
172 vector<boost::shared_ptr<AudioFileSource> > sources;
173 uint32_t nchans;
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" */
181 return true;
184 if (path.length() == 0) {
186 for (uint32_t n=0; n < nchans; ++n) {
188 for (cnt = 0; cnt < 999999; ++cnt) {
189 if (nchans == 1) {
190 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
191 legalize_for_path(region->name()).c_str(), cnt);
193 else {
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);
198 path = s;
200 if (::access (path.c_str(), F_OK) != 0) {
201 break;
205 if (cnt == 999999) {
206 error << "" << endmsg;
207 goto error_out;
212 try {
213 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (*session, path, false, session->frame_rate()));
216 catch (failed_constructor& err) {
217 goto error_out;
220 sources.push_back (fs);
223 else {
224 /* TODO: make filesources based on passed path */
228 to_read = region->length();
229 pos = region->position();
231 while (to_read) {
232 nframes64_t this_time;
234 this_time = min (to_read, chunk_size);
236 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
238 fs = (*src);
240 if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
241 break;
244 if (fs->write (buf, this_time) != this_time) {
245 error << "" << endmsg;
246 goto error_out;
250 to_read -= this_time;
251 pos += this_time;
254 time_t tnow;
255 struct tm* now;
256 time (&tnow);
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);
261 (*src)->mark_immutable ();
264 return true;
266 error_out:
268 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
269 (*i)->mark_for_remove ();
272 return 0;
276 Editor::write_audio_selection (TimeSelection& ts)
278 int ret = 0;
280 if (selection->tracks.empty()) {
281 return 0;
284 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
286 AudioTimeAxisView* atv;
288 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
289 continue;
292 if (atv->is_audio_track()) {
294 boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->get_diskstream()->playlist());
296 if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
297 ret = -1;
298 break;
303 return ret;
306 bool
307 Editor::write_audio_range (AudioPlaylist& playlist, uint32_t channels, list<AudioRange>& range)
309 boost::shared_ptr<AudioFileSource> fs;
310 const nframes64_t chunk_size = 4096;
311 nframes64_t nframes;
312 Sample buf[chunk_size];
313 gain_t gain_buffer[chunk_size];
314 nframes64_t pos;
315 char s[PATH_MAX+1];
316 uint32_t cnt;
317 string path;
318 vector<boost::shared_ptr<AudioFileSource> > sources;
320 for (uint32_t n=0; n < channels; ++n) {
322 for (cnt = 0; cnt < 999999; ++cnt) {
323 if (channels == 1) {
324 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
325 legalize_for_path(playlist.name()).c_str(), cnt);
327 else {
328 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
329 legalize_for_path(playlist.name()).c_str(), cnt, n);
332 if (::access (s, F_OK) != 0) {
333 break;
337 if (cnt == 999999) {
338 error << "" << endmsg;
339 goto error_out;
342 path = s;
344 try {
345 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (*session, path, false, session->frame_rate()));
348 catch (failed_constructor& err) {
349 goto error_out;
352 sources.push_back (fs);
357 for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
359 nframes = (*i).length();
360 pos = (*i).start;
362 while (nframes) {
363 nframes64_t this_time;
365 this_time = min (nframes, chunk_size);
367 for (uint32_t n=0; n < channels; ++n) {
369 fs = sources[n];
371 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
372 break;
375 if (fs->write (buf, this_time) != this_time) {
376 goto error_out;
380 nframes -= this_time;
381 pos += this_time;
384 list<AudioRange>::iterator tmp = i;
385 ++tmp;
387 if (tmp != range.end()) {
389 /* fill gaps with silence */
391 nframes = (*tmp).start - (*i).end;
393 while (nframes) {
395 nframes64_t this_time = min (nframes, chunk_size);
396 memset (buf, 0, sizeof (Sample) * this_time);
398 for (uint32_t n=0; n < channels; ++n) {
400 fs = sources[n];
401 if (fs->write (buf, this_time) != this_time) {
402 goto error_out;
406 nframes -= this_time;
410 i = tmp;
413 time_t tnow;
414 struct tm* now;
415 time (&tnow);
416 now = localtime (&tnow);
418 for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
419 (*s)->update_header (0, *now, tnow);
420 (*s)->mark_immutable ();
421 // do we need to ref it again?
424 return true;
426 error_out:
427 /* unref created files */
429 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
430 (*i)->mark_for_remove ();
433 return false;
436 void
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);