Arctic
Arctic

Reputation: 827

How do I resample an in-memory audio stream (byte[]) with NAudio?

I want to resample audio byte array from 8Khz to 48Khz. The audio stream is obtained as a byte[] from a network socket.

Reading Mark Heath's Blog about resampling using NAudio, I came across the following code

int outRate = 16000; 
var inFile = @"test.mp3"; 
var outFile = @"test resampled WDL.wav"; 
using (var reader = new AudioFileReader(inFile)) 
{ 
    var resampler = new WdlResamplingSampleProvider(reader, outRate); 
    WaveFileWriter.CreateWaveFile16(outFile, resampler); 
}

But this code acts on a file stream (AudioFileReader) rather than in memory data (byte[]). How could I modify this code to up-sample my byte array?

Edit: Basically I want to up-sample the 8 KHz data obtained from a network peer to 48 KHz and play using WASAPI.

Upvotes: 3

Views: 3959

Answers (1)

Mark Heath
Mark Heath

Reputation: 49482

Your input to the resampler could be a BufferedWaveProvider or a RawSourceWaveStream. You can't use CreateWaveFile16 to resample in real-time though. You'd need to read only the amount of audio you expect to be available and write it to the WAV file.

Upvotes: 2

Related Questions