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

Error during use WaveMixerStream32 with 16 bit PCM: 16kHz 1 channels

Aug 7, 2011 at 3:13 PM
Edited Aug 10, 2011 at 3:22 PM

Hi all,

i'm using naudio 1.4 in network audio chat scenario, after success working one to one communication, i'm trying to support multi client chat. I'm using WaveMixerStream32 to mix raw data coming from differents client. The waveformat of coming audio (for every clients) is 16 bit PCM 16Khz 1 channel.

Follow used code to configure WaveMixerStream32:

this.mixer = WaveMixerStream32();
var wavePlayer = new WaveOut();
wavePlayer.DesiredLatency = this.DesiredLatencyMs; //Value used for test is 100ms
wavePlayer.Init(mixer);

Mixer WaveFormat after this code is IeeeFloat, 16000 sample rate, 2 channels, 32 bitsPerSample.
When raw data coming I check if samples come from a new channel, if yes I add to mixer a new input stream as follow:

public void PlaySamples(int clientid, byte[] samples)
{
if (!this.ClientIsInitialized(clientid))
{
var waveProvider = new BufferedWaveProvider(this.format);
waveProvider.MaxQueuedBuffers = this.numberOfBuffers;
waveProvider.DiscardOnBufferOverflow = false;
var stream = new WaveProviderToWaveStream(waveProvider); //code of WaveProviderToWaveStream is omitted because out of scope
this.mixer.AddInputStream(new WaveChannel32(stream));
this.AddClient(clientid, waveProvider);
}
this.GetClient(clientid).AddSamples(samples, 0, samples.Length);
}

If I connect only one client and after first PlaySamples call (first sample is 3200 bytes long), mixer call Read method passing as parameter buffer 17640 bytes long. WaveChannel32 throw System.ArgumentException after Read, with follow message and StackTrace:
Message: "The byte buffer to bound must be 4 bytes aligned"

StackTrace:
at NAudio.Wave.WaveBuffer.BindTo(Byte[] bufferToBoundTo)  
at NAudio.Wave.WaveBuffer..ctor(Byte[] bufferToBoundTo)  
at NAudio.Wave.SampleProviders.Mono16SampleProvider.LoadNextChunk(IWaveProvider source, Int32 samplePairsRequired)  
at NAudio.Wave.WaveChannel32.Read(Byte[] destBuffer, Int32 offset, Int32 numBytes)  
at NAudio.Wave.WaveMixerStream32.Read(Byte[] buffer, Int32 offset, Int32 count)

If I use one to one configuration and init wavePlayer directly with WaveChannel32 excluding WaveMixerStream32 ,all work fine and no exception was threw.

I'm thinking problem is due to latency (100ms) set to configure wave player that trigger read buffer size request to mixer, but I don't know how choose latency size (or adjust user specified latency) in order to avoid error.

In last trunk code of naudio, BindTo method comment raise of exception adding comment: "WaveBuffer assumes the caller knows what they are doing. We will let this pass"; I'm sure, I don't know what I'm doing :P

Thanks for your support
Regards,
Gianluca