Skip to content

Commit

Permalink
Remove C comparison tests and benchmarks
Browse files Browse the repository at this point in the history
The implementation is proven to be equivalent to the C implementation
now and faster in any meaningful way so simplify the build system and
get rid of the C code.

Next various unit tests have to be added for the removed comparison
tests to ensure no regressions in the future.

Fixes #37
  • Loading branch information
sdroege committed Feb 14, 2021
1 parent f0e1810 commit 9994e5b
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 1,852 deletions.
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ cc = { version = "1.0", optional = true }
[dev-dependencies]
criterion = "0.3"
float_eq = "0.5"
ebur128-c = { package = "ebur128", version = "=0.1.1" }
quickcheck = "0.9"
quickcheck_macros = "0.9"
rand = "0.7"
hound = "3"

[features]
internal-tests = []
c-tests = ["cc", "internal-tests"] # and ebur128-c, quickcheck, quickcheck_macros, rand but dev-dependencies can't be optional...
reference-tests = []
internal-tests = []

capi = []

# Enabling this increases the precision of true-peak calculation slightly, but causes a significant
Expand Down
85 changes: 23 additions & 62 deletions benches/calc_gating_block.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

#[cfg(feature = "c-tests")]
fn calc_gating_block_c(
frames_per_block: usize,
audio_data: &[f64],
audio_data_index: usize,
channel_map: &[u32],
) -> f64 {
unsafe {
ebur128::filter::calc_gating_block_c(
frames_per_block,
audio_data.as_ptr(),
audio_data.len() / channel_map.len(),
audio_data_index,
channel_map.as_ptr(),
channel_map.len(),
)
}
}

