1
0
mirror of synced 2026-05-22 14:43:19 +00:00
Files
music-metadata/lib/musepack/sv7/BitReader.ts
T
2022-02-27 15:27:40 +01:00

60 lines
1.3 KiB
TypeScript

import { ITokenizer } from 'strtok3/core';
import * as Token from 'token-types';
export class BitReader {
public pos: number = 0;
private dword: number = undefined;
public constructor(private tokenizer: ITokenizer) {
}
/**
*
* @param bits 1..30 bits
*/
public async read(bits: number): Promise<number> {
while (this.dword === undefined) {
this.dword = await this.tokenizer.readToken(Token.UINT32_LE);
}
let out = this.dword;
this.pos += bits;
if (this.pos < 32) {
out >>>= (32 - this.pos);
return out & ((1 << bits) - 1);
} else {
this.pos -= 32;
if (this.pos === 0) {
this.dword = undefined;
return out & ((1 << bits) - 1);
} else {
this.dword = await this.tokenizer.readToken(Token.UINT32_LE);
if (this.pos) {
out <<= this.pos;
out |= this.dword >>> (32 - this.pos);
}
return out & ((1 << bits) - 1);
}
}
}
public async ignore(bits: number): Promise<number> {
if (this.pos > 0) {
const remaining = 32 - this.pos;
this.dword = undefined;
bits -= remaining;
this.pos = 0;
}
const remainder = bits % 32;
const numOfWords = (bits - remainder) / 32;
await this.tokenizer.ignore(numOfWords * 4);
return this.read(remainder);
}
}