Skip to content

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

Produce more helpful error messages when reading from file-like objects that have been seeked.

Peter Sobot5cf7cd883fec
diff --git a/pedalboard/io/ReadableAudioFile.h b/pedalboard/io/ReadableAudioFile.h
index 2ecd148..13524bc 100644
--- a/pedalboard/io/ReadableAudioFile.h
+++ b/pedalboard/io/ReadableAudioFile.h
@@ -54,9 +54,9 @@ public:
}
if (!reader)
- throw std::domain_error(
- "Failed to open audio file: file \"" + filename +
- "\" does not seem to be of a known or supported format.");
+ throw std::domain_error("Failed to open audio file: file \"" + filename +
+ "\" does not seem to contain audio data in a "
+ "known or supported format.");
}
ReadableAudioFile(std::unique_ptr<PythonInputStream> inputStream) {
@@ -65,12 +65,14 @@ public:
if (!inputStream->isSeekable()) {
PythonException::raise();
throw std::domain_error("Failed to open audio file-like object: input "
- "stream must be seekable.");
+ "stream " +
+ inputStream->getRepresentation() +
+ " must be seekable.");
}
- if (!reader) {
- auto originalStreamPosition = inputStream->getPosition();
+ auto originalStreamPosition = inputStream->getPosition();
+ if (!reader) {
for (int i = 0; i < formatManager.getNumKnownFormats(); i++) {
auto *af = formatManager.getKnownFormat(i);
@@ -87,7 +89,8 @@ public:
inputStream->setPosition(originalStreamPosition);
if (inputStream->getPosition() != originalStreamPosition) {
throw std::runtime_error(
- "Input file-like object did not seek to the expected position. "
+ "Input file-like object " + inputStream->getRepresentation() +
+ " did not seek to the expected position. "
"The provided file-like object must be fully seekable to allow "
"reading audio files.");
}
@@ -96,11 +99,35 @@ public:
PythonException::raise();
- if (!reader)
- throw std::domain_error(
- "Failed to open audio file-like object: " +
- inputStream->getRepresentation() +
- " does not seem to contain a known or supported format.");
+ if (!reader) {
+ std::ostringstream ss;
+ ss.imbue(std::locale(""));
+
+ ss << "Failed to open audio file-like object: ";
+ ss << inputStream->getRepresentation();
+
+ if (originalStreamPosition != 0) {
+ if (originalStreamPosition < inputStream->getTotalLength()) {
+ ss << " has its stream position set to " << originalStreamPosition;
+ ss << "bytes. Reading from this position did not produce "
+ "audio data in a known or supported format.";
+ } else {
+ ss << " has its stream position set to the end of the stream ("
+ << originalStreamPosition;
+ ss << "bytes).";
+ }
+ ss << " Try seeking this file-like object back to its start before "
+ "passing it to AudioFile";
+ } else if (inputStream->getTotalLength() == 0) {
+ ss << " is empty";
+ } else {
+ ss << " does not seem to contain audio data in a known or supported "
+ "format";
+ }
+ ss << ".";
+
+ throw std::domain_error(ss.str());
+ }
PythonException::raise();
}
diff --git a/tests/test_io.py b/tests/test_io.py
index db5d4d6..d525f80 100644
--- a/tests/test_io.py
+++ b/tests/test_io.py
@@ -316,6 +316,29 @@ def test_read_mp3_from_unnamed_stream(mp3_filename: str):
assert af is not None
[email protected]("extension", pedalboard.io.get_supported_write_formats())
+def test_read_from_end_of_stream_produces_helpful_error_message(extension: str):
+ buf = io.BytesIO()
+ buf.name = f"something.{extension}"
+ with pedalboard.io.AudioFile(buf, "w", 44100, 1) as af:
+ af.write(np.random.rand(44100))
+ buf.seek(len(buf.getvalue()))
+
+ try:
+ with pedalboard.io.AudioFile(buf) as af:
+ assert af.frames >= 44100
+ except Exception as e:
+ assert "end of the stream" in str(e)
+ assert "Try seeking" in str(e)
+
+
+def test_read_from_empty_stream_produces_helpful_error_message():
+ with pytest.raises(ValueError) as exc_info:
+ with pedalboard.io.AudioFile(io.BytesIO()):
+ pass
+ assert "is empty" in str(exc_info.value)
+
+
def test_file_like_exceptions_propagate():
audio_filename = FILENAMES_AND_SAMPLERATES[0][0]
stream = open(audio_filename, "rb")