Skip to content

πŸŽ› πŸ”Š A Python library for audio.

Prevent garbage being returned when reading certain formats.

Peter Sobot02b855e6b17f
diff --git a/pedalboard/io/AudioFile.h b/pedalboard/io/AudioFile.h
index 8f23510..a44fb22 100644
--- a/pedalboard/io/AudioFile.h
+++ b/pedalboard/io/AudioFile.h
@@ -156,12 +156,21 @@ public:
{
py::gil_scoped_release release;
+ // If the file being read does not have enough content, it _should_ pad
+ // the rest of the array with zeroes. Unfortunately, this does not seem to
+ // be true in practice, so we pre-zero the array to be returned here:
+ std::memset((void *)outputInfo.ptr, 0,
+ numChannels * numSamples * sizeof(float));
+
float **channelPointers = (float **)alloca(numChannels * sizeof(float *));
for (int c = 0; c < numChannels; c++) {
channelPointers[c] = ((float *)outputInfo.ptr) + (numSamples * c);
}
- reader->read(channelPointers, numChannels, currentPosition, numSamples);
+ if (!reader->read(channelPointers, numChannels, currentPosition,
+ numSamples)) {
+ throw std::runtime_error("Failed to read from file.");
+ }
}
currentPosition += numSamples;
diff --git a/tests/test_io.py b/tests/test_io.py
index 5f9d03a..766351d 100644
--- a/tests/test_io.py
+++ b/tests/test_io.py
@@ -59,7 +59,7 @@ def get_tolerance_for_format_and_bit_depth(extension: str, input_format, file_dt
if np.issubdtype(input_format, np.signedinteger):
input_bit_depth = np.dtype(input_format).itemsize * 8
return 4 / (2 ** min(file_bit_depth, input_bit_depth))
- return 4 / (2**file_bit_depth)
+ return 4 / (2 ** file_bit_depth)
# These formats offset the waveform substantially, and these tests don't do any realignment.
if extension in {".m4a", ".ac3", ".adts", ".mp4", ".mp2"}: