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

How to send two different frequencies to left and right channel on audio jack

Sep 21, 2011 at 7:08 PM

Hi,

I saw other posts related to sending two different frequencies to the left and right audio jack
from a Desktop PC.

I took an existing mono example from:

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

and changed it to have two channel 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;
     }
}

Regards

Sep 26, 2011 at 8:05 PM

Hello,

Try this:

    public class SineWaveProvider : WaveProvider32
    {
        private int sample = 0;

        public SineWaveProvider() : base (44100, 2)
        {
            Frequency1 = 1000;
            Frequency2 = 200;
            Amplitude1 = 0.05f;
            Amplitude2 = 0.25f;
        }
       
        public float Frequency1 { get; set; }
        public float Frequency2 { get; set; }
        public float Amplitude1 { get; set; }
        public float Amplitude2 { get; set; }

        public override int Read(float[] buffer, int offset, int sampleCount)
        {
            int sampleRate = WaveFormat.SampleRate;

            double freqCoef1 = 2 * Math.PI * Frequency1;
            double freqCoef2 = 2 * Math.PI * Frequency2;
           
            for (int n = 0; n < sampleCount; n+=2)
            {
                buffer[offset++] = (float)(Amplitude1 * Math.Sin((freqCoef1 * sample) / sampleRate));
                buffer[offset++] = (float)(Amplitude2 * Math.Sin((freqCoef2 * sample) / sampleRate));
                if (++sample >= sampleRate) sample = 0;
            }
           
            return sampleCount;
        }
    }

Sep 26, 2011 at 8:23 PM
Edited Sep 27, 2011 at 2:47 AM

a little correction :-)

        public override int Read(float[] buffer, int offset, int sampleCount)
        {
            int sampleRate = WaveFormat.SampleRate * WaveFormat.Channels;

            double freqCoef1 = 2 * Math.PI * Frequency1;
            double freqCoef2 = 2 * Math.PI * Frequency2;
           
            for (int n = 0; n < sampleCount; n+=2)
            {
                buffer[offset++] = (float)(Amplitude1 * Math.Sin((freqCoef1 * sample) / sampleRate));
                buffer[offset++] = (float)(Amplitude2 * Math.Sin((freqCoef2 * sample) / sampleRate));
                if (++sample >= sampleRate) sample = 0;
            }
           
            return sampleCount;
        }