使用juce制作vst插件

制作一个左声道延迟,右声道不作任何处理的插件

一.下载juce,visual studio 2019, adobe Audition软件

二,打开juce软件,project Name 改为myPlugin,其他设置保持默认即可(此处我用的是delayVst),点击create project...

 

 

 三,创建后的界面如下,其中processor负责数据的处理,Editor复制ui界面交互

 

 

四,删除 pluginEditor.cpp 和PluginEditor.h文件,重新建立一个main.cpp文件,界面如下

 

 

 

 

 五,其中main.cpp的代码如下

#include <JuceHeader.h>
#include "PluginProcessor.h"

juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
    return new DelayVstAudioProcessor();
}

  PluginProcessor.cpp代码如下

/*
  ==============================================================================

    This file contains the basic framework code for a JUCE plugin processor.

  ==============================================================================
*/

#include "PluginProcessor.h"


//==============================================================================
DelayVstAudioProcessor::DelayVstAudioProcessor()
#ifndef JucePlugin_PreferredChannelConfigurations
     : AudioProcessor (BusesProperties()
                     #if ! JucePlugin_IsMidiEffect
                      #if ! JucePlugin_IsSynth
                       .withInput  ("Input",  juce::AudioChannelSet::stereo(), true)
                      #endif
                       .withOutput ("Output", juce::AudioChannelSet::stereo(), true)
                     #endif
                       )
#endif
{
    addParameter(mTime = new juce::AudioParameterFloat("time", "Time", 0.0f, 2000.0f, 30.0f));
}

//==============================================================================
const juce::String DelayVstAudioProcessor::getName() const
{
    return JucePlugin_Name;
}

bool DelayVstAudioProcessor::acceptsMidi() const
{
   #if JucePlugin_WantsMidiInput
    return true;
   #else
    return false;
   #endif
}

bool DelayVstAudioProcessor::producesMidi() const
{
   #if JucePlugin_ProducesMidiOutput
    return true;
   #else
    return false;
   #endif
}

bool DelayVstAudioProcessor::isMidiEffect() const
{
   #if JucePlugin_IsMidiEffect
    return true;
   #else
    return false;
   #endif
}

double DelayVstAudioProcessor::getTailLengthSeconds() const
{
    return 0.0;
}

int DelayVstAudioProcessor::getNumPrograms()
{
    return 1;   // NB: some hosts don't cope very well if you tell them there are 0 programs,
                // so this should be at least 1, even if you're not really implementing programs.
}

int DelayVstAudioProcessor::getCurrentProgram()
{
    return 0;
}

void DelayVstAudioProcessor::setCurrentProgram (int index)
{
}

const juce::String DelayVstAudioProcessor::getProgramName (int index)
{
    return {};
}

void DelayVstAudioProcessor::changeProgramName (int index, const juce::String& newName)
{
}

//==============================================================================
void DelayVstAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
    mSampleRate = sampleRate;
    mDelayBuffer.setSize(getTotalNumOutputChannels(), 2.0 * (samplesPerBlock + sampleRate), false, false);
    mDelayBuffer.clear();
}

void DelayVstAudioProcessor::releaseResources()
{
    // When playback stops, you can use this as an opportunity to free up any
    // spare memory, etc.
}

#ifndef JucePlugin_PreferredChannelConfigurations
bool DelayVstAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
  #if JucePlugin_IsMidiEffect
    juce::ignoreUnused (layouts);
    return true;
  #else
    // This is the place where you check if the layout is supported.
    // In this template code we only support mono or stereo.
    // Some plugin hosts, such as certain GarageBand versions, will only
    // load plugins that support stereo bus layouts.
    if (layouts.getMainOutputChannelSet() != juce::AudioChannelSet::mono()
     && layouts.getMainOutputChannelSet() != juce::AudioChannelSet::stereo())
        return false;

    // This checks if the input layout matches the output layout
   #if ! JucePlugin_IsSynth
    if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
        return false;
   #endif

    return true;
  #endif
}
#endif

