Naudio playback in memory samples produces delays and unaesthetic gaps in the sound

729 views Asked by At

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.

1

There are 1 answers

2
Mark Heath On

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