-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
343 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,4 +195,6 @@ Cargo.lock | |
!.yarn/versions | ||
|
||
*.node | ||
dev/ | ||
dev/ | ||
./src/dasp/* | ||
./src/decoder/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.