Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NAudio - How to send sine wave only to one audio channel on jack

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/

like image 897
Chuck Sherman Avatar asked Oct 09 '22 23:10

Chuck Sherman


1 Answers

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

for (int n = 0; n < sampleCount; n += 2)
like image 67
Mark Heath Avatar answered Oct 13 '22 09:10

Mark Heath