Skip to content

Commit 62d5c16

Browse files
Use a macro definition and throw/raise a runtime error/exception.
1 parent 272a4a6 commit 62d5c16

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/samplerate.cpp

Lines changed: 9 additions & 2 deletions
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,14 +161,15 @@ class Resampler {
158161
if (channels != _channels || channels == 0)
159162
throw std::domain_error("Invalid number of channels in input data.");
160163

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

170174
// allocate output array
171175
std::vector<size_t> out_shape{new_size};
@@ -191,6 +195,9 @@ class Resampler {
191195
if ((size_t)src_data.output_frames_gen < new_size) {
192196
out_shape[0] = src_data.output_frames_gen;
193197
output.resize(out_shape);
198+
} else if ((size_t)src_data.output_frames_gen >= new_size) {
199+
// This means our fudge factor is too small.
200+
throw std::runtime_error("Generated more output samples than expected!");
194201
}
195202

196203
return output;

0 commit comments

Comments
 (0)