title: VST.NET Route Audio
body: <p>I just recently started making a VST Host and for that i'm using the VST.NET library for C#. Now i'm trying to create the routing between the Line in, through the Plugins and to the Line Out.
  My plan is to record the audio in the host and sent the recorded buffer (live when recorded not saving a temporary file) to the first plugin. The output of the first Plugin (which will be an Effects Plugin will the go live to the next plugin. After it got routed through all necessary plugins it should be routed to an audio output device.</p>
  
  <p>For the recording part i already took a look at NAudio and created the recording part like <a href="https://stackoverflow.com/questions/15101889/hearing-the-incoming-audio-from-mic">this</a>. Now i can get the Audio Buffer in my isDataAvailable method in the form of a byte array. Problem being that VST.NET uses the ProcessReplacing method (i think) to process audio to a plugin and this method requires a buffer in form of an instance of a VstAudioBuffer Array and the constructor of VstAudioBuffer requires a float* buffer.</p>
  
  <p>Now how should i convert those variables to send them to the plugin? Also in the end how can i then convert them back to send to WaveOut?</p>
  
  <p>Another question: Can i, after i call plugin1.processreplacing to send the recorded data to plugin1 call plugin2.processreplacing with the parameters inputBuffer being the outputbuffer of the processreplacing-call to plugin1 to send the processed audio to plugin2?</p>
  
  <p>I tried to modify the example from the link and made this:</p>
  
  <pre><code>void waveIn_DataAvailable(object sender, WaveInEventArgs e)
      {
          if (waveProvider != null)
              waveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
          int bytesRead = waveProvider.Read(naudioBuf,0,e.BytesRecorded);
          naudioBuf = e.Buffer;
  
          unsafe
          {
              fixed (byte* byteBuf = &amp;naudioBuf[0])
              {
                  float* floatBuf = (float*)byteBuf;
                  for (int i = 0; i &lt; e.BytesRecorded; i++)
                  {
                      vstBufIn[0][i] = *(floatBuf + i);
                  }
              }
          }
  
          cont.PluginCommandStub.MainsChanged(true);
          cont.PluginCommandStub.StartProcess();
          cont.PluginCommandStub.ProcessReplacing(vstBufIn, vstBufOut);
          cont.PluginCommandStub.StopProcess();
          cont.PluginCommandStub.MainsChanged(false);
  
          unsafe
          {
              float* tmpBufL = ((IDirectBufferAccess32)vstBufOut[0]).Buffer;
              byte[] buffer = BitConverter.GetBytes(*(tmpBufL));
              waveOutProv.AddSamples(buffer, 0, buffer.Length);
          }
      }
  </code></pre>
  
  <p>The setup for WaveIn and Out looks like this:</p>
  
  <pre><code>        WaveIn waveIn = new WaveIn(this.Handle);
          waveIn = new WaveIn(this.Handle);
          waveIn.BufferMilliseconds = 25;
          waveIn.DataAvailable += waveIn_DataAvailable;
  
          waveProvider = new BufferedWaveProvider(waveIn.WaveFormat);
          waveOutProv = new BufferedWaveProvider(waveIn.WaveFormat);
  
          waveOut = new WaveOut();
          waveOut.DesiredLatency = 100;
          waveOut.Init(waveOutProv);
  
          waveIn.StartRecording();
          waveOut.Play();
  
          this.sampleRate = 44100;
          this.channels = 2;
          this.blockSize = 4410;
  
          cont.PluginCommandStub.SetBlockSize(blockSize);
          cont.PluginCommandStub.SetSampleRate((float)sampleRate);
  
          vstBufManIn = new VstAudioBufferManager(channels, blockSize * channels);
          vstBufManOut = new VstAudioBufferManager(channels, blockSize * channels);
  
          vstBufIn = vstBufManIn.ToArray();
          vstBufOut = vstBufManOut.ToArray();
  
          naudioBuf = new byte[blockSize * channels * 4];
  </code></pre>
  
  <p>I'm getting an output and it also gets louder when the input in the mic is louder but it's just a weird "clicking noise". I'm guessing that i did something wrong with converting the vstBufOut to the bite array but i don't quite know what is wrong.</p>