//音频数据处理模块,,此处是代码关键
void DelayVstAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
    if(Bus* inputBus = getBus(true, 0))
    {
        auto time = mTime->get();
        // 将buffer中的数据拷贝到delaybuffer中
        for (int i = 0; i < mDelayBuffer.getNumChannels(); ++i)
        {
            const int intputChannelNum = inputBus->getChannelIndexInProcessBlockBuffer(std::min(i, inputBus->getNumberOfChannels()));
            wirteToDelayBuffer(buffer, intputChannelNum, i, mWritePos, 1.0, 1.0, true);
        }

        auto readPos = juce::roundToInt(mWritePos - (mSampleRate * time / 1000.0));
        if (readPos < 0)
            readPos += mDelayBuffer.getNumSamples();

        // 将delaybuffer中的数据拷贝到buffer中,实现一个声道的buffer数据延迟
        if (Bus* outputBus = getBus(false, 0))
        {
            readFromDelayBuffer(buffer, 0, 0, readPos, 1.0, 1.0, true);
        }

        mWritePos += buffer.getNumSamples();
        if (mWritePos >= mDelayBuffer.getNumSamples())
            mWritePos -= mDelayBuffer.getNumSamples();


    }
}

//==============================================================================
bool DelayVstAudioProcessor::hasEditor() const
{
    return true; // (change this to false if you choose to not supply an editor)
}


//==============================================================================
void DelayVstAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
    // You should use this method to store your parameters in the memory block.
    // You could do that either as raw data, or use the XML or ValueTree classes
    // as intermediaries to make it easy to save and load complex data.
    juce::MemoryOutputStream stream(destData, true);

    stream.writeFloat(*mTime);
}

void DelayVstAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
    // You should use this method to restore your parameters from this memory block,
    // whose contents will have been created by the getStateInformation() call.
    juce::MemoryInputStream stream(data, static_cast<size_t> (sizeInBytes), false);

    mTime->setValueNotifyingHost(stream.readFloat());
}

//==============================================================================


void DelayVstAudioProcessor::wirteToDelayBuffer(juce::AudioSampleBuffer& buffer,
    const int channelIn, const int channelOut,
    const int writePos, float startGain,
    float endGain, bool replacing)
{
    if (writePos + buffer.getNumSamples() <= mDelayBuffer.getNumSamples())
    {
        if (replacing)
            mDelayBuffer.copyFromWithRamp(channelOut, writePos, buffer.getReadPointer(channelIn), buffer.getNumSamples(), startGain, endGain);
        else
            mDelayBuffer.addFromWithRamp(channelOut, writePos, buffer.getReadPointer(channelIn), buffer.getNumSamples(), startGain, endGain);
    }
    else
    {
        const auto midPos = mDelayBuffer.getNumSamples() - writePos;
        const auto midGain = juce::jmap(float(midPos) / buffer.getNumSamples(), startGain, endGain);
        if (replacing)
        {
            mDelayBuffer.copyFromWithRamp(channelOut, writePos, buffer.getReadPointer(channelIn), midPos, startGain, midGain);
            mDelayBuffer.copyFromWithRamp(channelOut, 0, buffer.getReadPointer(channelIn, midPos), buffer.getNumSamples() - midPos, midGain, endGain);

        }
        else
        {
            mDelayBuffer.addFromWithRamp(channelOut, writePos, buffer.getReadPointer(channelIn), midPos, startGain, midGain);
            mDelayBuffer.addFromWithRamp(channelOut, 0, buffer.getReadPointer(channelIn, midPos), buffer.getNumSamples() - midPos, midGain, endGain);
        }
    }
}

