-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparametric_coding.py
More file actions
427 lines (360 loc) · 15.2 KB
/
parametric_coding.py
File metadata and controls
427 lines (360 loc) · 15.2 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import numpy as np
from typing import List, Tuple
class PSEncoder:
def __init__(
self,
sample_rate: int,
min_freq: float = 20.0,
max_freq: float = 20000.0,
freq_points: int = 32,
floor_db: float = -75.0,
use_grouping: bool = False,
log_scale: bool = True
):
"""Initialize the stereo audio analyzer.
Args:
sample_rate: Audio sample rate in Hz
min_freq: Minimum frequency to analyze in Hz
max_freq: Maximum frequency to analyze in Hz
freq_points: Number of frequency points to sample
floor_db: Noise floor in dB (signals below this are ignored)
use_grouping: If True, average over frequency bands
log_scale: If True, use logarithmic frequency spacing (default)
"""
self.sample_rate = sample_rate
self.floor_db = floor_db
self.use_grouping = use_grouping
self.log_scale = log_scale
# Pre-calculate target frequencies
self.set_freq(min_freq, max_freq, freq_points)
def set_freq(self, min_freq: float, max_freq: float, freq_points: int):
self.min_freq = min_freq
self.max_freq = max_freq
self.freq_points = freq_points
if self.log_scale:
self.target_freqs = np.logspace(
np.log10(min_freq),
np.log10(max_freq),
freq_points
)
else:
self.target_freqs = np.linspace(min_freq, max_freq, freq_points)
def analyze(
self,
audio_data: np.ndarray,
infloat=False
) -> List[Tuple[float, float, float, float]]:
"""Analyze stereo audio and extract IID (pan), IPD, and IC per frequency band.
Args:
audio_data: Stereo audio data with shape [samples, 2]
Returns:
List of tuples: [(freq, pan, ipd, ic), ...]
- freq: frequency in Hz
- pan: Inter-aural Intensity Difference (IID) as pan value [-1, 1]
- ipd: Inter-aural Phase Difference in radians [-π, π]
- ic: Inter-channel Coherence [0, 1]
"""
if audio_data.ndim != 2 or audio_data.shape[1] != 2:
raise ValueError("Audio must be stereo (shape: [samples, 2])")
# Normalize int16 to float
if infloat:
audio_float = audio_data
else:
audio_float = audio_data.astype(np.float32) / 32768.0
# Separate channels
left = audio_float[:, 0]
right = audio_float[:, 1]
# Calculate FFT for both channels
n_fft = len(left)
left_fft = np.fft.rfft(left)
right_fft = np.fft.rfft(right)
# Get frequency bins
freqs = np.fft.rfftfreq(n_fft, 1 / self.sample_rate)
# Calculate magnitude for each channel
left_mag = np.abs(left_fft)
right_mag = np.abs(right_fft)
# Calculate combined magnitude for floor detection
combined_mag = (left_mag + right_mag) / 2
# Convert floor from dB to linear scale
floor_linear = 10 ** (self.floor_db / 20.0)
# Calculate bandwidth for grouping
if self.use_grouping and self.freq_points > 1:
if self.log_scale:
# For log scale, bandwidth is proportional to frequency
bandwidths = self._calculate_log_bandwidths()
else:
bandwidth = (self.max_freq - self.min_freq) / self.freq_points
bandwidths = [bandwidth] * self.freq_points
else:
bandwidths = [0] * self.freq_points
# Analyze each frequency band
results = []
for target_freq, bandwidth in zip(self.target_freqs, bandwidths):
if self.use_grouping and bandwidth > 0:
freq_low = target_freq - bandwidth / 2
freq_high = target_freq + bandwidth / 2
mask = (freqs >= freq_low) & (freqs <= freq_high)
if np.any(mask):
pan, ipd, ic = self._analyze_band(
left_fft[mask], right_fft[mask],
left_mag[mask], right_mag[mask],
combined_mag[mask], floor_linear
)
else:
pan, ipd, ic = 0.0, 0.0, 0.0
else:
idx = np.argmin(np.abs(freqs - target_freq))
pan, ipd, ic = self._analyze_bin(
left_fft[idx], right_fft[idx],
left_mag[idx], right_mag[idx],
combined_mag[idx], floor_linear
)
results.append((float(target_freq), float(pan), float(ipd), float(ic)))
return results
def _calculate_log_bandwidths(self) -> List[float]:
"""Calculate bandwidths for logarithmic frequency spacing."""
bandwidths = []
log_freqs = np.log10(self.target_freqs)
for i in range(len(log_freqs)):
if i == 0:
# First band: extend to halfway to next band
log_width = (log_freqs[i + 1] - log_freqs[i])
elif i == len(log_freqs) - 1:
# Last band: extend to halfway from previous band
log_width = (log_freqs[i] - log_freqs[i - 1])
else:
# Middle bands: halfway to neighbors on each side
log_width = (log_freqs[i + 1] - log_freqs[i - 1]) / 2
# Convert log width to linear bandwidth
freq_low = 10 ** (log_freqs[i] - log_width / 2)
freq_high = 10 ** (log_freqs[i] + log_width / 2)
bandwidths.append(freq_high - freq_low)
return bandwidths
def _analyze_band(
self,
left_fft: np.ndarray,
right_fft: np.ndarray,
left_mag: np.ndarray,
right_mag: np.ndarray,
combined_mag: np.ndarray,
floor_linear: float
) -> Tuple[float, float, float]:
"""Analyze a frequency band and return pan, ipd, ic."""
band_left_mag = np.mean(left_mag)
band_right_mag = np.mean(right_mag)
band_combined_mag = np.mean(combined_mag)
if band_combined_mag < floor_linear:
return 0.0, 0.0, 0.0
# Calculate IID (pan)
total = band_left_mag + band_right_mag
if total > 1e-10:
pan_value = (band_left_mag - band_right_mag) / total
else:
pan_value = 0.0
pan_value = np.clip(pan_value, -1.0, 1.0)
# IPD - Phase difference (averaged over band)
left_phase = np.angle(left_fft)
right_phase = np.angle(right_fft)
phase_diff = left_phase - right_phase
phase_diff = np.arctan2(np.sin(phase_diff), np.cos(phase_diff))
# Weighted average by magnitude
weights = np.abs(left_fft) + np.abs(right_fft)
if np.sum(weights) > 1e-10:
ipd_value = np.average(phase_diff, weights=weights)
else:
ipd_value = 0.0
# IC - Inter-channel Coherence
cross_power = np.mean(left_fft * np.conj(right_fft))
left_power = np.mean(np.abs(left_fft) ** 2)
right_power = np.mean(np.abs(right_fft) ** 2)
if left_power > 1e-10 and right_power > 1e-10:
ic_value = np.abs(cross_power) / np.sqrt(left_power * right_power)
ic_value = np.clip(ic_value, 0.0, 1.0)
else:
ic_value = 0.0
return pan_value, ipd_value, ic_value
def _analyze_bin(
self,
left_fft: complex,
right_fft: complex,
left_mag: float,
right_mag: float,
combined_mag: float,
floor_linear: float
) -> Tuple[float, float, float]:
"""Analyze a single frequency bin and return pan, ipd, ic."""
if combined_mag < floor_linear:
return 0.0, 0.0, 0.0
# Calculate IID (pan)
total = left_mag + right_mag
if total > 1e-10:
pan_value = (left_mag - right_mag) / total
else:
pan_value = 0.0
pan_value = np.clip(pan_value, -1.0, 1.0)
# IPD - Phase difference
left_phase = np.angle(left_fft)
right_phase = np.angle(right_fft)
phase_diff = left_phase - right_phase
ipd_value = np.arctan2(np.sin(phase_diff), np.cos(phase_diff))
# IC - Inter-channel Coherence
cross_power = left_fft * np.conj(right_fft)
left_power = np.abs(left_fft) ** 2
right_power = np.abs(right_fft) ** 2
if left_power > 1e-10 and right_power > 1e-10:
ic_value = np.abs(cross_power) / np.sqrt(left_power * right_power)
ic_value = np.clip(ic_value, 0.0, 1.0)
else:
ic_value = 0.0
return pan_value, ipd_value, ic_value
class PSDecoder:
def __init__(
self,
sample_rate: int,
min_freq: float = 20.0,
max_freq: float = 20000.0,
freq_points: int = 32,
use_grouping: bool = False,
log_scale: bool = True,
stereo_width: float = 1.0
):
"""Initialize the stereo audio analyzer.
Args:
sample_rate: Audio sample rate in Hz
min_freq: Minimum frequency to analyze in Hz
max_freq: Maximum frequency to analyze in Hz
freq_points: Number of frequency points to sample
floor_db: Noise floor in dB (signals below this are ignored)
use_grouping: If True, average over frequency bands
log_scale: If True, use logarithmic frequency spacing (default)
stereo_width: Stereo width scaling factor
"""
self.sample_rate = sample_rate
self.use_grouping = use_grouping
self.log_scale = log_scale
self.stereo_width = stereo_width
# Pre-calculate target frequencies
self.set_freq(min_freq, max_freq, freq_points)
def set_freq(self, min_freq: float, max_freq: float, freq_points: int):
self.min_freq = min_freq
self.max_freq = max_freq
self.freq_points = freq_points
if self.log_scale:
self.target_freqs = np.logspace(
np.log10(min_freq),
np.log10(max_freq),
freq_points
)
else:
self.target_freqs = np.linspace(min_freq, max_freq, freq_points)
def _apply_stereo_width(self, left: np.ndarray, right: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""
Apply stereo width expansion using Mid-Side processing.
This is the standard algorithm used in DAWs.
"""
# Convert L/R to Mid-Side
mid = (left + right) / 2.0
side = (left - right) / 2.0
# Apply width to side signal
side_widened = side * self.stereo_width
# Convert back to L/R
left_out = mid + side_widened
right_out = mid - side_widened
max_val = max(np.max(np.abs(left_out)), np.max(np.abs(right_out)))
if max_val > 1.0:
left_out /= max_val
right_out /= max_val
return left_out, right_out
def apply(
self,
mono_audio: np.ndarray,
pan_values: List[float],
ipd_values: List[float],
ic_values: List[bool]
) -> np.ndarray:
"""Apply frequency-dependent IID (pan), IPD (phase), and IC (coherence) to mono audio.
Args:
mono_audio: Mono audio data
pan_values: Pan values for each frequency point [-1, 1]
ipd_values: Phase difference values for each frequency point [radians]
ic_values: Coherence flags for each frequency point [bool]
Returns:
Stereo audio data with shape [samples, 2]
"""
# Normalize if int16
if mono_audio.dtype == np.int16:
mono_float = mono_audio.astype(np.float32) / 32768.0
return_int16 = True
else:
mono_float = mono_audio.astype(np.float32)
return_int16 = False
# FFT
mono_fft = np.fft.rfft(mono_float)
freqs = np.fft.rfftfreq(len(mono_float), 1 / self.sample_rate)
# Calculate bandwidth for grouping
if self.use_grouping and self.freq_points > 1:
if self.log_scale:
bandwidths = self._calculate_log_bandwidths()
else:
bandwidth = (self.max_freq - self.min_freq) / self.freq_points
bandwidths = [bandwidth] * self.freq_points
else:
bandwidths = [0] * self.freq_points
# Initialize gain and phase shift arrays
left_gain = np.ones(len(freqs), dtype=np.float32)
right_gain = np.ones(len(freqs), dtype=np.float32)
right_phase_shift = np.ones(len(freqs), dtype=np.complex64)
# Apply stereo parameters to each frequency band
for freq, pan, ipd, ic, bandwidth in zip(
self.target_freqs, pan_values, ipd_values, ic_values, bandwidths
):
# Constant power panning
angle = (-pan + 1.0) * np.pi / 4.0
left_g = np.cos(angle)
right_g = np.sin(angle)
# If IC is False, blend toward mono
if not ic:
left_g = right_g = (left_g + right_g) / 2.0
ipd = 0.0 # remove phase difference
if self.use_grouping and bandwidth > 0:
freq_low = freq - bandwidth / 2
freq_high = freq + bandwidth / 2
mask = (freqs >= freq_low) & (freqs <= freq_high)
left_gain[mask] = left_g
right_gain[mask] = right_g
right_phase_shift[mask] *= np.exp(1j * -ipd)
else:
idx = np.argmin(np.abs(freqs - freq))
left_gain[idx] = left_g
right_gain[idx] = right_g
right_phase_shift[idx] *= np.exp(1j * -ipd)
# Apply gains and phase
left_fft = mono_fft * left_gain
right_fft = mono_fft * right_gain * right_phase_shift
left = np.fft.irfft(left_fft, n=len(mono_float))
right = np.fft.irfft(right_fft, n=len(mono_float))
# Apply stereo width
left, right = self._apply_stereo_width(left, right)
stereo = np.column_stack([left, right])
if return_int16:
stereo = np.clip(stereo * 32767.0, -32768, 32767).astype(np.int16)
return stereo
def _calculate_log_bandwidths(self) -> List[float]:
"""Calculate bandwidths for logarithmic frequency spacing."""
bandwidths = []
log_freqs = np.log10(self.target_freqs)
for i in range(len(log_freqs)):
if i == 0:
# First band: extend to halfway to next band
log_width = (log_freqs[i + 1] - log_freqs[i])
elif i == len(log_freqs) - 1:
# Last band: extend to halfway from previous band
log_width = (log_freqs[i] - log_freqs[i - 1])
else:
# Middle bands: halfway to neighbors on each side
log_width = (log_freqs[i + 1] - log_freqs[i - 1]) / 2
# Convert log width to linear bandwidth
freq_low = 10 ** (log_freqs[i] - log_width / 2)
freq_high = 10 ** (log_freqs[i] + log_width / 2)
bandwidths.append(freq_high - freq_low)
return bandwidths