Skip to content

rust: Port media-io/audio-math.h to obs-media-io - #179

Merged
zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/audio-math
Oct 11, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/audio-math

Conversation

@swilk19

@swilk19 swilk19 commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Refs #45

What and why

Ports mul_to_db and db_to_mul from libobs/media-io/audio-math.h to obs-media-io (obs_media_io::audio_math). This is item 4 of the media-io part 2 order on #45 (#45 (comment)), kept separate from #129. No intentional differences.

Header-inline exemption. Both functions are static inline, so nothing is swapped or exported. There is no ENABLE_RUST_LIBOBS guard and no rust-exports.d fragment, and the header is unmodified. The header defines no struct, so Tier 2 is just the unchanged cmocka test passing OFF and ON. Tier 1 and Tier 3 run against the non-inline oracle_mul_to_db / oracle_db_to_mul wrappers (oracle/audio_math.c, bindings in src/audio_math.rs).

Exactness, as set on #45.

  • The cmocka test stays portable: exact checks only where every libm is exact (0 and -0 → -inf, 1 → 0, +inf → +inf, db_to_mul(0) → 1, non-finite dB → 0, out-of-range dB → inf or 0, negative or NaN multiplier → NaN). Everything else uses a relative tolerance of about 8 ULPs, because glibc, UCRT and libSystem differ in the last bit.
  • Tier 3 is exact bit equality (any NaN equals any NaN) with no ULP tolerance. Both sides call the same platform log10f/powf. The core keeps 10.0f32.powf(db / 20.0) as written.
  • On pow(10, x) → exp10: Apple clang rewrites the oracle's powf(10, x) into __exp10f at -O1 and above. In release builds the C side therefore calls __exp10f while Rust calls powf. libSystem's powf(10, x) and __exp10f(x) agree bit for bit (checked over all 2,166,358,017 floats with x in [-10, 2], i.e. -200..+40 dB), and parity passes in debug and release on macOS. On Linux and with MSVC, parity passes in both profiles as well.

RED → GREEN

  • Characterization: 3ed35cb31 adds test/cmocka/test_audio_math.c and registers it as tests.d/test_audio_math.cmake (links libm outside MSVC, like the graphics math tests). It passes OFF and ON before any Rust.
  • RED commit: 54a7c41ca. Against the todo!() cores, audio_math fails 10/10 and audio_math_parity fails 4/4. Checked with soldr's cache disabled and a fresh target dir.
  • GREEN: 4e0a6c051. audio_math 10/10 and audio_math_parity 4/4 pass. Tier 2 Linux: 51/51 with ENABLE_RUST_LIBOBS OFF and ON, test_audio_math included, and libobs exports are identical (1752 symbols).

How has this been tested?

  • Tier 1, tests/audio_math.rs: ports test_audio_math.c case for case, with the same exact cases and tolerance. Edge cases: the exact zero results are +0, NaNs of either sign and any payload give 0 from db_to_mul, and the largest in-range dB values.
  • Tier 3, tests/audio_math_parity.rs: special values (zeros, infinities, NaNs, subnormals, the float extremes, the cmocka values, each with both signs), every 4096th bit pattern (about 1M floats), and proptest (20,000 cases each) over arbitrary bits and the audio range (multipliers 0..4, -200..40 dB).
  • Mutation check: computing powf as exp(x * ln 10), or log10 as ln(x) / ln 10, fails every parity test. Treating +inf dB as finite (checking !is_nan instead of is_finite) fails the special-value and bit-pattern sweeps. Making the zero check sign-sensitive is an equivalent mutant, since log10f(-0) is -inf too.
  • Linux x86_64 (WSL2): ./lint, ./unittest, and the stamped head. Parity also passes with --release.
  • Windows MSVC (x86_64-pc-windows-msvc, UCRT) via cargo-xwin: audio_math 10/10, audio_math_parity 4/4. The oracle there is built with clang-cl, and cmocka Tier 2 wasn't run locally.
  • macOS 26.7.1 on Apple Silicon (M2 Pro, Xcode 26.5, Apple clang 21): workspace tests 638/0, clippy and fmt clean, the focus tests pass in debug and release, and rust/tools/tier2-validate/validate.sh passes OFF and ON with identical exports (1863 symbols).

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it (item 4 of the claim approved on Phase 5 remaining: graphics math, media-io, parsers, obs-websocket Tier 4 #45; no open PR touches audio-math)
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • Local gate passed and the pushed head is stamped
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): none

🤖 Generated with Claude Code

