Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
Remove code duplication when checking frame compatibility for filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
ExPixel committed Dec 28, 2020
1 parent 5ca248d commit 59fc245
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 169 deletions.
28 changes: 2 additions & 26 deletions miniaudio/src/filters/band_pass_filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,7 @@ impl BPF2 {
impl Filter for BPF2 {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_bpf2_process_pcm_frames(
Expand Down Expand Up @@ -274,19 +262,7 @@ impl BPF {
impl Filter for BPF {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_bpf_process_pcm_frames(
Expand Down
14 changes: 1 addition & 13 deletions miniaudio/src/filters/biquad_filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,7 @@ impl Biquad {
impl Filter for Biquad {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

let result = unsafe {
sys::ma_biquad_process_pcm_frames(
Expand Down
42 changes: 3 additions & 39 deletions miniaudio/src/filters/high_pass_filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,7 @@ impl HPF1 {
impl Filter for HPF1 {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_hpf1_process_pcm_frames(
Expand Down Expand Up @@ -232,19 +220,7 @@ impl HPF2 {
impl Filter for HPF2 {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_hpf2_process_pcm_frames(
Expand Down Expand Up @@ -358,19 +334,7 @@ impl HPF {
impl Filter for HPF {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_hpf_process_pcm_frames(
Expand Down
14 changes: 1 addition & 13 deletions miniaudio/src/filters/high_shelf_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,7 @@ impl HighShelf2 {
impl Filter for HighShelf2 {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_hishelf2_process_pcm_frames(
Expand Down
42 changes: 3 additions & 39 deletions miniaudio/src/filters/low_pass_filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,7 @@ impl LPF1 {
impl Filter for LPF1 {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_lpf1_process_pcm_frames(
Expand Down Expand Up @@ -232,19 +220,7 @@ impl LPF2 {
impl Filter for LPF2 {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_lpf2_process_pcm_frames(
Expand Down Expand Up @@ -356,19 +332,7 @@ impl LPF {
impl Filter for LPF {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_lpf_process_pcm_frames(
Expand Down
14 changes: 1 addition & 13 deletions miniaudio/src/filters/low_shelf_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,7 @@ impl LowShelf2 {
impl Filter for LowShelf2 {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_loshelf2_process_pcm_frames(
Expand Down
31 changes: 31 additions & 0 deletions miniaudio/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,34 @@ use crate::frames::{Frames, FramesMut};
pub trait Filter {
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error>;
}

fn ensure_same_format(output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
Err(Error::InvalidArgs)
} else {
Ok(())
}
}

fn ensure_same_frame_count(output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.frame_count() != input.frame_count() {
ma_debug_panic!(
"output and input buffers did not have the same frame count (output: {}, input: {})",
output.frame_count(),
input.frame_count()
);
Err(Error::InvalidArgs)
} else {
Ok(())
}
}

fn ensure_frames_compat(output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
ensure_same_format(output, input)?;
ensure_same_frame_count(output, input)
}
14 changes: 1 addition & 13 deletions miniaudio/src/filters/notching_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,7 @@ impl Notch2 {
impl Filter for Notch2 {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_notch2_process_pcm_frames(
Expand Down
14 changes: 1 addition & 13 deletions miniaudio/src/filters/peaking_eq_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,7 @@ impl Peak2 {
impl Filter for Peak2 {
#[inline]
fn process_pcm_frames(&mut self, output: &mut FramesMut, input: &Frames) -> Result<(), Error> {
if output.format() != input.format() {
ma_debug_panic!(
"output and input format did not match (output: {:?}, input: {:?}",
output.format(),
input.format()
);
return Err(Error::InvalidArgs);
}

if output.frame_count() != input.frame_count() {
ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
return Err(Error::InvalidArgs);
}
super::ensure_frames_compat(output, input)?;

Error::from_c_result(unsafe {
sys::ma_peak2_process_pcm_frames(
Expand Down

0 comments on commit 59fc245

Please sign in to comment.