I have a university assignment that requires me to make a basic additive synth with ADSR envelope.
I have a good start and it works, that is until I add my envelope into it.
I have read lots, tried lots and been stuck lots and now I have to admit that I am stuck.
Presently the code compiles, it just doesn't make any sound. If I remove the line that is supposed to output the sample at the correct level for the stage in the envelope it works again. So I know it's something with the ADSR class.
I have attached the relevant stuff, and left my comments etc in, in case it helps you see what I was thinking.
if anyone can help at all I would be over the moon.
Thanks in advance
Ben
ADSR.h
Code: Select all
#ifndef __ADSR
#define __ADSR
#include <math.h>
class ADSR
{
public:
ADSR();
~ADSR();
//functions
int setvalues();
//int stage(int identify);
float process(float* pCurrentLevel, int attack, int decay, float sustain, int release, int* stage, long* pTime, int* pKeyState);
int internalTimer;
/*float attack(float currentLevel);
int decay(float currentLevel);
int sustain(float currentLevel);//is this stage needed as only keeping level same while note is on??? * by 1???
int release(float currentLevel);*/
};
#endif
ADSR.cpp
Code: Select all
#include "ADSR.h"
ADSR::ADSR()
{
}
ADSR::~ADSR()
{
}
float ADSR::process(float* pCurrentLevel, int attack, int decay, float sustain, int release, int* pstage, long* pTime, int* pKeyState)
{
//float target = 1.f;
while(*pTime < attack)
{
*pCurrentLevel = *pCurrentLevel + (1 / attack);
//*pTime++;
//*pstage = 2;
//target = 0.5;//sustain level
return *pCurrentLevel;
}
while(*pTime < (attack + decay))
{
*pCurrentLevel = *pCurrentLevel - (sustain / decay);
//*pTime;
//*pstage = 3;
//target = 0;
return *pCurrentLevel;
}
while(*pTime > (attack + decay))
{
*pCurrentLevel = *pCurrentLevel * 1;
internalTimer++;
return *pCurrentLevel;
}
while(*pTime < (attack + decay + internalTimer + release) && *pKeyState == 2)
{
*pCurrentLevel = *pCurrentLevel - (sustain / release);
return *pCurrentLevel;
}
}
VST_Plugin.h
Code: Select all
//-------------------------------------------------------------------------------------------------------
// 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 = 0;
const int NUMBER_OF_OUTPUTS = 2;
const int NUMBER_OF_PROGRAMS = 0;
const int NUMBER_OF_PARAMETERS = 5;
// Base frequency (A4- 440Hz) for use in generating frequency table
const float BASE_A4 = 440.0;
const float PI = 3.141592;
//-------------------------------------------------------------------------------------------------------
class VST_Plug_in : public AudioEffectX
{
public:
VST_Plug_in (audioMasterCallback audioMaster);
~VST_Plug_in ();
virtual void processReplacing (float** inputs, float** outputs, VstInt32 sampleFrames);
// synth functions
virtual VstInt32 processEvents (VstEvents* events);
virtual VstInt32 canDo (char* text);
float leftSample;
float* pLeftSample;
float rightSample;
float frequency;
float sampleRate;
long time;
long* pTime;
// 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();
int getDecay();
float getSustain();
int getRelease();
float partial1;
float partial2;
float partial3;
float partial4;
float partial5;
float partial6;
float partial7;
float partial8;
int adsrStage;
int* padsrStage;
ADSR env;
};
#endif
VST_Plugin.cpp
Code: Select all
//-------------------------------------------------------------------------------------------------------
// 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 : Stereo plugin which applies Gain [-oo, 0dB]
//
// © 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;
currentVelocity = 0.0;
currentNote = 0;
keyDown = 2;
pkeyDown = &keyDown;
adsrStage = 4;
padsrStage = &adsrStage;
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) ;
}
}
time = 0;
partial1 = 0.f;
partial2 = 0.f;
partial3 = 0.f;
partial4 = 0.f;
partial5 = 0.f;
partial6 = 0.f;
partial7 = 0.f;
partial8 = 0.f;
//get sample rate from host
sampleRate = getSampleRate();
ADSR env;
}
//-------------------------------------------------------------------------------------------------------
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 = (float)sin(2.0*PI*time++*(frequency/sampleRate))* 0.125;
partial2 = (float)sin(2.0*PI*time++*((2*frequency)/sampleRate))* 0.125;
partial3 = (float)sin(2.0*PI*time++*((3*frequency)/sampleRate))* 0.125;
partial4 = (float)sin(2.0*PI*time++*((4*frequency)/sampleRate))* 0.125;
partial5 = (float)sin(2.0*PI*time++*((5*frequency)/sampleRate))* 0.125;
partial6 = (float)sin(2.0*PI*time++*((6*frequency)/sampleRate))* 0.125;
partial7 = (float)sin(2.0*PI*time++*((7*frequency)/sampleRate))* 0.125;
partial8 = (float)sin(2.0*PI*time++*((8*frequency)/sampleRate))* 0.125;
leftSample = partial1 + partial2 + partial3 + partial4 + partial5 + partial6 + partial7 + partial8;
leftSample = (env.process(pLeftSample, getAttack(), getDecay(), getSustain(), getRelease(), padsrStage, pTime, pkeyDown)) * currentVelocity;
//leftSample = leftSample * currentVelocity;
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;
adsrStage = 1;
currentNote = liNote;
// velocity is liVelocity /127
currentVelocity = liVelocity / 127.f;
}
void VST_Plug_in::noteOff()
{
keyDown = 2;
adsrStage = 4;
currentVelocity = 0;
}
int VST_Plug_in::getAttack()
{
int attack = 44100;
//capture attack time in samples from slider
return attack;
}
int VST_Plug_in::getDecay()
{
int decay = 22050;
//decay from slider
return decay;
}
float VST_Plug_in::getSustain()
{
float sustain = 0.75;
//sustain from slider;
return sustain;
}
int VST_Plug_in::getRelease()
{
int release = 88200;
//release from slider
return release;
}