我有一个大学作业,要求我使用VST SDK 2.4构建一个简单的加法合成器。

到目前为止,我已经将部分求和和一个信封。

我认为包络有效,但不确定是否根据推子/滑块位置拉动其值,并且它们似乎不想打球。

我确信这与我的值/范围缩放有关,因为延音滑块似乎可以工作。

我花了很长时间重新检查我的代码,而无法解决问题。

不确定确切的代码位会有用,所以我已经附带了很多内容。

任何帮助是极大的赞赏。

提前致谢,



广告

#ifndef __ADSR
#define __ADSR

#include <math.h>

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

    int time, internalTimer, stage;
    float process(float CurrentLevel, int attack, int decay, float sustain, int release, int* pKeyState);
};
#endif


ADSR.cpp

#include "ADSR.h"

ADSR::ADSR()
{
    time = 0;
    stage = 4;
    internalTimer = 0;
}
ADSR::~ADSR()
{

}

float ADSR::process(float CurrentLevel, int attack, int decay, float sustain, int release, int* pKeyState)
{
    if(stage = 1 && time >= (attack + decay + internalTimer + release))
    {
        time = 0;
    }
    while(time < attack)
    {
        CurrentLevel = CurrentLevel * ((1 / attack) + 1);
        time++;
        return CurrentLevel;
    }

    while(time < (attack + decay))
    {
        CurrentLevel = CurrentLevel * (sustain / decay);
        time++;
        return CurrentLevel;
    }

    while(time > (attack + decay))
    {
        CurrentLevel = CurrentLevel * 1;
        internalTimer++;
        return CurrentLevel;
    }

    while(time < (attack + decay + internalTimer + release) && *pKeyState == 2)
    {
        CurrentLevel = CurrentLevel * (sustain / release);
        time++;
        return CurrentLevel;

    }
    return CurrentLevel;
}


VST_Plug_in.h

//-------------------------------------------------------------------------------------------------------
// This is the most basic plug in code there is!
// All this plug in will do is apply some gain to the input
// This is done in process replacing
//-------------------------------------------------------------------------------------------------------

#ifndef __VST_Plug_in__
#define __VST_Plug_in__

#include "audioeffectx.h"

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



const int NUMBER_OF_INPUTS = 2;
const int NUMBER_OF_OUTPUTS = 2;
const int NUMBER_OF_PROGRAMS = 1;
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);

    double leftSample;
    double rightSample;
    float frequency;
    float sampleRate;
    long time;
    long* pTime;
    float gain;

    // MIDI stuff
    // MIDI data : holds data about current state of MIDI (note on/off, frequency, velocity)
    int keyDown; // true : key is down, false : no key down
    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)

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

    int maxAttack, minAttack, attack, maxDecay, minDecay, decay, minRelease, maxRelease, release;
    float sustain;

    double partial1, partial2, partial3, partial4, partial5, partial6, partial7, partial8;

    ADSR env;

    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);
};

#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"

//-------------------------------------------------------------------------------------------------------
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
    leftSample  = 0.0;
    rightSample = 0.0;
    frequency = 0.0;
    gain = 1.f;
    currentVelocity = 0.0;
    currentNote = 0;
    keyDown = 2;
    pkeyDown = &keyDown;
    time = 0;
    pTime = &time;
    partial1 = partial2 = partial3 = partial4 = partial5 = partial6 = partial7 = partial8 = 0.f;
    maxAttack = 2 * 192000;
    minAttack = 1;
    attack = 44100;
    maxDecay = 192000;
    minDecay = 1;
    decay = 22050;
    maxRelease = 4 * 192000;
    minRelease = 1;
    release = 96000;
    sustain = 1.f;
    sampleRate = getSampleRate();//get sample rate from host

    ADSR env;

    isSynth (); // Informs host that this is a VSTi

    // 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];
    //ADSR env;

    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*time++*(frequency/sampleRate))* 0.125;
        partial2 = (double)sin(2.0*PI*time++*((2*frequency)/sampleRate))* 0.125;
        partial3 = (double)sin(2.0*PI*time++*((3*frequency)/sampleRate))* 0.125;
        partial4 = (double)sin(2.0*PI*time++*((4*frequency)/sampleRate))* 0.125;
        partial5 = (double)sin(2.0*PI*time++*((5*frequency)/sampleRate))* 0.125;
        partial6 = (double)sin(2.0*PI*time++*((6*frequency)/sampleRate))* 0.125;
        partial7 = (double)sin(2.0*PI*time++*((7*frequency)/sampleRate))* 0.125;
        partial8 = (double)sin(2.0*PI*time++*((8*frequency)/sampleRate))* 0.125;

        leftSample = partial1 + partial2 + partial3 + partial4 + partial5 + partial6 + partial7 + partial8;

        leftSample = leftSample * env.process(leftSample, attack, decay, sustain, release, pkeyDown);

        leftSample = leftSample * currentVelocity;
        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;
    time = 0;
    currentNote = liNote;

    // velocity is liVelocity /127
    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

// eg when it is displayed

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;
    }
}

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

int VST_Plug_in::getDecay(float value)
{
    decay = minDecay + ((maxDecay-minDecay) * value);
    return decay;
}
int VST_Plug_in::sendDecay()
{
    int 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;
}

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

最佳答案

我想出了推子的问题。

有一些应该是浮点数的顽皮整数。

快速更改数据类型,所有功能均按预期运行。

从那时起,我在代码中出现了另一个问题,但它与标题无关,因此将发布一个新问题。

谢谢

08-18 10:12