@swilk19
swilk19 requested a review from zackees as a code owner October 11, 2026 00:56
swilk19 and others added 3 commits October 10, 2026 18:04
Pins mul_to_db and db_to_mul from media-io/audio-math.h before the
Rust port. log10f and powf come from the platform libm, and glibc,
the UCRT and libSystem can differ in the last bit, so only results
every libm gets exactly are compared exactly: 0 and -0 -> -inf,
1 -> 0 and +inf -> +inf for mul_to_db; 0 -> 1, non-finite input -> 0
(+inf included), and finite input past float range -> inf or 0 for
db_to_mul. A negative multiplier or NaN gives NaN. Everything else,
including the float extremes, is compared with a relative tolerance
of about 8 ULPs. Characterized, not endorsed.

The header calls log10f/powf, so the test links libm outside MSVC,
like the graphics math tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 1 and Tier 3 tests for mul_to_db and db_to_mul against todo!()
cores in obs_media_io::audio_math. audio-math.h is header-inline and
defines no struct, so there is no shim, no layout test and no symbol
swap; Tier 2 is the unchanged characterization test passing OFF/ON.

The oracle wraps the header in non-inline oracle_mul_to_db and
oracle_db_to_mul (oracle/audio_math.c, bindings in src/audio_math.rs).

Tier 1 ports test_audio_math.c case for case, with its exact cases and
relative tolerance, and adds the sign of the exact zero results, NaNs
of either sign and any payload giving 0 from db_to_mul, and the largest
in-range dB values.

The C is scalar, so per the Phase 5 rule parity is exact bit equality
(any NaN equals any NaN), with no ULP tolerance: both sides call the
same platform log10f/powf. It covers special values (zeros,
infinities, NaNs, subnormals, the float extremes, the cmocka values,
each with both signs), every 4096th bit pattern, and proptest over
arbitrary bits and the audio range (multipliers 0..4, -200..40 dB).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mul_to_db and db_to_mul get Rust equivalents in
obs_media_io::audio_math, for Rust ports of the audio code that
converts between faders and gain. The header is static inline, so
nothing is swapped or exported. No intentional differences.

The cores are the header's expressions: -inf for a zero multiplier,
else 20 * log10(mul); 10^(db / 20) via powf for finite dB, else 0.
f32::log10 and f32::powf lower to the platform log10f/powf, the same
calls the C makes, so parity is bit-exact on each platform, also in
release builds (no pow(10, x) -> exp10 rewrite on either side).

Mutation check: computing powf as exp(x * ln 10) or log10 as
ln(x) / ln 10 fails every parity test; keeping +inf dB (checking
!is_nan instead of is_finite) fails the special-value and bit-pattern
sweeps. Making the zero check sign-sensitive passes, as expected:
log10f(-0) is -inf as well, so that mutant is equivalent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=3900f919177dac1fb933c78a8e8e1d7b400e92ae secs=164 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791680975,"gate":"rust/all/fmt","host":"linux-x86_64","key":"5987b139be6f71709ce8b1e5468e0beaadfcd29d1efcd0fa45a0c90dfb1b3310","lane":"lint","parents":["6d23f0ccfa214ad6144f0b4da6b7187c329dbf33"],"secs":54,"stamp":"856dffc539a0d984d0c682c8eb4289f8","tree":"3900f919177dac1fb933c78a8e8e1d7b400e92ae","v":1,"via":"run"}
Ci-Attestation: {"at":1791680975,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"5987b139be6f71709ce8b1e5468e0beaadfcd29d1efcd0fa45a0c90dfb1b3310","lane":"lint","parents":["6d23f0ccfa214ad6144f0b4da6b7187c329dbf33"],"secs":54,"stamp":"102162225499cd7dd23d4d6b3bcd3b61","tree":"3900f919177dac1fb933c78a8e8e1d7b400e92ae","v":1,"via":"run"}
Ci-Attestation: {"at":1791680975,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"72aa51d98402d5ac64565e50f9ada5e540cc12f73963586798e351cd46ba9145","lane":"rust-test","parents":["6d23f0ccfa214ad6144f0b4da6b7187c329dbf33"],"secs":46,"stamp":"e90f0df1f369bed465076d970be04f55","tree":"3900f919177dac1fb933c78a8e8e1d7b400e92ae","v":1,"via":"run"}
Ci-Attestation: {"at":1791680975,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"0e9dd62c5b41dfeb23ffdb9f275282bb2ffe294e7c4cf05331c0db242a684c87","lane":"tier2-linux","parents":["6d23f0ccfa214ad6144f0b4da6b7187c329dbf33"],"secs":63,"stamp":"c9c4908a0161afcf39f97745686f17eb","tree":"3900f919177dac1fb933c78a8e8e1d7b400e92ae","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified (soldr cache disabled) and rebased onto current master with no conflicts (old head 4e0a6c0, replaced with --force-with-lease). Thanks for using the new per-unit layout: a tests.d/ fragment, its own oracle file, and a one-line lib.rs.

  • RED at the tests commit: audio_math fails 10/10.
  • GREEN: the full suite passes; Tier 2 51/51 with the Rust port OFF and ON, exports IDENTICAL (1752).

The local gate passes and is stamped. I'll merge once CI is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants