mirror of
https://github.com/darksaid98/mediaplex.git
synced 2026-12-02 06:28:51 +00:00
perf: improve decode method
This commit is contained in:
1 parent
9f9d588667
commit
3aaba5739f
9 files changed
+39
-310
No files matched your search
@@ -1,23 +0,0 @@
|
||||
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 {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
use napi::bindgen_prelude::*;
|
||||
|
||||
#[napi]
|
||||
pub enum PCMFormat {
|
||||
S16LE,
|
||||
S16BE,
|
||||
S32LE,
|
||||
S32BE,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub enum AudioChannel {
|
||||
Mono,
|
||||
Stereo,
|
||||
}
|
||||
|
||||
mod equalizer;
|
||||
mod volume;
|
||||
@@ -1,165 +0,0 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
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(())
|
||||
}
|
||||
+10
-14
@@ -1,3 +1,5 @@
|
||||
use core::slice;
|
||||
|
||||
use napi::bindgen_prelude::*;
|
||||
use napi::Error;
|
||||
use napi::Result;
|
||||
@@ -28,11 +30,8 @@ pub fn get_opus_version() -> String {
|
||||
let version_string = unsafe { opus_get_version_string() };
|
||||
let version_cstr = unsafe { core::ffi::CStr::from_ptr(version_string) };
|
||||
|
||||
// fallback to libopus v1.4 (master), it was cloned from master branch
|
||||
let version_string = version_cstr
|
||||
.to_str()
|
||||
.unwrap_or("libopus v1.4 (master)")
|
||||
.to_owned();
|
||||
// fallback to commit hash
|
||||
let version_string = version_cstr.to_str().unwrap_or("101a71e0").to_owned();
|
||||
|
||||
version_string
|
||||
}
|
||||
@@ -109,11 +108,7 @@ impl JsOpusEncoder {
|
||||
|
||||
let mut out_buffer = vec![0u8; MAX_PACKET_SIZE];
|
||||
|
||||
let pcm = unsafe {
|
||||
let ptr = data.as_ptr() as *const i16;
|
||||
let len = data.len() / 2;
|
||||
std::slice::from_raw_parts(ptr, len)
|
||||
};
|
||||
let pcm = unsafe { std::mem::transmute::<&[u8], &[i16]>(&data) };
|
||||
|
||||
let frame_size = data.len() / 2 / (self.channels as usize);
|
||||
|
||||
@@ -164,12 +159,13 @@ impl JsOpusEncoder {
|
||||
|
||||
let decoded_length = decoded_samples * self.channels;
|
||||
|
||||
let out = unsafe {
|
||||
let ptr = out.as_ptr() as *const u8;
|
||||
std::slice::from_raw_parts(ptr, decoded_length as usize * 4)
|
||||
let out_slice = unsafe {
|
||||
slice::from_raw_parts_mut(out.as_mut_ptr() as *mut u8, decoded_length as usize * 2)
|
||||
};
|
||||
|
||||
Ok(out.to_vec().into())
|
||||
let out_vec = unsafe { std::mem::transmute::<&mut [i16], &mut [u8]>(&mut out) };
|
||||
|
||||
Ok(out_vec.to_vec().into())
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
|
||||
Reference in new issue
Block a user