Plugins_SDKv8_Example1
Audio Plugin (DSP) using SDK v8 - Example 2
Here is an example to start developping a plugin "MyPlugin8". We use the class "CMyPlugin8" to define our object but you can use what you want. The file "Main.cpp" is the caller of your object "CMyPlugin8" and is the entry point to communicate with VirtualDJ.
For this example, you need to include the following files from the SDK v8 in your project: vdjPlugin8.h (basic common base-class for all plugins) and vdjDsp8.h (base classes for all Dsp plugins)
You can use other compiler but please find one example of Visual C++ project for this project on a PC. It will generate a .dll file
For the case of this example, please copy all the files in the same folder.
MyPlugin8.h:
#ifndef MYPLUGIN8_H
#define MYPLUGIN8_H
#include "vdjDsp8.h"
#include <cmath>
// round() is only defined in C++11 so we use this workaround
#define round(v) ((int)floor((v)+0.5f))
class CMyPlugin8 : public IVdjPluginDsp8
{
public:
HRESULT VDJ_API OnLoad();
HRESULT VDJ_API OnGetPluginInfo(TVdjPluginInfo8 *infos);
ULONG VDJ_API Release();
HRESULT VDJ_API OnStart();
HRESULT VDJ_API OnStop();
HRESULT VDJ_API OnProcessSamples(float *buffer, int nb);
private:
int SongBpm2;
int StartPos;
// if you want to work on short samples (16 bit) instead of float samples (32 bit):
short bufferShort[8194]; // we consider a sound buffer of maximum 4096 samples (but it could be done dynamically)
void ConvertFloat2Short(short *BufferOut,float *BufferIn, int BufferSize);
void ConvertShort2Float(float *BufferOut,short *BufferIn, int BufferSize);
};
#endif
MyPlugin8.cpp:
#include "MyPlugin8.h"
//-----------------------------------------------------------------------------
HRESULT VDJ_API CMyPlugin8::OnLoad()
{
SongBpm2=0;
StartPos=0;
return S_OK;
}
//-----------------------------------------------------------------------------
HRESULT VDJ_API CMyPlugin8::OnGetPluginInfo(TVdjPluginInfo8 *infos)
{
infos->PluginName = "MyPlugin8";
infos->Author = "Atomix Productions";
infos->Description = "My first VirtualDJ 8 plugin";
infos->Version = "1.0";
infos->Flags = 0x00; // you could also use VDJFLAG_PROCESSAFTERSTOP if you want to process sound when the deck is stopped
infos->Bitmap = NULL;
return S_OK;
}
//---------------------------------------------------------------------------
ULONG VDJ_API CMyPlugin8::Release()
{
delete this;
return 0;
}
//---------------------------------------------------------------------------
HRESULT VDJ_API CMyPlugin8::OnStart()
{
// ADD YOUR CODE HERE WHEN THE AUDIO PLUGIN IS STARTED
SongBpm2 = SongBpm?SongBpm:(SampleRate/2); // by default 120bpm
StartPos = int(SongPosBeats * SongBpm2);
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT VDJ_API CMyPlugin8::OnStop()
{
// ADD YOUR CODE HERE WHEN THE AUDIO PLUGIN IS STOPPED
StartPos = 0;
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT VDJ_API CMyPlugin8::OnProcessSamples(float *buffer, int nb)
{
// ADD YOUR AUDIO TREATMENT CODE HERE USING THE AUDIO BUFFER *buffer OF 2*nb FLOAT SAMPLES (STEREO SIGNAL)
float out[2], in[2];
int pos;
int CurrentPos;
float xBeat;
SongBpm2 = SongBpm?SongBpm:(SampleRate/2);
pos = int(SongPosBeats * SongBpm2);
// ConvertFloat2Short(bufferShort,buffer,2*nb);
// we read the 'nb' stereo samples ( = 2 * 'nb' mono samples) from the sound buffer
for(int i=0;i<nb;i++)
{
CurrentPos = pos + i;
xBeat = (CurrentPos - StartPos)/float(SongBpm2); // 'x' beat(s) from StartPos
// it's a stereo signal so we have 2 channels.
for(int ch=0;ch<2;ch++)
{
in[ch] = buffer[2*i + ch];
// for the case of this example,we only keep the left channel (ch==0) and mute the right channel (ch==1)
if(ch==1) out[ch] = 0;
else out[ch] = in[ch];
buffer[2*i + ch] = out[ch];
}
}
// ConvertShort2Float(buffer,bufferShort,2*nb);
return S_OK;
}
//------------------------------------------------------------------------------------
void CMyPlugin8::ConvertFloat2Short(short *BufferOut,float *BufferIn, int BufferSize)
{
int v;
int j;
for(j=0;j<BufferSize;j++)
{
v = round (32768.0f * BufferIn[j]);
if (v < -32768) v = -32768;
else if (v > 32767) v = 32767;
BufferOut[j] = (short) v;
}
}
//------------------------------------------------------------------------------------
void CMyPlugin8::ConvertShort2Float(float *BufferOut,short *BufferIn,int BufferSize)
{
int j;
for(j=0;j<BufferSize;j++)
{
BufferOut[j] = (float) BufferIn[j] / 32768.0f;
}
}
Main.cpp:
// This is the standard DLL loader for COM object.
#include "MyPlugin8.h"
HRESULT VDJ_API DllGetClassObject(const GUID &rclsid,const GUID &riid,void** ppObject)
{
if (memcmp(&rclsid,&CLSID_VdjPlugin8,sizeof(GUID))==0 && memcmp(&riid,&IID_IVdjPluginDsp8,sizeof(GUID))==0)
{
*ppObject=new CMyPlugin8();
}
else
{
return CLASS_E_CLASSNOTAVAILABLE;
}
return NO_ERROR;
}