Skip to content

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

Fix tests and add integer format support.

Peter Sobot1e809d9820fa
diff --git a/pedalboard/io/AudioFile.h b/pedalboard/io/AudioFile.h
index a660c1d..414a08f 100644
--- a/pedalboard/io/AudioFile.h
+++ b/pedalboard/io/AudioFile.h
@@ -30,6 +30,8 @@ namespace py = pybind11;
namespace Pedalboard {
+static constexpr const unsigned int DEFAULT_AUDIO_BUFFER_SIZE_FRAMES = 8192;
+
class AudioFile {};
class ReadableAudioFile
@@ -75,6 +77,43 @@ public:
return reader->numChannels;
}
+ std::string getFileFormat() const {
+ if (!reader)
+ throw std::runtime_error("I/O operation on a closed file.");
+
+ return reader->getFormatName().toStdString();
+ }
+
+ std::string getFileDatatype() const {
+ if (!reader)
+ throw std::runtime_error("I/O operation on a closed file.");
+
+ if (reader->usesFloatingPointData) {
+ switch (reader->bitsPerSample) {
+ case 16: // OGG returns 16-bit int data, but internally stores floats
+ case 32:
+ return "float32";
+ case 64:
+ return "float64";
+ default:
+ return "unknown";
+ }
+ } else {
+ switch (reader->bitsPerSample) {
+ case 8:
+ return "int8";
+ case 16:
+ return "int16";
+ case 32:
+ return "int32";
+ case 64:
+ return "int64";
+ default:
+ return "unknown";
+ }
+ }
+ }
+
py::array_t<float> read(long long numSamples) {
if (numSamples == 0)
throw std::domain_error(
@@ -260,14 +299,13 @@ public:
}
}
- // TODO: Support int16, int32, etc
- void write(py::array_t<float, py::array::c_style> inputArray) {
+ template <typename SampleType>
+ void write(py::array_t<SampleType, py::array::c_style> inputArray) {
const juce::ScopedLock scopedLock(objectLock);
if (!writer)
throw std::runtime_error("I/O operation on a closed file.");
- // Numpy/Librosa convention is (num_samples, num_channels)
py::buffer_info inputInfo = inputArray.request();
unsigned int numChannels = 0;
@@ -324,16 +362,19 @@ public:
// channel is still the same on every iteration of the loop.
switch (inputChannelLayout) {
case ChannelLayout::Interleaved: {
+ std::vector<std::vector<SampleType>> deinterleaveBuffers;
+
// Use a temporary buffer to chunk the audio input
// and pass it into the writer, chunk by chunk, rather
// than de-interleaving the entire buffer at once:
deinterleaveBuffers.resize(numChannels);
- float **channelPointers = (float **)alloca(numChannels * sizeof(float *));
+ const SampleType **channelPointers =
+ (const SampleType **)alloca(numChannels * sizeof(SampleType *));
for (int startSample = 0; startSample < numSamples;
- startSample += DEINTERLEAVE_BUFFER_SIZE_FRAMES) {
- int samplesToWrite =
- std::min(numSamples - startSample, DEINTERLEAVE_BUFFER_SIZE_FRAMES);
+ startSample += DEFAULT_AUDIO_BUFFER_SIZE_FRAMES) {
+ int samplesToWrite = std::min(numSamples - startSample,
+ DEFAULT_AUDIO_BUFFER_SIZE_FRAMES);
for (int c = 0; c < numChannels; c++) {
deinterleaveBuffers[c].resize(samplesToWrite);
@@ -341,13 +382,13 @@ public:
// We're de-interleaving the data here, so we can't use copyFrom.
for (unsigned int i = 0; i < samplesToWrite; i++) {
- deinterleaveBuffers[c][i] = ((
- float *)(inputInfo.ptr))[((i + startSample) * numChannels) + c];
+ deinterleaveBuffers[c][i] =
+ ((SampleType
+ *)(inputInfo.ptr))[((i + startSample) * numChannels) + c];
}
}
- if (!writer->writeFromFloatArrays(channelPointers, numChannels,
- samplesToWrite)) {
+ if (!write(channelPointers, numChannels, samplesToWrite)) {
throw std::runtime_error("Unable to write data to audio file.");
}
}
@@ -356,12 +397,12 @@ public:
}
case ChannelLayout::NotInterleaved: {
// We can just pass all the data to writeFromFloatArrays:
- float **channelPointers = (float **)alloca(numChannels * sizeof(float *));
+ const SampleType **channelPointers =
+ (const SampleType **)alloca(numChannels * sizeof(SampleType *));
for (int c = 0; c < numChannels; c++) {
- channelPointers[c] = ((float *)inputInfo.ptr) + (numSamples * c);
+ channelPointers[c] = ((SampleType *)inputInfo.ptr) + (numSamples * c);
}
- if (!writer->writeFromFloatArrays(channelPointers, numChannels,
- numSamples)) {
+ if (!write(channelPointers, numChannels, numSamples)) {
throw std::runtime_error("Unable to write data to audio file.");
}
break;
@@ -374,6 +415,96 @@ public:
framesWritten += numSamples;
}
+ template <typename TargetType, typename InputType,
+ unsigned int bufferSize = DEFAULT_AUDIO_BUFFER_SIZE_FRAMES>
+ bool writeConvertingTo(const InputType **channels, int numChannels,
+ unsigned int numSamples) {
+ std::vector<std::vector<TargetType>> targetTypeBuffers;
+ targetTypeBuffers.resize(numChannels);
+
+ const TargetType **channelPointers =
+ (const TargetType **)alloca(numChannels * sizeof(TargetType *));
+ for (unsigned int startSample = 0; startSample < numSamples;
+ startSample += bufferSize) {
+ int samplesToWrite = std::min(numSamples - startSample, bufferSize);
+
+ for (int c = 0; c < numChannels; c++) {
+ targetTypeBuffers[c].resize(samplesToWrite);
+ channelPointers[c] = targetTypeBuffers[c].data();
+
+ for (unsigned int i = 0; i < samplesToWrite; i++) {
+ if constexpr (std::is_integral<InputType>::value) {
+ if constexpr (std::is_integral<TargetType>::value) {
+ targetTypeBuffers[c][i] =
+ ((int)channels[c][startSample + i])
+ << (std::numeric_limits<int>::digits -
+ std::numeric_limits<InputType>::digits);
+ } else if constexpr (std::is_same<TargetType, float>::value) {
+ juce::FloatVectorOperations::convertFixedToFloat(
+ targetTypeBuffers[c].data(), channels[c] + startSample, 1.0,
+ samplesToWrite);
+ } else {
+ // We should never get here - this would only be true
+ // if converting to double, which no formats require:
+ static_assert(std::is_integral<InputType>::value &&
+ std::is_same<TargetType, double>::value,
+ "Can't convert to double");
+ }
+
+ } else {
+ if constexpr (std::is_integral<TargetType>::value) {
+ // We should never get here - this would only be true
+ // if converting float to int, which JUCE handles for us:
+ static_assert(!std::is_integral<InputType>::value &&
+ std::is_integral<TargetType>::value,
+ "Can't convert int to float");
+ } else {
+ // Converting float to float:
+ targetTypeBuffers[c][i] = channels[c][startSample + i];
+ }
+ }
+ }
+ }
+
+ if (!write(channelPointers, numChannels, samplesToWrite)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ template <typename SampleType>
+ bool write(const SampleType **channels, int numChannels,
+ unsigned int numSamples) {
+ if constexpr (std::is_integral<SampleType>::value) {
+ if constexpr (std::is_same<SampleType, int>::value) {
+ if (writer->isFloatingPoint()) {
+ return writeConvertingTo<float>(channels, numChannels, numSamples);
+ } else {
+ return writer->write(channels, numSamples);
+ }
+ } else {
+ return writeConvertingTo<int>(channels, numChannels, numSamples);
+ }
+ } else {
+ if (writer->isFloatingPoint()) {
+ // Just pass the floating point data into the writer as if it were
+ // integer data. If the writer requires floating-point input data, this
+ // works (and is documented!)
+ return writer->write((const int **)channels, numSamples);
+ } else {
+ if constexpr (std::is_same<SampleType, float>::value) {
+ // Convert floating-point to fixed point, but let JUCE do that for us:
+ return writer->writeFromFloatArrays(channels, numChannels,
+ numSamples);
+ } else {
+ return writeConvertingTo<float>(channels, numChannels, numSamples);
+ }
+ }
+ }
+ }
+
void flush() {
if (!writer)
throw std::runtime_error("I/O operation on a closed file.");
@@ -425,11 +556,8 @@ private:
juce::AudioFormatManager formatManager;
std::string filename;
std::unique_ptr<juce::AudioFormatWriter> writer;
- std::vector<std::vector<float>> deinterleaveBuffers;
juce::CriticalSection objectLock;
int framesWritten = 0;
-
- static constexpr const unsigned int DEINTERLEAVE_BUFFER_SIZE_FRAMES = 1024;
};
inline void init_audiofile(py::module &m) {
@@ -498,7 +626,7 @@ inline void init_audiofile(py::module &m) {
"Read the given number of frames (samples in each channel) from the "
"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>).")
+ "shape (2, <length>). Returned data is always in float32 format.")
.def("seekable", &ReadableAudioFile::isSeekable,
"Returns True if the file is currently open and calls to seek() "
"will work.")
@@ -523,6 +651,7 @@ inline void init_audiofile(py::module &m) {
ss << " samplerate=" << file.getSampleRate();
ss << " channels=" << file.getNumChannels();
ss << " frames=" << file.getLengthInSamples();
+ ss << " file_dtype=" << file.getFileDatatype();
}
ss << " at " << &file;
ss << ">";
@@ -541,7 +670,11 @@ inline void init_audiofile(py::module &m) {
"Fetch the number of channels in the file.")
.def_property_readonly("frames", &ReadableAudioFile::getLengthInSamples,
"Fetch the total number of frames (samples per "
- "channel) in the file.");
+ "channel) in the file.")
+ .def_property_readonly(
+ "file_dtype", &ReadableAudioFile::getFileDatatype,
+ "Fetch the data type stored natively by the file. Note that read() "
+ "will always return a float32 array, regardless of this method.");
py::class_<WriteableAudioFile, AudioFile,
std::shared_ptr<WriteableAudioFile>>(
@@ -569,7 +702,36 @@ inline void init_audiofile(py::module &m) {
},
py::arg("cls"), py::arg("filename"), py::arg("samplerate") = -1,
py::arg("num_channels") = 1)
- .def("write", &WriteableAudioFile::write, py::arg("samples"))
+ .def(
+ "write",
+ [](WriteableAudioFile &file, py::array_t<char> samples) {
+ file.write<char>(samples);
+ },
+ py::arg("samples").noconvert())
+ .def(
+ "write",
+ [](WriteableAudioFile &file, py::array_t<short> samples) {
+ file.write<short>(samples);
+ },
+ py::arg("samples").noconvert())
+ .def(
+ "write",
+ [](WriteableAudioFile &file, py::array_t<int> samples) {
+ file.write<int>(samples);
+ },
+ py::arg("samples").noconvert())
+ .def(
+ "write",
+ [](WriteableAudioFile &file, py::array_t<float> samples) {
+ file.write<float>(samples);
+ },
+ py::arg("samples").noconvert())
+ .def(
+ "write",
+ [](WriteableAudioFile &file, py::array_t<double> samples) {
+ file.write<double>(samples);
+ },
+ py::arg("samples").noconvert())
.def("flush", &WriteableAudioFile::flush)
.def("close", &WriteableAudioFile::close,
"Close the file, rendering this object unusable.")
diff --git a/pedalboard/io/__init__.py b/pedalboard/io/__init__.py
index 75b157f..a40dee2 100644
--- a/pedalboard/io/__init__.py
+++ b/pedalboard/io/__init__.py
@@ -1 +1 @@
-from pedalboard_native.io import *
\ No newline at end of file
+from pedalboard_native.io import * # noqa: F403, F401
diff --git a/tests/test_io.py b/tests/test_io.py
index 0d1b158..47857cb 100644
--- a/tests/test_io.py
+++ b/tests/test_io.py
@@ -27,7 +27,7 @@ import numpy as np
from .utils import generate_sine_at
EXPECTED_DURATION_SECONDS = 5
-EXPECT_LENGTH_TO_BE_EXACT = {"mp3", "wav", "aiff", "aifc", "caf", "ogg", "m4a", "mp4"}
+EXPECT_LENGTH_TO_BE_EXACT = {"wav", "aiff", "aifc", "caf", "ogg", "m4a", "mp4"}
TEST_AUDIO_FILES = {
44100: glob.glob(os.path.join(os.path.dirname(__file__), "audio", "correct", "*44100*")),
@@ -63,7 +63,8 @@ def test_read_constructor_dispatch():
# Support reading a file by using the appropriate subclass constructor just its filename:
assert isinstance(pedalboard.io.ReadableAudioFile(filename), pedalboard.io.ReadableAudioFile)
- # Don't support reading a file by passing a mode to the subclass constructor (which would be redundant):
+ # Don't support reading a file by passing a mode to the
+ # subclass constructor (which would be redundant):
with pytest.raises(TypeError) as e:
pedalboard.io.ReadableAudioFile(filename, "r")
assert "incompatible function arguments" in str(e)
@@ -86,7 +87,8 @@ def test_write_constructor_dispatch(tmp_path: pathlib.Path):
pedalboard.io.WriteableAudioFile(filename, 44100, 1), pedalboard.io.WriteableAudioFile
)
- # Don't support writing to a file by passing a mode to the subclass constructor (which would be redundant):
+ # Don't support writing to a file by passing a mode
+ # to the subclass constructor (which would be redundant):
with pytest.raises(TypeError) as e:
pedalboard.io.WriteableAudioFile(filename, "w", 44100, 1)
assert "incompatible function arguments" in str(e)
@@ -149,6 +151,7 @@ def test_basic_read(audio_filename: str, samplerate: float):
assert f"samplerate={int(af.samplerate)}" in repr(af)
assert f"channels={af.channels}" in repr(af)
assert f"frames={af.frames}" in repr(af)
+ assert f"file_dtype={af.file_dtype}" in repr(af)
af.close()
@@ -227,14 +230,15 @@ def test_fails_gracefully():
pedalboard.io.AudioFile(__file__)
with pytest.raises(ValueError):
- with pedalboard.io.AudioFile(__file__) as f:
+ with pedalboard.io.AudioFile(__file__):
pass
@pytest.mark.parametrize("audio_filename", UNSUPPORTED_FILENAMES)
def test_fails_on_unsupported_format(audio_filename: str):
with pytest.raises(ValueError):
- pedalboard.io.AudioFile(audio_filename)
+ af = pedalboard.io.AudioFile(audio_filename)
+ assert not af
@pytest.mark.parametrize("extension", pedalboard.io.get_supported_write_formats())
@@ -244,32 +248,52 @@ def test_fails_on_unsupported_format(audio_filename: str):
)
@pytest.mark.parametrize("num_channels", [1, 2, 8])
@pytest.mark.parametrize("transposed", [False, True])
[email protected]("input_format", [np.float32, np.float64, np.int8, np.int16, np.int32])
def test_basic_write(
tmp_path: pathlib.Path,
extension: str,
samplerate: float,
num_channels: int,
transposed: bool,
+ input_format,
):
filename = str(tmp_path / f"test{extension}")
- audio = generate_sine_at(samplerate, num_channels=num_channels).astype(np.float32)
+ original_audio = generate_sine_at(samplerate, num_channels=num_channels)
+
+ # Handle integer audio types by scaling the floating-point data to the full integer range:
+ if np.issubdtype(input_format, np.signedinteger):
+ _max = min(np.abs(np.iinfo(input_format).min), np.abs(np.iinfo(input_format).max))
+ audio = (original_audio * _max).astype(input_format)
+ else:
+ audio = original_audio.astype(input_format)
+
num_samples = audio.shape[-1]
- if transposed:
- audio = audio.T
+
with pedalboard.io.WriteableAudioFile(
filename, samplerate=samplerate, num_channels=num_channels
) as af:
- af.write(audio)
+ if transposed:
+ af.write(audio.T)
+ else:
+ af.write(audio)
+
assert os.path.exists(filename)
assert os.path.getsize(filename) > 0
with pedalboard.io.ReadableAudioFile(filename) as af:
assert af.samplerate == samplerate
assert af.channels == num_channels
- tolerance = 0.11 if extension == ".ogg" else 2 / (2**16)
+
+ if extension == ".ogg":
+ tolerance = 0.12
+ else:
+ file_bit_depth = int(af.file_dtype.replace("float", "").replace("int", ""))
+ tolerance = 4 / (2**file_bit_depth)
+ if np.issubdtype(input_format, np.signedinteger):
+ input_bit_depth = np.dtype(input_format).itemsize * 8
+ tolerance = 4 / (2 ** min(file_bit_depth, input_bit_depth))
+
as_written = af.read(num_samples)
- if transposed:
- as_written = as_written.T
- np.testing.assert_allclose(audio, np.squeeze(as_written), atol=tolerance)
+ np.testing.assert_allclose(original_audio, np.squeeze(as_written), atol=tolerance)
@pytest.mark.parametrize("extension", pedalboard.io.get_supported_write_formats())