| import torch | |
| def ild_gain(distance_m: torch.Tensor, | |
| clamp_min: float = 0.2, | |
| clamp_max: float = 5.0) -> torch.Tensor: | |
| """ | |
| Returns ILD gain (1/d² attenuation) for each ear. | |
| distance_m: scalar or tensor of shape (B,) | |
| Output: gain factor(s) ∈ [0, 1], same shape | |
| """ | |
| gain = 1.0 / torch.clamp(distance_m, min=clamp_min, max=clamp_max).pow(2) | |
| return gain | |
| def apply_ild(left: torch.Tensor, right: torch.Tensor, | |
| gain_left: torch.Tensor, gain_right: torch.Tensor) -> torch.Tensor: | |
| """ | |
| Apply ILD gains to L/R signals. Inputs: (B, T) | |
| Output: (B, 2, T) stereo | |
| """ | |
| return torch.stack([left * gain_left[:, None], | |
| right * gain_right[:, None]], dim=1) | |