0

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

vsthost image

  • SetBlockSize is the number of samples per 'slice' or 'cycle' the plugin processes at one time. So I think you want to set that to 4800 (not 400)...? SetSampleRate ... well sets the number of samples per second => 480000 One pointer: The Start-/StopProcess functions indicate to the plugin that the audio 'engine' (on the host) is running. So call StartProcess() at the end of the setup routine and StopProcess when you shut down the wave events for instance to change settings or when the app closes. Lots of plugins do not need these calls so you can also test when not calling them at all. [2c] – obiwanjacobi Nov 27 '20 at 08:13
  • THanks, I edited my code, It seems to be working. But the sound is interrupted.. Please help me. – Hùng Phạm Nov 28 '20 at 09:55
  • Look for allocations during the audio processing cycles. The GC will stop threads to collect disposed objects. For instance the VstAudioBufferManager is meant to be initialized once and its buffer (re)used during Process(). That part looks ok in your code - just make sure you allocate what you need up-front and not during the audio processing. [2c] – obiwanjacobi Nov 28 '20 at 15:32
  • When set bypass equals "true", it working fine, but when I set by pass equals "false", the sound seems to be missing some words – Hùng Phạm Nov 29 '20 at 14:52
  • You could try with another plugin that performs similar processing. That way you'll know if the problem is in your host code or if the plugin is the cause... – obiwanjacobi Nov 29 '20 at 18:08
  • thanks jacobi, I have resolved that problem... I have change the channel of waveIn 1 instead of 2, then it working fine... waveIn.WaveFormat = new WaveFormat((int)sampleRate, 16, 1); – Hùng Phạm Nov 30 '20 at 10:42
  • My program ran well at the beginning, then over a period of time, the sound latency gradually increased. I don't know where the cause is. Please give me advice – Hùng Phạm Jan 14 '21 at 10:09
  • Could be anything - impossible for me to tell... – obiwanjacobi Jan 14 '21 at 15:27

0 Answers0