diff --git a/Sources/AudioKitEX/AudioKitAU.swift b/Sources/AudioKitEX/AudioKitAU.swift index eec6eca..166417b 100644 --- a/Sources/AudioKitEX/AudioKitAU.swift +++ b/Sources/AudioKitEX/AudioKitAU.swift @@ -39,7 +39,7 @@ open class AudioKitAU: AUAudioUnit { } if let outputFormat = outputBusArray.first?.format { - allocateRenderResourcesDSP(dsp, outputFormat.channelCount, outputFormat.sampleRate) + allocateRenderResourcesDSP(dsp, outputFormat.channelCount, outputFormat.sampleRate, maximumFramesToRender) } } diff --git a/Sources/CAudioKitEX/Internals/DSPBase.mm b/Sources/CAudioKitEX/Internals/DSPBase.mm index cd837e6..42d717b 100644 --- a/Sources/CAudioKitEX/Internals/DSPBase.mm +++ b/Sources/CAudioKitEX/Internals/DSPBase.mm @@ -25,8 +25,9 @@ void setBufferDSP(DSPRef pDSP, AudioBufferList* buffer, size_t busIndex) pDSP->setBuffer(buffer, busIndex); } -void allocateRenderResourcesDSP(DSPRef pDSP, uint32_t channelCount, double sampleRate) +void allocateRenderResourcesDSP(DSPRef pDSP, uint32_t channelCount, double sampleRate, AUAudioFrameCount maxFramesToRender) { + pDSP->setMaximumFramesToRender(maxFramesToRender); pDSP->init(channelCount, sampleRate); } diff --git a/Sources/CAudioKitEX/include/DSPBase.h b/Sources/CAudioKitEX/include/DSPBase.h index ede76f5..c65693a 100644 --- a/Sources/CAudioKitEX/include/DSPBase.h +++ b/Sources/CAudioKitEX/include/DSPBase.h @@ -18,7 +18,7 @@ size_t inputBusCountDSP(DSPRef pDSP); bool canProcessInPlaceDSP(DSPRef pDSP); void setBufferDSP(DSPRef pDSP, AudioBufferList* buffer, size_t busIndex); -void allocateRenderResourcesDSP(DSPRef pDSP, uint32_t channelCount, double sampleRate); +void allocateRenderResourcesDSP(DSPRef pDSP, uint32_t channelCount, double sampleRate, AUAudioFrameCount maxFramesToRender); void deallocateRenderResourcesDSP(DSPRef pDSP); void resetDSP(DSPRef pDSP); @@ -89,6 +89,7 @@ struct DSPBase { int channelCount; double sampleRate; + AUAudioFrameCount maximumFramesToRender = 0; bool isInitialized = false; @@ -134,6 +135,12 @@ struct DSPBase { std::atomic isStarted{true}; void setBuffer(AudioBufferList* buffer, size_t busIndex); + + /// Set by AudioKitAU from AUAudioUnit.maximumFramesToRender before init(). + /// Subclasses may read `maximumFramesToRender` in their init() override to + /// pre-size scratch buffers to the worst-case render size. + void setMaximumFramesToRender(AUAudioFrameCount frames) { maximumFramesToRender = frames; } + AUAudioFrameCount getMaximumFramesToRender() const { return maximumFramesToRender; } size_t getInputBusCount() const { return inputBufferLists.size(); } virtual AUAudioFrameCount framesToPull(AUAudioFrameCount requestedOutputFrameCount) { return requestedOutputFrameCount; };