Skip to content

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

Working, click-free, correct GSM compressor at all sample rates.

Peter Sobot812ed286aeed
diff --git a/pedalboard/plugins/GSMCompressor.h b/pedalboard/plugins/GSMCompressor.h
index f1d0009..fe7038f 100644
--- a/pedalboard/plugins/GSMCompressor.h
+++ b/pedalboard/plugins/GSMCompressor.h
@@ -87,9 +87,10 @@ public:
resampledBuffer.setSize(1, maximumBlockSizeInGSMSampleRate +
GSM_FRAME_SIZE_SAMPLES +
(inStreamLatency / resamplerRatio));
- outputBuffer.setSize(1, spec.maximumBlockSize +
- gsmFrameSizeInNativeSampleRate +
- inStreamLatency);
+ outputBuffer.setSize(1,
+ spec.maximumBlockSize +
+ gsmFrameSizeInNativeSampleRate +
+ inStreamLatency);
// Feed one GSM frame's worth of silence at the start so that we
// can tolerate different buffer sizes without underrunning any buffers.
@@ -134,10 +135,6 @@ public:
}
}
- if (samplesProduced > 0 && samplesOutput < ioBlock.getNumSamples()) {
- throw std::runtime_error("Buffer underrun on the output!");
- }
-
samplesProduced += samplesOutput;
int samplesToReturn = std::min((long)(samplesProduced - inStreamLatency),
(long)ioBlock.getNumSamples());
@@ -276,27 +273,34 @@ public:
float *outputBufferPointer =
outputBuffer.getWritePointer(0) + samplesInOutputBuffer;
- float floatFrame[GSM_FRAME_SIZE_SAMPLES];
- juce::AudioDataConverters::convertInt16LEToFloat(frame, floatFrame,
- GSM_FRAME_SIZE_SAMPLES);
+ juce::AudioDataConverters::convertInt16LEToFloat(
+ frame, gsmOutputFrame + samplesInGsmOutputFrame,
+ GSM_FRAME_SIZE_SAMPLES);
+ samplesInGsmOutputFrame += GSM_FRAME_SIZE_SAMPLES;
- // Resample back up to the native sample rate and store in outputBuffer:
- int expectedOutputSamples = gsmFrameSizeInNativeSampleRate;
+ // Resample back up to the native sample rate and store in outputBuffer,
+ // using gsmOutputFrame as a temporary buffer to store up to 1 extra
+ // sample to compensate for rounding errors:
+ int expectedOutputSamples = samplesInGsmOutputFrame * resamplerRatio;
int samplesConsumed = gsmToNativeResampler.process(
- inverseResamplerRatio, floatFrame, outputBufferPointer,
+ inverseResamplerRatio, gsmOutputFrame, outputBufferPointer,
expectedOutputSamples);
+ std::memmove((char *)gsmOutputFrame,
+ (char *)(gsmOutputFrame + samplesConsumed),
+ (samplesInGsmOutputFrame - samplesConsumed) * sizeof(float));
+ samplesInGsmOutputFrame -= samplesConsumed;
samplesInOutputBuffer += expectedOutputSamples;
// Now that we're done with this chunk of resampledBuffer, move its
// contents to the left:
int samplesRemainingInResampledBuffer =
- samplesInResampledBuffer - samplesConsumed;
+ samplesInResampledBuffer - GSM_FRAME_SIZE_SAMPLES;
std::memmove(
(char *)resampledBuffer.getWritePointer(0),
- (char *)(resampledBuffer.getWritePointer(0) + samplesConsumed),
+ (char *)(resampledBuffer.getWritePointer(0) + GSM_FRAME_SIZE_SAMPLES),
samplesRemainingInResampledBuffer * sizeof(float));
- samplesInResampledBuffer -= samplesConsumed;
+ samplesInResampledBuffer -= GSM_FRAME_SIZE_SAMPLES;
}
}
@@ -313,6 +317,7 @@ public:
samplesInResampledBuffer = 0;
samplesInOutputBuffer = 0;
samplesInInputReservoir = 0;
+ samplesInGsmOutputFrame = 0;
samplesProduced = 0;
inStreamLatency = 0;
@@ -322,6 +327,9 @@ protected:
virtual int getLatencyHint() override { return inStreamLatency; }
private:
+ static constexpr size_t GSM_FRAME_SIZE_SAMPLES = 160;
+ static constexpr float GSM_SAMPLE_RATE = 8000;
+
double resamplerRatio = 1.0;
double inverseResamplerRatio = 1.0;
float gsmFrameSizeInNativeSampleRate;
@@ -337,21 +345,22 @@ private:
GSMWrapper decoder;
juce::Interpolators::Lagrange gsmToNativeResampler;
+ float gsmOutputFrame[GSM_FRAME_SIZE_SAMPLES + 1];
+ int samplesInGsmOutputFrame = 0;
+
juce::AudioBuffer<float> outputBuffer;
int samplesInOutputBuffer = 0;
int samplesProduced = 0;
int inStreamLatency = 0;
-
- static constexpr size_t GSM_FRAME_SIZE_SAMPLES = 160;
- static constexpr float GSM_SAMPLE_RATE = 8000;
};
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 "
- "phone connection. This plugin internally resamples the input audio to 8kHz.")
+ "phone connection. This plugin internally resamples the input audio to "
+ "8kHz.")
.def(py::init([]() { return new GSMCompressor(); }))
.def("__repr__", [](const GSMCompressor &plugin) {
std::ostringstream ss;
diff --git a/tests/test_gsm_compressor.py b/tests/test_gsm_compressor.py
new file mode 100644
index 0000000..b241fe7
--- /dev/null
+++ b/tests/test_gsm_compressor.py
@@ -0,0 +1,62 @@
+#! /usr/bin/env python
+#
+# Copyright 2021 Spotify AB
+#
+# Licensed under the GNU Public License, Version 3.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.gnu.org/licenses/gpl-3.0.html
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+import pytest
+import numpy as np
+from pedalboard import GSMCompressor
+
+# GSM is a _very_ lossy codec:
+GSM_ABSOLUTE_TOLERANCE = 0.75
+
+# Passing in a full-scale sine wave seems to often make GSM clip:
+SINE_WAVE_VOLUME = 0.9
+
+
+def generate_sine_at(
+ sample_rate: float,
+ fundamental_hz: float = 440.0,
+ num_seconds: float = 3.0,
+ num_channels: int = 1,
+) -> np.ndarray:
+ samples = np.arange(num_seconds * sample_rate)
+ sine_wave = np.sin(2 * np.pi * fundamental_hz * samples / sample_rate)
+ if num_channels == 2:
+ return np.stack([sine_wave, sine_wave])
+ return sine_wave
+
+
[email protected]("fundamental_hz", [440.0])
[email protected]("sample_rate", [8000, 11025, 22050, 32000, 32001, 44100, 48000])
[email protected]("buffer_size", [1, 32, 160, 8192])
[email protected]("duration", [1.0, 3.0, 30.0])
[email protected]("num_channels", [1, 2])
+def test_gsm_compressor(fundamental_hz: float, sample_rate: float, buffer_size: int, duration: float, num_channels: int):
+ signal = generate_sine_at(sample_rate, fundamental_hz, duration, num_channels) * SINE_WAVE_VOLUME
+ compressed = GSMCompressor()(signal, sample_rate, buffer_size=buffer_size)
+ np.testing.assert_allclose(signal, compressed, atol=GSM_ABSOLUTE_TOLERANCE)
+
+
[email protected]("sample_rate", [8000, 11025, 22050, 32000, 32001, 44100, 48000])
[email protected]("num_channels", [1])#, 2])
+def test_gsm_compressor_invariant_to_buffer_size(sample_rate: float, num_channels: int):
+ fundamental_hz = 400.0
+ duration = 3.0
+ signal = generate_sine_at(sample_rate, fundamental_hz, duration, num_channels)
+
+ compressed = [GSMCompressor()(signal, sample_rate, buffer_size=buffer_size) for buffer_size in (1, 32, 8192)]
+ for a, b in zip(compressed, compressed[1:]):
+ np.testing.assert_allclose(a, b)