Skip to content

Commit 7ac2a41

Browse files
authored
Merge pull request #3 from arlofaria-cartesia/fix-new-size
Increase expected size of output sample buffer, to correctly handle input termination
2 parents 4077ffe + 62d5c16 commit 7ac2a41

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/samplerate.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
#define VERSION_INFO "nightly"
4141
#endif
4242

43+
// This value was empirically and somewhat arbitrarily chosen; increase it for further safety.
44+
#define END_OF_INPUT_EXTRA_OUTPUT_FRAMES 10000
45+
4346
namespace py = pybind11;
4447
using namespace pybind11::literals;
4548

@@ -158,8 +161,15 @@ class Resampler {
158161
if (channels != _channels || channels == 0)
159162
throw std::domain_error("Invalid number of channels in input data.");
160163

164+
// Add a "fudge factor" to the size. This is because the actual number of
165+
// output samples generated on the last call when input is terminated can
166+
// be more than the expected number of output samples during mid-stream
167+
// steady-state processing. (Also, when the stream is started, the number
168+
// of output samples generated will generally be zero or otherwise less
169+
// than the number of samples in mid-stream processing.)
161170
const auto new_size =
162-
static_cast<size_t>(std::ceil(inbuf.shape[0] * sr_ratio));
171+
static_cast<size_t>(std::ceil(inbuf.shape[0] * sr_ratio))
172+
+ END_OF_INPUT_EXTRA_OUTPUT_FRAMES;
163173

164174
// allocate output array
165175
std::vector<size_t> out_shape{new_size};
@@ -188,6 +198,9 @@ class Resampler {
188198
if ((size_t)src_data.output_frames_gen < new_size) {
189199
out_shape[0] = src_data.output_frames_gen;
190200
output.resize(out_shape);
201+
} else if ((size_t)src_data.output_frames_gen >= new_size) {
202+
// This means our fudge factor is too small.
203+
throw std::runtime_error("Generated more output samples than expected!");
191204
}
192205

193206
return output;

0 commit comments

Comments
 (0)