0

对于大学作业,我必须编写一个简单的 VSTi 加法合成器。我已经完成了大部分工作,但我无法让我的 ADSR 信封做任何事情。

我认为(并且绝对不确定)时间变量和/或决定信封阶段的逻辑导致它无法按预期运行。

如果信封的时间变量在 processReplacing() 循环中增加,则代码符合但主机崩溃。

当在每个 adsr 乘法中增加时间时,它会在主机中编译和运行,只是不能按预期工作。

我会认为在 processReplacing() 循环中增加时间是可行的,显然不是。

如果有人能看到导致信封不起作用的原因并能指出我正确的方向,我将不胜感激。

我真的不确定要附加哪些代码,所以已经完成了所有这些。

谢谢,

VST_Plug_in.h

#ifndef __VST_Plug_in__
#define __VST_Plug_in__

#include "audioeffectx.h"
#include <math.h>
#include "ADSR.h"
#include "timer.h"

const int NUMBER_OF_INPUTS = 0;
const int NUMBER_OF_OUTPUTS = 2;
const int NUMBER_OF_PROGRAMS = 0;
const int NUMBER_OF_PARAMETERS = 5;

enum
    {
    kGain,
    kAttack,
    kDecay,
    kSustain,
    kRelease
    };

// Base frequency (A4- 440Hz) for use in generating frequency table
    const float BASE_A4  = 440.0;
    const double PI = 3.14159265358979323846;

class VST_Plug_in : public AudioEffectX
{
public:
    VST_Plug_in (audioMasterCallback audioMaster);
    ~VST_Plug_in ();

    virtual void processReplacing (float** inputs, float** outputs, VstInt32 sampleFrames);
    virtual VstInt32 processEvents (VstEvents* events);
    virtual VstInt32 canDo (char* text);

    // MIDI data : holds data about current state of MIDI (note on/off, frequency, velocity)
    int keyDown;
    int* pkeyDown;

    long currentNote; // the MIDI note number of the last note on (key down)
    float currentVelocity; // current MIDI note velocity (0 -> 1)
    float *m_pfFrequencyTable; // will store a list of frequency values (for note->frequency conversion)

    double partial1, partial2, partial3, partial4, partial5, partial6, partial7, partial8, leftSample, rightSample;
    float maxAttack, minAttack, attack, maxDecay, minDecay, decay, minRelease, maxRelease, release, sustain, frequency, sampleRate, gain;
    long partialTime;

    void noteOff ();
    void noteOn (long liNote, long liVelocity);
    float getAttack(float value);
    float sendAttack();
    float getDecay(float value);
    float sendDecay();
    float getSustain(float value);
    float sendSustain();
    float getRelease(float value);
    float sendRelease();

    virtual void setParameter (VstInt32 index, float value);
    virtual float getParameter (VstInt32 index);
    virtual void getParameterLabel (VstInt32 index, char* label);
    virtual void getParameterDisplay (VstInt32 index, char* text);
    virtual void getParameterName (VstInt32 index, char* text); 

    ADSR env;
    timer universalTime;
    long* pTime;
};

#endif

VST_Plug_in.cpp

//-------------------------------------------------------------------------------------------------------
// VST Plug-Ins SDK
// Version 2.4      $Date: 2005/11/15 15:14:03 $
// 
// Category     : VST 2.x SDK Samples
// Filename     : VST_Plug_in.cpp
// Created by   : Steinberg Media Technologies
// Description  : a crap additive synth
//
// © 2005, Steinberg Media Technologies, All Rights Reserved
//-------------------------------------------------------------------------------------------------------

#include "VST_Plug_in.h"
#include "audioeffectx.h"
#include <math.h>
#include "ADSR.h"
#include "timer.h"

AudioEffect* createEffectInstance (audioMasterCallback audioMaster)
{
    return new VST_Plug_in (audioMaster);
}

VST_Plug_in::VST_Plug_in (audioMasterCallback audioMaster)
: AudioEffectX (audioMaster, NUMBER_OF_PROGRAMS, NUMBER_OF_PARAMETERS)  
{
    setNumInputs (NUMBER_OF_INPUTS);        // stereo in
    setNumOutputs (NUMBER_OF_OUTPUTS);      // stereo out
    setUniqueID ('Add1');   // identify
    canProcessReplacing (); // supports replacing output
    isSynth (); // Informs host that this is a VSTi
    sampleRate = getSampleRate();//get sample rate from host

    leftSample  = 0.0;
    rightSample = 0.0;
    frequency = 0.0;
    gain = 1.f;
    currentVelocity = 0.f;
    currentNote = 0;
    keyDown = 2;
    pkeyDown = &keyDown;
    partialTime = 0;
    partial1 = partial2 = partial3 = partial4 = partial5 = partial6 = partial7 = partial8 = 0.0;
    maxAttack = 384000;
    minAttack = 1;
    attack = 44100;
    maxDecay = 192000;
    minDecay = 1;
    decay = 22050;
    maxRelease = 768000;
    minRelease = 1;
    release = 96000;
    sustain = 1.f;

    ADSR env;
    timer universalTime;
    pTime = universalTime.timeCount();

    // initialise frequency table
    m_pfFrequencyTable = new float [128] ; // 128 Midi notes
    if (m_pfFrequencyTable)
    {
        for (int i = 0; i<  128; i++)
        {
            m_pfFrequencyTable[i] = BASE_A4 *powf(2.f,(i-57)/12.f) ;
        }
    }   
}

