|
| 1 | +// Copyright AudioKit. All Rights Reserved. |
| 2 | + |
| 3 | +#include "SoundpipeDSPBase.h" |
| 4 | +#include "ParameterRamper.h" |
| 5 | +#include "Soundpipe.h" |
| 6 | + |
| 7 | +enum VocoderParameter : AUParameterAddress { |
| 8 | + VocoderParameterAttackTime, |
| 9 | + VocoderParameterReleaseTime, |
| 10 | + VocoderParameterBandwidthRatio |
| 11 | +}; |
| 12 | + |
| 13 | +class VocoderDSP : public SoundpipeDSPBase { |
| 14 | +private: |
| 15 | + sp_vocoder *vocoderL; |
| 16 | + sp_vocoder *vocoderR; |
| 17 | + ParameterRamper attackRamp{0.1}; |
| 18 | + ParameterRamper releaseRamp{0.1}; |
| 19 | + ParameterRamper bwRatioRamp{0.5}; |
| 20 | + |
| 21 | + float attackTimeL = 0.1; |
| 22 | + float releaseTimeL = 0.1; |
| 23 | + float bwRatioL = 0.5; |
| 24 | + float attackTimeR = 0.1; |
| 25 | + float releaseTimeR = 0.1; |
| 26 | + float bwRatioR = 0.5; |
| 27 | + |
| 28 | +public: |
| 29 | + VocoderDSP() { |
| 30 | + inputBufferLists.resize(2); // Set up for two input streams |
| 31 | + parameters[VocoderParameterAttackTime] = &attackRamp; |
| 32 | + parameters[VocoderParameterReleaseTime] = &releaseRamp; |
| 33 | + parameters[VocoderParameterBandwidthRatio] = &bwRatioRamp; |
| 34 | + } |
| 35 | + |
| 36 | + void init(int channelCount, double sampleRate) override { |
| 37 | + SoundpipeDSPBase::init(channelCount, sampleRate); |
| 38 | + sp_vocoder_create(&vocoderL); |
| 39 | + sp_vocoder_init(sp, vocoderL); |
| 40 | + sp_vocoder_create(&vocoderR); |
| 41 | + sp_vocoder_init(sp, vocoderR); |
| 42 | + } |
| 43 | + |
| 44 | + void deinit() override { |
| 45 | + SoundpipeDSPBase::deinit(); |
| 46 | + sp_vocoder_destroy(&vocoderL); |
| 47 | + sp_vocoder_destroy(&vocoderR); |
| 48 | + } |
| 49 | + |
| 50 | + void reset() override { |
| 51 | + SoundpipeDSPBase::reset(); |
| 52 | + if (!isInitialized) return; |
| 53 | + sp_vocoder_init(sp, vocoderL); |
| 54 | + sp_vocoder_init(sp, vocoderR); |
| 55 | + } |
| 56 | + |
| 57 | + void process(FrameRange range) override { |
| 58 | + for (int i : range) { |
| 59 | + float sourceInL = inputSample(0, i); // carrier input left |
| 60 | + float sourceInR = inputSample(1, i); // carrier input right |
| 61 | + float excitationInL = input2Sample(0, i); // modulator input left |
| 62 | + float excitationInR = input2Sample(1, i); // modulator input right |
| 63 | + float outSampleL; |
| 64 | + float outSampleR; |
| 65 | + |
| 66 | + attackTimeL = attackRamp.getAndStep(); |
| 67 | + releaseTimeL = releaseRamp.getAndStep(); |
| 68 | + bwRatioL = bwRatioRamp.getAndStep(); |
| 69 | + |
| 70 | + attackTimeR = attackTimeL; |
| 71 | + releaseTimeR = releaseTimeL; |
| 72 | + bwRatioR = bwRatioL; |
| 73 | + |
| 74 | + vocoderL->atk = &attackTimeL; |
| 75 | + vocoderL->rel = &releaseTimeL; |
| 76 | + vocoderL->bwratio = &bwRatioL; |
| 77 | + vocoderR->atk = &attackTimeR; |
| 78 | + vocoderR->rel = &releaseTimeR; |
| 79 | + vocoderR->bwratio = &bwRatioR; |
| 80 | + |
| 81 | + sp_vocoder_compute(sp, vocoderL, &sourceInL, &excitationInL, &outSampleL); |
| 82 | + sp_vocoder_compute(sp, vocoderR, &sourceInR, &excitationInR, &outSampleR); |
| 83 | + |
| 84 | + outputSample(0, i) = outSampleL; |
| 85 | + outputSample(1, i) = outSampleR; |
| 86 | + } |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +AK_REGISTER_DSP(VocoderDSP, "vcdr") |
| 91 | +AK_REGISTER_PARAMETER(VocoderParameterAttackTime) |
| 92 | +AK_REGISTER_PARAMETER(VocoderParameterReleaseTime) |
| 93 | +AK_REGISTER_PARAMETER(VocoderParameterBandwidthRatio) |
0 commit comments