Spaces:
Build error
Build error
File size: 9,459 Bytes
51da11a |
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 322 323 324 |
import torch
import numpy as np
import scipy.signal
from numba import jit
from deepafx_st.processors.processor import Processor
@jit(nopython=True)
def biqaud(
gain_dB: float,
cutoff_freq: float,
q_factor: float,
sample_rate: float,
filter_type: str,
):
"""Use design parameters to generate coeffieicnets for a specific filter type.
Args:
gain_dB (float): Shelving filter gain in dB.
cutoff_freq (float): Cutoff frequency in Hz.
q_factor (float): Q factor.
sample_rate (float): Sample rate in Hz.
filter_type (str): Filter type.
One of "low_shelf", "high_shelf", or "peaking"
Returns:
b (np.ndarray): Numerator filter coefficients stored as [b0, b1, b2]
a (np.ndarray): Denominator filter coefficients stored as [a0, a1, a2]
"""
A = 10 ** (gain_dB / 40.0)
w0 = 2.0 * np.pi * (cutoff_freq / sample_rate)
alpha = np.sin(w0) / (2.0 * q_factor)
cos_w0 = np.cos(w0)
sqrt_A = np.sqrt(A)
if filter_type == "high_shelf":
b0 = A * ((A + 1) + (A - 1) * cos_w0 + 2 * sqrt_A * alpha)
b1 = -2 * A * ((A - 1) + (A + 1) * cos_w0)
b2 = A * ((A + 1) + (A - 1) * cos_w0 - 2 * sqrt_A * alpha)
a0 = (A + 1) - (A - 1) * cos_w0 + 2 * sqrt_A * alpha
a1 = 2 * ((A - 1) - (A + 1) * cos_w0)
a2 = (A + 1) - (A - 1) * cos_w0 - 2 * sqrt_A * alpha
elif filter_type == "low_shelf":
b0 = A * ((A + 1) - (A - 1) * cos_w0 + 2 * sqrt_A * alpha)
b1 = 2 * A * ((A - 1) - (A + 1) * cos_w0)
b2 = A * ((A + 1) - (A - 1) * cos_w0 - 2 * sqrt_A * alpha)
a0 = (A + 1) + (A - 1) * cos_w0 + 2 * sqrt_A * alpha
a1 = -2 * ((A - 1) + (A + 1) * cos_w0)
a2 = (A + 1) + (A - 1) * cos_w0 - 2 * sqrt_A * alpha
elif filter_type == "peaking":
b0 = 1 + alpha * A
b1 = -2 * cos_w0
b2 = 1 - alpha * A
a0 = 1 + alpha / A
a1 = -2 * cos_w0
a2 = 1 - alpha / A
else:
pass
# raise ValueError(f"Invalid filter_type: {filter_type}.")
b = np.array([b0, b1, b2]) / a0
a = np.array([a0, a1, a2]) / a0
return b, a
# Adapted from https://github.com/csteinmetz1/pyloudnorm/blob/master/pyloudnorm/iirfilter.py
def parametric_eq(
x: np.ndarray,
sample_rate: float,
low_shelf_gain_dB: float = 0.0,
low_shelf_cutoff_freq: float = 80.0,
low_shelf_q_factor: float = 0.707,
first_band_gain_dB: float = 0.0,
first_band_cutoff_freq: float = 300.0,
first_band_q_factor: float = 0.707,
second_band_gain_dB: float = 0.0,
second_band_cutoff_freq: float = 1000.0,
second_band_q_factor: float = 0.707,
third_band_gain_dB: float = 0.0,
third_band_cutoff_freq: float = 4000.0,
third_band_q_factor: float = 0.707,
fourth_band_gain_dB: float = 0.0,
fourth_band_cutoff_freq: float = 8000.0,
fourth_band_q_factor: float = 0.707,
high_shelf_gain_dB: float = 0.0,
high_shelf_cutoff_freq: float = 1000.0,
high_shelf_q_factor: float = 0.707,
dtype=np.float32,
):
"""Six-band parametric EQ.
Low-shelf -> Band 1 -> Band 2 -> Band 3 -> Band 4 -> High-shelf
Args:
"""
# print(f"autodiff peq fs = {sample_rate}")
# -------- apply low-shelf filter --------
b, a = biqaud(
low_shelf_gain_dB,
low_shelf_cutoff_freq,
low_shelf_q_factor,
sample_rate,
"low_shelf",
)
sos0 = np.concatenate((b, a))
x = scipy.signal.lfilter(b, a, x)
# -------- apply first-band peaking filter --------
b, a = biqaud(
first_band_gain_dB,
first_band_cutoff_freq,
first_band_q_factor,
sample_rate,
"peaking",
)
sos1 = np.concatenate((b, a))
x = scipy.signal.lfilter(b, a, x)
# -------- apply second-band peaking filter --------
b, a = biqaud(
second_band_gain_dB,
second_band_cutoff_freq,
second_band_q_factor,
sample_rate,
"peaking",
)
sos2 = np.concatenate((b, a))
x = scipy.signal.lfilter(b, a, x)
# -------- apply third-band peaking filter --------
b, a = biqaud(
third_band_gain_dB,
third_band_cutoff_freq,
third_band_q_factor,
sample_rate,
"peaking",
)
sos3 = np.concatenate((b, a))
x = scipy.signal.lfilter(b, a, x)
# -------- apply fourth-band peaking filter --------
b, a = biqaud(
fourth_band_gain_dB,
fourth_band_cutoff_freq,
fourth_band_q_factor,
sample_rate,
"peaking",
)
sos4 = np.concatenate((b, a))
x = scipy.signal.lfilter(b, a, x)
# -------- apply high-shelf filter --------
b, a = biqaud(
high_shelf_gain_dB,
high_shelf_cutoff_freq,
high_shelf_q_factor,
sample_rate,
"high_shelf",
)
sos5 = np.concatenate((b, a))
x = scipy.signal.lfilter(b, a, x)
return x.astype(dtype)
class ParametricEQ(Processor):
def __init__(
self,
sample_rate,
min_gain_dB=-24.0,
default_gain_dB=0.0,
max_gain_dB=24.0,
min_q_factor=0.1,
default_q_factor=0.707,
max_q_factor=10,
eps=1e-8,
):
""" """
super().__init__()
self.sample_rate = sample_rate
self.eps = eps
self.ports = [
{
"name": "Lowshelf gain",
"min": min_gain_dB,
"max": max_gain_dB,
"default": default_gain_dB,
"units": "dB",
},
{
"name": "Lowshelf cutoff",
"min": 20.0,
"max": 200.0,
"default": 100.0,
"units": "Hz",
},
{
"name": "Lowshelf Q",
"min": min_q_factor,
"max": max_q_factor,
"default": default_q_factor,
"units": "",
},
{
"name": "First band gain",
"min": min_gain_dB,
"max": max_gain_dB,
"default": default_gain_dB,
"units": "dB",
},
{
"name": "First band cutoff",
"min": 200.0,
"max": 2000.0,
"default": 400.0,
"units": "Hz",
},
{
"name": "First band Q",
"min": min_q_factor,
"max": max_q_factor,
"default": 0.707,
"units": "",
},
{
"name": "Second band gain",
"min": min_gain_dB,
"max": max_gain_dB,
"default": default_gain_dB,
"units": "dB",
},
{
"name": "Second band cutoff",
"min": 800.0,
"max": 4000.0,
"default": 1000.0,
"units": "Hz",
},
{
"name": "Second band Q",
"min": min_q_factor,
"max": max_q_factor,
"default": default_q_factor,
"units": "",
},
{
"name": "Third band gain",
"min": min_gain_dB,
"max": max_gain_dB,
"default": default_gain_dB,
"units": "dB",
},
{
"name": "Third band cutoff",
"min": 2000.0,
"max": 8000.0,
"default": 4000.0,
"units": "Hz",
},
{
"name": "Third band Q",
"min": min_q_factor,
"max": max_q_factor,
"default": default_q_factor,
"units": "",
},
{
"name": "Fourth band gain",
"min": min_gain_dB,
"max": max_gain_dB,
"default": default_gain_dB,
"units": "dB",
},
{
"name": "Fourth band cutoff",
"min": 4000.0,
"max": (24000 // 2) * 0.9,
"default": 8000.0,
"units": "Hz",
},
{
"name": "Fourth band Q",
"min": min_q_factor,
"max": max_q_factor,
"default": default_q_factor,
"units": "",
},
{
"name": "Highshelf gain",
"min": min_gain_dB,
"max": max_gain_dB,
"default": default_gain_dB,
"units": "dB",
},
{
"name": "Highshelf cutoff",
"min": 4000.0,
"max": (24000 // 2) * 0.9,
"default": 8000.0,
"units": "Hz",
},
{
"name": "Highshelf Q",
"min": min_q_factor,
"max": max_q_factor,
"default": default_q_factor,
"units": "",
},
]
self.num_control_params = len(self.ports)
self.process_fn = parametric_eq
def forward(self, x, p, sample_rate=24000, **kwargs):
"All processing in the forward is in numpy."
return self.run_series(x, p, sample_rate)
|