Spaces:
Running
Running
Upload feature_extractor_cpu.py with huggingface_hub
Browse files- feature_extractor_cpu.py +287 -96
feature_extractor_cpu.py
CHANGED
|
@@ -131,12 +131,27 @@ warnings.filterwarnings('ignore'); EPS = 1e-10
|
|
| 131 |
# htdemucs switched from 4-stem (htdemucs) to 6-stem (htdemucs_6s).
|
| 132 |
# total_rms now includes guitar_rms + piano_rms (more accurate
|
| 133 |
# stem-sum denominator). All v7 cache entries are incompatible.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 135 |
ENG_DIMS = [
|
| 136 |
"lufs", # 0: Integrated loudness (LUFS)
|
| 137 |
"crest_factor", # 1: Peak-to-RMS ratio (global mix)
|
| 138 |
"drum_punch", # 2: Drum stem crest factor Γ drum_confidence (gated)
|
| 139 |
-
"
|
| 140 |
"stereo_width", # 4: Mid-side energy ratio
|
| 141 |
"brightness", # 5: Spectral centroid of mix (Hz)
|
| 142 |
"harmonic_complexity", # 6: Chroma entropy (harmonic diversity only)
|
|
@@ -145,7 +160,7 @@ ENG_DIMS = [
|
|
| 145 |
"clip_ratio", # 9: Clipped samples / total samples
|
| 146 |
"clip_max_run", # 10: Longest consecutive clipping run (samples)
|
| 147 |
"clip_mean_run", # 11: Mean clipping run length across all events
|
| 148 |
-
"
|
| 149 |
"vocal_level", # 13: Vocal stem RMS / total mix RMS (loudness when present)
|
| 150 |
"vocal_presence", # 14: Fraction of frames with active vocal signal (0β1)
|
| 151 |
"vocal_brightness", # 15: Spectral centroid of vocal stem (Hz)
|
|
@@ -157,16 +172,23 @@ ENG_DIMS = [
|
|
| 157 |
"peak_lufs", # 21: Max short-term LUFS
|
| 158 |
"rolloff", # 22: Spectral rolloff (Hz)
|
| 159 |
"bass_tightness", # 23: Bass stem crest factor
|
| 160 |
-
"
|
| 161 |
"phase_corr", # 25: L/R phase correlation
|
| 162 |
# ββ Drum sub-dimensions (v4+, semantics per version noted) βββββββββββββββ
|
| 163 |
-
"
|
| 164 |
"drum_cymbal_presence",# 27: HPSS sustained fraction Γ band prominence in 4β10 kHz [v6]
|
| 165 |
"drum_confidence", # 28: 0.55Γkick_conf + 0.30Γhihat_conf + 0.15Γcymbal_conf [v6]
|
| 166 |
"hihat_presence", # 29: Onset density of percussive 8β14 kHz drum component [v6 NEW]
|
| 167 |
# ββ Electric guitar dimensions (v8 NEW β htdemucs_6s guitar stem) ββββββββ
|
| 168 |
"egtr_presence", # 30: Composite electric guitar detection score [0,1] [v8 NEW]
|
| 169 |
"egtr_level", # 31: egtr_presence Γ (guitar_rms / total_rms) β guitar mix level [v8 NEW]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
]
|
| 171 |
|
| 172 |
# βοΏ½οΏ½οΏ½ Vocal activity detection threshold βββββββββββββββββββββββββββββββββββββββ
|
|
@@ -232,7 +254,7 @@ DRUM_KICK_WEIGHT = 0.55 # weight for kick_conf in drum_confidence
|
|
| 232 |
DRUM_HIHAT_WEIGHT = 0.30 # weight for hihat_presence in drum_confidence
|
| 233 |
DRUM_CYMBAL_WEIGHT = 0.15 # weight for cymbal_presence in drum_confidence
|
| 234 |
|
| 235 |
-
# ββ Electric guitar detection constants (
|
| 236 |
#
|
| 237 |
# EGTR_MID_FMIN / FMAX = 200β5000 Hz
|
| 238 |
# Core range of electric guitar fundamentals and dominant harmonics.
|
|
@@ -241,39 +263,89 @@ DRUM_CYMBAL_WEIGHT = 0.15 # weight for cymbal_presence in drum_confidence
|
|
| 241 |
# from the bass stem into the guitar stem. Stopping at 5 kHz keeps the
|
| 242 |
# measure out of the hi-hat / pick-noise band (handled by drum dims).
|
| 243 |
#
|
| 244 |
-
# EGTR_PICK_ONSET_SAT =
|
| 245 |
-
# Onset rate at which picking_density = 1.0. 8th notes at
|
| 246 |
-
# 16th notes at
|
| 247 |
-
#
|
| 248 |
-
#
|
|
|
|
| 249 |
# Tune UP if piano-only or pad-heavy tracks score above 0.30.
|
| 250 |
#
|
| 251 |
-
# EGTR_FLATNESS_SAT = 0.25
|
| 252 |
# Mid-band spectral flatness at which mid_saturation = 1.0.
|
| 253 |
-
# Clean electric guitar
|
| 254 |
-
#
|
| 255 |
-
# 0.
|
| 256 |
-
#
|
| 257 |
-
#
|
|
|
|
|
|
|
| 258 |
#
|
| 259 |
-
#
|
| 260 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
#
|
| 262 |
# TUNING GUIDANCE (post first training run):
|
| 263 |
# Check the printed standards table for egtr_presence and egtr_level.
|
| 264 |
-
# Lane refs with active electric guitar: egtr_presence β₯ 0.
|
| 265 |
-
# Non-lane refs without guitar: egtr_presence β€ 0.
|
| 266 |
-
# If guitar tracks score 0.
|
| 267 |
-
# and/or lower EGTR_FLATNESS_SAT (0.
|
| 268 |
-
# If piano/pad tracks score above 0.
|
| 269 |
-
# reduce
|
| 270 |
#
|
| 271 |
-
EGTR_MID_FMIN
|
| 272 |
-
EGTR_MID_FMAX
|
| 273 |
-
EGTR_PICK_ONSET_SAT
|
| 274 |
-
EGTR_FLATNESS_SAT
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
|
| 279 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -609,14 +681,17 @@ def apply_streaming_normalization(
|
|
| 609 |
return out, gain_db
|
| 610 |
|
| 611 |
|
| 612 |
-
def _compute_drum_bands(drum_mono: np.ndarray, mix_mono: np.ndarray, sr: int
|
|
|
|
| 613 |
"""
|
| 614 |
-
Returns (kick_dom, cymbal_presence, hihat_presence).
|
| 615 |
|
| 616 |
kick_dom β dominance ratio (unchanged from v5):
|
| 617 |
RMS(drum_stem, 40β120 Hz) / RMS(mix, 40β120 Hz)
|
| 618 |
cymbal_presence β HPSS sustained fraction Γ band prominence in 4β10 kHz [0,1]
|
| 619 |
hihat_presence β onset density of HPSS percussive 8β14 kHz component [0,1]
|
|
|
|
|
|
|
| 620 |
|
| 621 |
WHY HPSS INSTEAD OF DOMINANCE RATIO FOR CYMBAL / HI-HAT (v6):
|
| 622 |
|
|
@@ -701,36 +776,119 @@ def _compute_drum_bands(drum_mono: np.ndarray, mix_mono: np.ndarray, sr: int) ->
|
|
| 701 |
onset_rate = len(onsets) / max(duration, 1.0)
|
| 702 |
hihat_presence = float(min(onset_rate / HIHAT_ONSET_SAT, 1.0))
|
| 703 |
|
| 704 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 705 |
|
| 706 |
|
| 707 |
def _compute_guitar_dims(guitar_mono: np.ndarray, sr: int) -> tuple:
|
| 708 |
"""
|
| 709 |
-
Returns (egtr_presence, picking_density, mid_saturation).
|
| 710 |
All values in [0, 1].
|
| 711 |
|
| 712 |
-
egtr_presence β composite [0,1] electric guitar detection score:
|
| 713 |
-
EGTR_PICK_WEIGHT
|
| 714 |
-
+ EGTR_FLAT_WEIGHT
|
|
|
|
| 715 |
|
| 716 |
picking_density β onset density of the HPSS percussive component in
|
| 717 |
-
200β5000 Hz, normalized to EGTR_PICK_ONSET_SAT.
|
| 718 |
Detects pick/strum attack events per second.
|
| 719 |
-
Electric guitar:
|
| 720 |
-
Piano also produces picking onsets;
|
| 721 |
-
|
| 722 |
|
| 723 |
mid_saturation β spectral flatness of the guitar stem in 200β5000 Hz,
|
| 724 |
-
normalized to EGTR_FLATNESS_SAT.
|
| 725 |
-
|
| 726 |
-
|
|
|
|
|
|
|
|
|
|
| 727 |
Rewards harmonic saturation from magnetic pickups and
|
| 728 |
-
overdrive β the
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
Even with htdemucs_6s providing a dedicated guitar stem, there is residual
|
| 735 |
bleed from other instruments. HPSS separates the percussive (transient
|
| 736 |
attack) component from the harmonic (sustained) component within the guitar
|
|
@@ -739,27 +897,20 @@ def _compute_guitar_dims(guitar_mono: np.ndarray, sr: int) -> tuple:
|
|
| 739 |
bleed (synths, pads, vocals) tends to be sustained (β H component) rather
|
| 740 |
than transient (β P component) in this frequency range.
|
| 741 |
|
| 742 |
-
ACOUSTIC VS ELECTRIC LIMITATION:
|
| 743 |
-
Clean acoustic and clean electric guitar
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
contain acoustic-only tracks, the SVM learns the residual difference
|
| 753 |
-
from CLAP + other dimensions even if egtr_presence alone cannot
|
| 754 |
-
fully separate them.
|
| 755 |
-
|
| 756 |
-
Intermediate values (picking_density, mid_saturation) are returned for
|
| 757 |
-
debug/tuning but are NOT added to ENG_DIMS β only egtr_presence and
|
| 758 |
-
egtr_level are scoreable dimensions, preserving the atomicity principle.
|
| 759 |
"""
|
| 760 |
if np.sqrt(np.mean(guitar_mono ** 2)) < EPS:
|
| 761 |
# Silent guitar stem β no guitar in the track.
|
| 762 |
-
return 0.0, 0.0, 0.0
|
| 763 |
|
| 764 |
n_fft = 2048
|
| 765 |
hop = 512
|
|
@@ -796,28 +947,51 @@ def _compute_guitar_dims(guitar_mono: np.ndarray, sr: int) -> tuple:
|
|
| 796 |
raw_flatness = float(np.mean(frame_flat))
|
| 797 |
mid_saturation = float(min(raw_flatness / EGTR_FLATNESS_SAT, 1.0))
|
| 798 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 799 |
egtr_presence = float(
|
| 800 |
-
EGTR_PICK_WEIGHT
|
| 801 |
-
EGTR_FLAT_WEIGHT
|
|
|
|
| 802 |
)
|
| 803 |
|
| 804 |
-
return egtr_presence, picking_density, mid_saturation
|
| 805 |
|
| 806 |
|
| 807 |
def extract_mir_features(path):
|
| 808 |
"""
|
| 809 |
Returns (l3_vec, eng_vec, true_peak_dbfs).
|
| 810 |
|
| 811 |
-
eng_vec is a
|
| 812 |
Indices 0β25 are unchanged from cache version 3.
|
| 813 |
Indices 26β29 are drum sub-dimensions (v4βv6, unchanged in v8):
|
| 814 |
-
26
|
| 815 |
27 drum_cymbal_presence β HPSS sustained fraction Γ band prom in 4β10 kHz (v6)
|
| 816 |
28 drum_confidence β 0.55Γkick + 0.30Γhihat + 0.15Γcymbal (v6)
|
| 817 |
29 hihat_presence β onset density of percussive 8β14 kHz (v6 NEW)
|
| 818 |
Indices 30β31 are electric guitar dimensions (v8 NEW):
|
| 819 |
30 egtr_presence β composite guitar detection score [0,1]
|
| 820 |
31 egtr_level β egtr_presence Γ (guitar_rms / total_rms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 821 |
|
| 822 |
drum_punch (index 2) is gated by drum_confidence:
|
| 823 |
drum_punch = crest_factor(drum_stem) Γ drum_confidence
|
|
@@ -842,17 +1016,17 @@ def extract_mir_features(path):
|
|
| 842 |
(4Γ oversampled, in dBFS). Returned so the caller can enforce the -1.0 dBFS
|
| 843 |
true peak ceiling in apply_streaming_normalization() correctly.
|
| 844 |
|
| 845 |
-
Expected outcomes by source type (approximate):
|
| 846 |
Distorted electric guitar prominent in mix:
|
| 847 |
-
egtr_presence β 0.
|
| 848 |
-
Clean electric guitar
|
| 849 |
-
egtr_presence β 0.
|
| 850 |
Acoustic guitar only (no electric):
|
| 851 |
-
egtr_presence β 0.
|
| 852 |
Piano-led track (no guitar):
|
| 853 |
-
egtr_presence β 0.
|
| 854 |
Pad/synth-led track (no guitar):
|
| 855 |
-
egtr_presence β 0.
|
| 856 |
"""
|
| 857 |
y, sr = librosa.load(path, sr=44100, mono=True)
|
| 858 |
y_stereo, _ = librosa.load(path, sr=sr, mono=False)
|
|
@@ -880,10 +1054,11 @@ def extract_mir_features(path):
|
|
| 880 |
# ββ Drum sub-band analysis (v6, unchanged in v8) ββββββββββββββββββββββββββ
|
| 881 |
drum_mono = (src[_STEM_DRUMS][0] + src[_STEM_DRUMS][1]) / 2.0
|
| 882 |
|
| 883 |
-
|
|
|
|
| 884 |
|
| 885 |
# drum_confidence: weighted sum of three [0,1] drum indicators.
|
| 886 |
-
kick_conf = min(
|
| 887 |
hihat_conf = hihat_pres # already in [0, 1]
|
| 888 |
cymbal_conf = drum_cymbal_pres # already in [0, 1]
|
| 889 |
drum_conf = float(
|
|
@@ -898,10 +1073,10 @@ def extract_mir_features(path):
|
|
| 898 |
# ββ Bass crest factor βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 899 |
bass_tightness_val = np.max(np.abs(src[_STEM_BASS])) / (bass_rms + EPS)
|
| 900 |
|
| 901 |
-
# ββ Mix-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
voc_level = voc_rms / total_rms
|
| 906 |
|
| 907 |
# ββ Vocal sub-dimensions ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -914,21 +1089,31 @@ def extract_mir_features(path):
|
|
| 914 |
librosa.feature.spectral_centroid(y=voc_mono, sr=sr)
|
| 915 |
))
|
| 916 |
|
| 917 |
-
# ββ Electric guitar sub-dimensions (
|
| 918 |
guitar_mono = (src[_STEM_GUITAR][0] + src[_STEM_GUITAR][1]) / 2.0
|
| 919 |
-
egtr_pres, _picking, _flatness = _compute_guitar_dims(guitar_mono, sr)
|
| 920 |
-
# egtr_level: scale presence by the guitar's share of total mix RMS.
|
| 921 |
-
# Rewards tracks where guitar is both detectable AND prominent in the mix.
|
| 922 |
egtr_level_val = float(egtr_pres * (guitar_rms / total_rms))
|
| 923 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 924 |
# ββ Mid-side stereo width βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 925 |
mid, side = (y_stereo[0] + y_stereo[1]) / 2, (y_stereo[0] - y_stereo[1]) / 2
|
| 926 |
ms_ratio = (np.sqrt(np.mean(side**2)) /
|
| 927 |
(np.sqrt(np.mean(mid**2)) + np.sqrt(np.mean(side**2)) + EPS))
|
| 928 |
|
| 929 |
# ββ Short-term loudness (K-weighted, BS.1770 β no gating) ββββββββββββββββ
|
| 930 |
-
# Uses the same K-weighting filter as integrated LUFS. window=3s, hop=1s
|
| 931 |
-
# matches the original librosa.feature.rms() block framing.
|
| 932 |
st_lufs = _compute_short_term_lufs_array(y_stereo, sr, window_s=3.0, hop_s=1.0)
|
| 933 |
|
| 934 |
# ββ Clipping βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -939,7 +1124,7 @@ def extract_mir_features(path):
|
|
| 939 |
_compute_integrated_lufs(y_stereo, sr), # 0 lufs (ITU-R BS.1770-4 β K-weighted, gated)
|
| 940 |
np.max(np.abs(y)) / (np.sqrt(np.mean(y**2)) + EPS), # 1 crest_factor
|
| 941 |
drum_punch_val, # 2 drum_punch (gated)
|
| 942 |
-
|
| 943 |
ms_ratio, # 4 stereo_width
|
| 944 |
librosa.feature.spectral_centroid(y=y, sr=sr).mean(), # 5 brightness
|
| 945 |
entropy(np.mean(librosa.feature.chroma_cqt(y=y, sr=sr), axis=1)), # 6 harmonic_complexity
|
|
@@ -948,7 +1133,7 @@ def extract_mir_features(path):
|
|
| 948 |
c_ratio, # 9 clip_ratio
|
| 949 |
c_max, # 10 clip_max_run
|
| 950 |
c_mean, # 11 clip_mean_run
|
| 951 |
-
|
| 952 |
voc_level, # 13 vocal_level
|
| 953 |
voc_presence, # 14 vocal_presence
|
| 954 |
voc_brightness, # 15 vocal_brightness
|
|
@@ -960,14 +1145,20 @@ def extract_mir_features(path):
|
|
| 960 |
np.max(st_lufs), # 21 peak_lufs
|
| 961 |
np.mean(librosa.feature.spectral_rolloff(y=y, sr=sr)), # 22 rolloff
|
| 962 |
bass_tightness_val, # 23 bass_tightness
|
| 963 |
-
|
| 964 |
np.nan_to_num(np.corrcoef(y_stereo[0], y_stereo[1])[0, 1]), # 25 phase_corr
|
| 965 |
-
|
| 966 |
drum_cymbal_pres, # 27 drum_cymbal_presence (v6)
|
| 967 |
drum_conf, # 28 drum_confidence (v6)
|
| 968 |
hihat_pres, # 29 hihat_presence (v6)
|
| 969 |
egtr_pres, # 30 egtr_presence (v8 NEW)
|
| 970 |
egtr_level_val, # 31 egtr_level (v8 NEW)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 971 |
])
|
| 972 |
|
| 973 |
# ββ True peak (BS.1775-2, 4Γ oversampled) ββββββββββββββββββββββββββββββββ
|
|
|
|
| 131 |
# htdemucs switched from 4-stem (htdemucs) to 6-stem (htdemucs_6s).
|
| 132 |
# total_rms now includes guitar_rms + piano_rms (more accurate
|
| 133 |
# stem-sum denominator). All v7 cache entries are incompatible.
|
| 134 |
+
# v9 UPDATE β egtr_presence formula revised (same indices 30β31, new values):
|
| 135 |
+
# _compute_guitar_dims gains a third sub-dimension: tonal_sustain.
|
| 136 |
+
# tonal_sustain = H_mid_energy / (H_mid_energy + P_mid_energy + EPS)
|
| 137 |
+
# normalized to EGTR_SUSTAIN_SAT (0.70). Captures sustained guitar
|
| 138 |
+
# notes (legato lines, held power chords) that are tonal β H
|
| 139 |
+
# component β invisible to picking_density (percussive-only).
|
| 140 |
+
# Constants recalibrated for semi-clean and moderate-tempo guitar:
|
| 141 |
+
# EGTR_PICK_ONSET_SAT 4.0 β 3.0 (was too fast; 3/sec = 8th
|
| 142 |
+
# notes at 90 BPM)
|
| 143 |
+
# EGTR_FLATNESS_SAT 0.25 β 0.18 (was calibrated for heavy
|
| 144 |
+
# distortion; clean/crunch
|
| 145 |
+
# electric sits 0.06β0.14)
|
| 146 |
+
# Weights revised: 0.45Γpicking + 0.25Γflatness + 0.30Γsustain
|
| 147 |
+
# (was 0.65Γpicking + 0.35Γflatness β over-weighted fast picking).
|
| 148 |
+
# All v8 cache entries are incompatible (egtr_presence values shift).
|
| 149 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 150 |
ENG_DIMS = [
|
| 151 |
"lufs", # 0: Integrated loudness (LUFS)
|
| 152 |
"crest_factor", # 1: Peak-to-RMS ratio (global mix)
|
| 153 |
"drum_punch", # 2: Drum stem crest factor Γ drum_confidence (gated)
|
| 154 |
+
"drum_level", # 3: Drum stem RMS / total mix RMS [v10: renamed from drum_prominence]
|
| 155 |
"stereo_width", # 4: Mid-side energy ratio
|
| 156 |
"brightness", # 5: Spectral centroid of mix (Hz)
|
| 157 |
"harmonic_complexity", # 6: Chroma entropy (harmonic diversity only)
|
|
|
|
| 160 |
"clip_ratio", # 9: Clipped samples / total samples
|
| 161 |
"clip_max_run", # 10: Longest consecutive clipping run (samples)
|
| 162 |
"clip_mean_run", # 11: Mean clipping run length across all events
|
| 163 |
+
"other_level", # 12: Other stem (synth/pad/strings) RMS / total mix RMS [v10: renamed from other_prominence]
|
| 164 |
"vocal_level", # 13: Vocal stem RMS / total mix RMS (loudness when present)
|
| 165 |
"vocal_presence", # 14: Fraction of frames with active vocal signal (0β1)
|
| 166 |
"vocal_brightness", # 15: Spectral centroid of vocal stem (Hz)
|
|
|
|
| 172 |
"peak_lufs", # 21: Max short-term LUFS
|
| 173 |
"rolloff", # 22: Spectral rolloff (Hz)
|
| 174 |
"bass_tightness", # 23: Bass stem crest factor
|
| 175 |
+
"bass_level", # 24: Bass stem RMS / total mix RMS [v10: renamed from bass_prominence]
|
| 176 |
"phase_corr", # 25: L/R phase correlation
|
| 177 |
# ββ Drum sub-dimensions (v4+, semantics per version noted) βββββββββββββββ
|
| 178 |
+
"drum_kick_level", # 26: RMS(drum stem, 40β120 Hz) / RMS(full mix, 40β120 Hz) [v10: renamed from drum_kick_presence]
|
| 179 |
"drum_cymbal_presence",# 27: HPSS sustained fraction Γ band prominence in 4β10 kHz [v6]
|
| 180 |
"drum_confidence", # 28: 0.55Γkick_conf + 0.30Γhihat_conf + 0.15Γcymbal_conf [v6]
|
| 181 |
"hihat_presence", # 29: Onset density of percussive 8β14 kHz drum component [v6 NEW]
|
| 182 |
# ββ Electric guitar dimensions (v8 NEW β htdemucs_6s guitar stem) ββββββββ
|
| 183 |
"egtr_presence", # 30: Composite electric guitar detection score [0,1] [v8 NEW]
|
| 184 |
"egtr_level", # 31: egtr_presence Γ (guitar_rms / total_rms) β guitar mix level [v8 NEW]
|
| 185 |
+
# ββ Stem presence + level dimensions (v10 NEW) ββββββββββββββββββββββββββββ
|
| 186 |
+
"drum_presence", # 32: Fraction of 1-sec chunks with active drum stem signal [v10 NEW]
|
| 187 |
+
"other_presence", # 33: Fraction of 1-sec chunks with active other stem signal [v10 NEW]
|
| 188 |
+
"bass_presence", # 34: Fraction of 1-sec chunks with active bass stem signal [v10 NEW]
|
| 189 |
+
"drum_kick_presence", # 35: Onset density in 40β120 Hz kick band / DRUM_KICK_ONSET_SAT [v10 NEW]
|
| 190 |
+
"drum_cymbal_level", # 36: RMS(drum stem, 4β10 kHz) / total mix RMS [v10 NEW]
|
| 191 |
+
"hihat_level", # 37: RMS(drum stem, 8β14 kHz) / total mix RMS [v10 NEW]
|
| 192 |
]
|
| 193 |
|
| 194 |
# βοΏ½οΏ½οΏ½ Vocal activity detection threshold βββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 254 |
DRUM_HIHAT_WEIGHT = 0.30 # weight for hihat_presence in drum_confidence
|
| 255 |
DRUM_CYMBAL_WEIGHT = 0.15 # weight for cymbal_presence in drum_confidence
|
| 256 |
|
| 257 |
+
# ββ Electric guitar detection constants (v9) ββββββββββββββββββββββββββββββββββ
|
| 258 |
#
|
| 259 |
# EGTR_MID_FMIN / FMAX = 200β5000 Hz
|
| 260 |
# Core range of electric guitar fundamentals and dominant harmonics.
|
|
|
|
| 263 |
# from the bass stem into the guitar stem. Stopping at 5 kHz keeps the
|
| 264 |
# measure out of the hi-hat / pick-noise band (handled by drum dims).
|
| 265 |
#
|
| 266 |
+
# EGTR_PICK_ONSET_SAT = 3.0 (onsets/sec) [v9: was 4.0]
|
| 267 |
+
# Onset rate at which picking_density = 1.0. 8th notes at 90 bpm = 3/sec;
|
| 268 |
+
# 16th notes at 45 bpm = 3/sec. 4.0 was calibrated for fast-picking rock
|
| 269 |
+
# styles; moderate-tempo rhythm guitar (power chords, palm-muted chugging)
|
| 270 |
+
# sits at 2β3 onsets/sec and was being under-scored.
|
| 271 |
+
# Tune DOWN further if lane refs with active rhythm guitar score < 0.50.
|
| 272 |
# Tune UP if piano-only or pad-heavy tracks score above 0.30.
|
| 273 |
#
|
| 274 |
+
# EGTR_FLATNESS_SAT = 0.18 [v9: was 0.25]
|
| 275 |
# Mid-band spectral flatness at which mid_saturation = 1.0.
|
| 276 |
+
# Clean electric guitar (Fender/Strat clean tone): 0.06β0.14.
|
| 277 |
+
# Semi-clean / light crunch: 0.08β0.18.
|
| 278 |
+
# Crunchy/overdriven: 0.12β0.28. Full distortion: 0.20β0.40.
|
| 279 |
+
# Piano: 0.02β0.08. Acoustic guitar (clean): 0.03β0.10.
|
| 280 |
+
# 0.25 was only saturable by heavily overdriven guitar β clean and semi-clean
|
| 281 |
+
# electric guitar scored mid_saturation β€ 0.40 even with guitar throughout.
|
| 282 |
+
# 0.18 rewards any electric guitar content, not only distorted playing.
|
| 283 |
#
|
| 284 |
+
# EGTR_SUSTAIN_SAT = 0.70 [v9 NEW]
|
| 285 |
+
# H-component energy fraction at which tonal_sustain = 1.0.
|
| 286 |
+
# tonal_sustain = H_mid_energy / (H_mid_energy + P_mid_energy)
|
| 287 |
+
# where H = HPSS sustained component, P = HPSS percussive component,
|
| 288 |
+
# both measured in the 200β5000 Hz mid-band of the guitar stem.
|
| 289 |
+
#
|
| 290 |
+
# WHY THIS DIMENSION:
|
| 291 |
+
# picking_density and mid_saturation both measure the *attack/transient*
|
| 292 |
+
# character of guitar. A guitar playing sustained power chords, legato
|
| 293 |
+
# lines, or moderate-tempo chord progressions puts most of its energy into
|
| 294 |
+
# the H (sustained) component β the P (percussive) component only captures
|
| 295 |
+
# the brief pick attack. The entire sustain period is invisible to
|
| 296 |
+
# picking_density and adds no flatness signal. tonal_sustain explicitly
|
| 297 |
+
# captures this: as long as guitar notes are ringing, H_mid_energy is high
|
| 298 |
+
# relative to P_mid_energy, and tonal_sustain stays elevated regardless of
|
| 299 |
+
# tempo or gain stage.
|
| 300 |
+
#
|
| 301 |
+
# Expected ranges (guitar stem of htdemucs_6s):
|
| 302 |
+
# Electric guitar (any gain, any tempo): H_frac β 0.60β0.85 β sustain 0.86β1.0
|
| 303 |
+
# Acoustic guitar: H_frac β 0.55β0.75 β sustain 0.79β1.0
|
| 304 |
+
# Drum bleed in guitar stem: H_frac β 0.15β0.35 β sustain 0.21β0.50
|
| 305 |
+
# Low-level bleed / near-silence: Both near EPS β ratio noisy but
|
| 306 |
+
# egtr_level β 0 (guitar_rms near 0)
|
| 307 |
+
#
|
| 308 |
+
# 0.70 saturates at an H_frac consistent with guitar that is sustaining
|
| 309 |
+
# cleanly. Tracks with only transient bleed (drums) will score 0.21β0.50.
|
| 310 |
+
# Tune UP if non-guitar tracks score tonal_sustain > 0.60 consistently.
|
| 311 |
+
#
|
| 312 |
+
# WEIGHTS (v9):
|
| 313 |
+
# EGTR_PICK_WEIGHT = 0.45 (was 0.65) β rhythmic attack events
|
| 314 |
+
# EGTR_FLAT_WEIGHT = 0.25 (was 0.35) β harmonic saturation / gain stage
|
| 315 |
+
# EGTR_SUSTAIN_WEIGHT = 0.30 (NEW) β sustained tonal guitar content
|
| 316 |
+
# Sum = 1.00
|
| 317 |
+
#
|
| 318 |
+
# Rationale for reducing EGTR_PICK_WEIGHT from 0.65:
|
| 319 |
+
# The previous 0.65 weight made picking_density the near-exclusive gate.
|
| 320 |
+
# A track with guitar throughout but moderate tempo and clean tone could
|
| 321 |
+
# score egtr_presence β 0.62 even with guitar clearly audible in the mix,
|
| 322 |
+
# because picking_density was ~0.70 and mid_saturation was ~0.35.
|
| 323 |
+
# Redistributing weight to tonal_sustain captures guitar presence more
|
| 324 |
+
# robustly across gain stages and tempos.
|
| 325 |
#
|
| 326 |
# TUNING GUIDANCE (post first training run):
|
| 327 |
# Check the printed standards table for egtr_presence and egtr_level.
|
| 328 |
+
# Lane refs with active electric guitar: egtr_presence β₯ 0.65 (ideally 0.75β0.95)
|
| 329 |
+
# Non-lane refs without guitar: egtr_presence β€ 0.25
|
| 330 |
+
# If guitar tracks still score 0.30β0.50: lower EGTR_PICK_ONSET_SAT (3.0 β 2.0)
|
| 331 |
+
# and/or lower EGTR_FLATNESS_SAT (0.18 β 0.12).
|
| 332 |
+
# If piano/pad tracks score above 0.40: raise EGTR_SUSTAIN_SAT (0.70 β 0.80)
|
| 333 |
+
# or reduce EGTR_SUSTAIN_WEIGHT (0.30 β 0.20).
|
| 334 |
#
|
| 335 |
+
EGTR_MID_FMIN = 200.0 # Hz β guitar mid-band lower bound
|
| 336 |
+
EGTR_MID_FMAX = 5000.0 # Hz β guitar mid-band upper bound
|
| 337 |
+
EGTR_PICK_ONSET_SAT = 3.0 # onsets/sec at picking_density = 1.0 [v9: was 4.0]
|
| 338 |
+
EGTR_FLATNESS_SAT = 0.18 # flatness value at mid_saturation = 1.0 [v9: was 0.25]
|
| 339 |
+
EGTR_SUSTAIN_SAT = 0.70 # H-frac at tonal_sustain = 1.0 [v9 NEW]
|
| 340 |
+
EGTR_PICK_WEIGHT = 0.45 # weight for picking_density in egtr_presence [v9: was 0.65]
|
| 341 |
+
EGTR_FLAT_WEIGHT = 0.25 # weight for mid_saturation in egtr_presence [v9: was 0.35]
|
| 342 |
+
EGTR_SUSTAIN_WEIGHT = 0.30 # weight for tonal_sustain in egtr_presence [v9 NEW]
|
| 343 |
+
|
| 344 |
+
# ββ Stem temporal presence + kick onset constants (v10) βββββββββββββββββββββββ
|
| 345 |
+
DRUM_ACTIVITY_THRESHOLD = 0.01 # RMS threshold for an active drum chunk
|
| 346 |
+
BASS_ACTIVITY_THRESHOLD = 0.01 # RMS threshold for an active bass chunk
|
| 347 |
+
OTHER_ACTIVITY_THRESHOLD = 0.01 # RMS threshold for an active other chunk
|
| 348 |
+
DRUM_KICK_ONSET_SAT = 3.0 # kick onsets/sec at drum_kick_presence = 1.0
|
| 349 |
|
| 350 |
|
| 351 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 681 |
return out, gain_db
|
| 682 |
|
| 683 |
|
| 684 |
+
def _compute_drum_bands(drum_mono: np.ndarray, mix_mono: np.ndarray, sr: int,
|
| 685 |
+
total_rms: float) -> tuple:
|
| 686 |
"""
|
| 687 |
+
Returns (kick_dom, cymbal_presence, hihat_presence, cymbal_level, hihat_level).
|
| 688 |
|
| 689 |
kick_dom β dominance ratio (unchanged from v5):
|
| 690 |
RMS(drum_stem, 40β120 Hz) / RMS(mix, 40β120 Hz)
|
| 691 |
cymbal_presence β HPSS sustained fraction Γ band prominence in 4β10 kHz [0,1]
|
| 692 |
hihat_presence β onset density of HPSS percussive 8β14 kHz component [0,1]
|
| 693 |
+
cymbal_level β RMS(drum_stem, 4β10 kHz) / total_rms [v10 NEW]
|
| 694 |
+
hihat_level β RMS(drum_stem, 8β14 kHz) / total_rms [v10 NEW]
|
| 695 |
|
| 696 |
WHY HPSS INSTEAD OF DOMINANCE RATIO FOR CYMBAL / HI-HAT (v6):
|
| 697 |
|
|
|
|
| 776 |
onset_rate = len(onsets) / max(duration, 1.0)
|
| 777 |
hihat_presence = float(min(onset_rate / HIHAT_ONSET_SAT, 1.0))
|
| 778 |
|
| 779 |
+
# ββ Cymbal band level: absolute amplitude share in full mix βββββββββββββββ
|
| 780 |
+
cymbal_level = float(_band_rms_abs(drum_mono, sr, CYMBAL_BAND_FMIN, CYMBAL_BAND_FMAX)
|
| 781 |
+
/ (total_rms + EPS))
|
| 782 |
+
|
| 783 |
+
# ββ Hi-hat band level: absolute amplitude share in full mix ββββββββββββββ
|
| 784 |
+
hihat_level = float(_band_rms_abs(drum_mono, sr, HIHAT_BAND_FMIN, HIHAT_BAND_FMAX)
|
| 785 |
+
/ (total_rms + EPS))
|
| 786 |
+
|
| 787 |
+
return kick_dom, cymbal_presence, hihat_presence, cymbal_level, hihat_level
|
| 788 |
+
|
| 789 |
+
|
| 790 |
+
def _compute_stem_presence(stem_stereo: np.ndarray, sr: int, threshold: float,
|
| 791 |
+
chunk_s: float = 1.0) -> float:
|
| 792 |
+
"""
|
| 793 |
+
Fraction of 1-second chunks in which the stem's RMS exceeds threshold.
|
| 794 |
+
|
| 795 |
+
1.0 = stem active throughout the entire track.
|
| 796 |
+
0.5 = stem active in roughly half the track.
|
| 797 |
+
0.0 = stem silent throughout.
|
| 798 |
+
|
| 799 |
+
threshold = 0.01 (linear, β -40 dBFS) matches VOCAL_ACTIVITY_THRESHOLD.
|
| 800 |
+
stem_stereo: (2, n_samples) stereo stem array at sr Hz.
|
| 801 |
+
"""
|
| 802 |
+
mono = (stem_stereo[0] + stem_stereo[1]) / 2.0
|
| 803 |
+
chunk_len = max(int(chunk_s * sr), 1)
|
| 804 |
+
n_chunks = max(len(mono) // chunk_len, 1)
|
| 805 |
+
active = sum(
|
| 806 |
+
1 for i in range(n_chunks)
|
| 807 |
+
if np.sqrt(np.mean(mono[i * chunk_len : (i + 1) * chunk_len] ** 2)) > threshold
|
| 808 |
+
)
|
| 809 |
+
return float(active / n_chunks)
|
| 810 |
+
|
| 811 |
+
|
| 812 |
+
def _compute_kick_onset_presence(drum_mono: np.ndarray, sr: int) -> float:
|
| 813 |
+
"""
|
| 814 |
+
Onset density of percussive events in the kick band (40β120 Hz), normalised
|
| 815 |
+
to DRUM_KICK_ONSET_SAT. Returns a value in [0, 1].
|
| 816 |
+
|
| 817 |
+
Measures *rhythmic density* of kick hits β distinct from drum_kick_level
|
| 818 |
+
which measures low-end amplitude. A bass note scores high level, near-zero
|
| 819 |
+
presence. A kick drum scores high on both.
|
| 820 |
+
"""
|
| 821 |
+
if np.sqrt(np.mean(drum_mono ** 2)) < EPS:
|
| 822 |
+
return 0.0
|
| 823 |
+
lo = max(DRUM_KICK_FMIN, 1.0)
|
| 824 |
+
hi = min(DRUM_KICK_FMAX, sr / 2.0 - 1.0)
|
| 825 |
+
if lo >= hi:
|
| 826 |
+
return 0.0
|
| 827 |
+
hop = 512
|
| 828 |
+
sos = butter(2, [lo, hi], btype='bandpass', fs=sr, output='sos')
|
| 829 |
+
kick_sig = sosfilt(sos, drum_mono)
|
| 830 |
+
oenv = librosa.onset.onset_strength(y=kick_sig, sr=sr, hop_length=hop)
|
| 831 |
+
onsets = librosa.onset.onset_detect(onset_envelope=oenv, sr=sr, hop_length=hop)
|
| 832 |
+
duration = len(drum_mono) / sr
|
| 833 |
+
onset_rate = len(onsets) / max(duration, 1.0)
|
| 834 |
+
return float(min(onset_rate / DRUM_KICK_ONSET_SAT, 1.0))
|
| 835 |
|
| 836 |
|
| 837 |
def _compute_guitar_dims(guitar_mono: np.ndarray, sr: int) -> tuple:
|
| 838 |
"""
|
| 839 |
+
Returns (egtr_presence, picking_density, mid_saturation, tonal_sustain).
|
| 840 |
All values in [0, 1].
|
| 841 |
|
| 842 |
+
egtr_presence β composite [0,1] electric guitar detection score (v9):
|
| 843 |
+
EGTR_PICK_WEIGHT Γ picking_density
|
| 844 |
+
+ EGTR_FLAT_WEIGHT Γ mid_saturation
|
| 845 |
+
+ EGTR_SUSTAIN_WEIGHT Γ tonal_sustain
|
| 846 |
|
| 847 |
picking_density β onset density of the HPSS percussive component in
|
| 848 |
+
200β5000 Hz, normalized to EGTR_PICK_ONSET_SAT (3.0).
|
| 849 |
Detects pick/strum attack events per second.
|
| 850 |
+
Electric guitar: 1.5β5+ onsets/sec. Sustained pads: 0β0.5.
|
| 851 |
+
Piano also produces picking onsets; mid_saturation and
|
| 852 |
+
tonal_sustain together gate against false-positive piano.
|
| 853 |
|
| 854 |
mid_saturation β spectral flatness of the guitar stem in 200β5000 Hz,
|
| 855 |
+
normalized to EGTR_FLATNESS_SAT (0.18).
|
| 856 |
+
Clean electric guitar: 0.06β0.14 (mid_sat β 0.33β0.78).
|
| 857 |
+
Semi-clean / light crunch: 0.08β0.18 (mid_sat β 0.44β1.0).
|
| 858 |
+
Crunchy/overdriven: 0.12β0.28 (mid_sat β 0.67β1.0).
|
| 859 |
+
Piano: 0.02β0.08 (mid_sat β 0.11β0.44).
|
| 860 |
+
Acoustic guitar: 0.03β0.10 (mid_sat β 0.17β0.56).
|
| 861 |
Rewards harmonic saturation from magnetic pickups and
|
| 862 |
+
overdrive β the acoustic signature of electric amplification.
|
| 863 |
+
|
| 864 |
+
tonal_sustain β fraction of mid-band energy in the HPSS H (sustained)
|
| 865 |
+
component, normalized to EGTR_SUSTAIN_SAT (0.70).
|
| 866 |
+
Formula:
|
| 867 |
+
H_mid_energy = mean(H[mid_mask]Β²)
|
| 868 |
+
P_mid_energy = mean(P[mid_mask]Β²)
|
| 869 |
+
raw_sustain = H_mid_energy / (H_mid_energy + P_mid_energy + EPS)
|
| 870 |
+
tonal_sustain = min(raw_sustain / EGTR_SUSTAIN_SAT, 1.0)
|
| 871 |
+
|
| 872 |
+
WHY THIS DIMENSION (v9):
|
| 873 |
+
picking_density and mid_saturation both measure attack/
|
| 874 |
+
transient character. A guitar playing sustained power chords,
|
| 875 |
+
legato lines, or moderate-tempo chord progressions puts most
|
| 876 |
+
of its energy into the H (sustained) HPSS component β the
|
| 877 |
+
P (percussive) component only captures the brief pick attack.
|
| 878 |
+
The sustained note body is invisible to picking_density and
|
| 879 |
+
adds no flatness signal relative to its RMS contribution.
|
| 880 |
+
tonal_sustain captures this: as long as guitar notes are
|
| 881 |
+
ringing, H_mid_energy stays elevated relative to P_mid_energy,
|
| 882 |
+
regardless of tempo or gain stage.
|
| 883 |
+
|
| 884 |
+
Expected ranges (guitar stem, htdemucs_6s):
|
| 885 |
+
Electric guitar (any gain, any tempo): H_frac β 0.60β0.85
|
| 886 |
+
Acoustic guitar: H_frac β 0.55β0.75
|
| 887 |
+
Drum bleed in guitar stem: H_frac β 0.15β0.35
|
| 888 |
+
Near-silence (minimal bleed): ratio noisy but
|
| 889 |
+
egtr_level β 0
|
| 890 |
+
|
| 891 |
+
WHY HPSS ON GUITAR STEM (v8, unchanged):
|
| 892 |
Even with htdemucs_6s providing a dedicated guitar stem, there is residual
|
| 893 |
bleed from other instruments. HPSS separates the percussive (transient
|
| 894 |
attack) component from the harmonic (sustained) component within the guitar
|
|
|
|
| 897 |
bleed (synths, pads, vocals) tends to be sustained (β H component) rather
|
| 898 |
than transient (β P component) in this frequency range.
|
| 899 |
|
| 900 |
+
ACOUSTIC VS ELECTRIC LIMITATION (unchanged from v8):
|
| 901 |
+
Clean acoustic and clean electric guitar score similarly on picking_density.
|
| 902 |
+
mid_saturation partially distinguishes them (electric pickups add resonance
|
| 903 |
+
that raises flatness vs acoustic). tonal_sustain is similar for both.
|
| 904 |
+
The CLAP embedding (already in the feature vector) carries the residual
|
| 905 |
+
discrimination; the SVM learns from CLAP + these dims together.
|
| 906 |
+
|
| 907 |
+
Intermediate values (picking_density, mid_saturation, tonal_sustain) are
|
| 908 |
+
returned for debug/tuning but are NOT added to ENG_DIMS β only egtr_presence
|
| 909 |
+
and egtr_level are scoreable dimensions, preserving the atomicity principle.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 910 |
"""
|
| 911 |
if np.sqrt(np.mean(guitar_mono ** 2)) < EPS:
|
| 912 |
# Silent guitar stem β no guitar in the track.
|
| 913 |
+
return 0.0, 0.0, 0.0, 0.0
|
| 914 |
|
| 915 |
n_fft = 2048
|
| 916 |
hop = 512
|
|
|
|
| 947 |
raw_flatness = float(np.mean(frame_flat))
|
| 948 |
mid_saturation = float(min(raw_flatness / EGTR_FLATNESS_SAT, 1.0))
|
| 949 |
|
| 950 |
+
# ββ Tonal sustain: fraction of mid-band energy in sustained H component βββ
|
| 951 |
+
# Captures guitar notes that are ringing/sustaining β the body of each note
|
| 952 |
+
# after the pick attack, which is entirely invisible to picking_density.
|
| 953 |
+
# H (harmonic/sustained) component of HPSS in 200β5000 Hz.
|
| 954 |
+
# P (percussive/transient) component in same band (pick attacks only).
|
| 955 |
+
# raw_sustain = H_energy / (H_energy + P_energy) β [0, 1].
|
| 956 |
+
# When guitar notes sustain: H >> P, raw_sustain high β tonal_sustain high.
|
| 957 |
+
# When only drum transients bleed in: P >> H, raw_sustain low.
|
| 958 |
+
H_mid = H[mid_mask, :]
|
| 959 |
+
P_mid = P[mid_mask, :]
|
| 960 |
+
H_mid_energy = float(np.mean(H_mid ** 2))
|
| 961 |
+
P_mid_energy = float(np.mean(P_mid ** 2))
|
| 962 |
+
raw_sustain = H_mid_energy / (H_mid_energy + P_mid_energy + EPS)
|
| 963 |
+
tonal_sustain = float(min(raw_sustain / EGTR_SUSTAIN_SAT, 1.0))
|
| 964 |
+
|
| 965 |
egtr_presence = float(
|
| 966 |
+
EGTR_PICK_WEIGHT * picking_density +
|
| 967 |
+
EGTR_FLAT_WEIGHT * mid_saturation +
|
| 968 |
+
EGTR_SUSTAIN_WEIGHT * tonal_sustain
|
| 969 |
)
|
| 970 |
|
| 971 |
+
return egtr_presence, picking_density, mid_saturation, tonal_sustain
|
| 972 |
|
| 973 |
|
| 974 |
def extract_mir_features(path):
|
| 975 |
"""
|
| 976 |
Returns (l3_vec, eng_vec, true_peak_dbfs).
|
| 977 |
|
| 978 |
+
eng_vec is a 38-element array whose indices correspond to ENG_DIMS.
|
| 979 |
Indices 0β25 are unchanged from cache version 3.
|
| 980 |
Indices 26β29 are drum sub-dimensions (v4βv6, unchanged in v8):
|
| 981 |
+
26 drum_kick_level β kick dominance ratio (v5, renamed v10)
|
| 982 |
27 drum_cymbal_presence β HPSS sustained fraction Γ band prom in 4β10 kHz (v6)
|
| 983 |
28 drum_confidence β 0.55Γkick + 0.30Γhihat + 0.15Γcymbal (v6)
|
| 984 |
29 hihat_presence β onset density of percussive 8β14 kHz (v6 NEW)
|
| 985 |
Indices 30β31 are electric guitar dimensions (v8 NEW):
|
| 986 |
30 egtr_presence β composite guitar detection score [0,1]
|
| 987 |
31 egtr_level β egtr_presence Γ (guitar_rms / total_rms)
|
| 988 |
+
Indices 32β37 are stem presence + level dimensions (v10 NEW):
|
| 989 |
+
32 drum_presence β fraction of 1-sec chunks with active drum signal
|
| 990 |
+
33 other_presence β fraction of 1-sec chunks with active other signal
|
| 991 |
+
34 bass_presence β fraction of 1-sec chunks with active bass signal
|
| 992 |
+
35 drum_kick_presence β kick onset density in 40β120 Hz / DRUM_KICK_ONSET_SAT
|
| 993 |
+
36 drum_cymbal_level β RMS(drum, 4β10 kHz) / total_rms
|
| 994 |
+
37 hihat_level β RMS(drum, 8β14 kHz) / total_rms
|
| 995 |
|
| 996 |
drum_punch (index 2) is gated by drum_confidence:
|
| 997 |
drum_punch = crest_factor(drum_stem) Γ drum_confidence
|
|
|
|
| 1016 |
(4Γ oversampled, in dBFS). Returned so the caller can enforce the -1.0 dBFS
|
| 1017 |
true peak ceiling in apply_streaming_normalization() correctly.
|
| 1018 |
|
| 1019 |
+
Expected outcomes by source type (approximate, v9 constants):
|
| 1020 |
Distorted electric guitar prominent in mix:
|
| 1021 |
+
egtr_presence β 0.75β1.00, egtr_level β 0.20β0.50
|
| 1022 |
+
Clean/semi-clean electric guitar throughout:
|
| 1023 |
+
egtr_presence β 0.55β0.85, egtr_level β 0.10β0.35
|
| 1024 |
Acoustic guitar only (no electric):
|
| 1025 |
+
egtr_presence β 0.40β0.70, egtr_level β 0.05β0.20
|
| 1026 |
Piano-led track (no guitar):
|
| 1027 |
+
egtr_presence β 0.10β0.35, egtr_level β 0.01β0.10
|
| 1028 |
Pad/synth-led track (no guitar):
|
| 1029 |
+
egtr_presence β 0.05β0.25, egtr_level β 0.00β0.08
|
| 1030 |
"""
|
| 1031 |
y, sr = librosa.load(path, sr=44100, mono=True)
|
| 1032 |
y_stereo, _ = librosa.load(path, sr=sr, mono=False)
|
|
|
|
| 1054 |
# ββ Drum sub-band analysis (v6, unchanged in v8) ββββββββββββββββββββββββββ
|
| 1055 |
drum_mono = (src[_STEM_DRUMS][0] + src[_STEM_DRUMS][1]) / 2.0
|
| 1056 |
|
| 1057 |
+
drum_kick_lvl, drum_cymbal_pres, hihat_pres, drum_cymbal_lvl, hihat_lvl = \
|
| 1058 |
+
_compute_drum_bands(drum_mono, y, sr, total_rms)
|
| 1059 |
|
| 1060 |
# drum_confidence: weighted sum of three [0,1] drum indicators.
|
| 1061 |
+
kick_conf = min(drum_kick_lvl / DRUM_KICK_SAT, 1.0)
|
| 1062 |
hihat_conf = hihat_pres # already in [0, 1]
|
| 1063 |
cymbal_conf = drum_cymbal_pres # already in [0, 1]
|
| 1064 |
drum_conf = float(
|
|
|
|
| 1073 |
# ββ Bass crest factor βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1074 |
bass_tightness_val = np.max(np.abs(src[_STEM_BASS])) / (bass_rms + EPS)
|
| 1075 |
|
| 1076 |
+
# ββ Mix-level ratios (stem RMS / total RMS) βββββββββββββββββββββββββββββββ
|
| 1077 |
+
drum_lvl = drum_rms / total_rms
|
| 1078 |
+
bass_lvl = bass_rms / total_rms
|
| 1079 |
+
other_lvl = other_rms / total_rms
|
| 1080 |
voc_level = voc_rms / total_rms
|
| 1081 |
|
| 1082 |
# ββ Vocal sub-dimensions ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 1089 |
librosa.feature.spectral_centroid(y=voc_mono, sr=sr)
|
| 1090 |
))
|
| 1091 |
|
| 1092 |
+
# ββ Electric guitar sub-dimensions (v9 updated) ββββββββββββββββββββββββββ
|
| 1093 |
guitar_mono = (src[_STEM_GUITAR][0] + src[_STEM_GUITAR][1]) / 2.0
|
| 1094 |
+
egtr_pres, _picking, _flatness, _sustain = _compute_guitar_dims(guitar_mono, sr)
|
|
|
|
|
|
|
| 1095 |
egtr_level_val = float(egtr_pres * (guitar_rms / total_rms))
|
| 1096 |
|
| 1097 |
+
# ββ Stem temporal presence (v10 NEW) βββββββββββββββββββββββββββββββββββββ
|
| 1098 |
+
drum_presence_val = _compute_stem_presence(
|
| 1099 |
+
src[_STEM_DRUMS], sr, DRUM_ACTIVITY_THRESHOLD
|
| 1100 |
+
)
|
| 1101 |
+
other_presence_val = _compute_stem_presence(
|
| 1102 |
+
src[_STEM_OTHER], sr, OTHER_ACTIVITY_THRESHOLD
|
| 1103 |
+
)
|
| 1104 |
+
bass_presence_val = _compute_stem_presence(
|
| 1105 |
+
src[_STEM_BASS], sr, BASS_ACTIVITY_THRESHOLD
|
| 1106 |
+
)
|
| 1107 |
+
|
| 1108 |
+
# ββ Kick onset density (v10 NEW) βββββββββββββββββββββββββββββββββββββββββ
|
| 1109 |
+
drum_kick_pres_val = _compute_kick_onset_presence(drum_mono, sr)
|
| 1110 |
+
|
| 1111 |
# ββ Mid-side stereo width βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1112 |
mid, side = (y_stereo[0] + y_stereo[1]) / 2, (y_stereo[0] - y_stereo[1]) / 2
|
| 1113 |
ms_ratio = (np.sqrt(np.mean(side**2)) /
|
| 1114 |
(np.sqrt(np.mean(mid**2)) + np.sqrt(np.mean(side**2)) + EPS))
|
| 1115 |
|
| 1116 |
# ββ Short-term loudness (K-weighted, BS.1770 β no gating) ββββββββββββββββ
|
|
|
|
|
|
|
| 1117 |
st_lufs = _compute_short_term_lufs_array(y_stereo, sr, window_s=3.0, hop_s=1.0)
|
| 1118 |
|
| 1119 |
# ββ Clipping βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 1124 |
_compute_integrated_lufs(y_stereo, sr), # 0 lufs (ITU-R BS.1770-4 β K-weighted, gated)
|
| 1125 |
np.max(np.abs(y)) / (np.sqrt(np.mean(y**2)) + EPS), # 1 crest_factor
|
| 1126 |
drum_punch_val, # 2 drum_punch (gated)
|
| 1127 |
+
drum_lvl, # 3 drum_level (v10: renamed from drum_prominence)
|
| 1128 |
ms_ratio, # 4 stereo_width
|
| 1129 |
librosa.feature.spectral_centroid(y=y, sr=sr).mean(), # 5 brightness
|
| 1130 |
entropy(np.mean(librosa.feature.chroma_cqt(y=y, sr=sr), axis=1)), # 6 harmonic_complexity
|
|
|
|
| 1133 |
c_ratio, # 9 clip_ratio
|
| 1134 |
c_max, # 10 clip_max_run
|
| 1135 |
c_mean, # 11 clip_mean_run
|
| 1136 |
+
other_lvl, # 12 other_level (v10: renamed from other_prominence)
|
| 1137 |
voc_level, # 13 vocal_level
|
| 1138 |
voc_presence, # 14 vocal_presence
|
| 1139 |
voc_brightness, # 15 vocal_brightness
|
|
|
|
| 1145 |
np.max(st_lufs), # 21 peak_lufs
|
| 1146 |
np.mean(librosa.feature.spectral_rolloff(y=y, sr=sr)), # 22 rolloff
|
| 1147 |
bass_tightness_val, # 23 bass_tightness
|
| 1148 |
+
bass_lvl, # 24 bass_level (v10: renamed from bass_prominence)
|
| 1149 |
np.nan_to_num(np.corrcoef(y_stereo[0], y_stereo[1])[0, 1]), # 25 phase_corr
|
| 1150 |
+
drum_kick_lvl, # 26 drum_kick_level (v10: renamed from drum_kick_presence)
|
| 1151 |
drum_cymbal_pres, # 27 drum_cymbal_presence (v6)
|
| 1152 |
drum_conf, # 28 drum_confidence (v6)
|
| 1153 |
hihat_pres, # 29 hihat_presence (v6)
|
| 1154 |
egtr_pres, # 30 egtr_presence (v8 NEW)
|
| 1155 |
egtr_level_val, # 31 egtr_level (v8 NEW)
|
| 1156 |
+
drum_presence_val, # 32 drum_presence (v10 NEW)
|
| 1157 |
+
other_presence_val, # 33 other_presence (v10 NEW)
|
| 1158 |
+
bass_presence_val, # 34 bass_presence (v10 NEW)
|
| 1159 |
+
drum_kick_pres_val, # 35 drum_kick_presence (v10 NEW)
|
| 1160 |
+
drum_cymbal_lvl, # 36 drum_cymbal_level (v10 NEW)
|
| 1161 |
+
hihat_lvl, # 37 hihat_level (v10 NEW)
|
| 1162 |
])
|
| 1163 |
|
| 1164 |
# ββ True peak (BS.1775-2, 4Γ oversampled) ββββββββββββββββββββββββββββββββ
|