#[cfg(feature = "internal-tests")]
fn calc_gating_block(
frames_per_block: usize,
audio_data: &[f64],
Expand All @@ -35,53 +15,34 @@ fn calc_gating_block(
}

pub fn criterion_benchmark(c: &mut Criterion) {
#[cfg(feature = "internal-tests")]
{
let mut data = vec![0f64; 48_000 * 3 * 2];
let mut accumulator = 0.0;
let step = 2.0 * std::f64::consts::PI * 440.0 / 48_000.0;
for out in data.chunks_exact_mut(2) {
let val = f64::sin(accumulator);
out[0] = val;
out[1] = val;
accumulator += step;
}

let channel_map = [ebur128::Channel::Left; 2];

let frames_per_block = 144_000;
let mut data = vec![0f64; 48_000 * 3 * 2];
let mut accumulator = 0.0;
let step = 2.0 * std::f64::consts::PI * 440.0 / 48_000.0;
for out in data.chunks_exact_mut(2) {
let val = f64::sin(accumulator);
out[0] = val;
out[1] = val;
accumulator += step;
}

let mut group = c.benchmark_group("calc gating block: 48kHz 2ch");
let channel_map = [ebur128::Channel::Left; 2];

#[cfg(feature = "c-tests")]
{
let channel_map_c = [1; 2];
let frames_per_block = 144_000;

group.bench_function("C", |b| {
b.iter(|| {
calc_gating_block_c(
black_box(frames_per_block),
black_box(&data),
black_box(0),
black_box(&channel_map_c),
)
})
});
}
let mut group = c.benchmark_group("calc gating block: 48kHz 2ch");

group.bench_function("Rust", |b| {
b.iter(|| {
calc_gating_block(
black_box(frames_per_block),
black_box(&data),
black_box(0),
black_box(&channel_map),
)
})
});
group.bench_function("Rust", |b| {
b.iter(|| {
calc_gating_block(
black_box(frames_per_block),
black_box(&data),
black_box(0),
black_box(&channel_map),
)
})
});

group.finish();
}
group.finish();
}

criterion_group!(benches, criterion_benchmark);
Expand Down
134 changes: 11 additions & 123 deletions benches/ebur128.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use ebur128::{EbuR128, Mode};
use ebur128_c::Mode as ModeC;

#[cfg(feature = "c-tests")]
use ebur128_c::EbuR128 as EbuR128C;

#[cfg(feature = "c-tests")]
fn get_results_c(ebu: &EbuR128C, mode: ModeC) {
if mode.contains(ModeC::I) {
black_box(ebu.loudness_global().unwrap());
}
if mode.contains(ModeC::M) {
black_box(ebu.loudness_momentary().unwrap());
}
if mode.contains(ModeC::S) {
black_box(ebu.loudness_shortterm().unwrap());
}
black_box(ebu.loudness_window(1).unwrap());

if mode.contains(ModeC::LRA) {
black_box(ebu.loudness_range().unwrap());
}

if mode.contains(ModeC::SAMPLE_PEAK) {
black_box(ebu.sample_peak(0).unwrap());
black_box(ebu.sample_peak(1).unwrap());
black_box(ebu.prev_sample_peak(0).unwrap());
black_box(ebu.prev_sample_peak(1).unwrap());
}

if mode.contains(ModeC::TRUE_PEAK) {
black_box(ebu.true_peak(0).unwrap());
black_box(ebu.true_peak(1).unwrap());
black_box(ebu.prev_true_peak(0).unwrap());
black_box(ebu.prev_true_peak(1).unwrap());
}

if mode.contains(ModeC::I) {
black_box(ebu.relative_threshold().unwrap());
}
}

fn get_results(ebu: &EbuR128, mode: Mode) {
if mode.contains(Mode::I) {
Expand Down Expand Up @@ -79,48 +39,24 @@ fn get_results(ebu: &EbuR128, mode: Mode) {

pub fn criterion_benchmark(c: &mut Criterion) {
let modes = [
("M", Mode::M, ModeC::M),
("S", Mode::S, ModeC::S),
("I", Mode::I, ModeC::I),
("LRA", Mode::LRA, ModeC::LRA),
("SAMPLE_PEAK", Mode::SAMPLE_PEAK, ModeC::SAMPLE_PEAK),
("TRUE_PEAK", Mode::TRUE_PEAK, ModeC::TRUE_PEAK),
(
"I histogram",
Mode::I | Mode::HISTOGRAM,
ModeC::I | ModeC::HISTOGRAM,
),
(
"LRA histogram",
Mode::LRA | Mode::HISTOGRAM,
ModeC::LRA | ModeC::HISTOGRAM,
),
(
"all",
Mode::all() & !Mode::HISTOGRAM,
ModeC::all() & !ModeC::HISTOGRAM,
),
("all histogram", Mode::all(), ModeC::all()),
("M", Mode::M),
("S", Mode::S),
("I", Mode::I),
("LRA", Mode::LRA),
("SAMPLE_PEAK", Mode::SAMPLE_PEAK),
("TRUE_PEAK", Mode::TRUE_PEAK),
("I histogram", Mode::I | Mode::HISTOGRAM),
("LRA histogram", Mode::LRA | Mode::HISTOGRAM),
("all", Mode::all() & !Mode::HISTOGRAM),
("all histogram", Mode::all()),
];

#[allow(unused_variables)]
for (name, mode, mode_c) in &modes {
for (name, mode) in &modes {
let mode = *mode;
#[cfg(feature = "c-tests")]
let mode_c = *mode_c;

let mut group = c.benchmark_group(format!("ebur128 create: 48kHz 2ch {}", name));

#[cfg(feature = "c-tests")]
{
group.bench_function("C", |b| {
b.iter(|| {
let ebu =
EbuR128C::new(black_box(2), black_box(48_000), black_box(mode_c)).unwrap();
drop(black_box(ebu));
})
});
}
group.bench_function("Rust", |b| {
b.iter(|| {
let ebu = EbuR128::new(black_box(2), black_box(48_000), black_box(mode)).unwrap();
Expand Down Expand Up @@ -149,18 +85,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let mut group = c.benchmark_group(format!("ebur128 process: 48kHz i16 2ch {}", name));

#[cfg(feature = "c-tests")]
{
group.bench_function("C", |b| {
b.iter(|| {
let mut ebu =
EbuR128C::new(black_box(2), black_box(48_000), black_box(mode_c)).unwrap();
ebu.add_frames_i16(&data).unwrap();

get_results_c(&ebu, black_box(mode_c));
})
});
}
group.bench_function("Rust/Interleaved", |b| {
b.iter(|| {
let mut ebu =
Expand Down Expand Up @@ -201,18 +125,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let mut group = c.benchmark_group(format!("ebur128 process: 48kHz i32 2ch {}", name));

#[cfg(feature = "c-tests")]
{
group.bench_function("C", |b| {
b.iter(|| {
let mut ebu =
EbuR128C::new(black_box(2), black_box(48_000), black_box(mode_c)).unwrap();
ebu.add_frames_i32(&data).unwrap();

get_results_c(&ebu, black_box(mode_c));
})
});
}
group.bench_function("Rust/Interleaved", |b| {
b.iter(|| {
let mut ebu =
Expand Down Expand Up @@ -253,18 +165,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let mut group = c.benchmark_group(format!("ebur128 process: 48kHz f32 2ch {}", name));

#[cfg(feature = "c-tests")]
{
group.bench_function("C", |b| {
b.iter(|| {
let mut ebu =
EbuR128C::new(black_box(2), black_box(48_000), black_box(mode_c)).unwrap();
ebu.add_frames_f32(&data).unwrap();

get_results_c(&ebu, black_box(mode_c));
})
});
}
group.bench_function("Rust/Interleaved", |b| {
b.iter(|| {
let mut ebu =
Expand Down Expand Up @@ -305,18 +205,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let mut group = c.benchmark_group(format!("ebur128 process: 48kHz f64 2ch {}", name));

#[cfg(feature = "c-tests")]
{
group.bench_function("C", |b| {
b.iter(|| {
let mut ebu =
EbuR128C::new(black_box(2), black_box(48_000), black_box(mode_c)).unwrap();
ebu.add_frames_f64(&data).unwrap();

get_results_c(&ebu, black_box(mode_c));
})
});
}
group.bench_function("Rust/Interleaved", |b| {
b.iter(|| {
let mut ebu =
Expand Down
Loading

0 comments on commit 9994e5b

Please sign in to comment.