#include <cmath>
#undef _ISOC99_SOURCE
#undef _ISOC9X_SOURCE
-#undef __USE_SVID
+#undef __USE_SVID
#define __USE_SVID 1
#include <cstdlib>
#undef __USE_SVID
/* ...*/
-#include <ardour/export_utilities.h>
+#include "ardour/export_utilities.h"
#include <string.h>
-#include <ardour/export_failed.h>
-#include <ardour/gdither.h>
-#include <ardour/dB.h>
-#include <pbd/failed_constructor.h>
+#include "ardour/export_failed.h"
+#include "ardour/gdither.h"
+#include "ardour/dB.h"
+#include "pbd/failed_constructor.h"
#include "i18n.h"
SampleRateConverter::SampleRateConverter (uint32_t channels, nframes_t in_rate, nframes_t out_rate, int quality) :
channels (channels),
leftover_frames (0),
+ max_leftover_frames (0),
+ frames_in (0),
+ frames_out(0),
data_in (0),
leftover_data (0),
data_out (0),
active = false;
return;
}
-
+
active = true;
int err;
if ((src_state = src_new (quality, channels, &err)) == 0) {
- throw ExportFailed (string_compose (_("cannot initialize sample rate conversion: %1"), src_strerror (err)), "Cannot initialize sample rate conversion");
+ throw ExportFailed (string_compose (X_("Cannot initialize sample rate conversion: %1"), src_strerror (err)));
}
-
+
src_data.src_ratio = out_rate / (double) in_rate;
}
if (src_state) {
src_delete (src_state);
}
- if (data_out) {
- delete [] data_out;
- }
+
+ delete [] data_out;
+
if (leftover_data) {
free (leftover_data);
}
}
/* Manage memory */
-
+
nframes_t out_samples_max = (nframes_t) ceil (frames * src_data.src_ratio * channels);
if (data_out_size < out_samples_max) {
- free (data_out);
+ delete[] data_out;
+
data_out = new float[out_samples_max];
src_data.data_out = data_out;
-
+
max_leftover_frames = 4 * frames;
leftover_data = (float *) realloc (leftover_data, max_leftover_frames * channels * sizeof (float));
if (!leftover_data) {
- throw ExportFailed (_("A memory allocation error occured during sample rate conversion"), "Samplerate conversion failed");
+ throw ExportFailed (X_("A memory allocation error occured during sample rate conversion"));
}
-
+
data_out_size = out_samples_max;
}
int err;
int cnt = 0;
nframes_t frames_out_total = 0;
-
+
do {
src_data.output_frames = out_samples_max / channels;
src_data.end_of_input = end_of_input;
src_data.data_in = leftover_data;
if (cnt == 0) {
-
+
/* first time, append new data from data_in into the leftover_data buffer */
memcpy (leftover_data + (leftover_frames * channels), data_in, frames_in * channels * sizeof(float));
src_data.input_frames = frames_in + leftover_frames;
} else {
-
+
/* otherwise, just use whatever is still left in leftover_data; the contents
were adjusted using memmove() right after the last SRC call (see
below)
src_data.input_frames = leftover_frames;
}
-
+
} else {
src_data.data_in = data_in;
++cnt;
if ((err = src_process (src_state, &src_data)) != 0) {
- throw ExportFailed (_("an error occured during sample rate conversion"), string_compose ("an error occured during sample rate conversion: %1", src_strerror (err)));
+ throw ExportFailed (string_compose ("An error occured during sample rate conversion: %1", src_strerror (err)));
}
-
+
frames_out = src_data.output_frames_gen;
leftover_frames = src_data.input_frames - src_data.input_frames_used;
memmove (leftover_data, (char *) (src_data.data_in + (src_data.input_frames_used * channels)),
leftover_frames * channels * sizeof(float));
}
-
-
+
+
nframes_t frames_written = piped_to->write (data_out, frames_out);
- if (frames_written < 0) {
- return frames_written;
- } else {
- frames_out_total += frames_written;
- }
+ frames_out_total += frames_written;
} while (leftover_frames > frames_in);
-
+
return frames_out_total;
}
if (data_width != 24) {
data_width = sizeof (TOut) * 8;
}
-
+
GDitherSize dither_size = GDitherFloat;
switch (data_width) {
case 24:
dither_size = GDither32bit;
}
-
+
dither = gdither_new ((GDitherType) type, channels, dither_size, data_width);
}
if (dither) {
gdither_free (dither);
}
- if (data_out) {
- delete data_out;
- }
+
+ delete[] data_out;
}
template <typename TOut>
SampleFormatConverter<TOut>::process (float * data, nframes_t frames)
{
/* Make sure we have enough memory allocated */
-
+
size_t data_size = channels * frames * sizeof (TOut);
if (data_size > data_out_size) {
- free (data_out);
+
+ delete[] data_out;
+
data_out = new TOut[data_size];
data_out_size = data_size;
}
-
+
/* Do conversion */
-
+
if (data_width < 32) {
for (uint32_t chn = 0; chn < channels; ++chn) {
gdither_runf (dither, chn, frames, data, data_out);
}
} else {
for (uint32_t chn = 0; chn < channels; ++chn) {
-
+
TOut * ob = data_out;
const double int_max = (float) INT_MAX;
const double int_min = (float) INT_MIN;
-
+
nframes_t i;
for (nframes_t x = 0; x < frames; ++x) {
i = chn + (x * channels);
-
+
if (data[i] > 1.0f) {
ob[i] = static_cast<TOut> (INT_MAX);
} else if (data[i] < -1.0f) {
}
}
}
-
+
/* Write forward */
-
+
return GraphSinkVertex<float, TOut>::piped_to->write (data_out, frames);
}
data[x] = 1.0f;
} else if (data[x] < -1.0f) {
data[x] = -1.0f;
- }
+ }
}
}
-
+
return piped_to->write (data, frames);
}
channels (channels),
enabled (false)
{
- target = dB_to_coefficient (target_dB);
-
+ target = dB_to_coefficient (target_dB);
+
if (target == 1.0f) {
/* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
that we may have clipped.
void
Normalizer::set_peak (float peak)
-{
+{
if (peak == 0.0f || peak == target) {
/* don't even try */
enabled = false;