VST_Plug_in::~VST_Plug_in ()
{
    // nothing to do here
}

// this is where the intresting stuff happens :0
void VST_Plug_in::processReplacing (float** inputs, float** outputs, VstInt32 sampleFrames)
{
    float* out1 = outputs[0];
    float* out2 = outputs[1];

    for(int i = 0; i < sampleFrames; i++)
    {
        // NEW : only send out audio if there is a note on currently
        frequency = m_pfFrequencyTable[currentNote];

        partial1 = (double)sin(2.0*PI*partialTime++*(frequency/sampleRate))* 0.125;//Nyquist frequency issues.  
        partial2 = (double)sin(2.0*PI*partialTime*((2*frequency)/sampleRate))* 0.125;//Obvious looping/foldback of frequencies with higher notes.
        partial3 = (double)sin(2.0*PI*partialTime*((3*frequency)/sampleRate))* 0.125;//Appears mostly fixed when time is only 
        partial4 = (double)sin(2.0*PI*partialTime*((4*frequency)/sampleRate))* 0.125;//incremented with the fundamental.
        partial5 = (double)sin(2.0*PI*partialTime*((5*frequency)/sampleRate))* 0.125;
        partial6 = (double)sin(2.0*PI*partialTime*((6*frequency)/sampleRate))* 0.125;
        partial7 = (double)sin(2.0*PI*partialTime*((7*frequency)/sampleRate))* 0.125;
        partial8 = (double)sin(2.0*PI*partialTime*((8*frequency)/sampleRate))* 0.125; 
        leftSample = partial1 + partial2 + partial3 + partial4 + partial5 + partial6 + partial7 + partial8;

        leftSample = (env.process(leftSample, attack, decay, sustain, release, pkeyDown, pTime) * currentVelocity); //needs to be pointer as data is duplicated when sent.
        //(*pTime)++; //crashes host if uncommented
        leftSample = leftSample * gain;
        rightSample = leftSample;

        // write samples to output buffer 
        (*out1++) = leftSample;
        (*out2++) = rightSample;
    }
}

// NEW : overriden function, tells host what the plugin can do (see notes)
VstInt32 VST_Plug_in::canDo(char *text)
{
    if (!strcmp (text, "receiveVstEvents"))     // SimpleSynth can receive VST events
        return 1;
    if (!strcmp (text, "receiveVstMidiEvent"))  // SimpleSynth can receive VST MIDI events
        return 1;
    return -1;  // explicitly can't do; 0 => don't know 
}

// NEW : this process function is called to collect incoming VST events
VstInt32 VST_Plug_in::processEvents (VstEvents* events)
{
    // parse event list
    for (long i = 0; i < events->numEvents; i++)
    {
        if ((events->events[i])->type == kVstMidiType)
        {
            VstMidiEvent* event = (VstMidiEvent*)events->events[i];
            char* midiData = event->midiData;
            long status = midiData[0] & 0xf0;       // ignoring channel

            if (status == 0x90 || status == 0x80)   // we only look at notes
            {
                long note = midiData[1];
                long velocity = midiData[2];

                if (status == 0x80)
                {
                    velocity = 0;   
                    // set velocity to zero if it is a note off message
                }
                if (!velocity && (note == currentNote))
                {
                    noteOff ();
                }
                else
                {
                    noteOn (note, velocity);
                }
            }
        }
    }
    return 1; // indicate that we wish to receive more events
}

void VST_Plug_in::noteOn(long liNote, long liVelocity)
{
    keyDown = 1;
    partialTime = 0;
    currentNote = liNote;
    currentVelocity = liVelocity / 127.f;
}

void VST_Plug_in::noteOff()
{
    keyDown = 2;
    currentVelocity = 0;
}

// this function is called whenever the fader is moved
void VST_Plug_in::setParameter (VstInt32 index, float value)
{
    switch (index)
    {
        case kGain : gain = value;break;  
        case kAttack : attack = getAttack(value); break; //need vale in samples for envelope here
        case kDecay : decay = getDecay(value); break;
        case kSustain : sustain = getSustain(value); break;
        case kRelease : release = getRelease(value); break;
    }
}

// this function is called whenever the gui requires data
float VST_Plug_in::getParameter (VstInt32 index)

{
    switch (index)
    {
        case kGain : return gain;break;
        case kAttack : return sendAttack(); break; //need 0 - 1 val here
        case kDecay : return sendDecay();break;
        case kSustain : return sendSustain();break;
        case kRelease : return sendRelease();break;
    }
}

// getParameterName places the parameter name on the plug in
void VST_Plug_in::getParameterName (VstInt32 index, char* label)

