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

Playing sound with AsioOut very noisy and distortion

Mar 22, 2015 at 3:53 PM
I'm trying to take the sound from one AsioOut Input and put it to an output. My first try was to save it in a wave file, that's no problem.
When i pass the bytes from the input into the BufferedWaveProvider which is passed to the AsioOut output object at initialisation i get a very high distortion on my audio signal.
Poorly i can't figure out whats wrong while playing the signal, because the signal in the recorded wave file is ok.

That's my initialisation of all objects
int nSource = int.Parse(txtSourceChannel.Text);
            int nDest = int.Parse(txtDestChannel.Text);

            buffer = new BufferedWaveProvider(wFormat);
            buffer.DiscardOnBufferOverflow = true;

            input = new AsioOut(cmbAsioDrivers.SelectedItem.ToString());
            input.InputChannelOffset = nSource;
            input.InitRecordAndPlayback(buffer, 1, 44100);
            input.AudioAvailable += asioOut_AudioAvailable;
            input.PlaybackStopped += input_PlaybackStopped;
            output = new AsioOut(cmbAsioDrivers.SelectedItem.ToString());
            output.ChannelOffset = 1;

            this.wavWriter = new WaveFileWriter(@"D:\test.wav", new WaveFormat(44100, 24, 1));

Thats the data available event handler of the asioOut input object
private readonly byte[] value24 = new byte[3];
        void asioOut_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
            float[] samples = new float[e.SamplesPerBuffer];
            byte[] buf = new byte[e.SamplesPerBuffer];

            for (int i = 0; i < e.InputBuffers.Length; i++)
                Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer);
                Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer);

            int count = e.GetAsInterleavedSamples(samples);

            //wavWriter.WriteSamples(samples, 0, samples.Length);       //works, result is ok

            for (int s = 0; s < count; s++)
                byte[] bytes = BitConverter.GetBytes((Int32)(Int32.MaxValue * samples[s]));
                //Array.Copy(buf, s * 4, bytes, 0, 4);
                buffer.AddSamples(bytes, 0, 4);

                value24[0] = bytes[1];
                value24[1] = bytes[2];
                value24[2] = bytes[3];
                wavWriter.Write(value24, 0, 3);

            //buffer.AddSamples(buf, 0, buf.Length);

            e.WrittenToOutputBuffers = true; 
When i try to copy the input buffer to the output buffer of the same asioOut input object i can hear the signal very low and there is some distortion on the signal to (like a flanger effect).

P.S. when i try to play a mp3 or wave file with the AudioFileReader and pass the reader ti the asioOut output object instead of BufferedWaveProvider there are no problems while playing.