fix(flac): add FLAC frame-sync scan to getAlignedSegmentSize; extend IFormatDecoder rawData param
StreamDecoder peeks candidate bytes; FlacFormatDecoder scans backward for 0xFF/0xF8 sync. Fixes mid-stream decode failure where segments started mid-frame.
This commit is contained in:
@@ -27,8 +27,7 @@ export class FlacFormatDecoder implements IFormatDecoder {
|
||||
if (buf.length < 4) return null;
|
||||
if (buf[0] !== FLAC_MAGIC[0] || buf[1] !== FLAC_MAGIC[1] ||
|
||||
buf[2] !== FLAC_MAGIC[2] || buf[3] !== FLAC_MAGIC[3]) {
|
||||
console.warn('FlacFormatDecoder: invalid fLaC magic — corrupt stream');
|
||||
return null;
|
||||
return null; // silently; StreamDecoder will error when MAX_HEADER_SEARCH_BYTES is exhausted
|
||||
}
|
||||
|
||||
let sampleRate = 0;
|
||||
@@ -123,16 +122,42 @@ export class FlacFormatDecoder implements IFormatDecoder {
|
||||
info: FormatInfo,
|
||||
availableBytes: number,
|
||||
requestedSize: number,
|
||||
streamComplete: boolean
|
||||
streamComplete: boolean,
|
||||
rawData?: Uint8Array
|
||||
): number {
|
||||
// FLAC frames vary in size and can't be aligned without scanning. Use a minimum
|
||||
// threshold so the wrapped segment carries at least one complete block; the browser
|
||||
// FLAC decoder tolerates a partial leading frame once STREAMINFO is prepended.
|
||||
const minSize = 16384; // 16 KB — covers one block at typical 44100/24-bit/4096-sample settings.
|
||||
|
||||
if (availableBytes === 0) return 0;
|
||||
if (!streamComplete && availableBytes < minSize) return 0;
|
||||
return Math.min(requestedSize, availableBytes);
|
||||
const candidate = Math.min(requestedSize, availableBytes);
|
||||
|
||||
if (!rawData || rawData.length === 0) {
|
||||
// No scan data — conservative threshold to avoid tiny unusable segments
|
||||
if (!streamComplete && availableBytes < 16384) return 0;
|
||||
return candidate;
|
||||
}
|
||||
|
||||
// Scan backward from the candidate boundary to find the last FLAC frame sync code.
|
||||
const boundary = FlacFormatDecoder.findLastFlacFrame(rawData, candidate);
|
||||
if (boundary <= 0) {
|
||||
if (streamComplete) return candidate; // flush remaining bytes (stream done)
|
||||
return 0; // wait for more data
|
||||
}
|
||||
return boundary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan backward from `maxBytes` in `rawData` to find the start of the last valid FLAC
|
||||
* audio frame. FLAC frame sync: 0xFF followed by a byte where top 7 bits are 0xF8
|
||||
* (i.e. (byte & 0xFE) === 0xF8 — covers both blocking-strategy variants 0xF8 and 0xF9).
|
||||
* Returns the byte offset of that sync, or 0 if none is found (causes caller to wait).
|
||||
*/
|
||||
private static findLastFlacFrame(rawData: Uint8Array, maxBytes: number): number {
|
||||
const limit = Math.min(maxBytes, rawData.length);
|
||||
// Need at least 2 bytes to verify sync pair; skip the very last byte.
|
||||
for (let i = limit - 2; i > 0; i--) {
|
||||
if (rawData[i] === 0xFF && (rawData[i + 1] & 0xFE) === 0xF8) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
wrapSegment(info: FormatInfo, rawBytes: Uint8Array): Uint8Array {
|
||||
|
||||
Reference in New Issue
Block a user