Skip to content

Commit 15506fd

Browse files
committed
chore: rewrite
1 parent 4555b9f commit 15506fd

File tree

29 files changed

+731
-425
lines changed

29 files changed

+731
-425
lines changed

.github/renovate.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3-
"extends": ["config:base", "group:allNonMajor", ":preserveSemverRanges", ":disablePeerDependencies"],
3+
"extends": [
4+
"config:base",
5+
"group:allNonMajor",
6+
":preserveSemverRanges",
7+
":disablePeerDependencies"
8+
],
49
"labels": ["dependencies"],
510
"packageRules": [
611
{

.github/workflows/lint.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
branches:
66
- main
77
tags-ignore:
8-
- '**'
8+
- "**"
99
pull_request:
1010
concurrency:
1111
group: ${{ github.workflow }}-${{ github.ref }}
@@ -21,7 +21,7 @@ jobs:
2121
uses: actions/setup-node@v4
2222
with:
2323
node-version: 20
24-
cache: 'yarn'
24+
cache: "yarn"
2525

2626
- name: Install
2727
uses: dtolnay/rust-toolchain@stable

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,25 @@ The default probe size is `2MB`, but you can adjust this as needed by passing th
5656

5757
```js
5858
// probe only 1024 bytes
59-
const { result } = await mediaplex.probeStream(stream, 1024)
59+
const { result } = await mediaplex.probeStream(stream, 1024);
6060

6161
// probe 5 MB
62-
const { result } = await mediaplex.probeStream(stream, 5 * 1024 * 1024)
62+
const { result } = await mediaplex.probeStream(stream, 5 * 1024 * 1024);
6363
```
6464

6565
## Opus Encoder
6666

6767
Mediaplex also includes an Opus encoder/decoder, which can be used as a drop-in replacement for [`@discordjs/opus`](https://github.com/discordjs/opus). Here's an example on how to use it:
6868

6969
```js
70-
const { OpusEncoder, getOpusVersion } = require('mediaplex')
70+
const { OpusEncoder, getOpusVersion } = require("mediaplex");
7171

72-
console.log(getOpusVersion()) // libopus xxx
72+
console.log(getOpusVersion()); // libopus xxx
7373

74-
const encoder = new OpusEncoder(48000, 2)
74+
const encoder = new OpusEncoder(48000, 2);
7575

76-
const encoded = encoder.encode(buffer)
77-
const decoded = encoder.decode(encoded)
76+
const encoded = encoder.encode(buffer);
77+
const decoded = encoder.decode(encoded);
7878
```
7979

8080
You can use `OpusEncoder` to encode pcm data to opus and decode opus data to pcm format. Stream interface is provided by [@discord-player/opus](https://npm.im/@discord-player/opus) package.

__test__/index.spec.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import test from 'ava'
1+
import test from "ava";
22

3-
test('sum from native', (t) => {
4-
t.pass()
5-
})
3+
test("sum from native", (t) => {
4+
t.pass();
5+
});

benchmark/common.mjs

+43-28
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,74 @@
1-
import { readFileSync } from 'node:fs'
2-
import djs from '@discordjs/opus'
3-
import opusscript from 'opusscript'
4-
import mediaplex from '../index.js'
5-
import * as evanOpus from '@evan/opus'
6-
import * as evanOpusWasm from '@evan/opus/wasm/index.mjs'
7-
import * as simdEvanOpus from '@evan/wasm/target/opus/node.mjs'
1+
import { readFileSync } from "node:fs";
2+
import djs from "@discordjs/opus";
3+
import opusscript from "opusscript";
4+
import mediaplex from "../index.js";
5+
import * as evanOpus from "@evan/opus";
6+
import * as evanOpusWasm from "@evan/opus/wasm/index.mjs";
7+
import * as simdEvanOpus from "@evan/wasm/target/opus/node.mjs";
88

99
export const generatePCMSample = (sampleSize) => {
10-
return readFileSync(new URL(`./data/sample.pcm`, import.meta.url)).subarray(0, sampleSize)
11-
}
10+
return readFileSync(new URL(`./data/sample.pcm`, import.meta.url)).subarray(
11+
0,
12+
sampleSize,
13+
);
14+
};
1215

1316
export const generateOpusSample = () => {
14-
return readFileSync(new URL(`./data/sample.opus`, import.meta.url))
15-
}
17+
return readFileSync(new URL(`./data/sample.opus`, import.meta.url));
18+
};
1619

17-
export const createMediaplexEncoder = (config) => new mediaplex.OpusEncoder(config.SAMPLE_RATE, config.CHANNELS)
18-
export const createDjsEncoder = (config) => new djs.OpusEncoder(config.SAMPLE_RATE, config.CHANNELS)
20+
export const createMediaplexEncoder = (config) =>
21+
new mediaplex.OpusEncoder(config.SAMPLE_RATE, config.CHANNELS);
22+
export const createDjsEncoder = (config) =>
23+
new djs.OpusEncoder(config.SAMPLE_RATE, config.CHANNELS);
1924
export const createOpusScriptWasmEncoder = (config) =>
20-
new opusscript(config.SAMPLE_RATE, config.CHANNELS, opusscript.Application.AUDIO, {
21-
wasm: true,
22-
})
25+
new opusscript(
26+
config.SAMPLE_RATE,
27+
config.CHANNELS,
28+
opusscript.Application.AUDIO,
29+
{
30+
wasm: true,
31+
},
32+
);
2333
export const createOpusScriptAsmEncoder = (config) =>
24-
new opusscript(config.SAMPLE_RATE, config.CHANNELS, opusscript.Application.AUDIO, {
25-
wasm: false,
26-
})
34+
new opusscript(
35+
config.SAMPLE_RATE,
36+
config.CHANNELS,
37+
opusscript.Application.AUDIO,
38+
{
39+
wasm: false,
40+
},
41+
);
2742
export const createEvanOpusEncoder = (config) =>
2843
new evanOpus.Encoder({
29-
application: 'voip',
44+
application: "voip",
3045
channels: config.CHANNELS,
3146
sample_rate: config.SAMPLE_RATE,
32-
})
47+
});
3348
export const createEvanOpusDecoder = (config) =>
3449
new evanOpus.Decoder({
3550
channels: config.CHANNELS,
3651
sample_rate: config.SAMPLE_RATE,
37-
})
52+
});
3853
export const createEvanOpusEncoderWasm = (config) =>
3954
new evanOpusWasm.Encoder({
40-
application: 'voip',
55+
application: "voip",
4156
channels: config.CHANNELS,
4257
sample_rate: config.SAMPLE_RATE,
43-
})
58+
});
4459
export const createEvanOpusDecoderWasm = (config) =>
4560
new evanOpusWasm.Decoder({
4661
channels: config.CHANNELS,
4762
sample_rate: config.SAMPLE_RATE,
48-
})
63+
});
4964
export const createSimdEvanOpusEncoder = (config) =>
5065
new simdEvanOpus.Encoder({
51-
application: 'voip',
66+
application: "voip",
5267
channels: config.CHANNELS,
5368
sample_rate: config.SAMPLE_RATE,
54-
})
69+
});
5570
export const createSimdEvanOpusDecoder = (config) =>
5671
new simdEvanOpus.Decoder({
5772
channels: config.CHANNELS,
5873
sample_rate: config.SAMPLE_RATE,
59-
})
74+
});

