@echogarden/wave-codec
Version:
A fully-featured WAVE format encoder and decoder. Written in pure TypeScript.
33 lines • 1.18 kB
JavaScript
export function encodeAscii(asciiString) {
const len = asciiString.length;
const buffer = new Uint8Array(len);
for (let readOffset = 0; readOffset < len; readOffset++) {
const charCode = asciiString.charCodeAt(readOffset);
if (charCode >= 128) {
throw new Error(`Character '${asciiString[readOffset]}' (code: ${charCode}) at offset ${readOffset} can't be encoded as a standard ASCII character`);
}
buffer[readOffset] = charCode;
}
return buffer;
}
export function decodeAscii(buffer) {
const maxChunkSize = 2 ** 24;
const decoder = new ChunkedAsciiDecoder();
for (let readOffset = 0; readOffset < buffer.length; readOffset += maxChunkSize) {
const chunk = buffer.subarray(readOffset, readOffset + maxChunkSize);
decoder.writeChunk(chunk);
}
return decoder.toString();
}
export class ChunkedAsciiDecoder {
str = '';
textDecoder = new TextDecoder('windows-1252');
writeChunk(chunk) {
const decodedChunk = this.textDecoder.decode(chunk);
this.str += decodedChunk;
}
toString() {
return this.str;
}
}
//# sourceMappingURL=Ascii.js.map