Update ldq_perceptual_layer.py
Browse files- ldq_perceptual_layer.py +18 -16
ldq_perceptual_layer.py
CHANGED
|
@@ -17,45 +17,47 @@ def apply_perceptual_mixing(audio: np.ndarray, features: Dict[str, Any], sr: int
|
|
| 17 |
if audio.ndim == 1:
|
| 18 |
audio = audio[:, np.newaxis]
|
| 19 |
|
| 20 |
-
# 1. Multiband saturation based on contrast
|
| 21 |
contrast = features.get("contrast", 0.15)
|
| 22 |
-
sat_amount = contrast *
|
| 23 |
|
| 24 |
-
#
|
| 25 |
if sat_amount > 0:
|
| 26 |
-
audio = np.tanh(audio * (1 + sat_amount)) / np.tanh(1 + sat_amount)
|
| 27 |
|
| 28 |
# 2. Sidechain compression based on hue
|
| 29 |
hue = features.get("average_hue", 180)
|
| 30 |
# Redder hue = faster release
|
| 31 |
release_time = 0.1 + (hue / 360) * 0.3 # 0.1-0.4 seconds
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
# For now, simple gain reduction based on edge density
|
| 35 |
edge_density = features.get("edge_density", 0.02)
|
| 36 |
-
reduction = 1.0 - (edge_density *
|
| 37 |
-
reduction = max(0.
|
| 38 |
audio *= reduction
|
| 39 |
|
| 40 |
# 3. Stereo width based on keypoint distribution
|
| 41 |
chaos = features.get("chaos_keypoints", 0)
|
| 42 |
-
width = 1.0 + (chaos / 1000) * 0.
|
| 43 |
if audio.shape[1] == 2:
|
| 44 |
mid = (audio[:, 0] + audio[:, 1]) / 2
|
| 45 |
side = (audio[:, 0] - audio[:, 1]) / 2
|
| 46 |
audio[:, 0] = mid + side * width
|
| 47 |
audio[:, 1] = mid - side * width
|
| 48 |
|
| 49 |
-
# 4. Reverb based on color variance
|
| 50 |
colorfulness = features.get("colorfulness", 0.1)
|
| 51 |
-
reverb_decay = 0.
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
delay_samples = int(sr * 0.05) # 50ms
|
| 56 |
if len(audio) > delay_samples:
|
| 57 |
reverb = np.roll(audio, delay_samples, axis=0)
|
| 58 |
-
reverb *= 0.
|
| 59 |
-
audio += reverb * min(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
return audio
|
|
|
|
| 17 |
if audio.ndim == 1:
|
| 18 |
audio = audio[:, np.newaxis]
|
| 19 |
|
| 20 |
+
# 1. Multiband saturation based on contrast (reduced for less buzz)
|
| 21 |
contrast = features.get("contrast", 0.15)
|
| 22 |
+
sat_amount = contrast * 1.5 # Reduced from 3.0
|
| 23 |
|
| 24 |
+
# Soft saturation
|
| 25 |
if sat_amount > 0:
|
| 26 |
+
audio = np.tanh(audio * (1 + sat_amount * 0.5)) / np.tanh(1 + sat_amount * 0.5)
|
| 27 |
|
| 28 |
# 2. Sidechain compression based on hue
|
| 29 |
hue = features.get("average_hue", 180)
|
| 30 |
# Redder hue = faster release
|
| 31 |
release_time = 0.1 + (hue / 360) * 0.3 # 0.1-0.4 seconds
|
| 32 |
|
| 33 |
+
# Gentle gain reduction based on edge density
|
|
|
|
| 34 |
edge_density = features.get("edge_density", 0.02)
|
| 35 |
+
reduction = 1.0 - (edge_density * 1.0) # Less reduction
|
| 36 |
+
reduction = max(0.7, reduction)
|
| 37 |
audio *= reduction
|
| 38 |
|
| 39 |
# 3. Stereo width based on keypoint distribution
|
| 40 |
chaos = features.get("chaos_keypoints", 0)
|
| 41 |
+
width = 1.0 + (chaos / 1000) * 0.3 # Less width
|
| 42 |
if audio.shape[1] == 2:
|
| 43 |
mid = (audio[:, 0] + audio[:, 1]) / 2
|
| 44 |
side = (audio[:, 0] - audio[:, 1]) / 2
|
| 45 |
audio[:, 0] = mid + side * width
|
| 46 |
audio[:, 1] = mid - side * width
|
| 47 |
|
| 48 |
+
# 4. Reverb based on color variance (reduced)
|
| 49 |
colorfulness = features.get("colorfulness", 0.1)
|
| 50 |
+
reverb_decay = 0.3 + colorfulness * 3.0 # 0.3-3.3 seconds (shorter)
|
| 51 |
|
| 52 |
+
if reverb_decay > 0.8:
|
| 53 |
+
delay_samples = int(sr * 0.03) # 30ms (shorter)
|
|
|
|
| 54 |
if len(audio) > delay_samples:
|
| 55 |
reverb = np.roll(audio, delay_samples, axis=0)
|
| 56 |
+
reverb *= 0.2 # Less reverb
|
| 57 |
+
audio += reverb * min(0.6, reverb_decay / 4.0)
|
| 58 |
+
|
| 59 |
+
# 5. Gentle EQ: cut above 5 kHz to reduce buzz
|
| 60 |
+
sos = signal.butter(2, 5000, btype='low', fs=sr, output='sos')
|
| 61 |
+
audio = signal.sosfilt(sos, audio, axis=0)
|
| 62 |
|
| 63 |
return audio
|