Commit 8095eeea 8095eeeafc7fb9f4f55aac1ddb8b39b35a3953b1 by cnb.bofCdSsphPA

Prefer the repo fingerprint matcher in the real-directory song-centric pipeline

Constraint: Improve the current directory-to-feature path using components already present in the repo, without depending on unavailable heavyweight semantic runtimes.
Rejected: Keep exact-lane validation on a purely ad-hoc local hash path | It underuses the repo's existing fingerprint extraction capability and weakens evidence for the real pipeline.
Confidence: high
Scope-risk: narrow
Directive: In host-level song-centric pipeline validation, prefer ChromaprintMatcher-backed fingerprints first and use local_wavehash only as fallback.
Tested: /usr/local/miniconda3/bin/python acr-engine/scripts/enrich_songcentric_manifest_with_local_features.py on the real wav smoke manifest; imported the enriched manifest into postgres://d2:d2pass@127.0.0.1:5432/d2 schema acr_songcentric_test twice and verified counts stayed media_entity=9, audio_object=22, feature_fact=24, set_membership=9 on rerun; matcher_fingerprint_count=5 and fallback_fingerprint_count=0; git diff --check; /usr/local/miniconda3/bin/python scripts/check_markdown_links.py --root docs returned OK for 11 active markdown files
Not-tested: true external chromaprint library integration and semantic-model-backed enrichment on this host
1 parent 5e00c5b0
{"song": {"biz_key": "song_alpha", "title": "song alpha", "artist_name": "artist a"}, "asset": {"source_type": "official", "storage_uri": "/workspace/acr-engine/data/songcentric_builder_smoke/song_alpha/artist_a/clip1.wav", "storage_scheme": "file", "checksum": "path:/workspace/acr-engine/data/songcentric_builder_smoke/song_alpha/artist_a/clip1.wav", "codec": "wav", "sample_rate": 16000, "channels": 1, "duration_ms": 8000}, "windows": [{"start_ms": 0, "end_ms": 5000, "features": [{"feature_type": "fingerprint", "model_name": "chromaprint_matcher", "model_version": "phase1_local", "feature_set_name": "chromaprint_matcher_5s", "fingerprint_value": "dc0c731425f360787f462da693ff4a50", "checksum": "chromaprint:dc0c731425f36078", "metadata_json": {"hash_count": 2643, "hash_sample": [[1842187, 11], [1842188, 11], [1842189, 11], [1842201, 11], [1842212, 11], [1842213, 11], [1842214, 11], [1842438, 11]]}}, {"feature_type": "embedding", "model_name": "local_wavehash_embed", "model_version": "v1", "feature_set_name": "wavehash_embed_5s", "feature_schema_ver": "v1", "embedding_dim": 8, "embedding_uri": "inline://593c7a661cc87444:0:5000", "vector_table_name": "audio_embedding_vector_8_placeholder", "checksum": "emb:593c7a661cc87444", "metadata_json": {"energy": 30555200, "rate": 16000, "channels": 1}}]}, {"start_ms": 2500, "end_ms": 7500, "features": [{"feature_type": "fingerprint", "model_name": "chromaprint_matcher", "model_version": "phase1_local", "feature_set_name": "chromaprint_matcher_5s", "fingerprint_value": "dc0c731425f360787f462da693ff4a50", "checksum": "chromaprint:dc0c731425f36078", "metadata_json": {"hash_count": 2643, "hash_sample": [[1842187, 11], [1842188, 11], [1842189, 11], [1842201, 11], [1842212, 11], [1842213, 11], [1842214, 11], [1842438, 11]]}}, {"feature_type": "embedding", "model_name": "local_wavehash_embed", "model_version": "v1", "feature_set_name": "wavehash_embed_5s", "feature_schema_ver": "v1", "embedding_dim": 8, "embedding_uri": "inline://593c7a661cc87444:2500:7500", "vector_table_name": "audio_embedding_vector_8_placeholder", "checksum": "emb:593c7a661cc87444", "metadata_json": {"energy": 30555200, "rate": 16000, "channels": 1}}]}, {"start_ms": 3000, "end_ms": 8000, "features": [{"feature_type": "fingerprint", "model_name": "chromaprint_matcher", "model_version": "phase1_local", "feature_set_name": "chromaprint_matcher_5s", "fingerprint_value": "dc0c731425f360787f462da693ff4a50", "checksum": "chromaprint:dc0c731425f36078", "metadata_json": {"hash_count": 2643, "hash_sample": [[1842187, 11], [1842188, 11], [1842189, 11], [1842201, 11], [1842212, 11], [1842213, 11], [1842214, 11], [1842438, 11]]}}, {"feature_type": "embedding", "model_name": "local_wavehash_embed", "model_version": "v1", "feature_set_name": "wavehash_embed_5s", "feature_schema_ver": "v1", "embedding_dim": 8, "embedding_uri": "inline://593c7a661cc87444:3000:8000", "vector_table_name": "audio_embedding_vector_8_placeholder", "checksum": "emb:593c7a661cc87444", "metadata_json": {"energy": 30555200, "rate": 16000, "channels": 1}}]}], "memberships": [{"set_type": "reference_set", "set_name": "phase1_hot_reference_v1", "member_type": "asset", "priority": 100}]}
{"song": {"biz_key": "song_beta", "title": "song beta", "artist_name": "artist b"}, "asset": {"source_type": "official", "storage_uri": "/workspace/acr-engine/data/songcentric_builder_smoke/song_beta/artist_b/clip2.wav", "storage_scheme": "file", "checksum": "path:/workspace/acr-engine/data/songcentric_builder_smoke/song_beta/artist_b/clip2.wav", "codec": "wav", "sample_rate": 16000, "channels": 1, "duration_ms": 6000}, "windows": [{"start_ms": 0, "end_ms": 5000, "features": [{"feature_type": "fingerprint", "model_name": "chromaprint_matcher", "model_version": "phase1_local", "feature_set_name": "chromaprint_matcher_5s", "fingerprint_value": "d8fc2442b4ec3ce5ae180c5845cffccb", "checksum": "chromaprint:d8fc2442b4ec3ce5", "metadata_json": {"hash_count": 2202, "hash_sample": [[2763289, 23], [2763524, 23], [2763541, 23], [2763549, 23], [2763566, 23], [2763801, 23], [2764050, 23], [2764075, 23]]}}, {"feature_type": "embedding", "model_name": "local_wavehash_embed", "model_version": "v1", "feature_set_name": "wavehash_embed_5s", "feature_schema_ver": "v1", "embedding_dim": 8, "embedding_uri": "inline://4ed2ccfa55b10b88:0:5000", "vector_table_name": "audio_embedding_vector_8_placeholder", "checksum": "emb:4ed2ccfa55b10b88", "metadata_json": {"energy": 30555680, "rate": 16000, "channels": 1}}]}, {"start_ms": 1000, "end_ms": 6000, "features": [{"feature_type": "fingerprint", "model_name": "chromaprint_matcher", "model_version": "phase1_local", "feature_set_name": "chromaprint_matcher_5s", "fingerprint_value": "d8fc2442b4ec3ce5ae180c5845cffccb", "checksum": "chromaprint:d8fc2442b4ec3ce5", "metadata_json": {"hash_count": 2202, "hash_sample": [[2763289, 23], [2763524, 23], [2763541, 23], [2763549, 23], [2763566, 23], [2763801, 23], [2764050, 23], [2764075, 23]]}}, {"feature_type": "embedding", "model_name": "local_wavehash_embed", "model_version": "v1", "feature_set_name": "wavehash_embed_5s", "feature_schema_ver": "v1", "embedding_dim": 8, "embedding_uri": "inline://4ed2ccfa55b10b88:1000:6000", "vector_table_name": "audio_embedding_vector_8_placeholder", "checksum": "emb:4ed2ccfa55b10b88", "metadata_json": {"energy": 30555680, "rate": 16000, "channels": 1}}]}], "memberships": [{"set_type": "reference_set", "set_name": "phase1_hot_reference_v1", "member_type": "asset", "priority": 100}]}
{
"schema": "acr_songcentric_test",
"manifest": "acr-engine/data/pgvector_eval/music20/songcentric_directory_manifest_with_features_v2.jsonl",
"imported": [
{
"song_id": 8,
"asset_id": 16,
"window_ids": [
17,
18,
19
],
"feature_ids": [
20,
11,
21,
13,
22,
15
],
"membership_ids": [
8
]
},
{
"song_id": 9,
"asset_id": 20,
"window_ids": [
21,
22
],
"feature_ids": [
23,
17,
24,
19
],
"membership_ids": [
9
]
}
],
"counts": {
"media_entity": 9,
"audio_object": 22,
"feature_fact": 24,
"set_membership": 9
},
"window_lineage_sample": {
"window_id": 22,
"asset_id": 20,
"song_id": 9,
"title": "song beta",
"start_ms": 1000,
"end_ms": 6000
},
"feature_lineage_sample": {
"feature_type": "fingerprint",
"model_name": "chromaprint_matcher",
"model_version": "phase1_local",
"feature_set_name": "chromaprint_matcher_5s",
"window_id": 22,
"song_id": 9,
"title": "song beta"
}
}
\ No newline at end of file
{
"schema": "acr_songcentric_test",
"manifest": "acr-engine/data/pgvector_eval/music20/songcentric_directory_manifest_with_features_v2.jsonl",
"imported": [
{
"song_id": 8,
"asset_id": 16,
"window_ids": [
17,
18,
19
],
"feature_ids": [
20,
11,
21,
13,
22,
15
],
"membership_ids": [
8
]
},
{
"song_id": 9,
"asset_id": 20,
"window_ids": [
21,
22
],
"feature_ids": [
23,
17,
24,
19
],
"membership_ids": [
9
]
}
],
"counts": {
"media_entity": 9,
"audio_object": 22,
"feature_fact": 24,
"set_membership": 9
},
"window_lineage_sample": {
"window_id": 22,
"asset_id": 20,
"song_id": 9,
"title": "song beta",
"start_ms": 1000,
"end_ms": 6000
},
"feature_lineage_sample": {
"feature_type": "fingerprint",
"model_name": "chromaprint_matcher",
"model_version": "phase1_local",
"feature_set_name": "chromaprint_matcher_5s",
"window_id": 22,
"song_id": 9,
"title": "song beta"
}
}
\ No newline at end of file
{
"input_manifest": "/workspace/acr-engine/data/pgvector_eval/music20/songcentric_directory_manifest.jsonl",
"output_manifest": "/workspace/acr-engine/data/pgvector_eval/music20/songcentric_directory_manifest_with_features_v2.jsonl",
"rows": 2,
"wav_windows_seen": 5,
"features_added": 10,
"matcher_fingerprint_count": 5,
"fallback_fingerprint_count": 0
}
\ No newline at end of file
......@@ -4,10 +4,16 @@ from __future__ import annotations
import argparse
import hashlib
import json
import math
import wave
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
import sys
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from src.engines.chromaprint_matcher import ChromaprintMatcher, load_audio_mono
def load_jsonl(path: Path):
for line in path.read_text().splitlines():
......@@ -26,14 +32,29 @@ def read_wav_stats(path: Path, start_ms: int, end_ms: int) -> dict:
wf.setpos(min(start_frame, wf.getnframes()))
frames = wf.readframes(max(end_frame - start_frame, 0))
digest = hashlib.sha256(frames).hexdigest()
energy = sum(abs(b - 128) for b in frames[: min(len(frames), 4000)]) if sampwidth == 1 else sum(abs(int.from_bytes(frames[i:i+2], 'little', signed=True)) for i in range(0, min(len(frames), 8000), 2))
return {
'digest': digest,
'energy': energy,
'rate': rate,
'channels': n_channels,
'bytes_read': len(frames),
}
if sampwidth == 1:
energy = sum(abs(b - 128) for b in frames[: min(len(frames), 4000)])
else:
energy = sum(abs(int.from_bytes(frames[i:i+2], 'little', signed=True)) for i in range(0, min(len(frames), 8000), 2))
return {'digest': digest, 'energy': energy, 'rate': rate, 'channels': n_channels, 'bytes_read': len(frames)}
def extract_matcher_fingerprint(path: Path, start_ms: int, end_ms: int) -> dict | None:
try:
matcher = ChromaprintMatcher(sr=16000)
y, _ = load_audio_mono(str(path), sr=matcher.sr)
start = int(start_ms * matcher.sr / 1000)
end = int(end_ms * matcher.sr / 1000)
segment = y[start:end]
hashes = matcher.extract_hashes(segment)
digest = hashlib.sha256(json.dumps(hashes[:128]).encode('utf-8')).hexdigest()
return {
'fingerprint_value': digest[:32],
'checksum': f'chromaprint:{digest[:16]}',
'metadata_json': {'hash_count': len(hashes), 'hash_sample': hashes[:8]},
}
except Exception:
return None
def main() -> int:
......@@ -52,24 +73,40 @@ def main() -> int:
rows = []
feature_count = 0
wav_assets = 0
wav_windows_seen = 0
matcher_fp_count = 0
fallback_fp_count = 0
for row in load_jsonl(in_path):
asset = row['asset']
asset_path = Path(asset['storage_uri'])
for idx, window in enumerate(row.get('windows', []), start=1):
for window in row.get('windows', []):
features = window.setdefault('features', [])
if asset_path.suffix.lower() == '.wav' and asset_path.exists():
wav_assets += 1
wav_windows_seen += 1
stats = read_wav_stats(asset_path, window['start_ms'], window['end_ms'])
fp = {
'feature_type': 'fingerprint',
'model_name': 'local_wavehash',
'model_version': 'v1',
'feature_set_name': 'wavehash_5s',
'fingerprint_value': stats['digest'][:32],
'checksum': f"fp:{stats['digest'][:16]}",
'metadata_json': {'energy': stats['energy'], 'bytes_read': stats['bytes_read']},
}
matcher_fp = extract_matcher_fingerprint(asset_path, window['start_ms'], window['end_ms'])
if matcher_fp is not None:
fp = {
'feature_type': 'fingerprint',
'model_name': 'chromaprint_matcher',
'model_version': 'phase1_local',
'feature_set_name': 'chromaprint_matcher_5s',
'fingerprint_value': matcher_fp['fingerprint_value'],
'checksum': matcher_fp['checksum'],
'metadata_json': matcher_fp['metadata_json'],
}
matcher_fp_count += 1
else:
fp = {
'feature_type': 'fingerprint',
'model_name': 'local_wavehash',
'model_version': 'v1',
'feature_set_name': 'wavehash_5s',
'fingerprint_value': stats['digest'][:32],
'checksum': f"fp:{stats['digest'][:16]}",
'metadata_json': {'energy': stats['energy'], 'bytes_read': stats['bytes_read']},
}
fallback_fp_count += 1
emb = {
'feature_type': 'embedding',
'model_name': 'local_wavehash_embed',
......@@ -91,8 +128,10 @@ def main() -> int:
'input_manifest': str(in_path),
'output_manifest': str(out_path),
'rows': len(rows),
'wav_assets_seen': wav_assets,
'wav_windows_seen': wav_windows_seen,
'features_added': feature_count,
'matcher_fingerprint_count': matcher_fp_count,
'fallback_fingerprint_count': fallback_fp_count,
}
if report_path:
report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2))
......
## 2026-06-04
- 升级 `enrich_songcentric_manifest_with_local_features.py`:目录链中的 fingerprint 现优先复用仓库内 `ChromaprintMatcher`,并在 live PostgreSQL 上验证 5 个 wav windows 全部命中 matcher 路径、`fallback_fingerprint_count=0`
- 新增 `acr-engine/scripts/enrich_songcentric_manifest_with_local_features.py`,可对真实 wav 目录生成的 manifest 自动补本地 deterministic fingerprint/embedding,再导入 `feature_fact`;已在 live PostgreSQL 上验证 `audio files -> manifest -> features -> import` 闭环与幂等性。
- 新增 `acr-engine/scripts/build_songcentric_manifest_from_directory.py`,把真实音频目录自动转换为 song-centric manifest;并用本地真实 wav smoke 目录在 live PostgreSQL 上验证了 `audio files -> manifest -> import` 链路及幂等性。
......
......@@ -300,6 +300,18 @@ flowchart TD
当前特征补全脚本:[`acr-engine/scripts/enrich_songcentric_manifest_with_local_features.py`](../acr-engine/scripts/enrich_songcentric_manifest_with_local_features.py)
### 4.8 目录链中的 exact lane 提升
当前 `enrich_songcentric_manifest_with_local_features.py` 已优先复用仓库内 `ChromaprintMatcher` 生成 fingerprint;只有失败时才回退到 `local_wavehash`
本轮 fresh evidence:
- `wav_windows_seen = 5`
- `matcher_fingerprint_count = 5`
- `fallback_fingerprint_count = 0`
这说明当前目录链里的 exact lane 已经不只是临时 hash,而是优先接上了仓库现有 fingerprint 提取能力。
---
## 5. 最常用 SQL 样例
......