Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test: cover resampler options under a comma-decimal locale

Richie McIlroy87bb19f98c66
diff --git a/Cargo.lock b/Cargo.lock
index ffb628ea4..40746da6c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1328,6 +1328,7 @@ dependencies = [
"cidre",
"cpal 0.15.3 (git+https://github.com/CapSoftware/cpal?rev=6013cb5f8bd3)",
"ffmpeg-next",
+ "libc",
"serde",
"serde_json",
"tempfile",
diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml
index a9431abb6..22063d582 100644
--- a/crates/audio/Cargo.toml
+++ b/crates/audio/Cargo.toml
@@ -34,5 +34,8 @@ windows = { workspace = true, features = [
[dev-dependencies]
tempfile = "3"
+[target.'cfg(unix)'.dev-dependencies]
+libc = "0.2"
+
[lints]
workspace = true
diff --git a/crates/audio/src/audio_data.rs b/crates/audio/src/audio_data.rs
index e790421b1..8a02642a1 100644
--- a/crates/audio/src/audio_data.rs
+++ b/crates/audio/src/audio_data.rs
@@ -447,6 +447,85 @@ mod tests {
use super::*;
use std::path::Path;
+ #[cfg(unix)]
+ struct CommaDecimalThreadLocale {
+ locale: libc::locale_t,
+ previous: libc::locale_t,
+ }
+
+ #[cfg(unix)]
+ impl CommaDecimalThreadLocale {
+ fn activate() -> Option<Self> {
+ for name in [
+ c"pt_BR.UTF-8",
+ c"pt_BR.utf8",
+ c"de_DE.UTF-8",
+ c"de_DE.utf8",
+ c"fr_FR.UTF-8",
+ c"fr_FR.utf8",
+ ] {
+ let locale = unsafe {
+ libc::newlocale(libc::LC_NUMERIC_MASK, name.as_ptr(), std::ptr::null_mut())
+ };
+ if locale.is_null() {
+ continue;
+ }
+ let previous = unsafe { libc::uselocale(locale) };
+ let parsed = unsafe { libc::strtod(c"0,5".as_ptr(), std::ptr::null_mut()) };
+ if (parsed - 0.5).abs() < f64::EPSILON {
+ return Some(Self { locale, previous });
+ }
+ unsafe {
+ libc::uselocale(previous);
+ libc::freelocale(locale);
+ }
+ }
+ None
+ }
+ }
+
+ #[cfg(unix)]
+ impl Drop for CommaDecimalThreadLocale {
+ fn drop(&mut self) {
+ unsafe {
+ libc::uselocale(self.previous);
+ libc::freelocale(self.locale);
+ }
+ }
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn high_quality_resampler_options_parse_under_comma_decimal_locale() {
+ let Some(_locale) = CommaDecimalThreadLocale::activate() else {
+ eprintln!("skipping: no comma-decimal locale is installed");
+ return;
+ };
+
+ let resampler = resampling::Context::get_with(
+ avformat::Sample::F32(avformat::sample::Type::Planar),
+ ChannelLayout::STEREO,
+ 44_100,
+ AudioData::SAMPLE_FORMAT,
+ ChannelLayout::STEREO,
+ AudioData::SAMPLE_RATE,
+ high_quality_resampler_options(),
+ )
+ .expect("resampler options must parse under a comma-decimal LC_NUMERIC");
+
+ let mut cutoff = 0.0;
+ let result = unsafe {
+ ffmpeg::ffi::av_opt_get_double(
+ resampler.as_ptr() as *mut std::ffi::c_void,
+ c"cutoff".as_ptr(),
+ 0,
+ &mut cutoff,
+ )
+ };
+ assert_eq!(result, 0);
+ assert!((cutoff - 0.97).abs() < 1e-9, "cutoff was {cutoff}");
+ }
+
#[test]
fn reused_resampler_output_matches_fresh_buffers_for_variable_frames() {
for source_rate in [16_000, 44_100, 48_000, 96_000] {