5
votes

I took an existing mono (non-stereo) NAudio example for Visual Studio 2010 from:

http://mark-dot-net.blogspot.com/2009/10/playback-of-sine-wave-in-naudio.html

and changed it to have two channel stereo audio as shown below:

public abstract class WaveProvider32 : IWaveProvider 
{ 
  public WaveProvider32() : this(44100, 2) // Was 44100, 1
  {
  }
.
.
. 
}

When I try to place the correct sample value in the first float in buffer and a zero in the second float in buffer, I was expecting to get a sine wave on the right channel and no audio on the left.

I'm seeing the same frequency 10x lower amplitude out of phase sine wave on the left channel vs. the right channel.

Is that from some kind of signal bleed through or am I not understanding how the code should work?

Here is a sample of how I changed WaveProvider32:

public class SineWaveProvider32 : WaveProvider32 
{
.
.
.
public override int Read(float[] buffer, int offset, int sampleCount) 
{ 
  int sampleRate = WaveFormat.SampleRate; 
  for (int n = 0; n < sampleCount; n += 1) 
  { 
    buffer[n+offset] = (float)(Amplitude * Math.Sin((2 * Math.PI * sample * Frequency) /    sampleRate)); 

    buffer[n+offset+1] = (float)(0); 
    sample++; 

    if (sample >= sampleRate) 
    {
      sample = 0; 
    }
  } 
  return sampleCount; 
}
}

Any advice on what I'm doing wrong?

Regards

Note: The NAudio project is located at:

http://naudio.codeplex.com/

1
You saved me a quite a bit of time! Thanks. Unfortunately, this example plays some nasty clicks all the time. github.com/naudio/sinegenerator-sample - this example with similar concept plays a way more smoother (clicks are quieter).Artem Bobritsky
Both ways I just get clicks and no balance control (of course with the fix in the answer here). I'm wondering if this works at allErdal G.

1 Answers

4
votes

Your for loop should have += 2, not += 1.

for (int n = 0; n < sampleCount; n += 2)