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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""First-Order Ambisonics (FOA) utilities."""
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
from typing import Tuple
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def deg2rad(degrees: float) -> float:
|
| 9 |
+
"""Convert degrees to radians."""
|
| 10 |
+
return degrees * np.pi / 180.0
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def encode_foa_analytic(
|
| 14 |
+
mono: np.ndarray,
|
| 15 |
+
azimuth_deg: float,
|
| 16 |
+
elevation_deg: float,
|
| 17 |
+
normalization: str = "SN3D"
|
| 18 |
+
) -> np.ndarray:
|
| 19 |
+
"""
|
| 20 |
+
Encode mono signal to FOA using analytic panning.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
mono: Mono audio signal, shape (n_samples,)
|
| 24 |
+
azimuth_deg: Azimuth angle in degrees (-180 to 180, 0=front)
|
| 25 |
+
elevation_deg: Elevation angle in degrees (-90 to 90, 0=level)
|
| 26 |
+
normalization: "SN3D" or "N3D"
|
| 27 |
+
|
| 28 |
+
Returns:
|
| 29 |
+
FOA signal, shape (4, n_samples) with channels [W, X, Y, Z]
|
| 30 |
+
"""
|
| 31 |
+
theta = deg2rad(azimuth_deg)
|
| 32 |
+
phi = deg2rad(elevation_deg)
|
| 33 |
+
|
| 34 |
+
# Standard FOA encoding
|
| 35 |
+
W = mono / np.sqrt(2) # Omnidirectional (SN3D normalization)
|
| 36 |
+
X = mono * np.cos(theta) * np.cos(phi) # Left-Right
|
| 37 |
+
Y = mono * np.sin(theta) * np.cos(phi) # Front-Back
|
| 38 |
+
Z = mono * np.sin(phi) # Up-Down
|
| 39 |
+
|
| 40 |
+
foa = np.stack([W, X, Y, Z], axis=0)
|
| 41 |
+
|
| 42 |
+
if normalization == "N3D":
|
| 43 |
+
# Convert SN3D to N3D (scale W by sqrt(2))
|
| 44 |
+
foa[0] *= np.sqrt(2)
|
| 45 |
+
|
| 46 |
+
return foa
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def encode_foa_analytic_torch(
|
| 50 |
+
mono: torch.Tensor,
|
| 51 |
+
azimuth_deg: float,
|
| 52 |
+
elevation_deg: float,
|
| 53 |
+
normalization: str = "SN3D"
|
| 54 |
+
) -> torch.Tensor:
|
| 55 |
+
"""
|
| 56 |
+
PyTorch version of FOA encoding.
|
| 57 |
+
|
| 58 |
+
Args:
|
| 59 |
+
mono: Mono audio signal, shape (batch, n_samples) or (n_samples,)
|
| 60 |
+
azimuth_deg: Azimuth angle in degrees
|
| 61 |
+
elevation_deg: Elevation angle in degrees
|
| 62 |
+
normalization: "SN3D" or "N3D"
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
FOA signal, shape (batch, 4, n_samples) or (4, n_samples)
|
| 66 |
+
"""
|
| 67 |
+
theta = torch.tensor(deg2rad(azimuth_deg), dtype=mono.dtype, device=mono.device)
|
| 68 |
+
phi = torch.tensor(deg2rad(elevation_deg), dtype=mono.dtype, device=mono.device)
|
| 69 |
+
|
| 70 |
+
# Add batch dim if needed
|
| 71 |
+
if mono.ndim == 1:
|
| 72 |
+
mono = mono.unsqueeze(0)
|
| 73 |
+
squeeze_output = True
|
| 74 |
+
else:
|
| 75 |
+
squeeze_output = False
|
| 76 |
+
|
| 77 |
+
# Standard FOA encoding
|
| 78 |
+
W = mono / np.sqrt(2)
|
| 79 |
+
X = mono * torch.cos(theta) * torch.cos(phi)
|
| 80 |
+
Y = mono * torch.sin(theta) * torch.cos(phi)
|
| 81 |
+
Z = mono * torch.sin(phi)
|
| 82 |
+
|
| 83 |
+
foa = torch.stack([W, X, Y, Z], dim=1) # (batch, 4, n_samples)
|
| 84 |
+
|
| 85 |
+
if normalization == "N3D":
|
| 86 |
+
foa[:, 0] *= np.sqrt(2)
|
| 87 |
+
|
| 88 |
+
if squeeze_output:
|
| 89 |
+
foa = foa.squeeze(0)
|
| 90 |
+
|
| 91 |
+
return foa
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def compute_intensity_vector(foa: np.ndarray) -> Tuple[float, float]:
|
| 95 |
+
"""
|
| 96 |
+
Compute azimuth and elevation from FOA intensity vector.
|
| 97 |
+
|
| 98 |
+
Args:
|
| 99 |
+
foa: FOA signal, shape (4, n_samples)
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
(azimuth_deg, elevation_deg)
|
| 103 |
+
"""
|
| 104 |
+
W, X, Y, Z = foa
|
| 105 |
+
|
| 106 |
+
# Compute time-averaged intensity vector
|
| 107 |
+
Ix = np.mean(W * X)
|
| 108 |
+
Iy = np.mean(W * Y)
|
| 109 |
+
Iz = np.mean(W * Z)
|
| 110 |
+
|
| 111 |
+
# Convert to angles
|
| 112 |
+
azimuth_rad = np.arctan2(Iy, Ix)
|
| 113 |
+
elevation_rad = np.arctan2(Iz, np.sqrt(Ix**2 + Iy**2))
|
| 114 |
+
|
| 115 |
+
azimuth_deg = azimuth_rad * 180.0 / np.pi
|
| 116 |
+
elevation_deg = elevation_rad * 180.0 / np.pi
|
| 117 |
+
|
| 118 |
+
return azimuth_deg, elevation_deg
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def compute_intensity_vector_torch(foa: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 122 |
+
"""
|
| 123 |
+
PyTorch version of intensity vector computation.
|
| 124 |
+
|
| 125 |
+
Args:
|
| 126 |
+
foa: FOA signal, shape (batch, 4, n_samples) or (4, n_samples)
|
| 127 |
+
|
| 128 |
+
Returns:
|
| 129 |
+
(azimuth_deg, elevation_deg) tensors
|
| 130 |
+
"""
|
| 131 |
+
if foa.ndim == 2:
|
| 132 |
+
foa = foa.unsqueeze(0)
|
| 133 |
+
squeeze_output = True
|
| 134 |
+
else:
|
| 135 |
+
squeeze_output = False
|
| 136 |
+
|
| 137 |
+
W, X, Y, Z = foa[:, 0], foa[:, 1], foa[:, 2], foa[:, 3]
|
| 138 |
+
|
| 139 |
+
# Compute time-averaged intensity vector
|
| 140 |
+
Ix = torch.mean(W * X, dim=-1)
|
| 141 |
+
Iy = torch.mean(W * Y, dim=-1)
|
| 142 |
+
Iz = torch.mean(W * Z, dim=-1)
|
| 143 |
+
|
| 144 |
+
# Convert to angles
|
| 145 |
+
azimuth_rad = torch.atan2(Iy, Ix)
|
| 146 |
+
elevation_rad = torch.atan2(Iz, torch.sqrt(Ix**2 + Iy**2))
|
| 147 |
+
|
| 148 |
+
azimuth_deg = azimuth_rad * 180.0 / np.pi
|
| 149 |
+
elevation_deg = elevation_rad * 180.0 / np.pi
|
| 150 |
+
|
| 151 |
+
if squeeze_output:
|
| 152 |
+
azimuth_deg = azimuth_deg.squeeze(0)
|
| 153 |
+
elevation_deg = elevation_deg.squeeze(0)
|
| 154 |
+
|
| 155 |
+
return azimuth_deg, elevation_deg
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def foa_to_stereo_simple(foa: np.ndarray) -> np.ndarray:
|
| 159 |
+
"""
|
| 160 |
+
Simple stereo downmix from FOA (just using W, X for L/R).
|
| 161 |
+
|
| 162 |
+
Args:
|
| 163 |
+
foa: FOA signal, shape (4, n_samples)
|
| 164 |
+
|
| 165 |
+
Returns:
|
| 166 |
+
Stereo signal, shape (2, n_samples)
|
| 167 |
+
"""
|
| 168 |
+
W, X, Y, Z = foa
|
| 169 |
+
|
| 170 |
+
# Simple stereo decode: L = W + X, R = W - X
|
| 171 |
+
L = (W + X) / np.sqrt(2)
|
| 172 |
+
R = (W - X) / np.sqrt(2)
|
| 173 |
+
|
| 174 |
+
return np.stack([L, R], axis=0)
|