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/audio_port.h>
50 #include <ardour/audioengine.h>
51 #include <ardour/audio_diskstream.h>
52 #include <ardour/panner.h>
57 using namespace ARDOUR;
61 convert_spec_to_info (AudioExportSpecification& spec, SF_INFO& sfinfo)
63 if (spec.path.length() == 0) {
64 error << _("Export: no output file specified") << endmsg;
68 /* XXX add checks that the directory path exists, and also
69 check if we are overwriting an existing file...
72 sfinfo.format = spec.format;
73 sfinfo.samplerate = spec.sample_rate;
74 sfinfo.frames = spec.end_frame - spec.start_frame + 1;
75 sfinfo.channels = min (spec.channels, 2U);
80 AudioExportSpecification::AudioExportSpecification ()
85 AudioExportSpecification::~AudioExportSpecification ()
91 AudioExportSpecification::init ()
108 max_leftover_frames = 0;
113 do_freewheel = false;
117 AudioExportSpecification::clear ()
125 src_delete (src_state);
130 gdither_free (dither);
151 freewheel_connection.disconnect ();
157 AudioExportSpecification::prepare (nframes_t blocksize, nframes_t frate)
160 GDitherSize dither_size;
165 error << _("illegal frame range in export specification") << endmsg;
169 if (start_frame >= end_frame) {
170 error << _("illegal frame range in export specification") << endmsg;
174 if ((data_width = sndfile_data_width(format)) == 0) {
175 error << _("Bad data width size. Report me!") << endmsg;
179 switch (data_width) {
181 dither_size = GDither8bit;
185 dither_size = GDither16bit;
189 dither_size = GDither32bit;
193 dither_size = GDitherFloat;
197 if (convert_spec_to_info (*this, sfinfo)) {
201 /* XXX make sure we have enough disk space for the output */
203 if ((out = sf_open (path.c_str(), SFM_WRITE, &sfinfo)) == 0) {
204 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
205 error << string_compose(_("Export: cannot open output file \"%1\" (%2)"), path, errbuf) << endmsg;
209 dataF = new float[blocksize * channels];
211 if (sample_rate != frame_rate) {
214 if ((src_state = src_new (src_quality, channels, &err)) == 0) {
215 error << string_compose (_("cannot initialize sample rate conversion: %1"), src_strerror (err)) << endmsg;
219 src_data.src_ratio = sample_rate / (double) frame_rate;
220 out_samples_max = (nframes_t) ceil (blocksize * src_data.src_ratio * channels);
221 dataF2 = new float[out_samples_max];
223 max_leftover_frames = 4 * blocksize;
224 leftoverF = new float[max_leftover_frames * channels];
228 out_samples_max = blocksize * channels;
231 dither = gdither_new (dither_type, channels, dither_size, data_width);
233 /* allocate buffers where dithering and output will occur */
235 switch (data_width) {
250 sample_bytes = 0; // float format
255 output_data = (void*) malloc (sample_bytes * out_samples_max);
262 AudioExportSpecification::process (nframes_t nframes)
264 float* float_buffer = 0;
270 nframes_t to_write = 0;
275 /* now do sample rate conversion */
277 if (sample_rate != frame_rate) {
281 src_data.output_frames = out_samples_max / channels;
282 src_data.end_of_input = ((pos + nframes) >= end_frame);
283 src_data.data_out = dataF2;
285 if (leftover_frames > 0) {
287 /* input data will be in leftoverF rather than dataF */
289 src_data.data_in = leftoverF;
293 /* first time, append new data from dataF into the leftoverF buffer */
295 memcpy (leftoverF + (leftover_frames * channels), dataF, nframes * channels * sizeof(float));
296 src_data.input_frames = nframes + leftover_frames;
299 /* otherwise, just use whatever is still left in leftoverF; the contents
300 were adjusted using memmove() right after the last SRC call (see
304 src_data.input_frames = leftover_frames;
309 src_data.data_in = dataF;
310 src_data.input_frames = nframes;
316 if ((err = src_process (src_state, &src_data)) != 0) {
317 error << string_compose (_("an error occured during sample rate conversion: %1"),
323 to_write = src_data.output_frames_gen;
324 leftover_frames = src_data.input_frames - src_data.input_frames_used;
326 if (leftover_frames > 0) {
327 if (leftover_frames > max_leftover_frames) {
328 error << _("warning, leftover frames overflowed, glitches might occur in output") << endmsg;
329 leftover_frames = max_leftover_frames;
331 memmove (leftoverF, (char *) (src_data.data_in + (src_data.input_frames_used * channels)),
332 leftover_frames * channels * sizeof(float));
335 float_buffer = dataF2;
339 /* no SRC, keep it simple */
343 float_buffer = dataF;
347 memset (output_data, 0, sample_bytes * to_write * channels);
350 switch (data_width) {
354 for (chn = 0; chn < channels; ++chn) {
355 gdither_runf (dither, chn, to_write, float_buffer, output_data);
360 for (chn = 0; chn < channels; ++chn) {
362 int *ob = (int *) output_data;
363 const double int_max = (float) INT_MAX;
364 const double int_min = (float) INT_MIN;
366 for (x = 0; x < to_write; ++x) {
367 i = chn + (x * channels);
369 if (float_buffer[i] > 1.0f) {
371 } else if (float_buffer[i] < -1.0f) {
374 if (float_buffer[i] >= 0.0f) {
375 ob[i] = lrintf (int_max * float_buffer[i]);
377 ob[i] = - lrintf (int_min * float_buffer[i]);
385 for (x = 0; x < to_write * channels; ++x) {
386 if (float_buffer[x] > 1.0f) {
387 float_buffer[x] = 1.0f;
388 } else if (float_buffer[x] < -1.0f) {
389 float_buffer[x] = -1.0f;
395 /* and export to disk */
397 switch (data_width) {
399 /* XXXX no way to deliver 8 bit audio to libsndfile */
404 written = sf_writef_short (out, (short*) output_data, to_write);
409 written = sf_writef_int (out, (int*) output_data, to_write);
413 written = sf_writef_float (out, float_buffer, to_write);
417 if ((nframes_t) written != to_write) {
418 sf_error_str (out, errbuf, sizeof (errbuf) - 1);
419 error << string_compose(_("Export: could not write data to output file (%1)"), errbuf) << endmsg;
424 } while (leftover_frames >= nframes);
430 Session::start_audio_export (AudioExportSpecification& spec)
434 if (spec.prepare (current_block_size, frame_rate())) {
438 spec.pos = spec.start_frame;
439 spec.end_frame = spec.end_frame;
440 spec.total_frames = spec.end_frame - spec.start_frame;
442 spec.freewheel_connection = _engine.Freewheel.connect (sigc::bind (mem_fun (*this, &Session::process_export), &spec));
444 if ((ret = _engine.freewheel (true)) == 0) {
446 spec.do_freewheel = false;
453 Session::stop_audio_export (AudioExportSpecification& spec)
455 /* can't use stop_transport() here because we need
456 an immediate halt and don't require all the declick
457 stuff that stop_transport() implements.
460 realtime_stop (true);
461 schedule_butler_transport_work ();
463 /* restart slaving */
465 if (post_export_slave != None) {
466 Config->set_slave_source (post_export_slave);
468 locate (post_export_position, false, false, false);
474 spec.running = false;
480 Session::prepare_to_export (AudioExportSpecification& spec)
484 wait_till_butler_finished ();
486 /* take everyone out of awrite to avoid disasters */
489 boost::shared_ptr<RouteList> r = routes.reader ();
491 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
492 (*i)->protect_automation ();
496 /* get everyone to the right position */
499 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
501 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
502 if ((*i)-> seek (spec.start_frame, true)) {
503 error << string_compose (_("%1: cannot seek to %2 for export"),
504 (*i)->name(), spec.start_frame)
511 /* make sure we are actually rolling */
513 if (get_record_enabled()) {
514 disable_record (false);
521 post_export_slave = Config->get_slave_source ();
522 post_export_position = _transport_frame;
524 Config->set_slave_source (None);
526 /* get transport ready */
528 set_transport_speed (1.0, false);
529 butler_transport_work ();
530 g_atomic_int_set (&butler_should_do_transport_work, 0);
533 /* we are ready to go ... */
542 Session::process_export (nframes_t nframes, AudioExportSpecification* spec)
547 nframes_t this_nframes;
549 /* This is not required to be RT-safe because we are running while freewheeling */
551 if (spec->do_freewheel == false) {
553 /* first time in export function: get set up */
555 if (prepare_to_export (*spec)) {
556 spec->running = false;
561 spec->do_freewheel = true;
565 /* finished, but still freewheeling */
566 process_without_events (nframes);
570 if (!spec->running || spec->stop || (this_nframes = min ((spec->end_frame - spec->pos), nframes)) == 0) {
571 process_without_events (nframes);
572 return stop_audio_export (*spec);
575 /* make sure we've caught up with disk i/o, since
576 we're running faster than realtime c/o JACK.
579 wait_till_butler_finished ();
581 /* do the usual stuff */
583 process_without_events (nframes);
585 /* and now export the results */
587 nframes = this_nframes;
589 memset (spec->dataF, 0, sizeof (spec->dataF[0]) * nframes * spec->channels);
591 /* foreach output channel ... */
593 for (chn = 0; chn < spec->channels; ++chn) {
595 AudioExportPortMap::iterator mi = spec->port_map.find (chn);
597 if (mi == spec->port_map.end()) {
598 /* no ports exported to this channel */
602 vector<PortChannelPair>& mapped_ports ((*mi).second);
604 for (vector<PortChannelPair>::iterator t = mapped_ports.begin(); t != mapped_ports.end(); ++t) {
606 /* OK, this port's output is supposed to appear on this channel
609 AudioPort* const port = dynamic_cast<AudioPort*>((*t).first);
611 cerr << "FIXME: Non-audio export" << endl;
614 Sample* port_buffer = port->get_audio_buffer().data(nframes);
616 /* now interleave the data from the channel into the float buffer */
618 for (x = 0; x < nframes; ++x) {
619 spec->dataF[chn+(x*spec->channels)] += (float) port_buffer[x];
624 if (spec->process (nframes)) {
628 spec->pos += nframes;
629 spec->progress = 1.0 - (((float) spec->end_frame - spec->pos) / spec->total_frames);
631 /* and we're good to go */
637 sf_close (spec->out);
639 unlink (spec->path.c_str());
640 spec->running = false;