颤抖中获取样本

Get sample in tremor

由于更简单的集成(在 ESP-32 上使用),我必须在我的项目中使用 tremor 来解码 ogg vorbis。它的 docks 说:

It returns up to the specified number of bytes of decoded audio in host-endian, signed 16 bit PCM format. If the audio is multichannel, the channels are interleaved in the output buffer.

Signature: long ov_read(OggVorbis_File *vf, char *buffer, int length, int *bitstream);

现在我对如何从 char 数组中读取 16 位有符号样本感到困惑。我是否必须遵循此处的一些建议 Convert 2 char into 1 int 或做一些其他事情?

一次迭代缓冲区的两个元素。由于数据采用小端形式(根据文档),您可以直接将两个字符表示为带符号的 16 位整数,在本例中为 'short'

long numBytesRead = ov_read(vf, buffer, length, bitstream); //length is typically 4096

if( numBytesRead > 0 )
{
    for(int i=0; (i+1)<numBytesRead; i=i+2)
    {
        unsigned char high = (unsigned char)buffer[i];
        unsigned char low = (unsigned char)buffer[i + 1];

        int16_t var = (int16_t)( (low << 8) | high );
        //here var is signed 16 bit integer.
    }
}