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.
21 /* see gdither.cc for why we have to do this */
23 #define _ISOC9X_SOURCE 1
24 #define _ISOC99_SOURCE 1
37 #include <sigc++/bind.h>
39 #include <pbd/error.h>
40 #include <glibmm/thread.h>
42 #include <ardour/gdither.h>
43 #include <ardour/timestamps.h>
44 #include <ardour/ardour.h>
45 #include <ardour/session.h>
46 #include <ardour/export.h>
47 #include <ardour/sndfile_helpers.h>
48 #include <ardour/port.h>
49 #include <ardour/audioengine.h>
50 #include <ardour/audio_diskstream.h>
51 #include <ardour/panner.h>
56 using namespace ARDOUR;
60 convert_spec_to_info (AudioExportSpecification& spec, SF_INFO& sfinfo)
62 if (spec.path.length() == 0) {
63 error << _("Export: no output file specified") << endmsg;
67 /* XXX add checks that the directory path exists, and also
68 check if we are overwriting an existing file...
71 sfinfo.format = spec.format;
72 sfinfo.samplerate = spec.sample_rate;
73 sfinfo.frames = spec.end_frame - spec.start_frame + 1;
74 sfinfo.channels = min (spec.channels, 2U);
79 AudioExportSpecification::AudioExportSpecification ()
84 AudioExportSpecification::~AudioExportSpecification ()
90 AudioExportSpecification::init ()
107 max_leftover_frames = 0;
112 do_freewheel = false;
116 AudioExportSpecification::clear ()
124 src_delete (src_state);
129 gdither_free (dither);
150 freewheel_connection.disconnect ();
156 AudioExportSpecification::prepare (nframes_t blocksize, nframes_t frate)
159 GDitherSize dither_size;
164 error << _("illegal frame range in export specification") << endmsg;
168 if (start_frame >= end_frame) {
169 error << _("illegal frame range in export specification") << endmsg;
173 if ((data_width = sndfile_data_width(format)) == 0) {
174 error << _("Bad data width size. Report me!") << endmsg;
178 switch (data_width) {
180 dither_size = GDither8bit;
184 dither_size = GDither16bit;
188 dither_size = GDither32bit;
192 dither_size = GDitherFloat;
196 if (convert_spec_to_info (*this, sfinfo)) {
200 /* XXX make sure we have enough disk space for the output */
202 if ((out = sf_open (path.c_str(), SFM_WRITE, &sfinfo)) == 0) {
203 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
204 error << string_compose(_("Export: cannot open output file \"%1\" (%2)"), path, errbuf) << endmsg;
208 dataF = new float[blocksize * channels];
210 if (sample_rate != frame_rate) {
213 if ((src_state = src_new (src_quality, channels, &err)) == 0) {
214 error << string_compose (_("cannot initialize sample rate conversion: %1"), src_strerror (err)) << endmsg;
218 src_data.src_ratio = sample_rate / (double) frame_rate;
219 out_samples_max = (nframes_t) ceil (blocksize * src_data.src_ratio * channels);
220 dataF2 = new float[out_samples_max];
222 max_leftover_frames = 4 * blocksize;
223 leftoverF = new float[max_leftover_frames * channels];
227 out_samples_max = blocksize * channels;
230 dither = gdither_new (dither_type, channels, dither_size, data_width);
232 /* allocate buffers where dithering and output will occur */
234 switch (data_width) {
249 sample_bytes = 0; // float format
254 output_data = (void*) malloc (sample_bytes * out_samples_max);
261 AudioExportSpecification::process (nframes_t nframes)
263 float* float_buffer = 0;
269 nframes_t to_write = 0;
274 /* now do sample rate conversion */
276 if (sample_rate != frame_rate) {
280 src_data.output_frames = out_samples_max / channels;
281 src_data.end_of_input = ((pos + nframes) >= end_frame);
282 src_data.data_out = dataF2;
284 if (leftover_frames > 0) {
286 /* input data will be in leftoverF rather than dataF */
288 src_data.data_in = leftoverF;
292 /* first time, append new data from dataF into the leftoverF buffer */
294 memcpy (leftoverF + (leftover_frames * channels), dataF, nframes * channels * sizeof(float));
295 src_data.input_frames = nframes + leftover_frames;
298 /* otherwise, just use whatever is still left in leftoverF; the contents
299 were adjusted using memmove() right after the last SRC call (see
303 src_data.input_frames = leftover_frames;
308 src_data.data_in = dataF;
309 src_data.input_frames = nframes;
315 if ((err = src_process (src_state, &src_data)) != 0) {
316 error << string_compose (_("an error occured during sample rate conversion: %1"),
322 to_write = src_data.output_frames_gen;
323 leftover_frames = src_data.input_frames - src_data.input_frames_used;
325 if (leftover_frames > 0) {
326 if (leftover_frames > max_leftover_frames) {
327 error << _("warning, leftover frames overflowed, glitches might occur in output") << endmsg;
328 leftover_frames = max_leftover_frames;
330 memmove (leftoverF, (char *) (src_data.data_in + (src_data.input_frames_used * channels)),
331 leftover_frames * channels * sizeof(float));
334 float_buffer = dataF2;
338 /* no SRC, keep it simple */
342 float_buffer = dataF;
346 memset (output_data, 0, sample_bytes * to_write * channels);
349 switch (data_width) {
353 for (chn = 0; chn < channels; ++chn) {
354 gdither_runf (dither, chn, to_write, float_buffer, output_data);
359 for (chn = 0; chn < channels; ++chn) {
361 int *ob = (int *) output_data;
362 const double int_max = (float) INT_MAX;
363 const double int_min = (float) INT_MIN;
365 for (x = 0; x < to_write; ++x) {
366 i = chn + (x * channels);
368 if (float_buffer[i] > 1.0f) {
370 } else if (float_buffer[i] < -1.0f) {
373 if (float_buffer[i] >= 0.0f) {
374 ob[i] = lrintf (int_max * float_buffer[i]);
376 ob[i] = - lrintf (int_min * float_buffer[i]);
384 for (x = 0; x < to_write * channels; ++x) {
385 if (float_buffer[x] > 1.0f) {
386 float_buffer[x] = 1.0f;
387 } else if (float_buffer[x] < -1.0f) {
388 float_buffer[x] = -1.0f;
394 /* and export to disk */
396 switch (data_width) {
398 /* XXXX no way to deliver 8 bit audio to libsndfile */
403 written = sf_writef_short (out, (short*) output_data, to_write);
408 written = sf_writef_int (out, (int*) output_data, to_write);
412 written = sf_writef_float (out, float_buffer, to_write);
416 if ((nframes_t) written != to_write) {
417 sf_error_str (out, errbuf, sizeof (errbuf) - 1);
418 error << string_compose(_("Export: could not write data to output file (%1)"), errbuf) << endmsg;
423 } while (leftover_frames >= nframes);
429 Session::start_audio_export (AudioExportSpecification& spec)
433 if (spec.prepare (current_block_size, frame_rate())) {
437 spec.pos = spec.start_frame;
438 spec.end_frame = spec.end_frame;
439 spec.total_frames = spec.end_frame - spec.start_frame;
441 spec.freewheel_connection = _engine.Freewheel.connect (sigc::bind (mem_fun (*this, &Session::process_export), &spec));
443 if ((ret = _engine.freewheel (true)) == 0) {
445 spec.do_freewheel = false;
452 Session::stop_audio_export (AudioExportSpecification& spec)
454 /* can't use stop_transport() here because we need
455 an immediate halt and don't require all the declick
456 stuff that stop_transport() implements.
459 realtime_stop (true);
460 schedule_butler_transport_work ();
462 /* restart slaving */
464 if (post_export_slave != None) {
465 Config->set_slave_source (post_export_slave);
467 locate (post_export_position, false, false, false);
473 spec.running = false;
479 Session::prepare_to_export (AudioExportSpecification& spec)
483 wait_till_butler_finished ();
485 /* take everyone out of awrite to avoid disasters */
488 boost::shared_ptr<RouteList> r = routes.reader ();
490 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
491 (*i)->protect_automation ();
495 /* get everyone to the right position */
498 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
500 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
501 if ((*i)-> seek (spec.start_frame, true)) {
502 error << string_compose (_("%1: cannot seek to %2 for export"),
503 (*i)->name(), spec.start_frame)
510 /* make sure we are actually rolling */
512 if (get_record_enabled()) {
513 disable_record (false);
520 post_export_slave = Config->get_slave_source ();
521 post_export_position = _transport_frame;
523 Config->set_slave_source (None);
525 /* get transport ready */
527 set_transport_speed (1.0, false);
528 butler_transport_work ();
529 g_atomic_int_set (&butler_should_do_transport_work, 0);
532 /* we are ready to go ... */
541 Session::process_export (nframes_t nframes, AudioExportSpecification* spec)
546 nframes_t this_nframes;
548 /* This is not required to be RT-safe because we are running while freewheeling */
550 if (spec->do_freewheel == false) {
552 /* first time in export function: get set up */
554 if (prepare_to_export (*spec)) {
555 spec->running = false;
560 spec->do_freewheel = true;
564 /* finished, but still freewheeling */
565 process_without_events (nframes);
569 if (!spec->running || spec->stop || (this_nframes = min ((spec->end_frame - spec->pos), nframes)) == 0) {
570 process_without_events (nframes);
571 return stop_audio_export (*spec);
574 /* make sure we've caught up with disk i/o, since
575 we're running faster than realtime c/o JACK.
578 wait_till_butler_finished ();
580 /* do the usual stuff */
582 process_without_events (nframes);
584 /* and now export the results */
586 nframes = this_nframes;
588 memset (spec->dataF, 0, sizeof (spec->dataF[0]) * nframes * spec->channels);
590 /* foreach output channel ... */
592 for (chn = 0; chn < spec->channels; ++chn) {
594 AudioExportPortMap::iterator mi = spec->port_map.find (chn);
596 if (mi == spec->port_map.end()) {
597 /* no ports exported to this channel */
601 vector<PortChannelPair>& mapped_ports ((*mi).second);
603 for (vector<PortChannelPair>::iterator t = mapped_ports.begin(); t != mapped_ports.end(); ++t) {
605 /* OK, this port's output is supposed to appear on this channel
608 Port* port = (*t).first;
609 Sample* port_buffer = port->get_buffer (nframes);
611 /* now interleave the data from the channel into the float buffer */
613 for (x = 0; x < nframes; ++x) {
614 spec->dataF[chn+(x*spec->channels)] += (float) port_buffer[x];
619 if (spec->process (nframes)) {
623 spec->pos += nframes;
624 spec->progress = 1.0 - (((float) spec->end_frame - spec->pos) / spec->total_frames);
626 /* and we're good to go */
632 sf_close (spec->out);
634 unlink (spec->path.c_str());
635 spec->running = false;