LichKing
LichKing

Reputation: 265

Converting 16 bit audio sample to char

I am using matrix creator board and I am trying to make a python wrapper for their mic array C++ code.

The library has a read function that returns a uint16_t array of 128 samples.

I'm trying to compare it to ALSA readi function that writes the buffer to a char array that the user passes as an argument to the function.

The question is how does ALSA write 16-bit samples to char array when single char is only 8 bit wide? And how could I do the same with uint16_t array and pass it to python so I'll get the same result as with ALSA readi function?

Upvotes: 0

Views: 217

Answers (1)

Kyle54
Kyle54

Reputation: 133

A uint16 can be passed as 2 bytes (uint8), the c++ code probably uses a union so that the data can be saved as a uint16 while the USB code can pass the data as bytes.

As for the python code, this is the function I use to convert 8-bit bytes to uint16

def convert_int8_int16(self, byte_array):
    new_array = [0]*(len(byte_array)/2)
    for i in range(len(byte_array)/2):
        new_array[i] = byte_array.pop(0)*256 + byte_array.pop(0)

    return new_array

This will go through the byte array and add the values to a new uint16 array.

Upvotes: 1

Related Questions