void DelayVstAudioProcessor::readFromDelayBuffer(juce::AudioSampleBuffer& buffer,
    const int channelIn, const int channelOut,
    const int readPos,
    float startGain, float endGain,
    bool replacing)
{
    if (readPos + buffer.getNumSamples() <= mDelayBuffer.getNumSamples())
    {
        if (replacing)
            buffer.copyFromWithRamp(channelOut, 0, mDelayBuffer.getReadPointer(channelIn, readPos), buffer.getNumSamples(), startGain, endGain);
        else
            buffer.addFromWithRamp(channelOut, 0, mDelayBuffer.getReadPointer(channelIn, readPos), buffer.getNumSamples(), startGain, endGain);
    }
    else
    {
        const auto midPos = mDelayBuffer.getNumSamples() - readPos;
        const auto midGain = juce::jmap(float(midPos) / buffer.getNumSamples(), startGain, endGain);
        if (replacing)
        {
            buffer.copyFromWithRamp(channelOut, 0, mDelayBuffer.getReadPointer(channelIn, readPos), midPos, startGain, midGain);
            buffer.copyFromWithRamp(channelOut, midPos, mDelayBuffer.getReadPointer(channelIn), buffer.getNumSamples() - midPos, midGain, endGain);
        }
        else
        {
            buffer.addFromWithRamp(channelOut, 0, mDelayBuffer.getReadPointer(channelIn, readPos), midPos, startGain, midGain);
            buffer.addFromWithRamp(channelOut, midPos, mDelayBuffer.getReadPointer(channelIn), buffer.getNumSamples() - midPos, midGain, endGain);
        }
    }
}

  

PluginEditor.h代码如下
/*
  ==============================================================================

    This file contains the basic framework code for a JUCE plugin processor.

  ==============================================================================
*/

#pragma once

#include <JuceHeader.h>
using namespace juce;

//==============================================================================
/**
*/
class DelayVstAudioProcessor  : public juce::AudioProcessor
{
public:
    //==============================================================================
    DelayVstAudioProcessor();

    //==============================================================================
    void prepareToPlay (double sampleRate, int samplesPerBlock) override;
    void releaseResources() override;

    void wirteToDelayBuffer(juce::AudioSampleBuffer& buffer,
        const int channelIn, const int channelOut,
        const int writePos, float startGain,
        float endGain, bool replacing);

    void DelayVstAudioProcessor::readFromDelayBuffer(juce::AudioSampleBuffer& buffer,
        const int channelIn, const int channelOut,
        const int readPos,
        float startGain, float endGain,
        bool replacing);
   #ifndef JucePlugin_PreferredChannelConfigurations
    bool isBusesLayoutSupported (const BusesLayout& layouts) const override;
   #endif

    void processBlock (juce::AudioBuffer<float>&, juce::MidiBuffer&) override;

    juce::AudioProcessorEditor* createEditor() override { return new juce::GenericAudioProcessorEditor(*this); }
    //==============================================================================
    bool hasEditor() const override;

    //==============================================================================
    const juce::String getName() const override;

    bool acceptsMidi() const override;
    bool producesMidi() const override;
    bool isMidiEffect() const override;
    double getTailLengthSeconds() const override;

    //==============================================================================
    int getNumPrograms() override;
    int getCurrentProgram() override;
    void setCurrentProgram (int index) override;
    const juce::String getProgramName (int index) override;
    void changeProgramName (int index, const juce::String& newName) override;

    //==============================================================================
    void getStateInformation (juce::MemoryBlock& destData) override;
    void setStateInformation (const void* data, int sizeInBytes) override;



private:
    //==============================================================================
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DelayVstAudioProcessor)
    
    juce::AudioSampleBuffer mDelayBuffer;
    int mWritePos = 0;
    double mSampleRate = 0;

    juce::AudioParameterFloat* mTime;

};

  六,在juce中代开visual studio 2019,然后右键选择myPlugin_VST,点击生成即可生成vst插件

 

 

七,将生成的VST插件放入C:\Program Files\Common Files\VST3目录,打开adobe audition软件,点击 效果->音频增效管理器->扫描增效工具,扫描结果见下图。如果不能显示插件,尝试重启或者更换audition版本,我使用的2020版本正常。

 

 八,在audition软件中点击 文件- > 新建  --> 多轨道会话,选择一个轨道插入音频文件,然后点击 效果 -> 批处理 技能看到和使用插件,效果如下

 

 

参考资料:

https://github.com/ffAudio/ffTapeDelay/blob/master/Source/TapeDelayProcessor.cpp

posted @ 2021-03-03 17:38  繁华如梦个人笔记  阅读(2076)  评论(1编辑  收藏  举报