Apply the envelope gain before noise injection, not after
Browse files at this commit90930ddb97b839891b5b2ed518f69ac7f656d05eParent: 92f96f7- fix
- apply the envelope gain before noise injection, not after
3 changed files +37 -24
- Cargo.lock +2 -0
- crates/ec-aac/src/sbr_env.rs +30 -24
- crates/ec-aac/src/sbr_payload.rs +5 -0
Cargo.lockmodified+2 -0
| @@ -228,6 +228,8 @@ version = "0.1.0" | |||
| 228 | dependencies = [ | 228 | dependencies = [ |
| 229 | "ec-core", | 229 | "ec-core", |
| 230 | "ec-dsp", | 230 | "ec-dsp", |
| 231 | + "ec-matroska", | ||
| 232 | + "ec-mp4", | ||
| 231 | ] | 233 | ] |
| 232 | 234 | ||
| 233 | [[package]] | 235 | [[package]] |
crates/ec-aac/src/sbr_env.rsmodified+30 -24
| @@ -152,30 +152,6 @@ pub fn adjust( | |||
| 152 | let kx = tables.kx as usize; | 152 | let kx = tables.kx as usize; |
| 153 | let limiter_max = LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)]; | 153 | let limiter_max = LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)]; |
| 154 | 154 | ||
| 155 | - // Noise floor first: every target bin gets the transmitted floor for | ||
| 156 | - // its noise-time-segment and noise-frequency-band, independent of the | ||
| 157 | - // envelope grid (they are different, coarser grids over the same axes). | ||
| 158 | - for (ni, (t0, t1)) in ch.t_noise.windows(2).map(|w| (w[0], w[1])).enumerate() { | ||
| 159 | - if ni >= noise_energy.len() { | ||
| 160 | - break; | ||
| 161 | - } | ||
| 162 | - for q in 0..tables.n_q { | ||
| 163 | - let lo = tables.f_noise[q] as usize; | ||
| 164 | - let hi = tables.f_noise[q + 1] as usize; | ||
| 165 | - let energy = noise_energy[ni].get(q).copied().unwrap_or(0.0); | ||
| 166 | - let amp = (energy / (hi - lo).max(1) as f64).sqrt(); | ||
| 167 | - for band in lo..hi { | ||
| 168 | - if band < kx || band - kx >= hf.len() { | ||
| 169 | - continue; | ||
| 170 | - } | ||
| 171 | - let row = &mut hf[band - kx]; | ||
| 172 | - for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { | ||
| 173 | - row[slot] = row[slot] + rng.complex_unit().scale(amp); | ||
| 174 | - } | ||
| 175 | - } | ||
| 176 | - } | ||
| 177 | - } | ||
| 178 | - | ||
| 179 | // Gain toward the transmitted envelope, per (envelope, band) cell, | 155 | // Gain toward the transmitted envelope, per (envelope, band) cell, |
| 180 | // limited relative to that cell's own noise floor. | 156 | // limited relative to that cell's own noise floor. |
| 181 | for (ei, (t0, t1)) in ch.t_env.windows(2).map(|w| (w[0], w[1])).enumerate() { | 157 | for (ei, (t0, t1)) in ch.t_env.windows(2).map(|w| (w[0], w[1])).enumerate() { |
| @@ -270,6 +246,36 @@ pub fn adjust( | |||
| 270 | } | 246 | } |
| 271 | } | 247 | } |
| 272 | } | 248 | } |
| 249 | + | ||
| 250 | + // Noise floor last: every target bin gets the transmitted floor for its | ||
| 251 | + // noise-time-segment and noise-frequency-band, independent of the | ||
| 252 | + // envelope grid (they are different, coarser grids over the same axes). | ||
| 253 | + // This must run after the envelope-gain loop above -- gain is computed | ||
| 254 | + // from the HF-signal-only estimate and must not also rescale the noise | ||
| 255 | + // that lands on top of it (injecting noise first made the gain step | ||
| 256 | + // measure signal+noise together and rescale both, which distorts the | ||
| 257 | + // noise/signal split by however much noise dominates a given cell -- a | ||
| 258 | + // content- and channel-dependent error, not a uniform scale bug). | ||
| 259 | + for (ni, (t0, t1)) in ch.t_noise.windows(2).map(|w| (w[0], w[1])).enumerate() { | ||
| 260 | + if ni >= noise_energy.len() { | ||
| 261 | + break; | ||
| 262 | + } | ||
| 263 | + for q in 0..tables.n_q { | ||
| 264 | + let lo = tables.f_noise[q] as usize; | ||
| 265 | + let hi = tables.f_noise[q + 1] as usize; | ||
| 266 | + let energy = noise_energy[ni].get(q).copied().unwrap_or(0.0); | ||
| 267 | + let amp = (energy / (hi - lo).max(1) as f64).sqrt(); | ||
| 268 | + for band in lo..hi { | ||
| 269 | + if band < kx || band - kx >= hf.len() { | ||
| 270 | + continue; | ||
| 271 | + } | ||
| 272 | + let row = &mut hf[band - kx]; | ||
| 273 | + for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { | ||
| 274 | + row[slot] = row[slot] + rng.complex_unit().scale(amp); | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + } | ||
| 278 | + } | ||
| 273 | } | 279 | } |
| 274 | 280 | ||
| 275 | fn noise_band_for(tables: &BandTables, low_band: usize) -> usize { | 281 | fn noise_band_for(tables: &BandTables, low_band: usize) -> usize { |
crates/ec-aac/src/sbr_payload.rsmodified+5 -0
| @@ -320,6 +320,11 @@ impl SbrParser { | |||
| 320 | row[b] = row[b - 1] + delta; | 320 | row[b] = row[b - 1] + delta; |
| 321 | } | 321 | } |
| 322 | } | 322 | } |
| 323 | + if std::env::var("EC_AAC_SBR_ENV_DEBUG").is_ok() { | ||
| 324 | + eprintln!( | ||
| 325 | + "ENVDBG dt={dt} balance={balance} amp_res={amp_res} bands={bands} prev={prev:?} row={row:?}" | ||
| 326 | + ); | ||
| 327 | + } | ||
| 323 | Ok(row) | 328 | Ok(row) |
| 324 | } | 329 | } |
| 325 | 330 | ||