parse method
Implementation
@override
ParserTag parse(RandomAccessFile reader) {
reader.setPositionSync(0);
buffer = Buffer(randomAccessFile: reader);
// first page : contains samplerate and bitrate
// second page : contains the metadata
// it may not contains all the metadata so we would have to get them later
final pages = [
_parseUniquePage(),
_parseUniquePage(),
];
VorbisMetadata m = VorbisMetadata();
for (final page in pages) {
final content = page.data;
if (String.fromCharCodes(content.sublist(0, 8)) == "OpusHead") {
m.sampleRate = getUint32LE(content.sublist(12, 16));
m.bitrate = getUint32LE(content.sublist(13, 17));
} else if (String.fromCharCodes(content.sublist(0, 8)) == "OpusTags") {
_parseVorbisComment(content, 8, m);
} else if (String.fromCharCodes(content.sublist(0, 7)) ==
String.fromCharCodes(
[0x03, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73])) // "\x03vorbis"
{
_parseVorbisComment(content, 7, m);
} else if (String.fromCharCodes(content.sublist(0, 7)) ==
String.fromCharCodes(
[0x01, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73])) // "\x01vorbis"
{
m.sampleRate = getUint32LE(content.sublist(12, 16));
m.bitrate = getUint32LE(content.sublist(20, 24));
}
}
// we need the sample rate to calculate the duration
// it's mandatory
// we have X samples per second (the sample rate obvio)
// and a page contains exactly X samples
if ((m.duration == null || m.duration == Duration.zero) &&
m.sampleRate != null) {
OggPage? page;
while (page?.headerType != 0x04) {
page = _parseUniquePageHeader();
}
m.duration = Duration(
seconds: (page!.granulePosition - pages.first.granulePosition) ~/
m.sampleRate!,
);
}
reader.closeSync();
return m;
}