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
39 #include <ardour/export_utilities.h>
43 #include <ardour/export_failed.h>
44 #include <ardour/gdither.h>
45 #include <ardour/dB.h>
46 #include <pbd/failed_constructor.h>
54 /* SampleRateConverter */
56 SampleRateConverter::SampleRateConverter (uint32_t channels, nframes_t in_rate, nframes_t out_rate, int quality) :
65 if (in_rate == out_rate) {
73 if ((src_state = src_new (quality, channels, &err)) == 0) {
74 throw ExportFailed (string_compose (_("cannot initialize sample rate conversion: %1"), src_strerror (err)), "Cannot initialize sample rate conversion");
77 src_data.src_ratio = out_rate / (double) in_rate;
80 SampleRateConverter::~SampleRateConverter ()
83 src_delete (src_state);
94 SampleRateConverter::process (float * data, nframes_t frames)
98 return piped_to->write (data, frames);
103 nframes_t out_samples_max = (nframes_t) ceil (frames * src_data.src_ratio * channels);
104 if (data_out_size < out_samples_max) {
107 data_out = new float[out_samples_max];
108 src_data.data_out = data_out;
110 max_leftover_frames = 4 * frames;
111 leftover_data = (float *) realloc (leftover_data, max_leftover_frames * channels * sizeof (float));
112 if (!leftover_data) {
113 throw ExportFailed (_("A memory allocation error occured during sample rate conversion"), "Samplerate conversion failed");
116 data_out_size = out_samples_max;
126 nframes_t frames_out_total = 0;
129 src_data.output_frames = out_samples_max / channels;
130 src_data.end_of_input = end_of_input;
131 src_data.data_out = data_out;
133 if (leftover_frames > 0) {
135 /* input data will be in leftover_data rather than data_in */
137 src_data.data_in = leftover_data;
141 /* first time, append new data from data_in into the leftover_data buffer */
143 memcpy (leftover_data + (leftover_frames * channels), data_in, frames_in * channels * sizeof(float));
144 src_data.input_frames = frames_in + leftover_frames;
147 /* otherwise, just use whatever is still left in leftover_data; the contents
148 were adjusted using memmove() right after the last SRC call (see
152 src_data.input_frames = leftover_frames;
157 src_data.data_in = data_in;
158 src_data.input_frames = frames_in;
164 if ((err = src_process (src_state, &src_data)) != 0) {
165 throw ExportFailed (_("an error occured during sample rate conversion"), string_compose ("an error occured during sample rate conversion: %1", src_strerror (err)));
168 frames_out = src_data.output_frames_gen;
169 leftover_frames = src_data.input_frames - src_data.input_frames_used;
171 if (leftover_frames > 0) {
172 if (leftover_frames > max_leftover_frames) {
173 error << _("warning, leftover frames overflowed, glitches might occur in output") << endmsg;
174 leftover_frames = max_leftover_frames;
176 memmove (leftover_data, (char *) (src_data.data_in + (src_data.input_frames_used * channels)),
177 leftover_frames * channels * sizeof(float));
181 nframes_t frames_written = piped_to->write (data_out, frames_out);
182 if (frames_written < 0) {
183 return frames_written;
185 frames_out_total += frames_written;
188 } while (leftover_frames > frames_in);
191 return frames_out_total;
194 /* SampleFormatConverter */
196 template <typename TOut>
197 SampleFormatConverter<TOut>::SampleFormatConverter (uint32_t channels, ExportFormatBase::DitherType type, int data_width_) :
199 data_width (data_width_),
205 if (data_width != 24) {
206 data_width = sizeof (TOut) * 8;
209 GDitherSize dither_size = GDitherFloat;
211 switch (data_width) {
213 dither_size = GDither8bit;
217 dither_size = GDither16bit;
220 dither_size = GDither32bit;
223 dither = gdither_new ((GDitherType) type, channels, dither_size, data_width);
226 template <typename TOut>
227 SampleFormatConverter<TOut>::~SampleFormatConverter ()
230 gdither_free (dither);
237 template <typename TOut>
239 SampleFormatConverter<TOut>::process (float * data, nframes_t frames)
241 /* Make sure we have enough memory allocated */
243 size_t data_size = channels * frames * sizeof (TOut);
244 if (data_size > data_out_size) {
246 data_out = new TOut[data_size];
247 data_out_size = data_size;
252 if (data_width < 32) {
253 for (uint32_t chn = 0; chn < channels; ++chn) {
254 gdither_runf (dither, chn, frames, data, data_out);
257 for (uint32_t chn = 0; chn < channels; ++chn) {
259 TOut * ob = data_out;
260 const double int_max = (float) INT_MAX;
261 const double int_min = (float) INT_MIN;
264 for (nframes_t x = 0; x < frames; ++x) {
265 i = chn + (x * channels);
267 if (data[i] > 1.0f) {
268 ob[i] = static_cast<TOut> (INT_MAX);
269 } else if (data[i] < -1.0f) {
270 ob[i] = static_cast<TOut> (INT_MIN);
272 if (data[i] >= 0.0f) {
273 ob[i] = lrintf (int_max * data[i]);
275 ob[i] = - lrintf (int_min * data[i]);
284 return GraphSinkVertex<float, TOut>::piped_to->write (data_out, frames);
289 SampleFormatConverter<float>::process (float * data, nframes_t frames)
292 for (nframes_t x = 0; x < frames * channels; ++x) {
293 if (data[x] > 1.0f) {
295 } else if (data[x] < -1.0f) {
301 return piped_to->write (data, frames);
304 template class SampleFormatConverter<short>;
305 template class SampleFormatConverter<int>;
306 template class SampleFormatConverter<float>;
310 Normalizer::Normalizer (uint32_t channels, float target_dB) :
314 target = dB_to_coefficient (target_dB);
316 if (target == 1.0f) {
317 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
318 that we may have clipped.
320 target -= FLT_EPSILON;
324 Normalizer::~Normalizer ()
330 Normalizer::set_peak (float peak)
332 if (peak == 0.0f || peak == target) {
337 gain = target / peak;
342 Normalizer::process (float * data, nframes_t frames)
345 for (nframes_t i = 0; i < (channels * frames); ++i) {
349 return piped_to->write (data, frames);