MixingSampleProvider to Mix and Output Two Buffers

Mar 18, 2013 at 11:28 PM
Edited Mar 18, 2013 at 11:29 PM
Hello,

I've got a WaveStream object whose Read method populates buffers for sinewave signal in one instance and static sample array in the other instance. Now I'd like to mix together and output via DirectSoundOut. Since I only need mono for each buffer, I thought perhaps to populate L/R channels with respective samples, but that seems ugly. Any applicable examples for which I could leverage MixingSampleProvider? Just not sure how it interfaces with DirectSoundOut. Would we even require WaveStream?

Thanks much...
Coordinator
Mar 19, 2013 at 11:13 AM
There are various SampleToWaveProvider classes in NAudio to allow you to pass your sample provider into DirectSoundOut. In the next version of NAudio, I intend for all WaveOut devices to accept a SampleProvider directly to make this a bit less confusiing
Mar 19, 2013 at 6:39 PM
Edited Mar 19, 2013 at 6:39 PM
Ok that being stated, I'm trying to get a proof of concept before I expend much more time on development. Trying to write to Left and Right channels of DirectSoundOut stream, however, only one frequency (that of double sine) comes out both channels. Any way I can write two separate sinewaves to buffer?
       public override int Read(byte[] buffer, int offset, int count)
        {
            int samples = count / 2;
            int samplesPerWave = (int)(44100 / this.frequency * 2);
            int counter = 0;
            if (toneOutput == 0)//for sine wave tone generation
            {

                for (int i = 0; i < samples; i+=2)
                {
                    //sine wave
                    double sine = amplitude * Math.Sin(Math.PI * 2 * frequency * time);
                    double sine2 = amplitude * Math.Sin(Math.PI * 2 * (frequency*2) * time);
                    time += 1.0 / 44100;
                    short truncated = (short)Math.Round(sine * (Math.Pow(2, 15) - 1));
                    short truncated2 = (short)Math.Round(sine2 * (Math.Pow(2, 15) - 1));

                    buffer[i * 2] = (byte)(truncated & 0x00ff);
                    buffer[i * 2 + 1] = (byte)((truncated & 0xff00) >> 8);

                    buffer[(i+1) * 2] = (byte)(truncated2 & 0x00ff);
                    buffer[(i+1) * 2 + 1] = (byte)((truncated2 & 0xff00) >> 8);
Mar 19, 2013 at 11:40 PM
Edited Mar 19, 2013 at 11:41 PM
Pretty sure I found my answer in the WaveStream class. Simply override

public override WaveFormat WaveFormat
        {
            get { return new WaveFormat(44100, 16, __2__); }
        }
setting channels arg to 2