benchmark/decoder.mjs

+34-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as mitata from 'mitata'
1+
import * as mitata from "mitata";
22

33
// prettier-ignore
44
import {
@@ -16,40 +16,40 @@ const config = {
1616
FRAME_SIZE: 960,
1717
SAMPLE_RATE: 48000,
1818
CHANNELS: 2,
19-
}
19+
};
2020

21-
const mediaplexEncoder = createMediaplexEncoder(config)
22-
const nativeEncoder = createDjsEncoder(config)
23-
const wasmEncoder = createOpusScriptWasmEncoder(config)
24-
const asmEncoder = createOpusScriptAsmEncoder(config)
25-
const evanOpus = createEvanOpusDecoder(config)
26-
const evanOpusWasm = createEvanOpusDecoderWasm(config)
27-
const evanWasmOpus = createSimdEvanOpusDecoder(config)
21+
const mediaplexEncoder = createMediaplexEncoder(config);
22+
const nativeEncoder = createDjsEncoder(config);
23+
const wasmEncoder = createOpusScriptWasmEncoder(config);
24+
const asmEncoder = createOpusScriptAsmEncoder(config);
25+
const evanOpus = createEvanOpusDecoder(config);
26+
const evanOpusWasm = createEvanOpusDecoderWasm(config);
27+
const evanWasmOpus = createSimdEvanOpusDecoder(config);
2828

29-
const SAMPLE = generateOpusSample()
29+
const SAMPLE = generateOpusSample();
3030

31-
mitata.group('OpusDecoder', () => {
32-
mitata.bench('mediaplex', () => {
33-
mediaplexEncoder.decode(SAMPLE)
34-
})
35-
mitata.bench('@discordjs/opus', () => {
36-
nativeEncoder.decode(SAMPLE)
37-
})
38-
mitata.bench('opusscript', () => {
39-
wasmEncoder.decode(SAMPLE)
40-
})
41-
mitata.bench('opusscript (no wasm)', () => {
42-
asmEncoder.decode(SAMPLE)
43-
})
44-
mitata.bench('@evan/opus', () => {
45-
evanOpus.decode(SAMPLE)
46-
})
47-
mitata.bench('@evan/opus (wasm)', () => {
48-
evanOpusWasm.decode(SAMPLE)
49-
})
50-
mitata.bench('@evan/wasm', () => {
51-
evanWasmOpus.decode(SAMPLE)
52-
})
53-
})
31+
mitata.group("OpusDecoder", () => {
32+
mitata.bench("mediaplex", () => {
33+
mediaplexEncoder.decode(SAMPLE);
34+
});
35+
mitata.bench("@discordjs/opus", () => {
36+
nativeEncoder.decode(SAMPLE);
37+
});
38+
mitata.bench("opusscript", () => {
39+
wasmEncoder.decode(SAMPLE);
40+
});
41+
mitata.bench("opusscript (no wasm)", () => {
42+
asmEncoder.decode(SAMPLE);
43+
});
44+
mitata.bench("@evan/opus", () => {
45+
evanOpus.decode(SAMPLE);
46+
});
47+
mitata.bench("@evan/opus (wasm)", () => {
48+
evanOpusWasm.decode(SAMPLE);
49+
});
50+
mitata.bench("@evan/wasm", () => {
51+
evanWasmOpus.decode(SAMPLE);
52+
});
53+
});
5454

