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) :
58 max_leftover_frames (0),
67 if (in_rate == out_rate) {
75 if ((src_state = src_new (quality, channels, &err)) == 0) {
76 throw ExportFailed (string_compose (X_("Cannot initialize sample rate conversion: %1"), src_strerror (err)));
79 src_data.src_ratio = out_rate / (double) in_rate;
82 SampleRateConverter::~SampleRateConverter ()
85 src_delete (src_state);
96 SampleRateConverter::process (float * data, nframes_t frames)
100 return piped_to->write (data, frames);
105 nframes_t out_samples_max = (nframes_t) ceil (frames * src_data.src_ratio * channels);
106 if (data_out_size < out_samples_max) {
110 data_out = new float[out_samples_max];
111 src_data.data_out = data_out;
113 max_leftover_frames = 4 * frames;
114 leftover_data = (float *) realloc (leftover_data, max_leftover_frames * channels * sizeof (float));
115 if (!leftover_data) {
116 throw ExportFailed (X_("A memory allocation error occured during sample rate conversion"));
119 data_out_size = out_samples_max;
129 nframes_t frames_out_total = 0;
132 src_data.output_frames = out_samples_max / channels;
133 src_data.end_of_input = end_of_input;
134 src_data.data_out = data_out;
136 if (leftover_frames > 0) {
138 /* input data will be in leftover_data rather than data_in */
140 src_data.data_in = leftover_data;
144 /* first time, append new data from data_in into the leftover_data buffer */
146 memcpy (leftover_data + (leftover_frames * channels), data_in, frames_in * channels * sizeof(float));
147 src_data.input_frames = frames_in + leftover_frames;
150 /* otherwise, just use whatever is still left in leftover_data; the contents
151 were adjusted using memmove() right after the last SRC call (see
155 src_data.input_frames = leftover_frames;
160 src_data.data_in = data_in;
161 src_data.input_frames = frames_in;
167 if ((err = src_process (src_state, &src_data)) != 0) {
168 throw ExportFailed (string_compose ("An error occured during sample rate conversion: %1", src_strerror (err)));
171 frames_out = src_data.output_frames_gen;
172 leftover_frames = src_data.input_frames - src_data.input_frames_used;
174 if (leftover_frames > 0) {
175 if (leftover_frames > max_leftover_frames) {
176 error << _("warning, leftover frames overflowed, glitches might occur in output") << endmsg;
177 leftover_frames = max_leftover_frames;
179 memmove (leftover_data, (char *) (src_data.data_in + (src_data.input_frames_used * channels)),
180 leftover_frames * channels * sizeof(float));
184 nframes_t frames_written = piped_to->write (data_out, frames_out);
185 if (frames_written < 0) {
186 return frames_written;
188 frames_out_total += frames_written;
191 } while (leftover_frames > frames_in);
194 return frames_out_total;
197 /* SampleFormatConverter */
199 template <typename TOut>
200 SampleFormatConverter<TOut>::SampleFormatConverter (uint32_t channels, ExportFormatBase::DitherType type, int data_width_) :
202 data_width (data_width_),
208 if (data_width != 24) {
209 data_width = sizeof (TOut) * 8;
212 GDitherSize dither_size = GDitherFloat;
214 switch (data_width) {
216 dither_size = GDither8bit;
220 dither_size = GDither16bit;
223 dither_size = GDither32bit;
226 dither = gdither_new ((GDitherType) type, channels, dither_size, data_width);
229 template <typename TOut>
230 SampleFormatConverter<TOut>::~SampleFormatConverter ()
233 gdither_free (dither);
239 template <typename TOut>
241 SampleFormatConverter<TOut>::process (float * data, nframes_t frames)
243 /* Make sure we have enough memory allocated */
245 size_t data_size = channels * frames * sizeof (TOut);
246 if (data_size > data_out_size) {
250 data_out = new TOut[data_size];
251 data_out_size = data_size;
256 if (data_width < 32) {
257 for (uint32_t chn = 0; chn < channels; ++chn) {
258 gdither_runf (dither, chn, frames, data, data_out);
261 for (uint32_t chn = 0; chn < channels; ++chn) {
263 TOut * ob = data_out;
264 const double int_max = (float) INT_MAX;
265 const double int_min = (float) INT_MIN;
268 for (nframes_t x = 0; x < frames; ++x) {
269 i = chn + (x * channels);
271 if (data[i] > 1.0f) {
272 ob[i] = static_cast<TOut> (INT_MAX);
273 } else if (data[i] < -1.0f) {
274 ob[i] = static_cast<TOut> (INT_MIN);
276 if (data[i] >= 0.0f) {
277 ob[i] = lrintf (int_max * data[i]);
279 ob[i] = - lrintf (int_min * data[i]);
288 return GraphSinkVertex<float, TOut>::piped_to->write (data_out, frames);
293 SampleFormatConverter<float>::process (float * data, nframes_t frames)
296 for (nframes_t x = 0; x < frames * channels; ++x) {
297 if (data[x] > 1.0f) {
299 } else if (data[x] < -1.0f) {
305 return piped_to->write (data, frames);
308 template class SampleFormatConverter<short>;
309 template class SampleFormatConverter<int>;
310 template class SampleFormatConverter<float>;
314 Normalizer::Normalizer (uint32_t channels, float target_dB) :
318 target = dB_to_coefficient (target_dB);
320 if (target == 1.0f) {
321 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
322 that we may have clipped.
324 target -= FLT_EPSILON;
328 Normalizer::~Normalizer ()
334 Normalizer::set_peak (float peak)
336 if (peak == 0.0f || peak == target) {
341 gain = target / peak;
346 Normalizer::process (float * data, nframes_t frames)
349 for (nframes_t i = 0; i < (channels * frames); ++i) {
353 return piped_to->write (data, frames);