This project has moved. For the latest updates, please go here.

2 Questions: Replaying and Stereo mixer

Jun 20, 2011 at 4:25 PM

Dear reader,

I will post my code without error checks and socket code to shorten the code.

So for my first question i use waveProvider.AddSamples to play the audio back, but i also write it to a file, However i have no idea how to play back that file.
Is it even possible in this format?

My second question is how can i record the Steoro mixer instead of the microphone (so the audio that would normally come out of the speakers).


Thanks allot for reading!


Speaker out:

codec = new SpeexChatCodec(rate);

waveOut = new WaveOut();
waveProvider = new BufferedWaveProvider(codec.RecordFormat);
waveProvider.DiscardOnBufferOverflow = true;

public void OnDataReceived(IAsyncResult asyn)
    //Received data in byte[] b
    byte[] decoded = codec.Decode(b, 0, b.Length);

    if (record)
        fs = new FileStream(string.Format("{0}{1:yyyy-MM-dd_HH-mm-ss}.wav", file, DateTime.Now), FileMode.Create);
        fs.Write(decoded, 0, decoded.Length);

    waveProvider.AddSamples(decoded, 0, decoded.Length);



Microphone in:


private WaveIn waveIn;
private INetworkChatCodec codec;                  

waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback());
waveIn.BufferMilliseconds = 50;
waveIn.DeviceNumber = device;
waveIn.WaveFormat = codec.RecordFormat;
waveIn.DataAvailable += waveIn_DataAvailable;

void waveIn_DataAvailable(object sender, WaveInEventArgs e)
//Encode with Speex
byte[] encoded = codec.Encode(e.Buffer, 0, e.BytesRecorded);
//Transmit encoded array


Jun 20, 2011 at 4:34 PM

Answer to your first question is simple - just use the WaveFileWriter with codec.RecordFormat as the WaveFormat instead of FileStream

Second question is more tricky since not all soundcards support this for WaveOut. Either you need to set your Record Mixer up to a "What you hear" option, or you need to use Wasapi loopback recording (not currently provided in NAudio, but example implementations are available if you search the forums).


Jun 20, 2011 at 4:56 PM

Woow that was fast!

Thanks mark! i had been messing around with, well other things that didn't make sense AT ALL.