mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-22 06:25:41 +00:00
refactor(audio): unify loudness/replay-gain prep via TrackGainInputs
audio_play and audio_chain_preload were both reading the same engine state (target_lufs, norm_mode, pre_analysis_db) and resolving the loudness cache, then feeding it into compute_gain — but with subtly different intermediate steps. audio_play split the resolve+post-resolve into two operations so it could log the cache value; chain_preload used the bundled `loudness_gain_db_or_startup` wrapper. Lift the shared logic into `resolve_track_gain_inputs(state, app, url, logical_id, js_loudness_gain_db) -> TrackGainInputs` in helpers.rs. The struct returns target_lufs, norm_mode, the cache-loudness value (for logging), and the post-resolve effective loudness for compute_gain. Both call sites now use the same helper; behaviour-preserving. Drops the now-unused `loudness_gain_db_or_startup` (audio_chain_preload was its only caller). audio/commands.rs: 676 → 642 LOC.
This commit is contained in:
@@ -152,32 +152,12 @@ pub async fn audio_play(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let target_lufs = f32::from_bits(state.normalization_target_lufs.load(Ordering::Relaxed));
|
||||
let cache_loudness = resolve_loudness_gain_from_cache(
|
||||
&app,
|
||||
&url,
|
||||
target_lufs,
|
||||
logical_trim.as_deref(),
|
||||
);
|
||||
let resolved_loudness_gain_db = cache_loudness;
|
||||
let norm_mode = state.normalization_engine.load(Ordering::Relaxed);
|
||||
let pre_analysis_db = loudness_pre_analysis_db_for_engine(&state);
|
||||
let startup_loudness_gain_db = if norm_mode == 2 {
|
||||
loudness_gain_db_after_resolve(
|
||||
cache_loudness,
|
||||
target_lufs,
|
||||
pre_analysis_db,
|
||||
false,
|
||||
loudness_gain_db,
|
||||
)
|
||||
} else {
|
||||
cache_loudness
|
||||
};
|
||||
let gain_inputs = resolve_track_gain_inputs(&state, &app, &url, logical_trim.as_deref(), loudness_gain_db);
|
||||
let (gain_linear, effective_volume) = compute_gain(
|
||||
norm_mode,
|
||||
gain_inputs.norm_mode,
|
||||
replay_gain_db,
|
||||
replay_gain_peak,
|
||||
startup_loudness_gain_db,
|
||||
gain_inputs.effective_loudness_db,
|
||||
pre_gain_db,
|
||||
fallback_db,
|
||||
volume,
|
||||
@@ -186,22 +166,22 @@ pub async fn audio_play(
|
||||
crate::app_deprintln!(
|
||||
"[normalization] audio_play track_id={:?} engine={} replay_gain_db={:?} replay_gain_peak={:?} loudness_gain_db={:?} gain_linear={:.4} current_gain_db={:?} target_lufs={:.2} volume={:.3} effective_volume={:.3}",
|
||||
playback_identity(&url),
|
||||
normalization_engine_name(norm_mode),
|
||||
normalization_engine_name(gain_inputs.norm_mode),
|
||||
replay_gain_db,
|
||||
replay_gain_peak,
|
||||
resolved_loudness_gain_db,
|
||||
gain_inputs.cache_loudness_db,
|
||||
gain_linear,
|
||||
current_gain_db,
|
||||
target_lufs,
|
||||
gain_inputs.target_lufs,
|
||||
volume,
|
||||
effective_volume
|
||||
);
|
||||
maybe_emit_normalization_state(
|
||||
&app,
|
||||
NormalizationStatePayload {
|
||||
engine: normalization_engine_name(norm_mode).to_string(),
|
||||
engine: normalization_engine_name(gain_inputs.norm_mode).to_string(),
|
||||
current_gain_db,
|
||||
target_lufs,
|
||||
target_lufs: gain_inputs.target_lufs,
|
||||
},
|
||||
);
|
||||
|
||||
@@ -574,27 +554,12 @@ pub async fn audio_chain_preload(
|
||||
// current track ends, and `Sink::set_volume` affects the WHOLE Sink (incl.
|
||||
// the still-playing current source). Volume for the chained track is
|
||||
// applied at the gapless transition in `spawn_progress_task`, not here.
|
||||
let target_lufs = f32::from_bits(state.normalization_target_lufs.load(Ordering::Relaxed));
|
||||
let norm_mode = state.normalization_engine.load(Ordering::Relaxed);
|
||||
let pre_analysis_db = loudness_pre_analysis_db_for_engine(&state);
|
||||
let chain_loudness_db = if norm_mode == 2 {
|
||||
loudness_gain_db_or_startup(
|
||||
&app,
|
||||
&url,
|
||||
target_lufs,
|
||||
logical_trim.as_deref(),
|
||||
pre_analysis_db,
|
||||
false,
|
||||
loudness_gain_db,
|
||||
)
|
||||
} else {
|
||||
resolve_loudness_gain_from_cache(&app, &url, target_lufs, logical_trim.as_deref())
|
||||
};
|
||||
let gain_inputs = resolve_track_gain_inputs(&state, &app, &url, logical_trim.as_deref(), loudness_gain_db);
|
||||
let (gain_linear, _effective_volume) = compute_gain(
|
||||
norm_mode,
|
||||
gain_inputs.norm_mode,
|
||||
replay_gain_db,
|
||||
replay_gain_peak,
|
||||
chain_loudness_db,
|
||||
gain_inputs.effective_loudness_db,
|
||||
pre_gain_db,
|
||||
fallback_db,
|
||||
volume,
|
||||
|
||||
Reference in New Issue
Block a user