2 Copyright (C) 1999-2008 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
38 #include <ardour/export_utilities.h>
42 #include <ardour/export_failed.h>
43 #include <ardour/gdither.h>
44 #include <ardour/dB.h>
45 #include <pbd/failed_constructor.h>
53 /* SampleRateConverter */
55 SampleRateConverter::SampleRateConverter (uint32_t channels, nframes_t in_rate, nframes_t out_rate, int quality) :
64 if (in_rate == out_rate) {
72 if ((src_state = src_new (quality, channels, &err)) == 0) {
73 throw ExportFailed (string_compose (X_("Cannot initialize sample rate conversion: %1"), src_strerror (err)));
76 src_data.src_ratio = out_rate / (double) in_rate;
79 SampleRateConverter::~SampleRateConverter ()
82 src_delete (src_state);
93 SampleRateConverter::process (float * data, nframes_t frames)
97 return piped_to->write (data, frames);
102 nframes_t out_samples_max = (nframes_t) ceil (frames * src_data.src_ratio * channels);
103 if (data_out_size < out_samples_max) {
106 data_out = new float[out_samples_max];
107 src_data.data_out = data_out;
109 max_leftover_frames = 4 * frames;
110 leftover_data = (float *) realloc (leftover_data, max_leftover_frames * channels * sizeof (float));
111 if (!leftover_data) {
112 throw ExportFailed (X_("A memory allocation error occured during sample rate conversion"));
115 data_out_size = out_samples_max;
125 nframes_t frames_out_total = 0;
128 src_data.output_frames = out_samples_max / channels;
129 src_data.end_of_input = end_of_input;
130 src_data.data_out = data_out;
132 if (leftover_frames > 0) {
134 /* input data will be in leftover_data rather than data_in */
136 src_data.data_in = leftover_data;
140 /* first time, append new data from data_in into the leftover_data buffer */
142 memcpy (leftover_data + (leftover_frames * channels), data_in, frames_in * channels * sizeof(float));
143 src_data.input_frames = frames_in + leftover_frames;
146 /* otherwise, just use whatever is still left in leftover_data; the contents
147 were adjusted using memmove() right after the last SRC call (see
151 src_data.input_frames = leftover_frames;
156 src_data.data_in = data_in;
157 src_data.input_frames = frames_in;
163 if ((err = src_process (src_state, &src_data)) != 0) {
164 throw ExportFailed (string_compose ("An error occured during sample rate conversion: %1", src_strerror (err)));
167 frames_out = src_data.output_frames_gen;
168 leftover_frames = src_data.input_frames - src_data.input_frames_used;
170 if (leftover_frames > 0) {
171 if (leftover_frames > max_leftover_frames) {
172 error << _("warning, leftover frames overflowed, glitches might occur in output") << endmsg;
173 leftover_frames = max_leftover_frames;
175 memmove (leftover_data, (char *) (src_data.data_in + (src_data.input_frames_used * channels)),
176 leftover_frames * channels * sizeof(float));
180 nframes_t frames_written = piped_to->write (data_out, frames_out);
181 if (frames_written < 0) {
182 return frames_written;
184 frames_out_total += frames_written;
187 } while (leftover_frames > frames_in);
190 return frames_out_total;
193 /* SampleFormatConverter */
195 template <typename TOut>
196 SampleFormatConverter<TOut>::SampleFormatConverter (uint32_t channels, ExportFormatBase::DitherType type, int data_width_) :
198 data_width (data_width_),
204 if (data_width != 24) {
205 data_width = sizeof (TOut) * 8;
208 GDitherSize dither_size = GDitherFloat;
210 switch (data_width) {
212 dither_size = GDither8bit;
216 dither_size = GDither16bit;
219 dither_size = GDither32bit;
222 dither = gdither_new ((GDitherType) type, channels, dither_size, data_width);
225 template <typename TOut>
226 SampleFormatConverter<TOut>::~SampleFormatConverter ()
229 gdither_free (dither);
236 template <typename TOut>
238 SampleFormatConverter<TOut>::process (float * data, nframes_t frames)
240 /* Make sure we have enough memory allocated */
242 size_t data_size = channels * frames * sizeof (TOut);
243 if (data_size > data_out_size) {
245 data_out = new TOut[data_size];
246 data_out_size = data_size;
251 if (data_width < 32) {
252 for (uint32_t chn = 0; chn < channels; ++chn) {
253 gdither_runf (dither, chn, frames, data, data_out);
256 for (uint32_t chn = 0; chn < channels; ++chn) {
258 TOut * ob = data_out;
259 const double int_max = (float) INT_MAX;
260 const double int_min = (float) INT_MIN;
263 for (nframes_t x = 0; x < frames; ++x) {
264 i = chn + (x * channels);
266 if (data[i] > 1.0f) {
267 ob[i] = static_cast<TOut> (INT_MAX);
268 } else if (data[i] < -1.0f) {
269 ob[i] = static_cast<TOut> (INT_MIN);
271 if (data[i] >= 0.0f) {
272 ob[i] = lrintf (int_max * data[i]);
274 ob[i] = - lrintf (int_min * data[i]);
283 return GraphSinkVertex<float, TOut>::piped_to->write (data_out, frames);
288 SampleFormatConverter<float>::process (float * data, nframes_t frames)
291 for (nframes_t x = 0; x < frames * channels; ++x) {
292 if (data[x] > 1.0f) {
294 } else if (data[x] < -1.0f) {
300 return piped_to->write (data, frames);
303 template class SampleFormatConverter<short>;
304 template class SampleFormatConverter<int>;
305 template class SampleFormatConverter<float>;
309 Normalizer::Normalizer (uint32_t channels, float target_dB) :
313 target = dB_to_coefficient (target_dB);
315 if (target == 1.0f) {
316 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
317 that we may have clipped.
319 target -= FLT_EPSILON;
323 Normalizer::~Normalizer ()
329 Normalizer::set_peak (float peak)
331 if (peak == 0.0f || peak == target) {
336 gain = target / peak;
341 Normalizer::process (float * data, nframes_t frames)
344 for (nframes_t i = 0; i < (channels * frames); ++i) {
348 return piped_to->write (data, frames);