NAudio
NAudio copied to clipboard
Help on cross-platform realtime resampling
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.
_continuousSpeechStreamis aStreamthat keeps growing as the connection is on and the bytes need to be resampled before processing.- using
WaveFormatConversionStreamworks 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 ?
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
Thanks @markheath , looking forward to reading your article.