C++ VSTi信封实现
对于大学作业,我必须编写一个简单的VSTi加法合成器。我已经完成了大部分工作,我只是无法让我的ADSR信封做任何事情 我认为(并且肯定不确定)时间变量和/或决定信封阶段的逻辑导致信封无法按预期运行 如果信封的时间变量在processReplacing()循环中递增,则代码符合要求,但会使主机崩溃 当在每次adsr乘法中增加时间时,它会编译并在主机中运行,只是不能按预期工作 我本以为ProcessReplacement()循环中的递增时间是应该做的,但显然不是 如果有人能看到是什么导致信封不起作用,并能为我指出正确的方向,我将非常感激 我真的不确定要附加哪些代码,所以我已经完成了所有的工作 谢谢 VST_插件.hC++ VSTi信封实现,c++,logic,vst,envelope,C++,Logic,Vst,Envelope,对于大学作业,我必须编写一个简单的VSTi加法合成器。我已经完成了大部分工作,我只是无法让我的ADSR信封做任何事情 我认为(并且肯定不确定)时间变量和/或决定信封阶段的逻辑导致信封无法按预期运行 如果信封的时间变量在processReplacing()循环中递增,则代码符合要求,但会使主机崩溃 当在每次adsr乘法中增加时间时,它会编译并在主机中运行,只是不能按预期工作 我本以为ProcessReplacement()循环中的递增时间是应该做的,但显然不是 如果有人能看到是什么导致信封不起作用
#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
\ifndef\u VST\u插件__
#定义VST插件__
#包括“audioeffectx.h”
#包括
#包括“ADSR.h”
#包括“timer.h”
常量整数输入的数量=0;
常量int输出的数量=2;
const int NUMBER OF_程序=0;
常数int参数的个数=5;
枚举
{
克根,
卡塔克,
kDecay,
卡斯坦,
克莱斯
};
//用于生成频率表的基频(A4-440Hz)
常量浮点数基数=440.0;
常数双PI=3.14159265358979323846;
类VST_插件:公共AudioEffectX
{
公众:
VST_插件(audioMasterCallback audioMaster);
~VST_插件();
虚拟空进程替换(浮点**输入,浮点**输出,VstInt32采样帧);
虚拟VstInt32 processEvents(VSTEEvents*事件);
虚拟VstInt32 canDo(字符*文本);
//MIDI数据:保存有关MIDI当前状态的数据(注意开/关、频率、速度)
int键控;
int*pkeyDown;
long currentNote;//上一个音符的MIDI音符编号(向下键)
float currentVelocity;//当前MIDI音符速度(0->1)
float*m_pfFrequencyTable;//将存储频率值列表(用于注释->频率转换)
双部分1,部分2,部分3,部分4,部分5,部分6,部分7,部分8,左样本,右样本;
浮动最大攻击、最小攻击、攻击、最大衰减、minDecay、衰减、最小释放、最大释放、释放、维持、频率、采样、增益;
长间隔时间;
作废票据();
无效票据(长票据、长活期);
浮点攻击(浮点值);
浮动发送攻击();
浮点值(浮点值);
浮点数衰减();
浮点数(浮点数);
float sendsupain();
float getRelease(浮点值);
浮动发送释放();
虚拟void setParameter(VstInt32索引,浮点值);
虚拟浮点getParameter(VstInt32索引);
虚拟void getParameterLabel(VstInt32索引,字符*标签);
虚拟void getParameterDisplay(VstInt32索引,字符*文本);
虚拟void getParameterName(VstInt32索引,字符*文本);
ADSR环境;
定时器通用时间;
长时间;
};
#恩迪夫
VST_插件.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;
}
//-------------------------------------------------------------------------------------------------------
//VST插件SDK
//版本2.4$日期:2005/11/15 15:14:03$
//
//类别:VST2.x SDK示例
//文件名:VST_Plug_in.cpp
//创建人:斯坦伯格媒体技术公司
//描述:一种垃圾添加剂合成器
//
//©2005,Steinberg Media Technologies,版权所有
//-------------------------------------------------------------------------------------------------------
#包括“VST_Plug_in.h”
#包括“audioeffectx.h”
#包括
#包括“ADSR.h”
#包括“timer.h”
AudioEffect*createEffectInstance(audioMasterCallback audioMaster)
{
返回新的VST_插件(audioMaster);
}
VST_插件::VST_插件(audioMasterCallback audioMaster)
:AudioEffectX(audioMaster、程序数量、参数数量)
{
setNumInputs(输入的数量);//立体声输入
setNumOutputs(输出的数量);//立体声输出
setUniqueID('Add1');//标识
canProcessReplacing();//支持替换输出
isSynth();//通知主机这是一个VSTi
sampleRate=getSampleRate();//从主机获取采样率
leftSample=0.0;
rightSample=0.0;
频率=0.0;
增益=1.f;
电流速度=0.f;
currentNote=0;
keyDown=2;
pkeyDown=&keyDown;
部分时间=0;
partial1=partial2=partial3=partial4=partial5=partial6=partial7=partial8=0.0;
maxAttack=384000;
米纳塔克=1;
攻击=44100;
最大衰减=192000;
minDecay=1;
衰变=22050;
maxRelease=768000;
minRelease=1;
释放=96000;
维持=1.f;
ADSR环境;
定时器通用时间;
pTime=universalTime.timeCount();
//初始化频率表
mpffrequencytable=新浮点[128];//128 Midi音符
中频(微波频率表)
{
对于(int i=0;i<128;i++)
{
m_pfFrequencyTable[i]=基4*powf(2.f,(i-57)/12.f);
}
}
}
VST_插件::~VST_插件()
{
//这里没什么可做的
}
//这就是有趣的事情发生的地方:0
void VST_插件::processReplacing(浮点**输入,浮点**输出,VstInt32采样帧)
{
float*out1=输出[0];
float*out2=输出[1];
对于(int i=0;i#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
#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;
}
}
#ifndef __timer
#define __timer
#include <math.h>
class timer
{
public:
timer();
~timer();
long time;
long* pTime;
long* timeCount();
};
#endif
#include <math.h>
#include "timer.h"
timer::timer()
{
time = 0;
pTime = &time;
}
timer::~timer()
{
}
long* timer::timeCount()
{
return pTime;
}