DeepSeekOracle commited on
Commit
079a716
·
verified ·
1 Parent(s): 89a2520

Upload ldq_perceptual_layer.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ldq_perceptual_layer.py +63 -0
ldq_perceptual_layer.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ LDQ Perceptual Feature Translation Layer
4
+ Professional mixing effects driven by image statistics
5
+ """
6
+
7
+ import numpy as np
8
+ import math
9
+ from scipy import signal
10
+ from typing import Dict, Any
11
+
12
+ def apply_perceptual_mixing(audio: np.ndarray, features: Dict[str, Any], sr: int) -> np.ndarray:
13
+ """
14
+ Apply multiband saturation, sidechain compression, stereo width, and reverb.
15
+ Returns polished audio.
16
+ """
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