mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-28 03:13:51 +02:00
Code review fixes
This commit is contained in:
+20
-10
@@ -13,23 +13,33 @@ export const readNextMp3FrameHeader = async (reader: Reader, startPos: number, u
|
||||
header: Mp3FrameHeader;
|
||||
startPos: number;
|
||||
} | null> => {
|
||||
const CHUNK_SIZE = 2 ** 16; // So we don't need to grab thousands of slices
|
||||
let currentPos = startPos;
|
||||
|
||||
// todo optimize this shit wtf is this
|
||||
|
||||
while (until === null || currentPos < until) {
|
||||
let slice = reader.requestSlice(currentPos, FRAME_HEADER_SIZE);
|
||||
const maxLength = until !== null
|
||||
? Math.min(CHUNK_SIZE, until - currentPos)
|
||||
: CHUNK_SIZE;
|
||||
|
||||
let slice = reader.requestSliceRange(currentPos, FRAME_HEADER_SIZE, maxLength);
|
||||
if (slice instanceof Promise) slice = await slice;
|
||||
if (!slice) break;
|
||||
if (!slice || slice.length < FRAME_HEADER_SIZE) break;
|
||||
|
||||
const word = readU32Be(slice);
|
||||
while (slice.remainingLength >= FRAME_HEADER_SIZE) {
|
||||
const posBeforeRead = slice.filePos;
|
||||
const word = readU32Be(slice);
|
||||
const remainingBytes = reader.fileSize !== null
|
||||
? reader.fileSize - currentPos
|
||||
: null;
|
||||
|
||||
const result = readMp3FrameHeader(word, reader.fileSize !== null ? reader.fileSize - currentPos : null);
|
||||
if (result.header) {
|
||||
return { header: result.header, startPos: currentPos };
|
||||
const result = readMp3FrameHeader(word, remainingBytes);
|
||||
if (result.header) {
|
||||
return { header: result.header, startPos: currentPos };
|
||||
}
|
||||
|
||||
slice.filePos = posBeforeRead + result.bytesAdvanced;
|
||||
currentPos = slice.filePos;
|
||||
}
|
||||
|
||||
currentPos += result.bytesAdvanced;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user