1

Resolved

Wrong BitsPerSample with WasapiLoopbackCapture

description

Hello

I have a 24 bits per sample / 192kHz sound board and I am using the Wasapi Loopback Capture method to get data from it.

When I print out the capabilities of the board, it shows 32 bits per sample instead of 24 bits per sample...
        private void button2_Click(object sender, EventArgs e)
        {
            waveIn = new WasapiLoopbackCapture();
            waveIn.DataAvailable += OnDataAvailable;
            waveIn.RecordingStopped += OnRecordingStopped;
            waveIn.StartRecording();

            Console.WriteLine(waveIn.WaveFormat.BitsPerSample);
            Console.WriteLine(waveIn.WaveFormat.AverageBytesPerSecond);
            Console.WriteLine(waveIn.WaveFormat.Channels);
            Console.WriteLine(waveIn.WaveFormat.SampleRate);
            Console.WriteLine(waveIn.WaveFormat.Encoding);
        }
Which prints out...
32
1536000
2
192000
Extensible
If I check the e.Buffer data that the callback delivers, I see that most of the bytes have data indeed, but I was hoping to see 3 bytes with data and then an empty byte (24 bits and a spare byte), but all bytes have data.

So, how should I merge those bytes and in which order?

The Audio Board specs are:

Realtek Semiconductor Corp.
Audio driver 6.0.1.5919
DirectX 11.0
ALC889A

Thanks!

file attachments

comments

markheath wrote Jul 15, 2013 at 1:34 PM

WASAPI loopback capture is capturing the audio format of the Windows mixing engine, which will be IEEE float 32 bit. The bit depth your soundcard is operating at has nothing to do with this - it could be 16 or 24 bit.

Linketo wrote Jul 16, 2013 at 1:36 AM

Hello Mark, thanks for your reply.

The object names of the WaveFormat object were misleading.

OK, the data I am getting is a float variable. In what order should I merge the 4 bytes to form a float? Little endian or big endian?

Regards

Linketo wrote Jul 16, 2013 at 2:52 AM

This code is working.
                Int32 sample_count = e.BytesRecorded / (waveIn.WaveFormat.BitsPerSample / 8);
                Single[] data = new Single[sample_count];

                for (int i = 0; i < sample_count; ++i)
                {
                    data[i] = BitConverter.ToSingle(e.Buffer, i * 4);
                }

Linketo wrote Jul 21, 2013 at 6:28 PM

I forgot to mention that data[] contains the left and right channels, make sure to separate them in your application.

This is a test app that I am working on...

http://youtu.be/RhOX0fU3beg

markheath wrote Jul 22, 2013 at 10:33 AM

nice video :)

btb4198 wrote Feb 19 at 10:30 PM

Linketo,

what do you mean by " ata[] contains the left and right channels, make sure to separate them in your application."

why do you have to separate them?
how would you separate them?

markheath wrote Feb 20 at 7:40 AM

the samples are interleaved left, right, left, right. So bytes 0,1,2,3 are left sample, 4,5,6,7 are a right sample, and so on.

Linketo wrote Feb 21 at 12:37 AM

Yeap, as Mark says the samples come in that way. This is the function I used to sum both channels into one signal.
        /// <summary>
        /// This is called when audio samples are ready
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnDataAvailable(object sender, WaveInEventArgs e)
        {
            Int32 sample_count = e.BytesRecorded / (waveIn.WaveFormat.BitsPerSample / 8);
            Single[] data = new Single[sample_count];

            for (int i = 0; i < sample_count; ++i)
            {
                data[i] = BitConverter.ToSingle(e.Buffer, i * 4);
            }

            int j = 0;
            Audio_Samples = new Double[sample_count / 2];
            for (int sample = 0; sample < data.Length; sample += 2)
            {
                Audio_Samples[j] = (Double)data[sample];
                Audio_Samples[j] += (Double)data[sample + 1];
                ++j;
            }

            Data_Available = true;
        }

btb4198 wrote Feb 23 at 6:22 PM

Does your fft have a lot of noisy low frequencies ?

Linketo wrote Aug 24 at 5:20 PM

Yeah, but those values are completely expected.