Rufus
Rufus

Reputation: 43

Why is the last byte of a WasapiLoopbackCapture sample always 3B,3C,3D,3E,BC,BC or BE

I am writing a program to display audio samples on a time/amplitude graph. I want to use the real-time audio playing on the computer and am using WasapiLoopbackCapture from the CSCore library, but the data shows on my graph always seems erroneous.

when I try to read the audio stream buffer with a bit depth of 32, the last byte of the array written to debug in

Debug.WriteLine(BitConverter.ToString(_sample));

is always 3B,3C,3D,3E,BC,BC or BE

I also found that reading the buffer as 32 bit (capture.WaveFormat.BitsPerSample returns 32) appears to show completely erroneous amplitudes on the graph whereas manually setting a bit depth
of 16 and reading the byte array with ReadInt16BigEndian appears to represent each sample somewhat accurately.

capture = new WasapiLoopbackCapture();

_bitDepth = capture.WaveFormat.BitsPerSample;
_byteDepth = bitDepth / 8;

Debug.WriteLine(bitDepth);

capture.DataAvailable += (s, a) =>
    {
        //for loop where i steps through each sample (each sample is multiple bytes)
        for(int i = 0; i < a.Buffer.Length; i += _byteDepth)
        {
            //creates new byte array with 4 bytes / 32 bits
            var _sample = new byte[_byteDepth];

            //copies 1 sample from the buffer into the _sample byte array
            Buffer.BlockCopy(a.Buffer, i, _sample,0,_byteDepth);

            Debug.WriteLine(BitConverter.ToString(_sample));
            
            //reads the byte array to an int
            var _intSample = BinaryPrimitives.ReadInt32BigEndian(_sample);

            currentData.Add(_intSample);
        }
    }; 

Upvotes: 1

Views: 83

Answers (1)

Mark Heath
Mark Heath

Reputation: 49522

WASAPI uses 32 bit floating point (float) as a sample format. It's not a 32 bit integer.

Upvotes: 2

Related Questions