Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript Fixes #112

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/ogg-opus-decoder/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/ogg-opus-decoder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ogg-opus-decoder",
"version": "1.6.13",
"version": "1.6.14",
"description": "Web Assembly streaming Ogg Opus decoder",
"type": "module",
"main": "./index.js",
Expand Down Expand Up @@ -55,6 +55,6 @@
"dependencies": {
"@wasm-audio-decoders/common": "9.0.5",
"codec-parser": "2.5.0",
"opus-decoder": "0.7.6"
"opus-decoder": "0.7.7"
}
}
1 change: 1 addition & 0 deletions src/opus-decoder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ Class that decodes Opus frames asynchronously within a web worker. Decoding is p
```javascript
const decoder = new OpusDecoderWebWorker({
forceStereo: false,
sampleRate: 48000,
channels: 2,
streamCount: 1,
coupledStreamCount: 1,
Expand Down
4 changes: 2 additions & 2 deletions src/opus-decoder/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/opus-decoder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opus-decoder",
"version": "0.7.6",
"version": "0.7.7",
"description": "Web Assembly streaming Opus decoder",
"type": "module",
"main": "./index.js",
Expand Down
52 changes: 44 additions & 8 deletions src/opus-decoder/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { DecodeError } from "@wasm-audio-decoders/common";

export interface OpusDecodedAudio {
export type OpusDecoderDefaultSampleRate = 48000;
export type OpusDecoderSampleRate =
| 8000
| 12000
| 16000
| 24000
| OpusDecoderDefaultSampleRate;

export interface OpusDecodedAudio<
SampleRate extends OpusDecoderSampleRate = OpusDecoderDefaultSampleRate,
> {
channelData: Float32Array[];
samplesDecoded: number;
sampleRate: 48000;
sampleRate: SampleRate;
errors: DecodeError[];
}

export class OpusDecoder {
export class OpusDecoder<
SampleRate extends OpusDecoderSampleRate | undefined = undefined,
> {
constructor(options?: {
forceStereo?: boolean;
sampleRate?: SampleRate;
preSkip?: number;
channels?: number;
streamCount?: number;
Expand All @@ -19,13 +32,24 @@ export class OpusDecoder {
ready: Promise<void>;
reset: () => Promise<void>;
free: () => void;
decodeFrame: (opusFrame: Uint8Array) => OpusDecodedAudio;
decodeFrames: (opusFrames: Uint8Array[]) => OpusDecodedAudio;
decodeFrame: (
opusFrame: Uint8Array,
) => OpusDecodedAudio<
SampleRate extends undefined ? OpusDecoderDefaultSampleRate : SampleRate
>;
decodeFrames: (
opusFrames: Uint8Array[],
) => OpusDecodedAudio<
SampleRate extends undefined ? OpusDecoderDefaultSampleRate : SampleRate
>;
}

export class OpusDecoderWebWorker {
export class OpusDecoderWebWorker<
SampleRate extends OpusDecoderSampleRate | undefined = undefined,
> {
constructor(options?: {
forceStereo?: boolean;
sampleRate?: SampleRate;
preSkip?: number;
channels?: number;
streamCount?: number;
Expand All @@ -35,8 +59,20 @@ export class OpusDecoderWebWorker {
ready: Promise<void>;
reset: () => Promise<void>;
free: () => Promise<void>;
decodeFrame: (opusFrame: Uint8Array) => Promise<OpusDecodedAudio>;
decodeFrames: (opusFrames: Uint8Array[]) => Promise<OpusDecodedAudio>;
decodeFrame: (
opusFrame: Uint8Array,
) => Promise<
OpusDecodedAudio<
SampleRate extends undefined ? OpusDecoderDefaultSampleRate : SampleRate
>
>;
decodeFrames: (
opusFrames: Uint8Array[],
) => Promise<
OpusDecodedAudio<
SampleRate extends undefined ? OpusDecoderDefaultSampleRate : SampleRate
>
>;
}

export { DecodeError };
25 changes: 25 additions & 0 deletions test/wasm-audio-decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ const oggOpusDecoder: OggOpusDecoder = new OggOpusDecoder();
const oggOpusDecoderWebWorker: OggOpusDecoderWebWorker =
new OggOpusDecoderWebWorker();

// test opus decoder sample rates
const opusDecoder8000 = new OpusDecoder({
sampleRate: 8000,
});
const opusDecoderWebWorker8000 = new OpusDecoderWebWorker({
sampleRate: 8000,
});

const opusDecoderDecode8000 = opusDecoder8000.decodeFrame(fakeData);
const opusDecoderDecodeSampleRate8000: number =
opusDecoderDecode8000.sampleRate;

let opusDecoder12000: OpusDecoder<12000>;
opusDecoder12000 = new OpusDecoder({
sampleRate: 12000,
});
let opusDecoderWebWorker12000: OpusDecoderWebWorker<12000>;
opusDecoderWebWorker12000 = new OpusDecoderWebWorker({
sampleRate: 12000,
});

const opusDecoderDecode12000 = opusDecoder12000.decodeFrame(fakeData);
const opusDecoderDecodeSampleRate12000: number =
opusDecoderDecode12000.sampleRate;

const flacDecoder: FLACDecoder = new FLACDecoder();
const flacDecoderWebWorker: FLACDecoderWebWorker = new FLACDecoderWebWorker();

Expand Down