feat(settings): normalization clipping guard (audio.normalization_prevent_clipping)
A new audio setting, on by default: the normalization boost is capped so the track's known peak stays 1 dB under full scale. Store column + migration, portable in the settings bundle, shown in diagnostics, and settable from qbzd (settings get/set audio.normalization_prevent_clipping). cc
This commit is contained in:
6 files changed
+42
-1
No files matched your search
@@ -38,6 +38,7 @@ pub struct RuntimeDiagnostics {
|
||||
Option<qbz_audio::alsa_hardware_volume::HardwareVolumeProbe>,
|
||||
pub audio_normalization_enabled: bool,
|
||||
pub audio_normalization_target_lufs: f32,
|
||||
pub audio_normalization_prevent_clipping: bool,
|
||||
pub audio_gapless_enabled: bool,
|
||||
pub audio_pw_force_bitperfect: bool,
|
||||
pub audio_stream_buffer_seconds: u8,
|
||||
@@ -141,6 +142,7 @@ pub fn runtime_diagnostics(i: &DiagnosticsInputs<'_>) -> RuntimeDiagnostics {
|
||||
}),
|
||||
audio_normalization_enabled: audio.normalization_enabled,
|
||||
audio_normalization_target_lufs: audio.normalization_target_lufs,
|
||||
audio_normalization_prevent_clipping: audio.normalization_prevent_clipping,
|
||||
audio_gapless_enabled: audio.gapless_enabled,
|
||||
audio_pw_force_bitperfect: audio.pw_force_bitperfect,
|
||||
audio_stream_buffer_seconds: audio.stream_buffer_seconds,
|
||||
|
||||
@@ -681,6 +681,7 @@ const AUDIO_PORTABLE: &[&str] = &[
|
||||
"playback_cache",
|
||||
"normalization_enabled",
|
||||
"normalization_target_lufs",
|
||||
"normalization_prevent_clipping",
|
||||
"gapless_enabled",
|
||||
"allow_quality_fallback",
|
||||
"sync_audio_on_startup",
|
||||
@@ -1209,6 +1210,9 @@ fn apply_audio_writes(data_root: &Path, writes: &[(&str, &Value)]) -> Result<(),
|
||||
"normalization_target_lufs" => {
|
||||
store.set_normalization_target_lufs(value.as_f64().unwrap_or(-14.0) as f32)?
|
||||
}
|
||||
"normalization_prevent_clipping" => {
|
||||
store.set_normalization_prevent_clipping(as_bool(value))?
|
||||
}
|
||||
"gapless_enabled" => store.set_gapless_enabled(as_bool(value))?,
|
||||
"allow_quality_fallback" => store.set_allow_quality_fallback(as_bool(value))?,
|
||||
"sync_audio_on_startup" => store.set_sync_audio_on_startup(as_bool(value))?,
|
||||
|
||||
@@ -80,6 +80,7 @@ fn portable_fields_apply_verbatim() {
|
||||
"gapless_enabled": true,
|
||||
"stream_buffer_seconds": 4,
|
||||
"normalization_target_lufs": -18.0,
|
||||
"normalization_prevent_clipping": false,
|
||||
"sync_audio_on_startup": true
|
||||
},
|
||||
"prefs": { "streaming_quality": "hires_plus" }
|
||||
|
||||
@@ -53,6 +53,10 @@ pub struct AudioSettings {
|
||||
/// Target loudness in LUFS for normalization.
|
||||
/// Common values: -14.0 (Spotify/YouTube), -18.0 (audiophile), -23.0 (EBU broadcast)
|
||||
pub normalization_target_lufs: f32,
|
||||
/// When true (default), the normalization boost is capped so the track's
|
||||
/// known peak stays 1 dB under full scale; off lets quiet tracks reach
|
||||
/// the target even if their peaks clip.
|
||||
pub normalization_prevent_clipping: bool,
|
||||
/// When true, consecutive same-format tracks play without gap.
|
||||
/// Works on Rodio (PipeWire/Pulse) and ALSA Direct backends. Requires cached tracks.
|
||||
pub gapless_enabled: bool,
|
||||
@@ -137,6 +141,7 @@ impl Default for AudioSettings {
|
||||
device_sample_rate_limits: HashMap::new(), // Per-device limits (empty = no limit)
|
||||
normalization_enabled: false, // Off by default — preserves bit-perfect pipeline
|
||||
normalization_target_lufs: -14.0, // Spotify/YouTube standard
|
||||
normalization_prevent_clipping: true, // Peak-aware boost cap (2026-09)
|
||||
gapless_enabled: true, // On by default — works for same-format tracks on all backends
|
||||
pw_force_bitperfect: false, // Off by default — experimental PipeWire feature
|
||||
sync_audio_on_startup: false, // Off by default — opt-in for stale-settings edge case
|
||||
@@ -225,6 +230,10 @@ impl AudioSettingsStore {
|
||||
"ALTER TABLE audio_settings ADD COLUMN normalization_target_lufs REAL DEFAULT -14.0",
|
||||
[],
|
||||
);
|
||||
let _ = conn.execute(
|
||||
"ALTER TABLE audio_settings ADD COLUMN normalization_prevent_clipping INTEGER DEFAULT 1",
|
||||
[],
|
||||
);
|
||||
let _ = conn.execute(
|
||||
"ALTER TABLE audio_settings ADD COLUMN gapless_enabled INTEGER DEFAULT 0",
|
||||
[],
|
||||
@@ -326,7 +335,7 @@ impl AudioSettingsStore {
|
||||
pub fn get_settings(&self) -> Result<AudioSettings, String> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT output_device, exclusive_mode, dac_passthrough, preferred_sample_rate, backend_type, alsa_plugin, alsa_hardware_volume, stream_first_track, stream_buffer_seconds, streaming_only, limit_quality_to_device, device_max_sample_rate, normalization_enabled, normalization_target_lufs, gapless_enabled, device_sample_rate_limits, pw_force_bitperfect, sync_audio_on_startup, quality_fallback_behavior, skip_sink_switch, allow_quality_fallback, reserve_dac_while_running, dsd_mode, alsa_hardware_volume_controls, playback_cache FROM audio_settings WHERE id = 1",
|
||||
"SELECT output_device, exclusive_mode, dac_passthrough, preferred_sample_rate, backend_type, alsa_plugin, alsa_hardware_volume, stream_first_track, stream_buffer_seconds, streaming_only, limit_quality_to_device, device_max_sample_rate, normalization_enabled, normalization_target_lufs, gapless_enabled, device_sample_rate_limits, pw_force_bitperfect, sync_audio_on_startup, quality_fallback_behavior, skip_sink_switch, allow_quality_fallback, reserve_dac_while_running, dsd_mode, alsa_hardware_volume_controls, playback_cache, normalization_prevent_clipping FROM audio_settings WHERE id = 1",
|
||||
[],
|
||||
|row| {
|
||||
// Parse backend_type from JSON string
|
||||
@@ -371,6 +380,7 @@ impl AudioSettingsStore {
|
||||
device_sample_rate_limits,
|
||||
normalization_enabled: row.get::<_, Option<i64>>(12)?.unwrap_or(0) != 0,
|
||||
normalization_target_lufs: row.get::<_, Option<f64>>(13)?.unwrap_or(-14.0) as f32,
|
||||
normalization_prevent_clipping: row.get::<_, Option<i64>>(25)?.unwrap_or(1) != 0,
|
||||
gapless_enabled: row.get::<_, Option<i64>>(14)?.unwrap_or(0) != 0,
|
||||
pw_force_bitperfect: row.get::<_, Option<i64>>(16)?.unwrap_or(0) != 0,
|
||||
sync_audio_on_startup: row.get::<_, Option<i64>>(17)?.unwrap_or(0) != 0,
|
||||
@@ -803,6 +813,16 @@ impl AudioSettingsStore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_normalization_prevent_clipping(&self, on: bool) -> Result<(), String> {
|
||||
self.conn
|
||||
.execute(
|
||||
"UPDATE audio_settings SET normalization_prevent_clipping = ?1 WHERE id = 1",
|
||||
params![on as i64],
|
||||
)
|
||||
.map_err(|e| format!("Failed to set normalization clipping guard: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset all audio settings to their default values
|
||||
pub fn reset_all(&self) -> Result<AudioSettings, String> {
|
||||
// ADR-003: quality_fallback_behavior must survive reset_all()
|
||||
@@ -853,6 +873,7 @@ impl AudioSettingsStore {
|
||||
allow_quality_fallback = ?20,
|
||||
reserve_dac_while_running = ?21,
|
||||
alsa_hardware_volume_controls = ?22,
|
||||
normalization_prevent_clipping = ?23,
|
||||
playback_cache = '{}'
|
||||
WHERE id = 1",
|
||||
params![
|
||||
@@ -878,6 +899,7 @@ impl AudioSettingsStore {
|
||||
defaults.allow_quality_fallback as i64,
|
||||
defaults.reserve_dac_while_running as i64,
|
||||
mixer_controls_json,
|
||||
defaults.normalization_prevent_clipping as i64,
|
||||
],
|
||||
)
|
||||
.map_err(|e| format!("Failed to reset audio settings: {}", e))?;
|
||||
@@ -1179,6 +1201,7 @@ mod tests {
|
||||
"device_max_sample_rate": null,
|
||||
"normalization_enabled": false,
|
||||
"normalization_target_lufs": -14.0,
|
||||
"normalization_prevent_clipping": true,
|
||||
"gapless_enabled": true,
|
||||
"pw_force_bitperfect": false,
|
||||
"sync_audio_on_startup": false,
|
||||
|
||||
@@ -791,6 +791,7 @@ fn build_audio_rows(g: &Gathered) -> Vec<Row> {
|
||||
"—",
|
||||
0,
|
||||
),
|
||||
row("Normalization Clipping Guard", yn(d.audio_normalization_prevent_clipping), "—", 0),
|
||||
row("Gapless", yn(d.audio_gapless_enabled), "—", 0),
|
||||
row(
|
||||
"PW Force Bitperfect",
|
||||
@@ -1092,6 +1093,7 @@ fn build_export_json(g: &Gathered) -> Value {
|
||||
"alsaHardwareVolumeProbe": d.audio_alsa_hardware_volume_probe,
|
||||
"normalizationEnabled": d.audio_normalization_enabled,
|
||||
"normalizationTargetLufs": d.audio_normalization_target_lufs,
|
||||
"normalizationPreventClipping": d.audio_normalization_prevent_clipping,
|
||||
"gaplessEnabled": d.audio_gapless_enabled,
|
||||
"pwForceBitperfect": d.audio_pw_force_bitperfect,
|
||||
"streamBufferSeconds": d.audio_stream_buffer_seconds,
|
||||
|
||||
@@ -90,6 +90,7 @@ const KEY_TABLE: &[(&str, ApplyClass)] = &[
|
||||
("audio.gapless_enabled", ApplyClass::Reload),
|
||||
("audio.normalization_enabled", ApplyClass::Reload),
|
||||
("audio.normalization_target_lufs", ApplyClass::Reload),
|
||||
("audio.normalization_prevent_clipping", ApplyClass::Reload),
|
||||
("audio.pw_force_bitperfect", ApplyClass::Reload),
|
||||
("audio.reserve_dac_while_running", ApplyClass::Reload),
|
||||
("audio.sync_audio_on_startup", ApplyClass::Reload),
|
||||
@@ -402,6 +403,7 @@ fn read_all(roots: &ProfileRoots) -> Result<Vec<(&'static str, String)>, String>
|
||||
"audio.gapless_enabled" => render_bool(audio.gapless_enabled),
|
||||
"audio.normalization_enabled" => render_bool(audio.normalization_enabled),
|
||||
"audio.normalization_target_lufs" => audio.normalization_target_lufs.to_string(),
|
||||
"audio.normalization_prevent_clipping" => render_bool(audio.normalization_prevent_clipping),
|
||||
"audio.pw_force_bitperfect" => render_bool(audio.pw_force_bitperfect),
|
||||
"audio.reserve_dac_while_running" => render_bool(audio.reserve_dac_while_running),
|
||||
"audio.sync_audio_on_startup" => render_bool(audio.sync_audio_on_startup),
|
||||
@@ -760,6 +762,13 @@ pub(crate) fn write_one(
|
||||
.set_normalization_target_lufs(v)
|
||||
.map_err(SetError::Io)?
|
||||
}
|
||||
"audio.normalization_prevent_clipping" => {
|
||||
let v = parse_bool(raw).map_err(SetError::Usage)?;
|
||||
open_audio(roots)
|
||||
.map_err(SetError::Io)?
|
||||
.set_normalization_prevent_clipping(v)
|
||||
.map_err(SetError::Io)?
|
||||
}
|
||||
"audio.pw_force_bitperfect" => {
|
||||
let v = parse_bool(raw).map_err(SetError::Usage)?;
|
||||
open_audio(roots)
|
||||
|
||||
Reference in new issue
Block a user