2 Copyright (C) 1999-2002 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 /* see gdither.cc for why we have to do this */
22 #define _ISOC9X_SOURCE 1
23 #define _ISOC99_SOURCE 1
36 #include <sigc++/bind.h>
38 #include <pbd/error.h>
39 #include <glibmm/thread.h>
41 #include <ardour/gdither.h>
42 #include <ardour/timestamps.h>
43 #include <ardour/ardour.h>
44 #include <ardour/session.h>
45 #include <ardour/export.h>
46 #include <ardour/sndfile_helpers.h>
47 #include <ardour/port.h>
48 #include <ardour/audioengine.h>
49 #include <ardour/audio_diskstream.h>
50 #include <ardour/panner.h>
55 using namespace ARDOUR;
59 convert_spec_to_info (AudioExportSpecification& spec, SF_INFO& sfinfo)
61 if (spec.path.length() == 0) {
62 error << _("Export: no output file specified") << endmsg;
66 /* XXX add checks that the directory path exists, and also
67 check if we are overwriting an existing file...
70 sfinfo.format = spec.format;
71 sfinfo.samplerate = spec.sample_rate;
72 sfinfo.frames = spec.end_frame - spec.start_frame + 1;
73 sfinfo.channels = min (spec.channels, 2U);
78 AudioExportSpecification::AudioExportSpecification ()
83 AudioExportSpecification::~AudioExportSpecification ()
89 AudioExportSpecification::init ()
106 max_leftover_frames = 0;
111 do_freewheel = false;
115 AudioExportSpecification::clear ()
123 src_delete (src_state);
128 gdither_free (dither);
149 freewheel_connection.disconnect ();
155 AudioExportSpecification::prepare (nframes_t blocksize, nframes_t frate)
158 GDitherSize dither_size;
163 error << _("illegal frame range in export specification") << endmsg;
167 if (start_frame >= end_frame) {
168 error << _("illegal frame range in export specification") << endmsg;
172 if ((data_width = sndfile_data_width(format)) == 0) {
173 error << _("Bad data width size. Report me!") << endmsg;
177 switch (data_width) {
179 dither_size = GDither8bit;
183 dither_size = GDither16bit;
187 dither_size = GDither32bit;
191 dither_size = GDitherFloat;
195 if (convert_spec_to_info (*this, sfinfo)) {
199 /* XXX make sure we have enough disk space for the output */
201 if ((out = sf_open (path.c_str(), SFM_WRITE, &sfinfo)) == 0) {
202 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
203 error << string_compose(_("Export: cannot open output file \"%1\" (%2)"), path, errbuf) << endmsg;
207 dataF = new float[blocksize * channels];
209 if (sample_rate != frame_rate) {
212 if ((src_state = src_new (src_quality, channels, &err)) == 0) {
213 error << string_compose (_("cannot initialize sample rate conversion: %1"), src_strerror (err)) << endmsg;
217 src_data.src_ratio = sample_rate / (double) frame_rate;
218 out_samples_max = (nframes_t) ceil (blocksize * src_data.src_ratio * channels);
219 dataF2 = new float[out_samples_max];
221 max_leftover_frames = 4 * blocksize;
222 leftoverF = new float[max_leftover_frames * channels];
226 out_samples_max = blocksize * channels;
229 dither = gdither_new (dither_type, channels, dither_size, data_width);
231 /* allocate buffers where dithering and output will occur */
233 switch (data_width) {
248 sample_bytes = 0; // float format
253 output_data = (void*) malloc (sample_bytes * out_samples_max);
260 AudioExportSpecification::process (nframes_t nframes)
262 float* float_buffer = 0;
268 nframes_t to_write = 0;
273 /* now do sample rate conversion */
275 if (sample_rate != frame_rate) {
279 src_data.output_frames = out_samples_max / channels;
280 src_data.end_of_input = ((pos + nframes) >= end_frame);
281 src_data.data_out = dataF2;
283 if (leftover_frames > 0) {
285 /* input data will be in leftoverF rather than dataF */
287 src_data.data_in = leftoverF;
291 /* first time, append new data from dataF into the leftoverF buffer */
293 memcpy (leftoverF + (leftover_frames * channels), dataF, nframes * channels * sizeof(float));
294 src_data.input_frames = nframes + leftover_frames;
297 /* otherwise, just use whatever is still left in leftoverF; the contents
298 were adjusted using memmove() right after the last SRC call (see
302 src_data.input_frames = leftover_frames;
307 src_data.data_in = dataF;
308 src_data.input_frames = nframes;
314 if ((err = src_process (src_state, &src_data)) != 0) {
315 error << string_compose (_("an error occured during sample rate conversion: %1"),
321 to_write = src_data.output_frames_gen;
322 leftover_frames = src_data.input_frames - src_data.input_frames_used;
324 if (leftover_frames > 0) {
325 if (leftover_frames > max_leftover_frames) {
326 error << _("warning, leftover frames overflowed, glitches might occur in output") << endmsg;
327 leftover_frames = max_leftover_frames;
329 memmove (leftoverF, (char *) (src_data.data_in + (src_data.input_frames_used * channels)),
330 leftover_frames * channels * sizeof(float));
333 float_buffer = dataF2;
337 /* no SRC, keep it simple */
341 float_buffer = dataF;
345 memset (output_data, 0, sample_bytes * to_write * channels);
348 switch (data_width) {
352 for (chn = 0; chn < channels; ++chn) {
353 gdither_runf (dither, chn, to_write, float_buffer, output_data);
358 for (chn = 0; chn < channels; ++chn) {
360 int *ob = (int *) output_data;
361 const double int_max = (float) INT_MAX;
362 const double int_min = (float) INT_MIN;
364 for (x = 0; x < to_write; ++x) {
365 i = chn + (x * channels);
367 if (float_buffer[i] > 1.0f) {
369 } else if (float_buffer[i] < -1.0f) {
372 if (float_buffer[i] >= 0.0f) {
373 ob[i] = lrintf (int_max * float_buffer[i]);
375 ob[i] = - lrintf (int_min * float_buffer[i]);
383 for (x = 0; x < to_write * channels; ++x) {
384 if (float_buffer[x] > 1.0f) {
385 float_buffer[x] = 1.0f;
386 } else if (float_buffer[x] < -1.0f) {
387 float_buffer[x] = -1.0f;
393 /* and export to disk */
395 switch (data_width) {
397 /* XXXX no way to deliver 8 bit audio to libsndfile */
402 written = sf_writef_short (out, (short*) output_data, to_write);
407 written = sf_writef_int (out, (int*) output_data, to_write);
411 written = sf_writef_float (out, float_buffer, to_write);
415 if ((nframes_t) written != to_write) {
416 sf_error_str (out, errbuf, sizeof (errbuf) - 1);
417 error << string_compose(_("Export: could not write data to output file (%1)"), errbuf) << endmsg;
422 } while (leftover_frames >= nframes);
428 Session::start_audio_export (AudioExportSpecification& spec)
430 if (spec.prepare (current_block_size, frame_rate())) {
434 spec.pos = spec.start_frame;
435 spec.end_frame = spec.end_frame;
436 spec.total_frames = spec.end_frame - spec.start_frame;
438 spec.do_freewheel = false; /* force a call to ::prepare_to_export() before proceeding to normal operation */
440 spec.freewheel_connection = _engine.Freewheel.connect (sigc::bind (mem_fun (*this, &Session::process_export), &spec));
442 return _engine.freewheel (true);
446 Session::stop_audio_export (AudioExportSpecification& spec)
448 /* don't stop freewheeling but do stop paying attention to it for now */
450 spec.freewheel_connection.disconnect ();
451 spec.clear (); /* resets running/stop etc */
457 Session::prepare_to_export (AudioExportSpecification& spec)
461 wait_till_butler_finished ();
463 /* take everyone out of awrite to avoid disasters */
466 boost::shared_ptr<RouteList> r = routes.reader ();
468 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
469 (*i)->protect_automation ();
473 /* get everyone to the right position */
476 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
478 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
479 if ((*i)-> seek (spec.start_frame, true)) {
480 error << string_compose (_("%1: cannot seek to %2 for export"),
481 (*i)->name(), spec.start_frame)
488 /* make sure we are actually rolling */
490 if (get_record_enabled()) {
491 disable_record (false);
498 post_export_slave = Config->get_slave_source ();
499 post_export_position = _transport_frame;
501 Config->set_slave_source (None);
503 /* get transport ready */
505 set_transport_speed (1.0, false);
506 butler_transport_work ();
507 g_atomic_int_set (&butler_should_do_transport_work, 0);
510 /* we are ready to go ... */
519 Session::process_export (nframes_t nframes, AudioExportSpecification* spec)
524 nframes_t this_nframes;
526 /* This is not required to be RT-safe because we are running while freewheeling */
528 if (spec->do_freewheel == false) {
530 /* first time in export function: get set up */
532 if (prepare_to_export (*spec)) {
533 spec->running = false;
538 spec->do_freewheel = true;
542 /* finished, but still freewheeling */
543 process_without_events (nframes);
547 if (!spec->running || spec->stop || (this_nframes = min ((spec->end_frame - spec->pos), nframes)) == 0) {
548 process_without_events (nframes);
549 return stop_audio_export (*spec);
552 /* make sure we've caught up with disk i/o, since
553 we're running faster than realtime c/o JACK.
556 wait_till_butler_finished ();
558 /* do the usual stuff */
560 process_without_events (nframes);
562 /* and now export the results */
564 nframes = this_nframes;
566 memset (spec->dataF, 0, sizeof (spec->dataF[0]) * nframes * spec->channels);
568 /* foreach output channel ... */
570 for (chn = 0; chn < spec->channels; ++chn) {
572 AudioExportPortMap::iterator mi = spec->port_map.find (chn);
574 if (mi == spec->port_map.end()) {
575 /* no ports exported to this channel */
579 vector<PortChannelPair>& mapped_ports ((*mi).second);
581 for (vector<PortChannelPair>::iterator t = mapped_ports.begin(); t != mapped_ports.end(); ++t) {
583 /* OK, this port's output is supposed to appear on this channel
586 Port* port = (*t).first;
587 Sample* port_buffer = port->get_buffer (nframes);
589 /* now interleave the data from the channel into the float buffer */
591 for (x = 0; x < nframes; ++x) {
592 spec->dataF[chn+(x*spec->channels)] += (float) port_buffer[x];
597 if (spec->process (nframes)) {
601 spec->pos += nframes;
602 spec->progress = 1.0 - (((float) spec->end_frame - spec->pos) / spec->total_frames);
604 /* and we're good to go */
610 sf_close (spec->out);
612 unlink (spec->path.c_str());
613 spec->running = false;
622 Session::finalize_audio_export ()
624 _engine.freewheel (false);
627 /* can't use stop_transport() here because we need
628 an immediate halt and don't require all the declick
629 stuff that stop_transport() implements.
632 realtime_stop (true);
633 schedule_butler_transport_work ();
635 /* restart slaving */
637 if (post_export_slave != None) {
638 Config->set_slave_source (post_export_slave);
640 locate (post_export_position, false, false, false);