π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Working, click-free, properly aligned GSM compression except at 11.025kHz.
Peter Sobotd29179c1efcb
diff --git a/pedalboard/plugins/GSMCompressor.h b/pedalboard/plugins/GSMCompressor.hindex 1faf55c..f1d0009 100644--- a/pedalboard/plugins/GSMCompressor.h+++ b/pedalboard/plugins/GSMCompressor.h@@ -60,24 +60,16 @@ public:reset();resamplerRatio = spec.sampleRate / GSM_SAMPLE_RATE;- resampledBuffer.setSize(- spec.numChannels,- (spec.maximumBlockSize * GSM_SAMPLE_RATE / spec.sampleRate));+ inverseResamplerRatio = GSM_SAMPLE_RATE / spec.sampleRate;- if (spec.numChannels != 1) {- throw std::domain_error(- "GSM compression currently only works on mono signals.");- }+ gsmFrameSizeInNativeSampleRate = GSM_FRAME_SIZE_SAMPLES * resamplerRatio;+ int maximumBlockSizeInGSMSampleRate =+ spec.maximumBlockSize / resamplerRatio;- inputBlockSizeForGSM = (GSM_FRAME_SIZE_SAMPLES * resamplerRatio);- if (spec.maximumBlockSize % (inputBlockSizeForGSM) != 0) {- throw std::domain_error(- "GSM compression currently requires a buffer "- "size of a multiple of " +- std::to_string(inputBlockSizeForGSM) +- ", but got " + std::to_string(inputBlockSizeForGSM) + ".");- }- printf("Maximum block size is %d, setting inputBlockSizeForGSM to %d samples at %f Hz.\n", spec.maximumBlockSize, inputBlockSizeForGSM, spec.sampleRate);+ // Store the remainder of the input: any samples that weren't consumed in+ // one pushSamples() call but would be consumable in the next one.+ inputReservoir.setSize(1, (int)std::ceil(resamplerRatio) ++ spec.maximumBlockSize);if (!encoder.getContext()) {throw std::runtime_error("Failed to initialize GSM encoder.");@@ -86,6 +78,25 @@ public:throw std::runtime_error("Failed to initialize GSM decoder.");}+ inStreamLatency = 0;++ // Add the resamplers' latencies so the output is properly aligned:+ inStreamLatency += nativeToGSMResampler.getBaseLatency() * resamplerRatio;+ inStreamLatency += gsmToNativeResampler.getBaseLatency() * resamplerRatio;++ resampledBuffer.setSize(1, maximumBlockSizeInGSMSampleRate ++ GSM_FRAME_SIZE_SAMPLES ++ (inStreamLatency / resamplerRatio));+ 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.+ std::vector<float> silence(gsmFrameSizeInNativeSampleRate);+ inStreamLatency += silence.size();+ pushSamples(silence.data(), silence.size());+lastSpec = spec;}}@@ -94,28 +105,158 @@ public:const juce::dsp::ProcessContextReplacing<float> &context) override final {auto ioBlock = context.getOutputBlock();- for (int blockStart = 0; blockStart < ioBlock.getNumSamples();- blockStart += inputBlockSizeForGSM) {- int blockEnd = blockStart + inputBlockSizeForGSM;- if (blockEnd > ioBlock.getNumSamples())- blockEnd = ioBlock.getNumSamples();+ // Mix all channels to mono first, if necessary; GSM (in reality) is+ // mono-only.+ if (ioBlock.getNumChannels() > 1) {+ float channelVolume = 1.0f / ioBlock.getNumChannels();+ for (int i = 0; i < ioBlock.getNumChannels(); i++) {+ ioBlock.getSingleChannelBlock(i) *= channelVolume;+ }++ // Copy all of the latter channels into the first channel,+ // which will be used for processing:+ auto firstChannel = ioBlock.getSingleChannelBlock(0);+ for (int i = 1; i < ioBlock.getNumChannels(); i++) {+ firstChannel += ioBlock.getSingleChannelBlock(i);+ }+ }++ // Actually do the GSM processing!+ pushSamples(ioBlock.getChannelPointer(0), ioBlock.getNumSamples());+ int samplesOutput =+ pullSamples(ioBlock.getChannelPointer(0), ioBlock.getNumSamples());++ // Copy the mono signal back out to all other channels:+ if (ioBlock.getNumChannels() > 1) {+ auto firstChannel = ioBlock.getSingleChannelBlock(0);+ for (int i = 1; i < ioBlock.getNumChannels(); i++) {+ ioBlock.getSingleChannelBlock(i).copyFrom(firstChannel);+ }+ }++ if (samplesProduced > 0 && samplesOutput < ioBlock.getNumSamples()) {+ throw std::runtime_error("Buffer underrun on the output!");+ }- int blockSize = blockEnd - blockStart;+ samplesProduced += samplesOutput;+ int samplesToReturn = std::min((long)(samplesProduced - inStreamLatency),+ (long)ioBlock.getNumSamples());+ if (samplesToReturn < 0)+ samplesToReturn = 0;- // Resample the input audio down to 8kHz.- float *inputSamples = ioBlock.getChannelPointer(0) + blockStart;- float *tempSamples = resampledBuffer.getWritePointer(0);- int samplesUsed = nativeToGSMResampler.process(resamplerRatio, inputSamples, tempSamples,- GSM_FRAME_SIZE_SAMPLES);+ return samplesToReturn;+ }++ /*+ * Return the number of samples needed for this+ * plugin to return a single GSM frame's worth of audio.+ */+ int spaceAvailableInResampledBuffer() const {+ return resampledBuffer.getNumSamples() - samplesInResampledBuffer;+ }++ int spaceAvailableInOutputBuffer() const {+ return outputBuffer.getNumSamples() - samplesInOutputBuffer;+ }++ /*+ * Push a certain number of input samples into the internal buffer(s)+ * of this plugin, as GSM coding processes audio 160 samples at a time.+ */+ void pushSamples(float *inputSamples, int numInputSamples) {+ float expectedOutputSamples = numInputSamples / resamplerRatio;++ if (spaceAvailableInResampledBuffer() < expectedOutputSamples) {+ throw std::runtime_error(+ "More samples were provided than can be buffered! This is an "+ "internal Pedalboard error and should be reported. Buffer had " ++ std::to_string(samplesInResampledBuffer) + "/" ++ std::to_string(resampledBuffer.getNumSamples()) ++ " samples at 8kHz, but was provided " ++ std::to_string(expectedOutputSamples) + ".");+ }++ float *resampledBufferPointer =+ resampledBuffer.getWritePointer(0) + samplesInResampledBuffer;++ int samplesUsed = 0;+ if (samplesInInputReservoir) {+ // Copy the input samples into the input reservoir and use that as the+ // resampler's input:+ expectedOutputSamples += (float)samplesInInputReservoir / resamplerRatio;+ inputReservoir.copyFrom(0, samplesInInputReservoir, inputSamples,+ numInputSamples);+ samplesUsed = nativeToGSMResampler.process(+ resamplerRatio, inputReservoir.getReadPointer(0),+ resampledBufferPointer, expectedOutputSamples);++ if (samplesUsed < numInputSamples + samplesInInputReservoir) {+ // Take the missing samples and put them at the start of the input+ // reservoir for next time:+ int unusedInputSampleCount =+ (numInputSamples + samplesInInputReservoir) - samplesUsed;+ inputReservoir.copyFrom(0, 0,+ inputReservoir.getReadPointer(0) + samplesUsed,+ unusedInputSampleCount);+ samplesInInputReservoir = unusedInputSampleCount;+ } else {+ samplesInInputReservoir = 0;+ }+ } else {+ samplesUsed = nativeToGSMResampler.process(resamplerRatio, inputSamples,+ resampledBufferPointer,+ expectedOutputSamples);++ if (samplesUsed < numInputSamples) {+ // Take the missing samples and put them at the start of the input+ // reservoir for next time:+ int unusedInputSampleCount = numInputSamples - samplesUsed;+ inputReservoir.copyFrom(0, 0, inputSamples + samplesUsed,+ unusedInputSampleCount);+ samplesInInputReservoir = unusedInputSampleCount;+ }+ }++ samplesInResampledBuffer += expectedOutputSamples;++ performEncodeAndDecode();+ }++ int pullSamples(float *outputSamples, int maxOutputSamples) {+ performEncodeAndDecode();++ // Copy the data out of outputBuffer and into the provided pointer, at+ // the right side of the buffer:+ int samplesToCopy = std::min(samplesInOutputBuffer, maxOutputSamples);+ int offsetInOutput = maxOutputSamples - samplesToCopy;+ juce::FloatVectorOperations::copy(outputSamples + offsetInOutput,+ outputBuffer.getWritePointer(0),+ samplesToCopy);+ samplesInOutputBuffer -= samplesToCopy;++ // Move remaining samples to the left side of the output buffer:+ std::memmove((char *)outputBuffer.getWritePointer(0),+ (char *)(outputBuffer.getWritePointer(0) + samplesToCopy),+ samplesInOutputBuffer * sizeof(float));++ performEncodeAndDecode();++ return samplesToCopy;+ }++ void performEncodeAndDecode() {+ while (samplesInResampledBuffer >= GSM_FRAME_SIZE_SAMPLES) {+ float *encodeBuffer = resampledBuffer.getWritePointer(0);// Convert samples to signed 16-bit integer first,// then pass to the GSM Encoder, then immediately back// around to the GSM decoder.short frame[GSM_FRAME_SIZE_SAMPLES];- juce::AudioDataConverters::convertFloatToInt16LE(tempSamples, frame,+ juce::AudioDataConverters::convertFloatToInt16LE(encodeBuffer, frame,GSM_FRAME_SIZE_SAMPLES);+ // Actually do the GSM encoding/decoding:gsm_frame encodedFrame;gsm_encode(encoder.getContext(), frame, encodedFrame);@@ -123,18 +264,40 @@ public:throw std::runtime_error("GSM decoder could not decode frame!");}- juce::AudioDataConverters::convertInt16LEToFloat(frame, tempSamples,+ if (spaceAvailableInOutputBuffer() < gsmFrameSizeInNativeSampleRate) {+ throw std::runtime_error(+ "Not enough space in output buffer to store a GSM frame! Needed " ++ std::to_string(gsmFrameSizeInNativeSampleRate) ++ " samples but only had " ++ std::to_string(spaceAvailableInOutputBuffer()) ++ " samples available. This is "+ "an internal Pedalboard error and should be reported.");+ }+ float *outputBufferPointer =+ outputBuffer.getWritePointer(0) + samplesInOutputBuffer;++ float floatFrame[GSM_FRAME_SIZE_SAMPLES];+ juce::AudioDataConverters::convertInt16LEToFloat(frame, floatFrame,GSM_FRAME_SIZE_SAMPLES);- // Resample back up to the native sample rate:- samplesUsed = gsmToNativeResampler.process(1.0 / resamplerRatio, tempSamples,- inputSamples, blockSize);- // if (samplesUsed != blockSize) {- // throw std::runtime_error("An incorrect number of samples were returned after resampling for GSM. This is an internal Pedalboard error and should be reported.");- // }+ // Resample back up to the native sample rate and store in outputBuffer:+ int expectedOutputSamples = gsmFrameSizeInNativeSampleRate;+ int samplesConsumed = gsmToNativeResampler.process(+ inverseResamplerRatio, floatFrame, outputBufferPointer,+ expectedOutputSamples);++ samplesInOutputBuffer += expectedOutputSamples;++ // Now that we're done with this chunk of resampledBuffer, move its+ // contents to the left:+ int samplesRemainingInResampledBuffer =+ samplesInResampledBuffer - samplesConsumed;+ std::memmove(+ (char *)resampledBuffer.getWritePointer(0),+ (char *)(resampledBuffer.getWritePointer(0) + samplesConsumed),+ samplesRemainingInResampledBuffer * sizeof(float));+ samplesInResampledBuffer -= samplesConsumed;}-- return ioBlock.getNumSamples();}void reset() override final {@@ -142,21 +305,43 @@ public:decoder.reset();nativeToGSMResampler.reset();gsmToNativeResampler.reset();+resampledBuffer.clear();+ outputBuffer.clear();+ inputReservoir.clear();++ samplesInResampledBuffer = 0;+ samplesInOutputBuffer = 0;+ samplesInInputReservoir = 0;++ samplesProduced = 0;+ inStreamLatency = 0;}protected:- virtual int getLatencyHint() override { return 0; }+ virtual int getLatencyHint() override { return inStreamLatency; }private:+ double resamplerRatio = 1.0;+ double inverseResamplerRatio = 1.0;+ float gsmFrameSizeInNativeSampleRate;++ juce::AudioBuffer<float> inputReservoir;+ int samplesInInputReservoir = 0;++ juce::Interpolators::Lagrange nativeToGSMResampler;+ juce::AudioBuffer<float> resampledBuffer;+ int samplesInResampledBuffer = 0;+GSMWrapper encoder;GSMWrapper decoder;- int inputBlockSizeForGSM;- double resamplerRatio = 1.0;- juce::Interpolators::ZeroOrderHold nativeToGSMResampler;- juce::Interpolators::WindowedSinc gsmToNativeResampler;- juce::AudioBuffer<float> resampledBuffer;+ juce::Interpolators::Lagrange gsmToNativeResampler;+ 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;@@ -166,7 +351,7 @@ 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.")+ "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;