I have VST2 plugin and when I add it into vsthost, It worked with Bypass equals true, when I set Bypass equals false, It only worked with Sample Rate = 48000hz and Buffer = 4800 samples (10b/s). Below image: vsthost image
So, I want to set that sample rate and buffer size into my code. Below is code that I use plugin for convert audio wavein/waveout:
public static void ConnectPlugin(bool restartDevice = false, int? inputDeviceNumber = 0, int? outputDeviceNumber = 1, bool byPass = false)
{
if (!restartDevice && waveIn != null) return;
if (waveIn != null) waveIn.Dispose();
if (waveOut != null) waveOut.Dispose();
sampleRate = 48000;
blockSize = (int)(4800);
waveIn = new WaveInEvent();
waveIn.BufferMilliseconds = 50;
waveIn.DataAvailable += Plugin_DataAvailable;
waveIn.DeviceNumber = inputDeviceNumber ?? 0;
waveIn.WaveFormat = new WaveFormat((int)sampleRate, 16, 2);
waveProviderout = new BufferedWaveProvider(waveIn.WaveFormat) { DiscardOnBufferOverflow = true };
int inputCount = _vstPlugin.PluginInfo.AudioInputCount;
int outputCount = _vstPlugin.PluginInfo.AudioOutputCount;
var inputMgr = new VstAudioBufferManager(inputCount, blockSize);
var outputMgr = new VstAudioBufferManager(outputCount, blockSize);
_vstPlugin.PluginCommandStub.Commands.SetBlockSize(blockSize);
_vstPlugin.PluginCommandStub.Commands.SetSampleRate(sampleRate);
_vstPlugin.PluginCommandStub.Commands.SetProcessPrecision(VstProcessPrecision.Process32);
// set param
_vstPlugin.PluginCommandStub.Commands.SetBypass(byPass);
inputBuffers = inputMgr.Buffers.ToArray();
outputBuffers = outputMgr.Buffers.ToArray();
waveOut = new WaveOutEvent();
waveOut.DesiredLatency = 100;
waveOut.DeviceNumber = outputDeviceNumber ?? 1;
waveOut.Init(waveProviderout);
waveOut.Play();
waveIn.StartRecording();
_vstPlugin.PluginCommandStub.Commands.MainsChanged(true);
}
private static void Plugin_DataAvailable(object sender, WaveInEventArgs e)
{
var device = (WaveInEvent)sender;
var naudioBuf = e.Buffer;
try
{
unsafe
{
int j = 0;
for (int i = 0; i < e.BytesRecorded; i++)
{
byte[] tmpbytearr = new byte[2];
tmpbytearr[0] = naudioBuf[i];
i++;
tmpbytearr[1] = naudioBuf[i];
Int16 tmpint = BitConverter.ToInt16(tmpbytearr, 0);
float f = (((float)tmpint / (float)Int16.MaxValue));
inputBuffers[0][j] = f;
inputBuffers[1][j] = f;
j++;
}
}
_vstPlugin.PluginCommandStub.Commands.StartProcess();
_vstPlugin.PluginCommandStub.Commands.ProcessReplacing(inputBuffers, outputBuffers);
_vstPlugin.PluginCommandStub.Commands.StopProcess();
//_vstPlugin.PluginCommandStub.EditorIdle();
byte[] bytebuffer;
unsafe
{
float* tmpBufLeft = ((IDirectBufferAccess32)outputBuffers[0]).Buffer;
float* tmpBufRight = ((IDirectBufferAccess32)outputBuffers[1]).Buffer;
bytebuffer = new byte[outputBuffers[0].SampleCount * 2];
int j = 0;
for (int i = 0; i < (outputBuffers[0].SampleCount * 2); i++)
{
Int16 tmpint = (Int16)((float)outputBuffers[1][j] * (float)Int16.MaxValue);
byte[] tmparr = BitConverter.GetBytes(tmpint);
bytebuffer[i] = tmparr[0];
i++;
bytebuffer[i] = tmparr[1];
j++;
}
}
waveProviderout.AddSamples(bytebuffer, 0, bytebuffer.Length);
}
catch (Exception ex)
{
Console.Out.WriteLine(ex.Message);
}
}
How do I can set sample rate and buffer in my code like vsthost