How to get sample rate by Ogg Vorbis Byte Buffer

1.3k Views Asked by At

I'm looking for the byte range in a OggVorbis header that contains the sample rate. In the specification is written that this information is in the identification header

The specification here shows the identification header:

It says, that the sample rate is found in byte 12-15. But how can I parse a byte array of an oggVorbis file to get this sample rate?

I thought the identification header must be the first block in the byte array and then the sample rate should be a Integer in Byte 12-15:

const buf = buffer.slice(12, 15);
const test = new Uint32Array(buf);

But it does not work.

Note: I don't want to use the Audio API to get the sample rate.

1

There are 1 best solutions below

0
On BEST ANSWER

According to the specification, the sample rate is the byte range between 40 and 44. The bit rate is in 48 and 52:

enter image description here

All you have to do is to slice from ArrayBuffer and write it in an Iteger:

// get sample rate
var bufferPart = buffer.slice(40, 48);
var bufferView = new Uint32Array(bufferPart);
var samplerate = bufferView[0];

// get bit rate
const bufferPart = buffer.slice(48, 52);
var bufferView = new Uint32Array(bufferPart);
var bitrate = bufferView[0];