gst-plugins-rs/audio/audiofx/tests/ebur128level.rs
Sebastian Dröge 65d625a4eb audiofx: Add new ebur128level element
This posts a message with the measured loudness levels similar to the
level element but uses the metrics defined as part of EBU R128.
2021-03-26 21:25:23 +02:00

150 lines
4.6 KiB
Rust

// Copyright (C) 2021 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use gst::prelude::*;
fn init() {
use std::sync::Once;
static INIT: Once = Once::new();
INIT.call_once(|| {
gst::init().unwrap();
gstrsaudiofx::plugin_register_static().expect("Failed to register rsaudiofx plugin");
});
}
#[test]
fn test_ebur128level_s16_interleaved() {
init();
run_test(
gst_audio::AudioLayout::Interleaved,
gst_audio::AUDIO_FORMAT_S16,
);
}
#[test]
fn test_ebur128level_s32_interleaved() {
init();
run_test(
gst_audio::AudioLayout::Interleaved,
gst_audio::AUDIO_FORMAT_S32,
);
}
#[test]
fn test_ebur128level_f32_interleaved() {
init();
run_test(
gst_audio::AudioLayout::Interleaved,
gst_audio::AUDIO_FORMAT_F32,
);
}
#[test]
fn test_ebur128level_f64_interleaved() {
init();
run_test(
gst_audio::AudioLayout::Interleaved,
gst_audio::AUDIO_FORMAT_F64,
);
}
#[test]
fn test_ebur128level_s16_non_interleaved() {
init();
run_test(
gst_audio::AudioLayout::NonInterleaved,
gst_audio::AUDIO_FORMAT_S16,
);
}
#[test]
fn test_ebur128level_s32_non_interleaved() {
init();
run_test(
gst_audio::AudioLayout::NonInterleaved,
gst_audio::AUDIO_FORMAT_S32,
);
}
#[test]
fn test_ebur128level_f32_non_interleaved() {
init();
run_test(
gst_audio::AudioLayout::NonInterleaved,
gst_audio::AUDIO_FORMAT_F32,
);
}
#[test]
fn test_ebur128level_f64_non_interleaved() {
init();
run_test(
gst_audio::AudioLayout::NonInterleaved,
gst_audio::AUDIO_FORMAT_F64,
);
}
fn run_test(layout: gst_audio::AudioLayout, format: gst_audio::AudioFormat) {
let mut h = gst_check::Harness::new_parse(&format!(
"audiotestsrc num-buffers=5 samplesperbuffer=48000 ! \
audioconvert ! \
audio/x-raw,layout={},format={},channels=2,rate=48000 ! \
ebur128level interval=500000000",
match layout {
gst_audio::AudioLayout::Interleaved => "interleaved",
gst_audio::AudioLayout::NonInterleaved => "non-interleaved",
_ => unimplemented!(),
},
format.to_str()
));
let bus = gst::Bus::new();
h.get_element().unwrap().set_bus(Some(&bus));
h.play();
// Pull all buffers until EOS
let mut num_buffers = 0;
while let Some(_buffer) = h.pull_until_eos().unwrap() {
num_buffers += 1;
}
assert_eq!(num_buffers, 5);
let mut num_msgs = 0;
while let Some(msg) = bus.pop() {
match msg.view() {
gst::MessageView::Element(msg) => {
let s = msg.get_structure().unwrap();
if s.get_name() == "ebur128-level" {
num_msgs += 1;
let timestamp = s.get_some::<u64>("timestamp").unwrap();
let running_time = s.get_some::<u64>("running-time").unwrap();
let stream_time = s.get_some::<u64>("stream-time").unwrap();
assert_eq!(timestamp, num_msgs * 500 * gst::MSECOND_VAL);
assert_eq!(running_time, num_msgs * 500 * gst::MSECOND_VAL);
assert_eq!(stream_time, num_msgs * 500 * gst::MSECOND_VAL);
// Check if all these exist
let _momentary_loudness = s.get_some::<f64>("momentary-loudness").unwrap();
let _shortterm_loudness = s.get_some::<f64>("shortterm-loudness").unwrap();
let _global_loudness = s.get_some::<f64>("global-loudness").unwrap();
let _relative_threshold = s.get_some::<f64>("relative-threshold").unwrap();
let _loudness_range = s.get_some::<f64>("loudness-range").unwrap();
let sample_peak = s.get::<gst::Array>("sample-peak").unwrap().unwrap();
assert_eq!(sample_peak.as_slice().len(), 2);
assert_eq!(sample_peak.as_slice()[0].type_(), glib::Type::F64);
let true_peak = s.get::<gst::Array>("true-peak").unwrap().unwrap();
assert_eq!(true_peak.as_slice().len(), 2);
assert_eq!(true_peak.as_slice()[0].type_(), glib::Type::F64);
}
}
_ => (),
}
}
assert_eq!(num_msgs, 10);
}