55-
await mitata.run()
55+
await mitata.run();

benchmark/encoder.mjs

+34-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as mitata from 'mitata'
1+
import * as mitata from "mitata";
22
// prettier-ignore
33
import {
44
createDjsEncoder,
@@ -15,40 +15,40 @@ const config = {
1515
FRAME_SIZE: 960,
1616
SAMPLE_RATE: 48000,
1717
CHANNELS: 2,
18-
}
18+
};
1919

20-
const mediaplexEncoder = createMediaplexEncoder(config)
21-
const nativeEncoder = createDjsEncoder(config)
22-
const wasmEncoder = createOpusScriptWasmEncoder(config)
23-
const asmEncoder = createOpusScriptAsmEncoder(config)
24-
const evanOpus = createEvanOpusEncoder(config)
25-
const evanOpusWasm = createEvanOpusEncoderWasm(config)
26-
const evanWasmOpus = createSimdEvanOpusEncoder(config)
20+
const mediaplexEncoder = createMediaplexEncoder(config);
21+
const nativeEncoder = createDjsEncoder(config);
22+
const wasmEncoder = createOpusScriptWasmEncoder(config);
23+
const asmEncoder = createOpusScriptAsmEncoder(config);
24+
const evanOpus = createEvanOpusEncoder(config);
25+
const evanOpusWasm = createEvanOpusEncoderWasm(config);
26+
const evanWasmOpus = createSimdEvanOpusEncoder(config);
2727

28-
const SAMPLE = generatePCMSample(config.FRAME_SIZE * config.CHANNELS * 6)
28+
const SAMPLE = generatePCMSample(config.FRAME_SIZE * config.CHANNELS * 6);
2929

30-
mitata.group('OpusEncoder', () => {
31-
mitata.bench('mediaplex', () => {
32-
mediaplexEncoder.encode(SAMPLE)
33-
})
34-
mitata.bench('@discordjs/opus', () => {
35-
nativeEncoder.encode(SAMPLE)
36-
})
37-
mitata.bench('opusscript', () => {
38-
wasmEncoder.encode(SAMPLE, config.FRAME_SIZE)
39-
})
40-
mitata.bench('opusscript (no wasm)', () => {
41-
asmEncoder.encode(SAMPLE, config.FRAME_SIZE)
42-
})
43-
mitata.bench('@evan/opus', () => {
44-
evanOpus.encode(SAMPLE)
45-
})
46-
mitata.bench('@evan/opus (wasm)', () => {
47-
evanOpusWasm.encode(SAMPLE)
48-
})
49-
mitata.bench('@evan/wasm', () => {
50-
evanWasmOpus.encode(SAMPLE)
51-
})
52-
})
30+
mitata.group("OpusEncoder", () => {
31+
mitata.bench("mediaplex", () => {
32+
mediaplexEncoder.encode(SAMPLE);
33+
});
34+
mitata.bench("@discordjs/opus", () => {
35+
nativeEncoder.encode(SAMPLE);
36+
});
37+
mitata.bench("opusscript", () => {
38+
wasmEncoder.encode(SAMPLE, config.FRAME_SIZE);
39+
});
40+
mitata.bench("opusscript (no wasm)", () => {
41+
asmEncoder.encode(SAMPLE, config.FRAME_SIZE);
42+
});
43+
mitata.bench("@evan/opus", () => {
44+
evanOpus.encode(SAMPLE);
45+
});
46+
mitata.bench("@evan/opus (wasm)", () => {
47+
evanOpusWasm.encode(SAMPLE);
48+
});
49+
mitata.bench("@evan/wasm", () => {
50+
evanWasmOpus.encode(SAMPLE);
51+
});
52+
});
5353

54-
await mitata.run()
54+
await mitata.run();

index.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { ProbeResult } from './js-binding';
2-
import type { Readable } from 'stream';
1+
import type { ProbeResult } from "./js-binding";
2+
import type { Readable } from "stream";
33

44
export class OpusEncoder {
55
public constructor(sampleRate: number, channels: number);
@@ -22,7 +22,7 @@ export {
2222
getOpusVersion,
2323
probe,
2424
probeSync,
25-
} from './js-binding';
25+
} from "./js-binding";
2626

2727
export type StreamProbeResult = {
2828
stream: Readable;
@@ -53,6 +53,6 @@ export interface ProbeStreamOptions {
5353

5454
export function probeStream(
5555
stream: Readable,
56-
options?: ProbeStreamOptions
56+
options?: ProbeStreamOptions,
5757
): Promise<StreamProbeResult>;
5858
export function readMetadata(result: ProbeResult): Metadata;

0 commit comments

Comments
 (0)