Skip to content

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

Fix remaining GSMFullRateCompressor issues.

Peter Sobot57cf05a39fbc
diff --git a/pedalboard/plugin_templates/FixedBlockSizePlugin.h b/pedalboard/plugin_templates/FixedBlockSizePlugin.h
index 14b4d5c..b696719 100644
--- a/pedalboard/plugin_templates/FixedBlockSizePlugin.h
+++ b/pedalboard/plugin_templates/FixedBlockSizePlugin.h
@@ -84,8 +84,10 @@ public:
int samplesOutputThisBlock = plugin.process(subContext);
if (samplesOutput > 0 && samplesOutputThisBlock < blockSize) {
- throw std::runtime_error("Plugin that using FixedBlockSize "
- "returned too few samples! This is an internal Pedalboard error and should be reported.");
+ throw std::runtime_error(
+ "Plugin that using FixedBlockSize "
+ "returned too few samples! This is an internal Pedalboard error "
+ "and should be reported.");
}
samplesOutput += samplesOutputThisBlock;
}
@@ -109,7 +111,7 @@ public:
// Copy the output back into ioBlock, right-aligned:
ioBlock
.getSubBlock(ioBlock.getNumSamples() - remainderInSamples,
- remainderInSamples)
+ remainderInSamples)
.copyFrom(subBlock);
samplesOutput += remainderInSamples;
@@ -120,21 +122,20 @@ public:
} else {
// We have to render three parts:
// 1) Push as many samples as possible into inputBuffer
- if (inputBuffer.getNumSamples() - inputBufferSamples < ioBlock.getNumSamples()) {
- throw std::runtime_error("Input buffer overflow! This is an internal Pedalboard error and should be reported.");
+ if (inputBuffer.getNumSamples() - inputBufferSamples <
+ ioBlock.getNumSamples()) {
+ throw std::runtime_error("Input buffer overflow! This is an internal "
+ "Pedalboard error and should be reported.");
}
- ioBlock.copyTo(inputBuffer, 0, inputBufferSamples, ioBlock.getNumSamples());
+ ioBlock.copyTo(inputBuffer, 0, inputBufferSamples,
+ ioBlock.getNumSamples());
inputBufferSamples += ioBlock.getNumSamples();
// 2) Copy the output from the previous render call into the ioBlock
int samplesOutput = 0;
- int minimumSamplesToOutput = ioBlock.getNumSamples();
- if (ioBlock.getNumSamples() == lastSpec.maximumBlockSize) {
- minimumSamplesToOutput += inStreamLatency;
- }
- if (outputBufferSamples >= minimumSamplesToOutput) {
+ if (outputBufferSamples >= ioBlock.getNumSamples()) {
ioBlock.copyFrom(outputBuffer, 0, 0, ioBlock.getNumSamples());
outputBufferSamples -= ioBlock.getNumSamples();
@@ -168,25 +169,31 @@ public:
if (samplesProcessedThisBlock > 0) {
// Move the output to the left side of the buffer:
- inputBlock.move(i + blockSize - samplesProcessedThisBlock, samplesProcessed, samplesProcessedThisBlock);
+ inputBlock.move(i + blockSize - samplesProcessedThisBlock,
+ samplesProcessed, samplesProcessedThisBlock);
}
-
+
samplesProcessed += samplesProcessedThisBlock;
}
// Copy the newly-processed data into the output buffer:
- if (outputBuffer.getNumSamples() < outputBufferSamples + samplesProcessed) {
- throw std::runtime_error("Output buffer overflow! This is an internal Pedalboard error and should be reported.");
+ if (outputBuffer.getNumSamples() <
+ outputBufferSamples + samplesProcessed) {
+ throw std::runtime_error("Output buffer overflow! This is an internal "
+ "Pedalboard error and should be reported.");
}
inputBlock.copyTo(outputBuffer, 0, outputBufferSamples, samplesProcessed);
outputBufferSamples += samplesProcessed;
// ... and move the remaining input data to the left of the input buffer:
- inputBlock.move(inputSamplesConsumed, 0, inputBufferSamples - inputSamplesConsumed);
+ inputBlock.move(inputSamplesConsumed, 0,
+ inputBufferSamples - inputSamplesConsumed);
inputBufferSamples -= inputSamplesConsumed;
- // ... and try to output the remaining output buffer contents if we now have enough:
- if (samplesOutput == 0 && outputBufferSamples >= minimumSamplesToOutput) {
+ // ... and try to output the remaining output buffer contents if we now
+ // have enough:
+ if (samplesOutput == 0 &&
+ outputBufferSamples >= ioBlock.getNumSamples()) {
ioBlock.copyFrom(outputBuffer, 0, 0, ioBlock.getNumSamples());
outputBufferSamples -= ioBlock.getNumSamples();
diff --git a/pedalboard/plugins/GSMCompressor.h b/pedalboard/plugins/GSMFullRateCompressor.h
similarity index 76%
rename from pedalboard/plugins/GSMCompressor.h
rename to pedalboard/plugins/GSMFullRateCompressor.h
index 2f403c9..9773ed4 100644
--- a/pedalboard/plugins/GSMCompressor.h
+++ b/pedalboard/plugins/GSMFullRateCompressor.h
@@ -53,9 +53,9 @@ private:
gsm _gsm = nullptr;
};
-class GSMCompressorInternal : public Plugin {
+class GSMFullRateCompressorInternal : public Plugin {
public:
- virtual ~GSMCompressorInternal(){};
+ virtual ~GSMFullRateCompressorInternal(){};
virtual void prepare(const juce::dsp::ProcessSpec &spec) override {
bool specChanged = lastSpec.sampleRate != spec.sampleRate ||
@@ -130,39 +130,48 @@ private:
GSMWrapper decoder;
};
-using GSMCompressor = ForceMono<
- Resample<PrimeWithSilence<
- FixedBlockSize<GSMCompressorInternal,
- GSMCompressorInternal::GSM_FRAME_SIZE_SAMPLES>,
- float, 1600>,
- float, GSMCompressorInternal::GSM_SAMPLE_RATE>>;
-
-inline void init_gsm_compressor(py::module &m) {
- py::class_<GSMCompressor, Plugin>(
- m, "GSMCompressor",
- "Apply an GSM compressor to emulate the sound of a GSM (\"2G\") cellular "
+/**
+ * Use the GSMFullRateCompressorInternal plugin, but:
+ * - ensure that it only ever sees fixed-size blocks of 160 samples
+ * - prime the input with a single block of silence
+ * - resample whatever input sample rate is provided down to 8kHz
+ * - only provide mono input to the plugin, and copy the mono signal
+ * back to stereo if necessary
+ */
+using GSMFullRateCompressor = ForceMono<Resample<
+ PrimeWithSilence<
+ FixedBlockSize<GSMFullRateCompressorInternal,
+ GSMFullRateCompressorInternal::GSM_FRAME_SIZE_SAMPLES>,
+ float, GSMFullRateCompressorInternal::GSM_FRAME_SIZE_SAMPLES>,
+ float, GSMFullRateCompressorInternal::GSM_SAMPLE_RATE>>;
+
+inline void init_gsm_full_rate_compressor(py::module &m) {
+ py::class_<GSMFullRateCompressor, Plugin>(
+ m, "GSMFullRateCompressor",
+ "Apply an GSM Full Rate compressor to emulate the sound of a GSM Full "
+ "Rate (\"2G\") cellular "
"phone connection. This plugin internally resamples the input audio to "
"8kHz.")
.def(py::init([](ResamplingQuality quality) {
- auto plugin = std::make_unique<GSMCompressor>();
+ auto plugin = std::make_unique<GSMFullRateCompressor>();
plugin->getNestedPlugin().setQuality(quality);
return plugin;
}),
py::arg("quality") = ResamplingQuality::WindowedSinc)
.def("__repr__",
- [](const GSMCompressor &plugin) {
+ [](const GSMFullRateCompressor &plugin) {
std::ostringstream ss;
- ss << "<pedalboard.GSMCompressor";
+ ss << "<pedalboard.GSMFullRateCompressor";
ss << " at " << &plugin;
ss << ">";
return ss.str();
})
.def_property(
"quality",
- [](GSMCompressor &plugin) {
+ [](GSMFullRateCompressor &plugin) {
return plugin.getNestedPlugin().getQuality();
},
- [](GSMCompressor &plugin, ResamplingQuality quality) {
+ [](GSMFullRateCompressor &plugin, ResamplingQuality quality) {
return plugin.getNestedPlugin().setQuality(quality);
});
}
diff --git a/pedalboard/python_bindings.cpp b/pedalboard/python_bindings.cpp
index b69ebe9..f3d6b25 100644
--- a/pedalboard/python_bindings.cpp
+++ b/pedalboard/python_bindings.cpp
@@ -41,7 +41,7 @@ namespace py = pybind11;
#include "plugins/Convolution.h"
#include "plugins/Delay.h"
#include "plugins/Distortion.h"
-#include "plugins/GSMCompressor.h"
+#include "plugins/GSMFullRateCompressor.h"
#include "plugins/Gain.h"
#include "plugins/HighpassFilter.h"
#include "plugins/Invert.h"
@@ -150,9 +150,9 @@ PYBIND11_MODULE(pedalboard_native, m) {
init_distortion(m);
init_gain(m);
- // Init Resample before GSMCompressor, which uses Resample::Quality:
+ // Init Resample before GSMFullRateCompressor, which uses Resample::Quality:
init_resample(m);
- init_gsm_compressor(m);
+ init_gsm_full_rate_compressor(m);
init_highpass(m);
init_invert(m);
@@ -172,6 +172,5 @@ PYBIND11_MODULE(pedalboard_native, m) {
init_add_latency(internal);
init_prime_with_silence_test_plugin(internal);
init_resample_with_latency(internal);
- init_prime_with_silence_test_plugin(internal);
init_fixed_size_block_test_plugin(internal);
};
diff --git a/tests/test_gsm_compressor.py b/tests/test_gsm_compressor.py
index 05596d0..b8cd4cc 100644
--- a/tests/test_gsm_compressor.py
+++ b/tests/test_gsm_compressor.py
@@ -17,7 +17,7 @@
import pytest
import numpy as np
-from pedalboard import GSMCompressor, Resample
+from pedalboard import GSMFullRateCompressor, Resample
from .utils import generate_sine_at
# GSM is a _very_ lossy codec:
@@ -27,7 +27,6 @@ GSM_ABSOLUTE_TOLERANCE = 0.75
SINE_WAVE_VOLUME = 0.9
-
@pytest.mark.parametrize("fundamental_hz", [440.0])
@pytest.mark.parametrize("sample_rate", [8000, 11025, 32001.2345, 44100, 48000])
@pytest.mark.parametrize("buffer_size", [1, 32, 160, 1_000_000])
@@ -54,7 +53,7 @@ def test_gsm_compressor(
signal = (
generate_sine_at(sample_rate, fundamental_hz, duration, num_channels) * SINE_WAVE_VOLUME
)
- output = GSMCompressor(quality=quality)(signal, sample_rate, buffer_size=buffer_size)
+ output = GSMFullRateCompressor(quality=quality)(signal, sample_rate, buffer_size=buffer_size)
np.testing.assert_allclose(signal, output, atol=GSM_ABSOLUTE_TOLERANCE)
@@ -80,7 +79,7 @@ def test_gsm_compressor_invariant_to_buffer_size(
signal = generate_sine_at(sample_rate, fundamental_hz, duration, num_channels)
compressed = [
- GSMCompressor(quality=quality)(signal, sample_rate, buffer_size=buffer_size)
+ GSMFullRateCompressor(quality=quality)(signal, sample_rate, buffer_size=buffer_size)
for buffer_size in (1, 32, 7000, 8192)
]
for a, b in zip(compressed, compressed[1:]):