1

Using BufferedWaveProvider for playback of audio samples which are stored in database as double[]

 _bufferedWaveProvider = new BufferedWaveProvider(Format)
                                {
                                    DiscardOnBufferOverflow = true,
                                    BufferDuration = TimeSpan.FromSeconds(5)
                                };

public void Consume(double[] samples, int offset, int count)
{
   samples.Paginate<double, float>(offset, count)
          .ForEach(x =>
          {
            byte[] consumeBuffer = x.ToBytes(ref _consumeBuffer);

            _bufferedWaveProvider.AddSamples(consumeBuffer, 0, _consumeBuffer.Length);
           });
}

The audio when played back is producing gaps in sound. Samples are sent inside Consume() method for every 100ms. Is there a problem that the WaveOut() is playing faster than we call Consume() method ? How do we synchronize this reading and playback.

jero2rome
  • 1,548
  • 1
  • 21
  • 39
  • 1
    the waveOut API uses a pull model by default. You and/or NAudio have turned it somehow into a push model so, yes, it is possible that your not calling `Consume` fast enough. Another possibility is that you're calling it to fast and the buffer is being dropped (`DiscardOnBufferOverflow`). You haven't shown how it is called though so it's not possible to provide a solid answer. – jaket Jun 16 '15 at 22:47

1 Answers1

1

A better choice here would be RawSourceWaveStream rather than BufferedWaveProvider, which would allow you to play directly from a MemoryStream containing the full audio.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • I use BufferedWaveProvider to perform, Pause() and Resume(), Mixing multiple channels, Equalizing. Don't know how to achieve these using MemoryStream. Can we initialize WaveOut first and then add data to MemoryStream ? it din't work. Moreover, I'm plotting the waveform while playing it, It was easier to use BufferedWaveProvider. – jero2rome Jun 17 '15 at 13:58
  • I assumed that since your data is in a database, then you actually have access to the whole audio data up front. – Mark Heath Jun 17 '15 at 15:15