File size: 752 Bytes
3e21dc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)