Skip to content

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

Add more test files.

Peter Sobot5fc74315a9cf
diff --git a/pedalboard/io/AudioFile.h b/pedalboard/io/AudioFile.h
index 50382d0..e628a15 100644
--- a/pedalboard/io/AudioFile.h
+++ b/pedalboard/io/AudioFile.h
@@ -82,12 +82,12 @@ public:
{
py::gil_scoped_release release;
- float *readPointers[numChannels];
+ float **channelPointers = (float **)alloca(numChannels * sizeof(float *));
for (int c = 0; c < numChannels; c++) {
- readPointers[c] = ((float *)outputInfo.ptr) + (numSamples * c);
+ channelPointers[c] = ((float *)outputInfo.ptr) + (numSamples * c);
}
- reader->read(readPointers, numChannels, currentPosition, numSamples);
+ reader->read(channelPointers, numChannels, currentPosition, numSamples);
}
currentPosition += numSamples;
@@ -99,9 +99,9 @@ public:
if (!reader)
throw std::runtime_error("I/O operation on a closed file.");
if (targetPosition > reader->lengthInSamples)
- throw std::domain_error("Seek out of bounds");
+ throw std::domain_error("Cannot seek beyond end of file (" + std::to_string(reader->lengthInSamples) + " frames).");
if (targetPosition < 0)
- throw std::domain_error("Seek out of bounds");
+ throw std::domain_error("Cannot seek before start of file.");
currentPosition = targetPosition;
}
@@ -117,6 +117,18 @@ public:
reader.reset();
}
+ bool isClosed() {
+ const juce::ScopedLock scopedLock(objectLock);
+ return reader == nullptr;
+ }
+
+ bool isSeekable() {
+ const juce::ScopedLock scopedLock(objectLock);
+
+ // At the moment, AudioFile instances are always seekable, as they're backed by files.
+ return !isClosed();
+ }
+
std::shared_ptr<AudioFile> enter() { return shared_from_this(); }
void exit(const py::object &type, const py::object &value,
@@ -135,16 +147,22 @@ private:
inline void init_audiofile(py::module &m) {
py::class_<AudioFile, std::shared_ptr<AudioFile>>(
m, "AudioFile",
- "An audio file reader interface, with native support for "
- "for Ogg Vorbis, MP3, WAV, FLAC, and AIFF files on all operating "
- "systems. On some platforms, the output")
+ "An audio file reader interface, with native support for for Ogg Vorbis, "
+ "MP3, WAV, FLAC, and AIFF files on all operating systems. On some "
+ "platforms, other formats may also be supported. (Use "
+ "AudioFile.supported_formats to see which formats are supported on the "
+ "current platform.)")
.def(py::init([](std::string filename) {
return std::make_unique<AudioFile>(filename);
}),
py::arg("filename"))
.def("read", &AudioFile::read, py::arg("num_frames") = 0,
"Read the given number of frames (samples in each channel) from the "
- "audio file at the current position.")
+ "audio file at the current position. Audio samples are returned in "
+ "the shape (channels, samples); i.e.: a stereo audio file will have "
+ "shape (2, <length>).")
+ .def("seekable", &AudioFile::isSeekable,
+ "Returns True if the file is currently open and calls to seek() will work.")
.def("seek", &AudioFile::seek, py::arg("position"),
"Seek the file to the provided location in frames.")
.def("tell", &AudioFile::tell,
@@ -153,6 +171,8 @@ inline void init_audiofile(py::module &m) {
"Close the file, rendering this object unusable.")
.def("__enter__", &AudioFile::enter)
.def("__exit__", &AudioFile::exit)
+ .def_property_readonly("closed", &AudioFile::isClosed,
+ "If the file has been closed, this property will be True.")
.def_property_readonly("samplerate", &AudioFile::getSampleRate,
"Fetch the sample rate of the file in samples "
"(per channel) per second (Hz).")
@@ -183,12 +203,10 @@ inline void init_audiofile(py::module &m) {
}
std::sort(output.begin(), output.end(),
- [](const std::string lhs,
- const std::string rhs) {
- return lhs < rhs;
- });
+ [](const std::string lhs, const std::string rhs) {
+ return lhs < rhs;
+ });
-
return output;
});
}
diff --git a/tests/audio/correct/empty.fake b/tests/audio/correct/empty.fake
new file mode 100644
index 0000000..e69de29
diff --git a/tests/audio/correct/mono_sine_at_44100Hz.ac3 b/tests/audio/correct/mono_sine_at_44100Hz.ac3
new file mode 100644
index 0000000..efdcbca
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_44100Hz.ac3 differ
diff --git a/tests/audio/correct/mono_sine_at_44100Hz.adts b/tests/audio/correct/mono_sine_at_44100Hz.adts
new file mode 100644
index 0000000..e805ded
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_44100Hz.adts differ
diff --git a/tests/audio/correct/mono_sine_at_44100.aiff b/tests/audio/correct/mono_sine_at_44100Hz.aifc
similarity index 100%
rename from tests/audio/correct/mono_sine_at_44100.aiff
rename to tests/audio/correct/mono_sine_at_44100Hz.aifc
diff --git a/tests/audio/correct/mono_sine_at_44100Hz.aiff b/tests/audio/correct/mono_sine_at_44100Hz.aiff
new file mode 100644
index 0000000..f627182
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_44100Hz.aiff differ
diff --git a/tests/audio/correct/mono_sine_at_44100Hz.caf b/tests/audio/correct/mono_sine_at_44100Hz.caf
new file mode 100644
index 0000000..9277374
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_44100Hz.caf differ
diff --git a/tests/audio/correct/mono_sine_at_44100.m4a b/tests/audio/correct/mono_sine_at_44100Hz.m4a
similarity index 100%
rename from tests/audio/correct/mono_sine_at_44100.m4a
rename to tests/audio/correct/mono_sine_at_44100Hz.m4a
diff --git a/tests/audio/correct/mono_sine_at_44100Hz.mp2 b/tests/audio/correct/mono_sine_at_44100Hz.mp2
new file mode 100644
index 0000000..175e6d3
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_44100Hz.mp2 differ
diff --git a/tests/audio/correct/mono_sine_at_44100Hz.mp4 b/tests/audio/correct/mono_sine_at_44100Hz.mp4
new file mode 100644
index 0000000..37a434b
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_44100Hz.mp4 differ
diff --git a/tests/audio/correct/mono_sine_at_48000Hz.ac3 b/tests/audio/correct/mono_sine_at_48000Hz.ac3
new file mode 100644
index 0000000..960cf64
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_48000Hz.ac3 differ
diff --git a/tests/audio/correct/mono_sine_at_48000Hz.adts b/tests/audio/correct/mono_sine_at_48000Hz.adts
new file mode 100644
index 0000000..85e4cc7
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_48000Hz.adts differ
diff --git a/tests/audio/correct/mono_sine_at_48000.aiff b/tests/audio/correct/mono_sine_at_48000Hz.aifc
similarity index 100%
rename from tests/audio/correct/mono_sine_at_48000.aiff
rename to tests/audio/correct/mono_sine_at_48000Hz.aifc
diff --git a/tests/audio/correct/mono_sine_at_48000Hz.aiff b/tests/audio/correct/mono_sine_at_48000Hz.aiff
new file mode 100644
index 0000000..7dd0122
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_48000Hz.aiff differ
diff --git a/tests/audio/correct/mono_sine_at_48000Hz.caf b/tests/audio/correct/mono_sine_at_48000Hz.caf
new file mode 100644
index 0000000..fde5e69
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_48000Hz.caf differ
diff --git a/tests/audio/correct/mono_sine_at_48000.m4a b/tests/audio/correct/mono_sine_at_48000Hz.m4a
similarity index 100%
rename from tests/audio/correct/mono_sine_at_48000.m4a
rename to tests/audio/correct/mono_sine_at_48000Hz.m4a
diff --git a/tests/audio/correct/mono_sine_at_48000Hz.mp2 b/tests/audio/correct/mono_sine_at_48000Hz.mp2
new file mode 100644
index 0000000..e790270
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_48000Hz.mp2 differ
diff --git a/tests/audio/correct/mono_sine_at_48000Hz.mp4 b/tests/audio/correct/mono_sine_at_48000Hz.mp4
new file mode 100644
index 0000000..dc7ce48
Binary files /dev/null and b/tests/audio/correct/mono_sine_at_48000Hz.mp4 differ
diff --git a/tests/test_io_read.py b/tests/test_io_read.py
index 2d41f00..212a100 100644
--- a/tests/test_io_read.py
+++ b/tests/test_io_read.py
@@ -20,6 +20,9 @@ import glob
import pytest
import pedalboard
+EXPECTED_DURATION_SECONDS = 5
+EXPECT_LENGTH_TO_BE_EXACT = {"mp3", "wav", "aiff", "aifc", "caf", "ogg", "m4a", "mp4"}
+
TEST_AUDIO_FILES = {
44100: glob.glob(os.path.join(os.path.dirname(__file__), "audio", "correct", "*44100*")),
48000: glob.glob(os.path.join(os.path.dirname(__file__), "audio", "correct", "*48000*")),
@@ -52,28 +55,64 @@ def test_basic_load(audio_filename: str, samplerate: float):
af = pedalboard.io.AudioFile(audio_filename)
assert af.samplerate == samplerate
assert af.channels == 1
- assert af.frames == int(samplerate * 5)
+ if any(ext in audio_filename for ext in EXPECT_LENGTH_TO_BE_EXACT):
+ assert af.frames == int(samplerate * EXPECTED_DURATION_SECONDS)
+ else:
+ assert af.frames >= int(samplerate * EXPECTED_DURATION_SECONDS)
- samples = af.read(af.frames)
- assert samples.shape == (1, int(samplerate * 5))
+ samples = af.read(int(samplerate * EXPECTED_DURATION_SECONDS))
+ assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS))
# File should no longer be useful:
af.read(1).nbytes == 0
+ # Seeking back to the start of the file should work:
+ assert af.seekable()
+ af.seek(0)
+ samples = af.read(int(samplerate * EXPECTED_DURATION_SECONDS))
+ assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS))
+
+ # Seeking to an arbitrary point should also work
+ af.seek(int(samplerate * EXPECTED_DURATION_SECONDS / 2))
+ samples = af.read(int(samplerate * EXPECTED_DURATION_SECONDS / 2))
+ assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS / 2))
+
+ af.close()
+
+ with pytest.raises(RuntimeError):
+ af.channels
+
+ with pytest.raises(RuntimeError):
+ af.read(1)
+
@pytest.mark.parametrize("audio_filename,samplerate", FILENAMES_AND_SAMPLERATES)
def test_use_as_context_manager(audio_filename: str, samplerate: float):
with pedalboard.io.AudioFile(audio_filename) as af:
assert af.samplerate == samplerate
assert af.channels == 1
- assert af.frames == int(samplerate * 5)
+ if any(ext in audio_filename for ext in EXPECT_LENGTH_TO_BE_EXACT):
+ assert af.frames == int(samplerate * EXPECTED_DURATION_SECONDS)
+ else:
+ assert af.frames >= int(samplerate * EXPECTED_DURATION_SECONDS)
- samples = af.read(af.frames)
- assert samples.shape == (1, int(samplerate * 5))
+ samples = af.read(int(samplerate * EXPECTED_DURATION_SECONDS))
+ assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS))
# File should no longer be useful:
af.read(1).nbytes == 0
+ # Seeking back to the start of the file should work:
+ assert af.seekable()
+ af.seek(0)
+ samples = af.read(int(samplerate * EXPECTED_DURATION_SECONDS))
+ assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS))
+
+ # Seeking to an arbitrary point should also work
+ af.seek(int(samplerate * EXPECTED_DURATION_SECONDS / 2))
+ samples = af.read(int(samplerate * EXPECTED_DURATION_SECONDS / 2))
+ assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS / 2))
+
with pytest.raises(RuntimeError):
af.channels
@@ -81,6 +120,14 @@ def test_use_as_context_manager(audio_filename: str, samplerate: float):
af.read(1)
+def test_context_manager_allows_exceptions():
+ with pytest.raises(AssertionError):
+ with pedalboard.io.AudioFile(FILENAMES_AND_SAMPLERATES[0][0]) as af:
+ assert False
+
+ assert af.closed
+
+
def test_fails_gracefully():
with pytest.raises(ValueError):
pedalboard.io.AudioFile(__file__)