- int64_t const pass_size = 256;
- shared_ptr<AudioBuffers> pass (new AudioBuffers (_channels, 256));
-
- while (1) {
- int const frames = swr_convert (_swr_context, (uint8_t **) pass->data(), pass_size, 0, 0);
-
- if (frames < 0) {
- throw EncodeError (_("could not run sample-rate converter"));
+ shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, 0));
+
+ while (in_frames > 0) {
+
+ /* Compute the resampled frames count and add 32 for luck */
+ int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
+
+ SRC_DATA data;
+ float* in_buffer = new float[in_frames * _channels];
+
+ {
+ float** p = in->data ();
+ float* q = in_buffer;
+ for (int i = 0; i < in_frames; ++i) {
+ for (int j = 0; j < _channels; ++j) {
+ *q++ = p[j][in_offset + i];
+ }
+ }
+ }
+
+ data.data_in = in_buffer;
+ data.input_frames = in_frames;
+
+ data.data_out = new float[max_resampled_frames * _channels];
+ data.output_frames = max_resampled_frames;
+
+ data.end_of_input = 0;
+ data.src_ratio = double (_out_rate) / _in_rate;
+
+ int const r = src_process (_src, &data);
+ if (r) {
+ delete[] data.data_in;
+ delete[] data.data_out;
+ throw EncodeError (
+ String::compose (
+ N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
+ src_strerror (r),
+ in_frames,
+ max_resampled_frames,
+ _channels
+ )
+ );