hybrid_engine.py
4.18 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
"""
Hybrid ACR Engine: Chromaprint fast pre-filter + ECAPA-TDNN deep re-ranking.
"""
import numpy as np
import librosa
from typing import List, Tuple, Optional, Dict
from pathlib import Path
import json
import time
class Candidate:
def __init__(self, song_id: str, chroma_score: float = 0.0, ecapa_score: float = 0.0):
self.song_id = song_id
self.chroma_score = chroma_score
self.ecapa_score = ecapa_score
self.metadata: Dict = {}
@property
def combined_score(self) -> float:
return 0.3 * self.chroma_score + 0.7 * self.ecapa_score
def __repr__(self):
return f"Candidate({self.song_id}, chroma={self.chroma_score:.3f}, ecapa={self.ecapa_score:.3f})"
class HybridEngine:
def __init__(
self,
chroma_matcher=None,
ecapa_embedder=None,
ref_embs: Optional[np.ndarray] = None,
ref_ids: Optional[List[str]] = None,
sr: int = 16000,
chroma_weight: float = 0.3,
ecapa_weight: float = 0.7,
reject_threshold: float = 0.4,
):
self.chroma = chroma_matcher
self.ecapa = ecapa_embedder
self.ref_embs = ref_embs
self.ref_ids = ref_ids
self.sr = sr
self.chroma_weight = chroma_weight
self.ecapa_weight = ecapa_weight
self.reject_threshold = reject_threshold
self.song_metadata: Dict[str, Dict] = {}
def load_metadata(self, metadata_path: str):
with open(metadata_path) as f:
items = json.load(f)
for item in items:
sid = item["song_id"]
if sid not in self.song_metadata:
base = item.get("base_freq", 0)
self.song_metadata[sid] = {
"song_id": sid,
"base_freq": base,
"audio_path": item.get("audio_path", ""),
}
def recognize(
self,
audio_path: str,
top_n: int = 5,
mode: str = "auto",
) -> List[Dict]:
start = time.time()
y, _ = librosa.load(audio_path, sr=self.sr, mono=True)
chroma_candidates: List[Candidate] = []
if self.chroma is not None:
chroma_matches = self.chroma.match(y, top_k=50)
seen = set()
for song_id, score in chroma_matches:
if song_id not in seen:
seen.add(song_id)
c = Candidate(song_id, chroma_score=score)
chroma_candidates.append(c)
ecapa_candidates: List[Candidate] = []
if self.ecapa is not None and self.ref_embs is not None:
query_emb = self.ecapa.extract_embedding_from_wave(y)
ref_norm = self.ref_embs / (
np.linalg.norm(self.ref_embs, axis=1, keepdims=True) + 1e-12
)
query_norm = query_emb / (np.linalg.norm(query_emb) + 1e-12)
scores = query_norm @ ref_norm.T
top_indices = np.argsort(-scores)[:top_n]
for idx in top_indices:
c = Candidate(self.ref_ids[idx], ecapa_score=float(scores[idx]))
ecapa_candidates.append(c)
combined: Dict[str, Candidate] = {}
for c in chroma_candidates:
combined[c.song_id] = c
for c in ecapa_candidates:
if c.song_id in combined:
combined[c.song_id].ecapa_score = c.ecapa_score
else:
combined[c.song_id] = c
for sid in list(combined.keys()):
combined[sid].metadata = self.song_metadata.get(sid, {})
results = sorted(
combined.values(),
key=lambda c: c.combined_score,
reverse=True,
)[:top_n]
elapsed = (time.time() - start) * 1000
output = []
for c in results:
output.append({
"song_id": c.song_id,
"confidence": round(c.combined_score, 4),
"chromaprint_score": round(c.chroma_score, 4),
"ecapa_score": round(c.ecapa_score, 4),
"metadata": c.metadata,
})
return {
"candidates": output,
"processing_time_ms": round(elapsed, 1),
"num_candidates": len(results),
}