title: Preparing Sound Data when needed
body: <p>I am using libPd to generate sound. LibPd can process a predefined number of Ticks and then fill a <code>float[]</code> with the generated output. I then use a <code>BufferedWaveProvider</code> to store the <code>float[]</code> after converting to a <code>byte[]</code>. </p>
  
  <p>Generation of audio can be really fast, so it is possible to compute 1 second of sound in quite short time, but it can also be slow, depending on the Pd patch. Is there a way to trigger processing data, when the <code>BufferedWaveProvider</code> has less than a predefined amount of data left?</p>
  
  <p>Currently I am generating audio in a background thread an just sleep for a while and hope that this is enough, and hope for <code>BufferedWaveProvider</code> not overflowing, and even when, then discard that data.</p>
  
  <pre><code>public void ProcessData(float[] output, int ticks)
  {
      while (LibPD.Process(ticks, new float[0], output) == 0)
      {
          if (BufferReady != null)
          {
              BufferReady(this, new BufferReadyEventArgs(output));
          }
          Thread.Sleep(999*BlockSize * ticks / 44100);
      }
  } 
  
  public void StartProcessing(float[] output)
  {
      ThreadPool.QueueUserWorkItem(new WaitCallback(SetPdOutput), output);
  }
  
  private void SetPdOutput(object state)
  {
      float[] output = state as float[];
      if (state == null)
      {
          return;
      }
      ProcessData(output, Ticks);
  }
  </code></pre>
  
  <p>While my code to glue together libPd and NAudio is this:</p>
  
  <pre><code>Buffer = new float[2 * _player.Ticks * _player.BlockSize]; // stereo, number of Ticks
  _soundOutput = new WasapiOut(AudioClientShareMode.Shared, 100);
  _audioBuffer = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(44100, 2))
  {
      BufferDuration = TimeSpan.FromSeconds(10),
      DiscardOnBufferOverflow = true
  };
  _soundOutput.Init(_audioBuffer);
  _soundOutput.Play();
  
  _player.BufferReady += ((sender, eventArgs) =&gt;
  {
      _audioBuffer.AddSamples(PcmFromFloat(output), 0, output.Length * 4);
  });
  _player.StartProcessing(Buffer);
  </code></pre>