mirror of
https://github.com/darksaid98/mediaplex.git
synced 2026-12-01 14:38:45 +00:00
chore: add @evan/opus benchmark
This commit is contained in:
1 parent
c641da4187
commit
9f9d588667
13 files changed
+343
-18
No files matched your search
+3
-1
@@ -195,4 +195,6 @@ Cargo.lock
|
|||||||
!.yarn/versions
|
!.yarn/versions
|
||||||
|
|
||||||
*.node
|
*.node
|
||||||
dev/
|
dev/
|
||||||
|
./src/dasp/*
|
||||||
|
./src/decoder/*
|
||||||
@@ -7,7 +7,6 @@ version = "0.0.0"
|
|||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# dasp = { version = "0.11.0" }
|
|
||||||
napi = { git = "https://github.com/napi-rs/napi-rs", default-features = false, features = [
|
napi = { git = "https://github.com/napi-rs/napi-rs", default-features = false, features = [
|
||||||
"napi4",
|
"napi4",
|
||||||
] }
|
] }
|
||||||
|
|||||||
@@ -92,46 +92,55 @@ You can use `OpusEncoder` to encode pcm data to opus and decode opus data to pcm
|
|||||||
|
|
||||||
#### Opus Benchmarks
|
#### Opus Benchmarks
|
||||||
|
|
||||||
Mediaplex includes benchmarks for the Opus encoder/decoder. Here are the results of the benchmarks on a Windows 11 machine with an i7-8700 3.2GHz processor:
|
Mediaplex includes benchmarks for the opus encoder/decoder. Here are the results of the benchmarks on a Windows 11 machine with an i7-8700 3.2GHz processor:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
$ yarn benchmark
|
$ yarn benchmark
|
||||||
|
|
||||||
Running "OpusEncoder Benchmark" suite...
|
Running "OpusEncoder Benchmark" suite...
|
||||||
Progress: 100%
|
|
||||||
|
|
||||||
mediaplex:
|
mediaplex:
|
||||||
3 502 ops/s, ±0.84% | 16.04% slower
|
3 575 ops/s, ±0.75% | 14.82% slower
|
||||||
|
|
||||||
@discordjs/opus:
|
@discordjs/opus:
|
||||||
3 185 ops/s, ±0.17% | 23.64% slower
|
3 169 ops/s, ±0.43% | 24.49% slower
|
||||||
|
|
||||||
|
@evan/opus:
|
||||||
|
3 310 ops/s, ±0.18% | 21.13% slower
|
||||||
|
|
||||||
|
@evan/opus (wasm):
|
||||||
|
2 259 ops/s, ±0.17% | 46.18% slower
|
||||||
|
|
||||||
opusscript:
|
opusscript:
|
||||||
4 171 ops/s, ±0.34% | fastest
|
4 197 ops/s, ±0.52% | fastest
|
||||||
|
|
||||||
opusscript (no wasm):
|
opusscript (no wasm):
|
||||||
261 ops/s, ±0.85% | slowest, 93.74% slower
|
266 ops/s, ±0.55% | slowest, 93.66% slower
|
||||||
|
|
||||||
Finished 4 cases!
|
Finished 6 cases!
|
||||||
Fastest: opusscript
|
Fastest: opusscript
|
||||||
Slowest: opusscript (no wasm)
|
Slowest: opusscript (no wasm)
|
||||||
|
|
||||||
Running "OpusDecoder Benchmark" suite...
|
Running "OpusDecoder Benchmark" suite...
|
||||||
Progress: 100%
|
|
||||||
|
|
||||||
mediaplex:
|
mediaplex:
|
||||||
9 838 ops/s, ±0.38% | 16.96% slower
|
9 951 ops/s, ±0.42% | 16.12% slower
|
||||||
|
|
||||||
@discordjs/opus:
|
@discordjs/opus:
|
||||||
11 848 ops/s, ±0.40% | fastest
|
11 864 ops/s, ±0.49% | fastest
|
||||||
|
|
||||||
|
@evan/opus:
|
||||||
|
11 470 ops/s, ±0.39% | 3.32% slower
|
||||||
|
|
||||||
|
@evan/opus (wasm):
|
||||||
|
7 436 ops/s, ±0.35% | 37.32% slower
|
||||||
|
|
||||||
opusscript:
|
opusscript:
|
||||||
6 100 ops/s, ±0.23% | 48.51% slower
|
6 101 ops/s, ±0.31% | 48.58% slower
|
||||||
|
|
||||||
opusscript (no wasm):
|
opusscript (no wasm):
|
||||||
2 589 ops/s, ±0.20% | slowest, 78.15% slower
|
2 261 ops/s, ±0.24% | slowest, 80.94% slower
|
||||||
|
|
||||||
Finished 4 cases!
|
Finished 6 cases!
|
||||||
Fastest: @discordjs/opus
|
Fastest: @discordjs/opus
|
||||||
Slowest: opusscript (no wasm)
|
Slowest: opusscript (no wasm)
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import { Buffer } from 'node:buffer';
|
|||||||
import djs from '@discordjs/opus';
|
import djs from '@discordjs/opus';
|
||||||
import opusscript from 'opusscript';
|
import opusscript from 'opusscript';
|
||||||
import mediaplex from '../index.js';
|
import mediaplex from '../index.js';
|
||||||
|
import * as evanOpus from '@evan/opus';
|
||||||
|
import * as evanOpusWasm from '@evan/opus/wasm/index.mjs';
|
||||||
|
|
||||||
export const generatePCMSample = (sampleSize) => {
|
export const generatePCMSample = (sampleSize) => {
|
||||||
const buffer = Buffer.alloc(sampleSize);
|
const buffer = Buffer.alloc(sampleSize);
|
||||||
@@ -57,4 +59,22 @@ export const createDjsEncoder = (config) => new djs.OpusEncoder(config.SAMPLE_RA
|
|||||||
export const createOpusScriptWasmEncoder = (config) => new opusscript(config.SAMPLE_RATE, config.CHANNELS, opusscript.Application.AUDIO);
|
export const createOpusScriptWasmEncoder = (config) => new opusscript(config.SAMPLE_RATE, config.CHANNELS, opusscript.Application.AUDIO);
|
||||||
export const createOpusScriptAsmEncoder = (config) => new opusscript(config.SAMPLE_RATE, config.CHANNELS, opusscript.Application.AUDIO, {
|
export const createOpusScriptAsmEncoder = (config) => new opusscript(config.SAMPLE_RATE, config.CHANNELS, opusscript.Application.AUDIO, {
|
||||||
wasm: false
|
wasm: false
|
||||||
|
});
|
||||||
|
export const createEvanOpusEncoder = (config) => new evanOpus.Encoder({
|
||||||
|
application: 'voip',
|
||||||
|
channels: config.CHANNELS,
|
||||||
|
sample_rate: config.SAMPLE_RATE
|
||||||
|
});
|
||||||
|
export const createEvanOpusDecoder = (config) => new evanOpus.Decoder({
|
||||||
|
channels: config.CHANNELS,
|
||||||
|
sample_rate: config.SAMPLE_RATE
|
||||||
|
});
|
||||||
|
export const createEvanOpusEncoderWasm = (config) => new evanOpusWasm.Encoder({
|
||||||
|
application: 'voip',
|
||||||
|
channels: config.CHANNELS,
|
||||||
|
sample_rate: config.SAMPLE_RATE
|
||||||
|
});
|
||||||
|
export const createEvanOpusDecoderWasm = (config) => new evanOpusWasm.Decoder({
|
||||||
|
channels: config.CHANNELS,
|
||||||
|
sample_rate: config.SAMPLE_RATE
|
||||||
});
|
});
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import b from 'benny';
|
import b from 'benny';
|
||||||
import { createDjsEncoder, createMediaplexEncoder, createOpusScriptAsmEncoder, createOpusScriptWasmEncoder, generateOpusSample } from './common.mjs';
|
import { createDjsEncoder, createMediaplexEncoder, createOpusScriptAsmEncoder, createOpusScriptWasmEncoder, generateOpusSample, createEvanOpusDecoder, createEvanOpusDecoderWasm } from './common.mjs';
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
FRAME_SIZE: 960,
|
FRAME_SIZE: 960,
|
||||||
@@ -11,6 +11,8 @@ const mediaplexEncoder = createMediaplexEncoder(config);
|
|||||||
const nativeEncoder = createDjsEncoder(config);
|
const nativeEncoder = createDjsEncoder(config);
|
||||||
const wasmEncoder = createOpusScriptWasmEncoder(config);
|
const wasmEncoder = createOpusScriptWasmEncoder(config);
|
||||||
const asmEncoder = createOpusScriptAsmEncoder(config);
|
const asmEncoder = createOpusScriptAsmEncoder(config);
|
||||||
|
const evanOpus = createEvanOpusDecoder(config);
|
||||||
|
const evanOpusWasm = createEvanOpusDecoderWasm(config);
|
||||||
|
|
||||||
const SAMPLE = generateOpusSample();
|
const SAMPLE = generateOpusSample();
|
||||||
|
|
||||||
@@ -22,6 +24,12 @@ b.suite(
|
|||||||
b.add('@discordjs/opus', () => {
|
b.add('@discordjs/opus', () => {
|
||||||
nativeEncoder.decode(SAMPLE);
|
nativeEncoder.decode(SAMPLE);
|
||||||
}),
|
}),
|
||||||
|
b.add('@evan/opus', () => {
|
||||||
|
evanOpus.decode(SAMPLE);
|
||||||
|
}),
|
||||||
|
b.add('@evan/opus (wasm)', () => {
|
||||||
|
evanOpusWasm.decode(SAMPLE);
|
||||||
|
}),
|
||||||
b.add('opusscript', () => {
|
b.add('opusscript', () => {
|
||||||
wasmEncoder.decode(SAMPLE);
|
wasmEncoder.decode(SAMPLE);
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import b from 'benny';
|
import b from 'benny';
|
||||||
import { createDjsEncoder, createMediaplexEncoder, createOpusScriptAsmEncoder, createOpusScriptWasmEncoder, generatePCMSample } from './common.mjs';
|
import { createDjsEncoder, createMediaplexEncoder, createOpusScriptAsmEncoder, createOpusScriptWasmEncoder, generatePCMSample, createEvanOpusEncoder, createEvanOpusEncoderWasm } from './common.mjs';
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
FRAME_SIZE: 960,
|
FRAME_SIZE: 960,
|
||||||
@@ -11,6 +11,8 @@ const mediaplexEncoder = createMediaplexEncoder(config);
|
|||||||
const nativeEncoder = createDjsEncoder(config);
|
const nativeEncoder = createDjsEncoder(config);
|
||||||
const wasmEncoder = createOpusScriptWasmEncoder(config);
|
const wasmEncoder = createOpusScriptWasmEncoder(config);
|
||||||
const asmEncoder = createOpusScriptAsmEncoder(config);
|
const asmEncoder = createOpusScriptAsmEncoder(config);
|
||||||
|
const evanOpus = createEvanOpusEncoder(config);
|
||||||
|
const evanOpusWasm = createEvanOpusEncoderWasm(config);
|
||||||
|
|
||||||
const SAMPLE = generatePCMSample(config.FRAME_SIZE * config.CHANNELS * 6);
|
const SAMPLE = generatePCMSample(config.FRAME_SIZE * config.CHANNELS * 6);
|
||||||
|
|
||||||
@@ -22,6 +24,12 @@ b.suite(
|
|||||||
b.add('@discordjs/opus', () => {
|
b.add('@discordjs/opus', () => {
|
||||||
nativeEncoder.encode(SAMPLE);
|
nativeEncoder.encode(SAMPLE);
|
||||||
}),
|
}),
|
||||||
|
b.add('@evan/opus', () => {
|
||||||
|
evanOpus.encode(SAMPLE);
|
||||||
|
}),
|
||||||
|
b.add('@evan/opus (wasm)', () => {
|
||||||
|
evanOpusWasm.encode(SAMPLE);
|
||||||
|
}),
|
||||||
b.add('opusscript', () => {
|
b.add('opusscript', () => {
|
||||||
wasmEncoder.encode(SAMPLE, config.FRAME_SIZE);
|
wasmEncoder.encode(SAMPLE, config.FRAME_SIZE);
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@discordjs/opus": "^0.9.0",
|
"@discordjs/opus": "^0.9.0",
|
||||||
|
"@evan/opus": "^1.0.2",
|
||||||
"@napi-rs/cli": "^2.16.1",
|
"@napi-rs/cli": "^2.16.1",
|
||||||
"@types/node": "^20.4.8",
|
"@types/node": "^20.4.8",
|
||||||
"ava": "^5.1.1",
|
"ava": "^5.1.1",
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[napi(object)]
|
||||||
|
pub struct EqualizerCoefficient {
|
||||||
|
pub beta: i32,
|
||||||
|
pub alpha: i32,
|
||||||
|
pub gamma: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub struct Equalizer {
|
||||||
|
coefficients: Vec<EqualizerCoefficient>,
|
||||||
|
format: PCMFormat,
|
||||||
|
channels: AudioChannel,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
impl Equalizer {
|
||||||
|
#[napi(constructor)]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
use napi::bindgen_prelude::*;
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub enum PCMFormat {
|
||||||
|
S16LE,
|
||||||
|
S16BE,
|
||||||
|
S32LE,
|
||||||
|
S32BE,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub enum AudioChannel {
|
||||||
|
Mono,
|
||||||
|
Stereo,
|
||||||
|
}
|
||||||
|
|
||||||
|
mod equalizer;
|
||||||
|
mod volume;
|
||||||
@@ -0,0 +1,165 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub struct VolumeTransformer {
|
||||||
|
volume: i32,
|
||||||
|
format: PCMFormat,
|
||||||
|
channels: AudioChannel,
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: investigate SIMD optimizations
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
impl VolumeTransformer {
|
||||||
|
#[napi(constructor)]
|
||||||
|
pub fn new(volume: i32, format: PCMFormat, channels: AudioChannel) -> Self {
|
||||||
|
Self {
|
||||||
|
volume,
|
||||||
|
format,
|
||||||
|
channels,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi(setter)]
|
||||||
|
pub fn set_volume(&mut self, volume: i32) {
|
||||||
|
if volume < 0 {
|
||||||
|
self.volume = 0;
|
||||||
|
} else {
|
||||||
|
self.volume = volume;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi(getter)]
|
||||||
|
pub fn get_volume(&self) -> i32 {
|
||||||
|
self.volume
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi(setter)]
|
||||||
|
pub fn set_format(&mut self, format: PCMFormat) {
|
||||||
|
self.format = format;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi(getter)]
|
||||||
|
pub fn get_format(&self) -> PCMFormat {
|
||||||
|
self.format
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi(setter)]
|
||||||
|
pub fn set_channels(&mut self, channels: AudioChannel) {
|
||||||
|
self.channels = channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi(getter)]
|
||||||
|
pub fn get_channels(&self) -> AudioChannel {
|
||||||
|
self.channels
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub fn process(&self, input: Buffer) -> Buffer {
|
||||||
|
// avoid computation if volume is 1, aka default volume
|
||||||
|
if self.volume == 1 {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.channels {
|
||||||
|
AudioChannel::Mono => self.process_mono_inner(input),
|
||||||
|
AudioChannel::Stereo => self.process_stereo_inner(input),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_stereo_inner(&self, input: Buffer) -> Buffer {
|
||||||
|
match self.format {
|
||||||
|
PCMFormat::S16LE => {
|
||||||
|
let mut output = Vec::with_capacity(input.len());
|
||||||
|
for i in (0..input.len()).step_by(4) {
|
||||||
|
let mut sample = i16::from_le_bytes([input[i], input[i + 1]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i16;
|
||||||
|
output.extend_from_slice(&sample.to_le_bytes());
|
||||||
|
let mut sample = i16::from_le_bytes([input[i + 2], input[i + 3]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i16;
|
||||||
|
output.extend_from_slice(&sample.to_le_bytes());
|
||||||
|
}
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
|
PCMFormat::S16BE => {
|
||||||
|
let mut output = Vec::with_capacity(input.len());
|
||||||
|
for i in (0..input.len()).step_by(4) {
|
||||||
|
let mut sample = i16::from_be_bytes([input[i], input[i + 1]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i16;
|
||||||
|
output.extend_from_slice(&sample.to_be_bytes());
|
||||||
|
let mut sample = i16::from_be_bytes([input[i + 2], input[i + 3]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i16;
|
||||||
|
output.extend_from_slice(&sample.to_be_bytes());
|
||||||
|
}
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
|
PCMFormat::S32LE => {
|
||||||
|
let mut output = Vec::with_capacity(input.len());
|
||||||
|
for i in (0..input.len()).step_by(8) {
|
||||||
|
let mut sample = i32::from_le_bytes([input[i], input[i + 1], input[i + 2], input[i + 3]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i32;
|
||||||
|
output.extend_from_slice(&sample.to_le_bytes());
|
||||||
|
let mut sample =
|
||||||
|
i32::from_le_bytes([input[i + 4], input[i + 5], input[i + 6], input[i + 7]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i32;
|
||||||
|
output.extend_from_slice(&sample.to_le_bytes());
|
||||||
|
}
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
|
PCMFormat::S32BE => {
|
||||||
|
let mut output = Vec::with_capacity(input.len());
|
||||||
|
for i in (0..input.len()).step_by(8) {
|
||||||
|
let mut sample = i32::from_be_bytes([input[i], input[i + 1], input[i + 2], input[i + 3]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i32;
|
||||||
|
output.extend_from_slice(&sample.to_be_bytes());
|
||||||
|
let mut sample =
|
||||||
|
i32::from_be_bytes([input[i + 4], input[i + 5], input[i + 6], input[i + 7]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i32;
|
||||||
|
output.extend_from_slice(&sample.to_be_bytes());
|
||||||
|
}
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_mono_inner(&self, input: Buffer) -> Buffer {
|
||||||
|
match self.format {
|
||||||
|
PCMFormat::S16LE => {
|
||||||
|
let mut output = Vec::with_capacity(input.len());
|
||||||
|
for i in (0..input.len()).step_by(2) {
|
||||||
|
let mut sample = i16::from_le_bytes([input[i], input[i + 1]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i16;
|
||||||
|
output.extend_from_slice(&sample.to_le_bytes());
|
||||||
|
}
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
|
PCMFormat::S16BE => {
|
||||||
|
let mut output = Vec::with_capacity(input.len());
|
||||||
|
for i in (0..input.len()).step_by(2) {
|
||||||
|
let mut sample = i16::from_be_bytes([input[i], input[i + 1]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i16;
|
||||||
|
output.extend_from_slice(&sample.to_be_bytes());
|
||||||
|
}
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
|
PCMFormat::S32LE => {
|
||||||
|
let mut output = Vec::with_capacity(input.len());
|
||||||
|
for i in (0..input.len()).step_by(4) {
|
||||||
|
let mut sample = i32::from_le_bytes([input[i], input[i + 1], input[i + 2], input[i + 3]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i32;
|
||||||
|
output.extend_from_slice(&sample.to_le_bytes());
|
||||||
|
}
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
|
PCMFormat::S32BE => {
|
||||||
|
let mut output = Vec::with_capacity(input.len());
|
||||||
|
for i in (0..input.len()).step_by(4) {
|
||||||
|
let mut sample = i32::from_be_bytes([input[i], input[i + 1], input[i + 2], input[i + 3]]);
|
||||||
|
sample = (sample as i32 * self.volume) as i32;
|
||||||
|
output.extend_from_slice(&sample.to_be_bytes());
|
||||||
|
}
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
use napi::bindgen_prelude::*;
|
||||||
|
use napi::{Error, Result};
|
||||||
|
use symphonia::core::codecs::{Decoder as SymphoniaDecoder, DecoderOptions, CODEC_TYPE_NULL};
|
||||||
|
use symphonia::core::formats::{Packet, Track};
|
||||||
|
use symphonia::core::io::MediaSourceStream;
|
||||||
|
use symphonia::core::probe::Hint;
|
||||||
|
|
||||||
|
struct Decoder {
|
||||||
|
decoder: Box<dyn SymphoniaDecoder>,
|
||||||
|
track: &Track,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder {
|
||||||
|
fn new(src_hint: Option<String>) -> Result<Self> {
|
||||||
|
let cursor = std::io::Cursor::new(Vec::new());
|
||||||
|
|
||||||
|
let mss = MediaSourceStream::new(Box::new(cursor), Default::default());
|
||||||
|
|
||||||
|
let mut hint = Hint::new();
|
||||||
|
if let Some(h) = src_hint {
|
||||||
|
hint.with_extension(h.as_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
let meta_opts: symphonia::core::meta::MetadataOptions = Default::default();
|
||||||
|
let fmt_opts: symphonia::core::formats::FormatOptions = Default::default();
|
||||||
|
|
||||||
|
let mut probed = symphonia::default::get_probe()
|
||||||
|
.format(&hint, mss, &fmt_opts, &meta_opts)
|
||||||
|
.map_err(|e| {
|
||||||
|
Error::new(
|
||||||
|
Status::GenericFailure,
|
||||||
|
format!("failed to probe media: {}", e),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let mut format = probed.format;
|
||||||
|
|
||||||
|
let track = format
|
||||||
|
.tracks()
|
||||||
|
.iter()
|
||||||
|
.find(|t| t.codec_params.codec != CODEC_TYPE_NULL)
|
||||||
|
.ok_or(Error::new(Status::GenericFailure, "no audio tracks found"))?;
|
||||||
|
|
||||||
|
let dec_opts: DecoderOptions = Default::default();
|
||||||
|
let mut decoder = symphonia::default::get_codecs()
|
||||||
|
.make(&track.codec_params, &dec_opts)
|
||||||
|
.map_err(|e| Error::new(Status::GenericFailure, "unsupported codec"))?;
|
||||||
|
|
||||||
|
Ok(Self { decoder, track })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode(&mut self, data: &[u8]) -> Result<()> {
|
||||||
|
self
|
||||||
|
.decoder
|
||||||
|
.decode(Packet::new_from_slice(self.track.id, ts, dur, buf));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode(data: &[u8]) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -4,5 +4,7 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate napi_derive;
|
extern crate napi_derive;
|
||||||
|
|
||||||
|
// mod dasp;
|
||||||
|
// mod decoder;
|
||||||
mod opus;
|
mod opus;
|
||||||
mod probe;
|
mod probe;
|
||||||
@@ -78,6 +78,13 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"@evan/opus@npm:^1.0.2":
|
||||||
|
version: 1.0.2
|
||||||
|
resolution: "@evan/opus@npm:1.0.2"
|
||||||
|
checksum: 5473155e79eb99a6bf5d2b35cbad5b2ff687e9aa5b1e2a7c15bd701eb3eb3986b6452079650823e8211727d8f033a6be86484e10c87f7eef5ebbbbfb7e10c928
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"@isaacs/cliui@npm:^8.0.2":
|
"@isaacs/cliui@npm:^8.0.2":
|
||||||
version: 8.0.2
|
version: 8.0.2
|
||||||
resolution: "@isaacs/cliui@npm:8.0.2"
|
resolution: "@isaacs/cliui@npm:8.0.2"
|
||||||
@@ -1506,6 +1513,7 @@ __metadata:
|
|||||||
resolution: "mediaplex@workspace:."
|
resolution: "mediaplex@workspace:."
|
||||||
dependencies:
|
dependencies:
|
||||||
"@discordjs/opus": ^0.9.0
|
"@discordjs/opus": ^0.9.0
|
||||||
|
"@evan/opus": ^1.0.2
|
||||||
"@napi-rs/cli": ^2.16.1
|
"@napi-rs/cli": ^2.16.1
|
||||||
"@types/node": ^20.4.8
|
"@types/node": ^20.4.8
|
||||||
ava: ^5.1.1
|
ava: ^5.1.1
|
||||||
|
|||||||
Reference in new issue
Block a user