Skip to content

Commit

Permalink
Merge pull request #27 from eshaz/fix-vorbis-modes
Browse files Browse the repository at this point in the history
Fix Vorbis Mode Parsing
  • Loading branch information
eshaz committed Aug 7, 2023
2 parents c0cdff1 + f78bbb7 commit 3ad0384
Show file tree
Hide file tree
Showing 63 changed files with 58,326 additions and 54,639 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ declare interface ICodecParserOptions {
onCodec?: (codec: CodecValue) => any;
onCodecUpdate?: (
codecHeaderData: CodecHeader,
updateTimestamp: number
updateTimestamp: number,
) => any;
enableLogging?: boolean;
enableFrameCRC32?: boolean;
}

declare class CodecParser<
T extends CodecFrame | OggPage = CodecFrame | OggPage
T extends CodecFrame | OggPage = CodecFrame | OggPage,
> {
public readonly codec: CodecValue;

Expand Down
1,094 changes: 566 additions & 528 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codec-parser",
"version": "2.4.2",
"version": "2.4.3",
"description": "Library that parses raw data from audio codecs into frames containing data, header values, duration, and other information.",
"main": "index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -37,8 +37,8 @@
"sideEffects": false,
"homepage": "https://github.com/eshaz/codec-parser#readme",
"devDependencies": {
"@types/jest": "^29.5.2",
"jest": "^29.5.0",
"prettier": "^2.8.8"
"@types/jest": "^29.5.3",
"jest": "^29.6.2",
"prettier": "^3.0.1"
}
}
36 changes: 21 additions & 15 deletions src/CodecParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
logError,
parseFrame,
checkCodecUpdate,
reset,
} from "./constants.js";
import HeaderCache from "./codecs/HeaderCache.js";
import MPEGParser from "./codecs/mpeg/MPEGParser.js";
Expand All @@ -59,7 +60,7 @@ export default class CodecParser {
onCodecUpdate,
enableLogging = false,
enableFrameCRC32 = true,
} = {}
} = {},
) {
this._inputMimeType = mimeType;
this._onCodec = onCodec || noOp;
Expand All @@ -68,16 +69,25 @@ export default class CodecParser {
this._enableLogging = enableLogging;
this._crc32 = enableFrameCRC32 ? crc32Function : noOp;

this._generator = this._getGenerator();
this._generator.next();
this[reset]();
}

/**
* @public
* @returns The detected codec
*/
get [codec]() {
return this._parser[codec];
return this._parser ? this._parser[codec] : "";
}

[reset]() {
this._headerCache = new HeaderCache(
this._onCodecHeader,
this._onCodecUpdate,
);

this._generator = this._getGenerator();
this._generator.next();
}

/**
Expand All @@ -95,8 +105,7 @@ export default class CodecParser {

this._flushing = false;

this._generator = this._getGenerator();
this._generator.next();
this[reset]();
}

/**
Expand Down Expand Up @@ -130,11 +139,6 @@ export default class CodecParser {
* @private
*/
*_getGenerator() {
this._headerCache = new HeaderCache(
this._onCodecHeader,
this._onCodecUpdate
);

if (this._inputMimeType.match(/aac/)) {
this._parser = new AACParser(this, this._headerCache, this._onCodec);
} else if (this._inputMimeType.match(/mpeg/)) {
Expand Down Expand Up @@ -201,7 +205,9 @@ export default class CodecParser {
this._sampleRate = frame[header][sampleRate];

frame[header][bitrate] =
Math.round(frame[data][length] / frame[duration]) * 8;
frame[duration] > 0
? Math.round(frame[data][length] / frame[duration]) * 8
: 0;
frame[frameNumber] = this._frameNumber++;
frame[totalBytesOut] = this._totalBytesOut;
frame[totalSamples] = this._totalSamples;
Expand All @@ -210,7 +216,7 @@ export default class CodecParser {

this._headerCache[checkCodecUpdate](
frame[header][bitrate],
frame[totalDuration]
frame[totalDuration],
);

this._totalBytesOut += frame[data][length];
Expand Down Expand Up @@ -256,12 +262,12 @@ export default class CodecParser {
messages.push(
`--stats--${"-".repeat(width - 9)}`,
...stats,
"-".repeat(width)
"-".repeat(width),
);

logger(
"codec-parser",
messages.reduce((acc, message) => acc + "\n " + message, "")
messages.reduce((acc, message) => acc + "\n " + message, ""),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/CodecFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class CodecFrame extends Frame {
const headerValue = yield* Header[getHeader](
codecParser,
headerCache,
readOffset
readOffset,
);

if (headerValue) {
Expand All @@ -49,7 +49,7 @@ export default class CodecFrame extends Frame {

const frame = (yield* codecParser[readRawData](
frameLengthValue,
readOffset
readOffset,
))[subarray](0, frameLengthValue);

return new Frame(headerValue, frame, samplesValue);
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/HeaderCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class HeaderCache {

// only update if codec data is available
const codecData = this._codecUpdateData.get(
this._headerCache.get(this._currentHeader)
this._headerCache.get(this._currentHeader),
);

if (this._codecShouldUpdate && codecData) {
Expand All @@ -62,7 +62,7 @@ export default class HeaderCache {
bitrate,
...codecData,
},
totalDuration
totalDuration,
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/codecs/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class Parser {
frameData = yield* this.Frame[getFrame](
this._codecParser,
this._headerCache,
0
0,
);
if (frameData) return frameData;
this._codecParser[incrementRawData](1); // increment to continue syncing
Expand All @@ -71,7 +71,7 @@ export default class Parser {
(yield* this.Header[getHeader](
this._codecParser,
this._headerCache,
frameLength
frameLength,
))
) {
this._headerCache[enable](); // start caching when synced
Expand All @@ -83,7 +83,7 @@ export default class Parser {

this._codecParser[logWarning](
`Missing ${frame} at ${frameLength} bytes from current position.`,
`Dropping current ${frame} and trying again.`
`Dropping current ${frame} and trying again.`,
);
this._headerCache[reset](); // frame is invalid and must re-sync and clear cache
this._codecParser[incrementRawData](1); // increment to invalidate the current frame
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/aac/AACFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class AACFrame extends CodecFrame {
AACFrame,
codecParser,
headerCache,
readOffset
readOffset,
);
}

Expand Down
16 changes: 8 additions & 8 deletions src/codecs/flac/FLACParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class FLACParser extends Parser {
const header = yield* FLACHeader[getHeader](
this._codecParser,
this._headerCache,
0
0,
);

if (header) {
Expand All @@ -96,12 +96,12 @@ export default class FLACParser extends Parser {
(yield* FLACHeader[getHeader](
this._codecParser,
this._headerCache,
nextHeaderOffset
nextHeaderOffset,
))
) {
// found a valid next frame header
let frameData = yield* this._codecParser[readRawData](
nextHeaderOffset
nextHeaderOffset,
);

if (!this._codecParser._flushing)
Expand All @@ -121,18 +121,18 @@ export default class FLACParser extends Parser {
}

nextHeaderOffset = yield* this._getNextFrameSyncOffset(
nextHeaderOffset + 1
nextHeaderOffset + 1,
);
}

this._codecParser[logWarning](
`Unable to sync FLAC frame after searching ${nextHeaderOffset} bytes.`
`Unable to sync FLAC frame after searching ${nextHeaderOffset} bytes.`,
);
this._codecParser[incrementRawData](nextHeaderOffset);
} else {
// not synced, increment data to continue syncing
this._codecParser[incrementRawData](
yield* this._getNextFrameSyncOffset(1)
yield* this._getNextFrameSyncOffset(1),
);
}
} while (true);
Expand All @@ -152,15 +152,15 @@ export default class FLACParser extends Parser {
[segments].map((segment) => {
const header = FLACHeader[getHeaderFromUint8Array](
segment,
this._headerCache
this._headerCache,
);

if (header) {
return new FLACFrame(segment, header, this._streamInfo);
} else {
this._codecParser[logWarning](
"Failed to parse Ogg FLAC frame",
"Skipping invalid FLAC frame"
"Skipping invalid FLAC frame",
);
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/mpeg/MPEGFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class MPEGFrame extends CodecFrame {
MPEGFrame,
codecParser,
headerCache,
readOffset
readOffset,
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/codecs/mpeg/MPEGHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export default class MPEGHeader extends CodecHeader {
const id3v2Header = yield* ID3v2.getID3v2Header(
codecParser,
headerCache,
readOffset
readOffset,
);

if (id3v2Header) {
Expand Down Expand Up @@ -298,7 +298,7 @@ export default class MPEGHeader extends CodecHeader {

header[frameLength] = Math.floor(
(125 * header[bitrate] * header[samples]) / header[sampleRate] +
header[framePadding]
header[framePadding],
);
if (!header[frameLength]) return null;

Expand Down
2 changes: 1 addition & 1 deletion src/codecs/opus/OpusFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class OpusFrame extends CodecFrame {
super(
header,
data,
((header[frameSize] * header[frameCount]) / 1000) * header[sampleRate]
((header[frameSize] * header[frameCount]) / 1000) * header[sampleRate],
);
}
}
4 changes: 2 additions & 2 deletions src/codecs/opus/OpusParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export default class OpusParser extends Parser {
const header = OpusHeader[getHeaderFromUint8Array](
this._identificationHeader,
segment,
this._headerCache
this._headerCache,
);

if (header) return new OpusFrame(segment, header);

this._codecParser[logError](
"Failed to parse Ogg Opus Header",
"Not a valid Ogg Opus file"
"Not a valid Ogg Opus file",
);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/vorbis/VorbisHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class VorbisHeader extends CodecHeader {
dataValue,
headerCache,
vorbisCommentsData,
vorbisSetupData
vorbisSetupData,
) {
// Must be at least 30 bytes.
if (dataValue[length] < 30)
Expand Down
Loading

0 comments on commit 3ad0384

Please sign in to comment.