{
    switch (index)
    {
        case kGain : vst_strncpy (label, "Gain", kVstMaxParamStrLen);break;
        case kAttack : vst_strncpy (label, "Attack", kVstMaxParamStrLen); break;
        case kDecay : vst_strncpy (label, "Decay", kVstMaxParamStrLen); break;
        case kSustain : vst_strncpy (label, "Sustain", kVstMaxParamStrLen); break;
        case kRelease : vst_strncpy (label, "Release", kVstMaxParamStrLen); break;
    }
}

// getParameterDisplay displays the parameter value on the plug-in
void VST_Plug_in::getParameterDisplay (VstInt32 index, char* text)

{
    switch (index)
    {
        case kGain : dB2string(gain, text, kVstMaxParamStrLen);break;
        case kAttack : float2string(attack, text, kVstMaxParamStrLen); break;
        case kDecay : float2string(decay, text, kVstMaxParamStrLen); break;
        case kSustain: dB2string(sustain, text, kVstMaxParamStrLen); break;
        case kRelease : float2string(release, text, kVstMaxParamStrLen); break;
    }
    /*linear display
    float2string(gain, text, kVstMaxParamStrLen);
    dB display
    dB2string (gain, text, kVstMaxParamStrLen);*/
}

void VST_Plug_in::getParameterLabel (VstInt32 index, char* label)

{
    switch (index)
    {
        case kGain : vst_strncpy (label, "dB", kVstMaxParamStrLen);break;
        case kAttack : vst_strncpy (label, "Samples", kVstMaxParamStrLen); break;
        case kDecay : vst_strncpy (label, "Samples", kVstMaxParamStrLen); break;
        case kSustain : vst_strncpy (label, "dB", kVstMaxParamStrLen); break;
        case kRelease : vst_strncpy (label, "Samples", kVstMaxParamStrLen); break;
    }
}

float VST_Plug_in::getAttack(float value)
{   
    attack = minAttack + ((maxAttack-minAttack) * value);
    return attack;
}
float VST_Plug_in::sendAttack()
{   
    float retAttVal;
    retAttVal = (attack - minAttack) / (maxAttack-minAttack);
    return retAttVal;
}

float VST_Plug_in::getDecay(float value)
{
    decay = minDecay + ((maxDecay-minDecay) * value);
    return decay;
}
float VST_Plug_in::sendDecay()
{
    float retDecVal;
    retDecVal = (decay - minDecay) / (maxDecay-minDecay);
    return retDecVal;
}

float VST_Plug_in::getSustain(float value)
{
    sustain = value;
    return sustain;
}
float VST_Plug_in::sendSustain()
{
    return sustain;
}

float VST_Plug_in::getRelease(float value)
{
    release = minRelease + ((maxRelease-minRelease) * value);
    return release;
}
float VST_Plug_in::sendRelease()
{
    float retRelVal;
    retRelVal = (release - minRelease) / (maxRelease-minRelease);
    return retRelVal;
}

ADSR.h

#ifndef __ADSR 
#define __ADSR

#include <math.h>

class ADSR
{
public:
    ADSR();
    ~ADSR();

    long susTimer; 
    int stage;
    double process(double currentSamp, float attack, float decay, float sustain, float release, int* pKeyState, long* pTime); 
};
#endif

ADSR.cpp

#include <math.h>
#include "ADSR.h"


ADSR::ADSR()
{
    stage = 1;
    susTimer = 0;
}
ADSR::~ADSR()
{
}

double ADSR::process(double currentSamp, float attack, float decay, float sustain, float release, int* pKeyState, long* pTime)
{
    if(stage == 1 && *pTime >= (attack + decay + susTimer + release))
    {
        *pTime = 0;
    }

    while(*pTime < attack)
    {
        currentSamp = currentSamp * ((1.0f / attack) + 1.0f);
        (*pTime)++;//doesn't crash but doesnt do anything either
        stage = 1;
        return currentSamp;
    }

    while(*pTime < (attack + decay))
    {
        currentSamp = currentSamp * (sustain / decay);
        (*pTime)++;//doesn't crash but doesnt do anything either
        stage = 2;
        return currentSamp;
    }

    while(*pTime > (attack + decay))
    {
        currentSamp = currentSamp * 1.0f;
        susTimer++;
        stage = 3;
        return currentSamp;
    }

    while(*pTime < (attack + decay + susTimer + release) && *pKeyState == 2)
    {
        currentSamp = currentSamp * (sustain / release);
        (*pTime)++;//doesn't crash but doesnt do anything either
        stage = 4;
        return currentSamp;
    }
}

计时器.h

#ifndef __timer
#define __timer

#include <math.h>


class timer
{
public:
    timer();
    ~timer();

    long time;
    long* pTime;

    long* timeCount();
};
#endif

计时器.cpp

#include <math.h>
#include "timer.h"


timer::timer()
{
    time = 0;
    pTime = &time;
}
timer::~timer()
{
}

long* timer::timeCount()
{
    return pTime;
}
4

0 回答 0