Skip to content

Add peak_dbfs/rms_dbfs to AudioStatistics, normalize_to_dbfs/apply_gain_db to AudioProcessing, and new dithering feature#2

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/add-statistics-feature
Draft

Add peak_dbfs/rms_dbfs to AudioStatistics, normalize_to_dbfs/apply_gain_db to AudioProcessing, and new dithering feature#2
Copilot wants to merge 3 commits intomainfrom
copilot/add-statistics-feature

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 6, 2026

Extends the operation trait surface with dBFS-aware statistics and processing helpers, and adds a new dithering feature module following the same end-to-end pattern as existing feature-gated modules (dynamic-range, etc.).

statistics feature — AudioStatistics

  • peak_dbfs() -> f6420·log₁₀(peak) via amplitude_to_db; -80 dB floor for silence
  • rms_dbfs() -> f6420·log₁₀(rms); paired dBFS companion to peak_dbfs

Both are default implementations built on the existing peak() and rms() methods.

processing feature — AudioProcessing

  • normalize_to_dbfs(target: f64) — converts target dBFS → linear, delegates to normalize(NormalizationConfig::peak(...))
  • apply_gain_db(db: f64) — converts dB → linear scale factor, delegates to scale()

Both are default implementations; no changes to existing normalize/scale logic.

New dithering feature

Mirrors the dynamic-range module layout exactly: feature flag → submodule → trait in traits.rs → config types in types.rs → re-exports in mod.rs and lib.rs.

Cargo.toml: dithering = ["random-generation"], added to full and full_no_plotting.

NoiseShape enum (types.rs):

  • Flat — pure TPDF (two uniform draws subtracted)
  • FWeighted — TPDF with first-order highpass shaping H(z) = 1 − 0.5·z⁻¹; per-channel state, no dynamic dispatch

AudioDithering trait (traits.rs + dithering.rs):

  • dither(shape: NoiseShape) -> Self — noise amplitude is 1 LSB of native type (1/MAX for integers; ~2⁻²³ for floats). Works in the normalised f64 domain via convert_to/convert_from.
  • requantize(bits: u32) -> AudioSampleResult<Self> — snaps each sample to a 2^(bits−1)-level grid in normalised f64 space; valid range [1, 32]. Distinct from to_type::<i16>() which rescales — this reduces precision within the same container type.
use audio_samples::{AudioDithering, AudioStatistics, AudioProcessing, NoiseShape};

// dBFS measurement
let peak_db = audio.peak_dbfs();   // e.g. -3.0
let rms_db  = audio.rms_dbfs();

// dBFS-targeted normalization and gain
let loud = audio.normalize_to_dbfs(-6.0)?;
let quiet = audio.apply_gain_db(-12.0);

// Dither before bit-depth reduction
let out = audio
    .dither(NoiseShape::FWeighted)
    .requantize(16)?;

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • googlechromelabs.github.io
    • Triggering command: /tmp/codeql-scratch-36880336822b49dd/dbs/rust/working/target/debug/build/plotly_static-03bec48163cb1e33/build-script-build /tmp/codeql-scratch-36880336822b49dd/dbs/rust/working/target/debug/build/plotly_static-03bec48163cb1e33/build-script-build --emit=dep-info,link -C embed-bitcode=no --cfg y-8bf299a6c1c85399/rustc0cHjNP/symbols.o y-8bf299a6c1c85399/build_script_build-8bf299a6c1c85399.build_script_build.7856c5/home/REDACTED/.rustup/toolchains/stable-x86_64-REDACTED-linux-gnu/lib/rustlib/x86_64-REDACTED-linux-gnu/lib/librustc_std_workspace_alloc-76b5fe9328c1063f.rlib y-8bf299a6c1c85399/build_script_build-8bf299a6c1c85399.build_script_build.7856c5/home/REDACTED/.rustup/toolchains/stable-x86_64-REDACTED-linux-gnu/lib/rustlib/x86_64-REDACTED-linux-gnu/lib/libminiz_oxide-2b6a8d2f6e1dc71b.rlib y-8bf299a6c1c85399/build_script_build-8bf299a6c1c85399.build_script_build.7856c5/home/REDACTED/.rustup/toolchains/stable-x86_64-REDACTED-linux-gnu/lib/rustlib/x86_64-REDACTED-linux-gnu/lib/libadler2-39ffdbc27c978ccc.rlib y-8bf299a6c1c85399/build_script_build-8bf299a6c1c85399.build_script_build.7856c59261�� y-8bf299a6c1c85399/build_script_build-8bf299a6c1c85399.build_script_build.7856c59261fca26f-cgu.4.rcgu.o y-8bf299a6c1c85399/build_script_build-8bf299a6c1c85399.bfhgfe28tz5rsrewzu21g7722.rcgu.o ug/d�� ug/deps/liblibc--Wl,--as-needed 64-REDACTED-linux-gnu/lib/libstd-46d936097e8c5b85.rlib 64-REDACTED-linux-gnu/lib/libpanic_unwind-e462f106b2b26a06.rlib 64-REDACTED-linux-gnu/lib/libobject-926daa94a00ee327.rlib ild.bfce194cf466c7c3-cgu.0.rcgu.o j9cveizfax.rcgu.o 64-REDACTED-linux-gnu/lib/libgimli-7aeefe72516b1ebd.rlib (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI and others added 2 commits April 6, 2026 09:53
Copilot AI changed the title [WIP] Add peak_dbfs and rms_dbfs methods to AudioStatistics Add peak_dbfs/rms_dbfs to AudioStatistics, normalize_to_dbfs/apply_gain_db to AudioProcessing, and new dithering feature Apr 6, 2026
Copilot AI requested a review from jmg049 April 6, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants