NAudio icon indicating copy to clipboard operation
NAudio copied to clipboard

Help on cross-platform realtime resampling

Open gabrielfreire opened this issue 2 years ago • 2 comments

I would love an example on how to resample audio from a stream in realtime using WdlResamplingSampleProvider, I was only able to make it work using the WaveFormatConversionStream but I need it to be cross-platform.

  • _continuousSpeechStream is a Stream that keeps growing as the connection is on and the bytes need to be resampled before processing.
  • using WaveFormatConversionStream works great here but only on Windows which is not ideal for me.

This is what I have so far:

while (_continuousSpeechStream.CanRead &&
          !waitTask.Wait(streamReadDelay) && _websocketClient.IsConnected)
{
            var currPosition = _continuousSpeechStream.Position;
            using var rawWaveStream = new RawSourceWaveStream(_continuousSpeechStream, 
                   WaveFormat.CreateIeeeFloatWaveFormat(44100, 1));
            
            rawWaveStream.Seek(currPosition, SeekOrigin.Begin);

            // THIS WORKS BUT ONLY ON WINDOWS
            // using var waveConversionStream = new WaveFormatConversionStream(new WaveFormat(16000, 16, 1), rawWaveStream); 

            var sampleProvider = new WaveToSampleProvider(rawWaveStream);
            var resamplingProvider = new WdlResamplingSampleProvider(sampleProvider, 16000);
            var waveProvider = new SampleToWaveProvider(resamplingProvider);

            //bytesRead =  await waveConversionStream.ReadAsync(buffer, 0, buffer.Length);
            bytesRead = waveProvider.Read(buffer, 0, buffer.Length); // only noise

            if (bytesRead > 0)
            {
                         // ............
            }
}

But the resulting audio is just noise, anyone ?

gabrielfreire avatar Feb 14 '23 10:02 gabrielfreire

For real-time I recommend starting with a BufferedWaveProvider and add each buffer of audio as it becomes available. Passing a network stream in is likely to be unreliable. Also make sure you just create one WdlResamplingSamplerProvider (don't make one for each incoming buffer).

I'm hoping to publish an article (on the Twilio blog) soon which uses this technique, so I'll try to remember to come back here and link it when its out

markheath avatar May 03 '23 13:05 markheath

Thanks @markheath , looking forward to reading your article.

gabrielfreire avatar May 03 '23 16:05 gabrielfreire