cvorbisoggvorbis

Get sample in tremor


I have to use tremor to decode ogg vorbis in my project due to much simplier integration (to use on ESP-32). Its docks says:

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);

Now I'm confused about how to read 16-bit signed samples from char array. Do I have to follow some advices from here Convert 2 char into 1 int or do some other thing?


Solution

  • Iterate the buffer two elements at a time. Since the data is in little-endian form ( according to documentation ) you can directly represent two characters as a signed 16 bit integer, in this case '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.
        }
    }