大学の課題で、単純な VSTi 加算シンセを作成する必要があります。ほとんどの作業は完了しましたが、ADSR エンベロープで何もできません。
時間変数、および/またはエンベロープのステージを決定するロジックが原因で、期待どおりに機能していないと思います (そして確実ではありません)。
エンベロープの時間変数が processReplaceing() ループ内でインクリメントされると、コードは準拠しますが、ホストがクラッシュします。
各 adsr 乗算内で時間をインクリメントすると、コンパイルしてホストで実行されますが、必要に応じて機能しません。
processReplaceing() ループで時間をインクリメントするのは当然のことだと思っていたでしょうが、どうやらそうではないようです。
エンベロープが機能しない原因を誰かが見て、正しい方向に私を向けることができれば、私は非常に感謝しています.
どのコードを添付すればよいか本当にわからないので、すべて完了しました。
ありがとう、
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;
}
}
timer.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;
}