Spaces:
Paused
Paused
File size: 10,548 Bytes
9d3cb0a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
import copy
import julius
import numpy as np
import scipy
import torch
import torch.nn.functional as F
import torchaudio
class Meter(torch.nn.Module):
"""Tensorized version of pyloudnorm.Meter. Works with batched audio tensors.
Parameters
----------
rate : int
Sample rate of audio.
filter_class : str, optional
Class of weighting filter used.
K-weighting' (default), 'Fenton/Lee 1'
'Fenton/Lee 2', 'Dash et al.'
by default "K-weighting"
block_size : float, optional
Gating block size in seconds, by default 0.400
zeros : int, optional
Number of zeros to use in FIR approximation of
IIR filters, by default 512
use_fir : bool, optional
Whether to use FIR approximation or exact IIR formulation.
If computing on GPU, ``use_fir=True`` will be used, as its
much faster, by default False
"""
def __init__(
self,
rate: int,
filter_class: str = "K-weighting",
block_size: float = 0.400,
zeros: int = 512,
use_fir: bool = False,
):
super().__init__()
self.rate = rate
self.filter_class = filter_class
self.block_size = block_size
self.use_fir = use_fir
G = torch.from_numpy(np.array([1.0, 1.0, 1.0, 1.41, 1.41]))
self.register_buffer("G", G)
# Compute impulse responses so that filtering is fast via
# a convolution at runtime, on GPU, unlike lfilter.
impulse = np.zeros((zeros,))
impulse[..., 0] = 1.0
firs = np.zeros((len(self._filters), 1, zeros))
passband_gain = torch.zeros(len(self._filters))
for i, (_, filter_stage) in enumerate(self._filters.items()):
firs[i] = scipy.signal.lfilter(filter_stage.b, filter_stage.a, impulse)
passband_gain[i] = filter_stage.passband_gain
firs = torch.from_numpy(firs[..., ::-1].copy()).float()
self.register_buffer("firs", firs)
self.register_buffer("passband_gain", passband_gain)
def apply_filter_gpu(self, data: torch.Tensor):
"""Performs FIR approximation of loudness computation.
Parameters
----------
data : torch.Tensor
Audio data of shape (nb, nch, nt).
Returns
-------
torch.Tensor
Filtered audio data.
"""
# Data is of shape (nb, nch, nt)
# Reshape to (nb*nch, 1, nt)
nb, nt, nch = data.shape
data = data.permute(0, 2, 1)
data = data.reshape(nb * nch, 1, nt)
# Apply padding
pad_length = self.firs.shape[-1]
# Apply filtering in sequence
for i in range(self.firs.shape[0]):
data = F.pad(data, (pad_length, pad_length))
data = julius.fftconv.fft_conv1d(data, self.firs[i, None, ...])
data = self.passband_gain[i] * data
data = data[..., 1 : nt + 1]
data = data.permute(0, 2, 1)
data = data[:, :nt, :]
return data
def apply_filter_cpu(self, data: torch.Tensor):
"""Performs IIR formulation of loudness computation.
Parameters
----------
data : torch.Tensor
Audio data of shape (nb, nch, nt).
Returns
-------
torch.Tensor
Filtered audio data.
"""
for _, filter_stage in self._filters.items():
passband_gain = filter_stage.passband_gain
a_coeffs = torch.from_numpy(filter_stage.a).float().to(data.device)
b_coeffs = torch.from_numpy(filter_stage.b).float().to(data.device)
_data = data.permute(0, 2, 1)
filtered = torchaudio.functional.lfilter(
_data, a_coeffs, b_coeffs, clamp=False
)
data = passband_gain * filtered.permute(0, 2, 1)
return data
def apply_filter(self, data: torch.Tensor):
"""Applies filter on either CPU or GPU, depending
on if the audio is on GPU or is on CPU, or if
``self.use_fir`` is True.
Parameters
----------
data : torch.Tensor
Audio data of shape (nb, nch, nt).
Returns
-------
torch.Tensor
Filtered audio data.
"""
if data.is_cuda or self.use_fir:
data = self.apply_filter_gpu(data)
else:
data = self.apply_filter_cpu(data)
return data
def forward(self, data: torch.Tensor):
"""Computes integrated loudness of data.
Parameters
----------
data : torch.Tensor
Audio data of shape (nb, nch, nt).
Returns
-------
torch.Tensor
Filtered audio data.
"""
return self.integrated_loudness(data)
def _unfold(self, input_data):
T_g = self.block_size
overlap = 0.75 # overlap of 75% of the block duration
step = 1.0 - overlap # step size by percentage
kernel_size = int(T_g * self.rate)
stride = int(T_g * self.rate * step)
unfolded = julius.core.unfold(input_data.permute(0, 2, 1), kernel_size, stride)
unfolded = unfolded.transpose(-1, -2)
return unfolded
def integrated_loudness(self, data: torch.Tensor):
"""Computes integrated loudness of data.
Parameters
----------
data : torch.Tensor
Audio data of shape (nb, nch, nt).
Returns
-------
torch.Tensor
Filtered audio data.
"""
if not torch.is_tensor(data):
data = torch.from_numpy(data).float()
else:
data = data.float()
input_data = copy.copy(data)
# Data always has a batch and channel dimension.
# Is of shape (nb, nt, nch)
if input_data.ndim < 2:
input_data = input_data.unsqueeze(-1)
if input_data.ndim < 3:
input_data = input_data.unsqueeze(0)
nb, nt, nch = input_data.shape
# Apply frequency weighting filters - account
# for the acoustic respose of the head and auditory system
input_data = self.apply_filter(input_data)
G = self.G # channel gains
T_g = self.block_size # 400 ms gating block standard
Gamma_a = -70.0 # -70 LKFS = absolute loudness threshold
unfolded = self._unfold(input_data)
z = (1.0 / (T_g * self.rate)) * unfolded.square().sum(2)
l = -0.691 + 10.0 * torch.log10((G[None, :nch, None] * z).sum(1, keepdim=True))
l = l.expand_as(z)
# find gating block indices above absolute threshold
z_avg_gated = z
z_avg_gated[l <= Gamma_a] = 0
masked = l > Gamma_a
z_avg_gated = z_avg_gated.sum(2) / masked.sum(2)
# calculate the relative threshold value (see eq. 6)
Gamma_r = (
-0.691 + 10.0 * torch.log10((z_avg_gated * G[None, :nch]).sum(-1)) - 10.0
)
Gamma_r = Gamma_r[:, None, None]
Gamma_r = Gamma_r.expand(nb, nch, l.shape[-1])
# find gating block indices above relative and absolute thresholds (end of eq. 7)
z_avg_gated = z
z_avg_gated[l <= Gamma_a] = 0
z_avg_gated[l <= Gamma_r] = 0
masked = (l > Gamma_a) * (l > Gamma_r)
z_avg_gated = z_avg_gated.sum(2) / masked.sum(2)
# # Cannot use nan_to_num (pytorch 1.8 does not come with GCP-supported cuda version)
# z_avg_gated = torch.nan_to_num(z_avg_gated)
z_avg_gated = torch.where(
z_avg_gated.isnan(), torch.zeros_like(z_avg_gated), z_avg_gated
)
z_avg_gated[z_avg_gated == float("inf")] = float(np.finfo(np.float32).max)
z_avg_gated[z_avg_gated == -float("inf")] = float(np.finfo(np.float32).min)
LUFS = -0.691 + 10.0 * torch.log10((G[None, :nch] * z_avg_gated).sum(1))
return LUFS.float()
@property
def filter_class(self):
return self._filter_class
@filter_class.setter
def filter_class(self, value):
from pyloudnorm import Meter
meter = Meter(self.rate)
meter.filter_class = value
self._filter_class = value
self._filters = meter._filters
class LoudnessMixin:
_loudness = None
MIN_LOUDNESS = -70
"""Minimum loudness possible."""
def loudness(
self, filter_class: str = "K-weighting", block_size: float = 0.400, **kwargs
):
"""Calculates loudness using an implementation of ITU-R BS.1770-4.
Allows control over gating block size and frequency weighting filters for
additional control. Measure the integrated gated loudness of a signal.
API is derived from PyLoudnorm, but this implementation is ported to PyTorch
and is tensorized across batches. When on GPU, an FIR approximation of the IIR
filters is used to compute loudness for speed.
Uses the weighting filters and block size defined by the meter
the integrated loudness is measured based upon the gating algorithm
defined in the ITU-R BS.1770-4 specification.
Parameters
----------
filter_class : str, optional
Class of weighting filter used.
K-weighting' (default), 'Fenton/Lee 1'
'Fenton/Lee 2', 'Dash et al.'
by default "K-weighting"
block_size : float, optional
Gating block size in seconds, by default 0.400
kwargs : dict, optional
Keyword arguments to :py:func:`audiotools.core.loudness.Meter`.
Returns
-------
torch.Tensor
Loudness of audio data.
"""
if self._loudness is not None:
return self._loudness.to(self.device)
original_length = self.signal_length
if self.signal_duration < 0.5:
pad_len = int((0.5 - self.signal_duration) * self.sample_rate)
self.zero_pad(0, pad_len)
# create BS.1770 meter
meter = Meter(
self.sample_rate, filter_class=filter_class, block_size=block_size, **kwargs
)
meter = meter.to(self.device)
# measure loudness
loudness = meter.integrated_loudness(self.audio_data.permute(0, 2, 1))
self.truncate_samples(original_length)
min_loudness = (
torch.ones_like(loudness, device=loudness.device) * self.MIN_LOUDNESS
)
self._loudness = torch.maximum(loudness, min_loudness)
return self._loudness.to(self.device)
|