-
Notifications
You must be signed in to change notification settings - Fork 0
test(analysis): add Tier 1 real-audio accuracy acceptance #890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
seonghobae
wants to merge
1
commit into
develop
from
cursor/bc-dbe25de1-cb48-476c-8ee5-3cdca5a13380-3545
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Real-audio accuracy acceptance | ||
|
|
||
| Next action: before claiming a rehearsal result is accurate, run the Tier 1 | ||
| decoded-PCM cases. A green unit suite that never opens a WAV is not buyer | ||
| evidence. | ||
|
|
||
| ```bash | ||
| uv run --project services/analysis-engine pytest \ | ||
| services/analysis-engine/tests/test_accuracy_acceptance.py | ||
| ``` | ||
|
|
||
| ## Why this lock exists | ||
|
|
||
| BandScope sells rehearsal guidance. A buyer cannot distinguish “the job | ||
| succeeded” from “the engine heard the chord and tempo that are in the file” | ||
| until decoded PCM is scored against a known label (Raffel et al., 2014). | ||
|
|
||
| This layer is Tier 1 of issue #770: tiny, license-clean, deterministic WAV | ||
| fixtures generated in process. It does not claim genre coverage, perceptual | ||
| stem quality, or private-corpus readiness. | ||
|
|
||
| ## Held cases | ||
|
|
||
| - `c-major-triad`: three seconds of C4+E4+G4 written to WAV, decoded, and | ||
| scored with duration-weighted chord recall. Pass when recall of `C` is at | ||
| least `0.70` (Odekerken et al., 2021). | ||
| - `click-120-bpm`: eight seconds of 120 BPM clicks decoded by | ||
| `TemporalAnalyzer`. Pass when estimated tempo satisfies Acc1 at 4% | ||
| (Schreiber & Müller, 2020). Acc1 does not credit half-time or double-time. | ||
| - Checksum mismatch fails closed. Do not score a tampered file as a pass. | ||
|
|
||
| ## Claim boundary | ||
|
|
||
| A passing case supports only the registered fixture, metric, engine version, | ||
| and tolerance. It does not establish universal musical correctness. | ||
|
|
||
| Keys-left, keys-right, and acoustic-guitar roles still carry arrangement | ||
| defaults (`C#`, `Emaj7`, `Eb`). Lead vocal harmony is the role that currently | ||
| reflects the `other` stem recognizer. Do not treat those defaults as | ||
| measured accuracy. | ||
|
|
||
| ## References | ||
|
|
||
| Odekerken, D., Koops, H. V., & Volk, A. (2021). Improving audio chord | ||
| estimation by alignment and integration of crowd-sourced symbolic music. | ||
| *Transactions of the International Society for Music Information Retrieval, | ||
| 4*(1), 141–155. https://doi.org/10.5334/tismir.81 | ||
|
|
||
| Raffel, C., McFee, B., Humphrey, E. J., Salamon, J., Nieto, O., Liang, D., & | ||
| Ellis, D. P. W. (2014). MIR_EVAL: A transparent implementation of common MIR | ||
| metrics. In *Proceedings of the 15th International Society for Music | ||
| Information Retrieval Conference* (pp. 367–372). | ||
|
|
||
| Schreiber, H., & Müller, M. (2020). Music tempo estimation: Are we done yet? | ||
| *Transactions of the International Society for Music Information Retrieval, | ||
| 3*(1), 111–125. https://doi.org/10.5334/tismir.43 | ||
|
|
||
| ## Security Notes | ||
|
|
||
| - Attack surface: generated WAV bytes, SHA-256 digests, and decoded PCM | ||
| passed into `ChordRecognizer` and `TemporalAnalyzer`. | ||
| - Trust boundary: untrusted audio and manifests; trusted repo-controlled | ||
| fixture generators and metric floors. | ||
| - Mitigations: no network, no shell, checksum fail-closed, bounded fixture | ||
| durations, no copyrighted commercial recordings. | ||
| - Test points: deterministic digest, C major recall, 120 BPM Acc1, checksum | ||
| mismatch, malformed manifest, silence must not pass as C major. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Real-audio accuracy acceptance (Tier 1) | ||
|
|
||
| **Goal:** Give BandScope a buyer-readable accuracy gate: decoded PCM from a | ||
| known WAV must recover the expected chord or tempo, with a versioned report. | ||
|
|
||
| **Architecture:** A new `bandscope_analysis.accuracy` package generates | ||
| license-clean fixtures, scores them with documented MIR metrics, and emits a | ||
| strict case-report schema. Tests write real WAV files and call production | ||
| `ChordRecognizer` and `TemporalAnalyzer` helpers. Stem separation stays out | ||
| of this slice. | ||
|
|
||
| **Tech Stack:** NumPy, soundfile, librosa beat tracking, pytest. | ||
|
|
||
| ## Security Notes | ||
|
|
||
| ### Attack surface | ||
|
|
||
| Generated WAV files, SHA-256 digests, decoded PCM arrays, and JSON-shaped | ||
| accuracy reports. | ||
|
|
||
| ### Trust boundary | ||
|
|
||
| Untrusted: any on-disk fixture bytes and any parsed report mapping. | ||
| Trusted: in-repo generators, metric definitions, and registered floors. | ||
|
|
||
| ### Mitigations | ||
|
|
||
| - No network and no shell interpolation. | ||
| - Checksum mismatch raises before tempo scoring. | ||
| - Manifest parsing fails closed on missing or mistyped fields. | ||
| - Fixtures are short, synthetic, and license-clean. | ||
|
|
||
| ### Test points | ||
|
|
||
| - Deterministic C major digest | ||
| - Duration-weighted C recall after file write | ||
| - 120 BPM Acc1 after file decode | ||
| - Checksum mismatch | ||
| - Malformed report rejection | ||
| - Silence must not pass as C major | ||
|
|
||
| ### Realistic threats | ||
|
|
||
| A tampered fixture or a missing digest could be scored as a pass and then | ||
| cited as release evidence. | ||
|
|
||
| ### Remaining risk | ||
|
|
||
| Tier 1 does not cover Demucs stems, private commercial recordings, or | ||
| CPU/GPU numeric parity. Those remain later #770 tiers. |
42 changes: 42 additions & 0 deletions
42
services/analysis-engine/src/bandscope_analysis/accuracy/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| """Real-audio accuracy acceptance helpers for BandScope rehearsal analysis. | ||
|
|
||
| Next action: run the Tier 1 fixture tests before claiming a harmony or tempo | ||
| result is accurate. These helpers score decoded PCM against known labels. They | ||
| do not replace Demucs stem separation or private-corpus benchmarks. | ||
| """ | ||
|
|
||
| from .evaluate import evaluate_c_major_pcm, evaluate_click_tempo_file | ||
| from .fixtures import ( | ||
| C_MAJOR_LABEL, | ||
| DEFAULT_CLICK_BPM, | ||
| DEFAULT_SAMPLE_RATE, | ||
| assert_fixture_checksum, | ||
| render_c_major_triad, | ||
| render_click_track, | ||
| write_pcm_wav, | ||
| ) | ||
| from .manifest import ( | ||
| AccuracyCaseReport, | ||
| build_case_report, | ||
| parse_case_report, | ||
| read_product_version, | ||
| ) | ||
| from .metrics import duration_weighted_chord_recall, tempo_acc1 | ||
|
|
||
| __all__ = [ | ||
| "AccuracyCaseReport", | ||
| "C_MAJOR_LABEL", | ||
| "DEFAULT_CLICK_BPM", | ||
| "DEFAULT_SAMPLE_RATE", | ||
| "assert_fixture_checksum", | ||
| "build_case_report", | ||
| "duration_weighted_chord_recall", | ||
| "evaluate_c_major_pcm", | ||
| "evaluate_click_tempo_file", | ||
| "parse_case_report", | ||
| "read_product_version", | ||
| "render_c_major_triad", | ||
| "render_click_track", | ||
| "tempo_acc1", | ||
| "write_pcm_wav", | ||
| ] |
79 changes: 79 additions & 0 deletions
79
services/analysis-engine/src/bandscope_analysis/accuracy/evaluate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """Score decoded PCM fixtures through production analysis helpers.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| from numpy.typing import NDArray | ||
|
|
||
| from bandscope_analysis.accuracy.fixtures import ( | ||
| C_MAJOR_LABEL, | ||
| DEFAULT_CLICK_BPM, | ||
| DEFAULT_SAMPLE_RATE, | ||
| assert_fixture_checksum, | ||
| ) | ||
| from bandscope_analysis.accuracy.manifest import AccuracyCaseReport, build_case_report | ||
| from bandscope_analysis.accuracy.metrics import duration_weighted_chord_recall, tempo_acc1 | ||
| from bandscope_analysis.chords.chord_recognizer import ChordRecognizer | ||
| from bandscope_analysis.temporal.analyzer import TemporalAnalyzer | ||
|
|
||
| C_MAJOR_RECALL_FLOOR = 0.70 | ||
|
|
||
|
|
||
| def evaluate_c_major_pcm( | ||
| audio: NDArray[np.floating], | ||
| sample_rate: int = DEFAULT_SAMPLE_RATE, | ||
| audio_sha256: str = "0" * 64, | ||
| ) -> AccuracyCaseReport: | ||
| """Score a C major triad through the production chord recognizer. | ||
|
|
||
| Args: | ||
| audio: Decoded mono PCM. Do not pass a chroma matrix. | ||
| sample_rate: Sample rate of ``audio``. | ||
| audio_sha256: Digest of the on-disk fixture that produced ``audio``. | ||
|
|
||
| Returns: | ||
| A case report whose metric is duration-weighted recall of ``C``. | ||
| """ | ||
| recognizer = ChordRecognizer() | ||
| tracked = recognizer.recognize(np.asarray(audio, dtype=np.float32), sr=sample_rate) | ||
| segments = [(item["start_time"], item["end_time"], item["chord"]) for item in tracked] | ||
| duration = float(len(audio) / sample_rate) | ||
| recall = duration_weighted_chord_recall(segments, C_MAJOR_LABEL, 0.0, duration) | ||
| return build_case_report( | ||
| case_id="c-major-triad", | ||
| audio_sha256=audio_sha256, | ||
| metric_name="duration_weighted_chord_recall", | ||
| metric_value=recall, | ||
| passed=recall >= C_MAJOR_RECALL_FLOOR, | ||
| true_label=C_MAJOR_LABEL, | ||
| ) | ||
|
|
||
|
|
||
| def evaluate_click_tempo_file( | ||
| audio_path: Path, | ||
| expected_sha256: str, | ||
| true_bpm: float = DEFAULT_CLICK_BPM, | ||
| ) -> AccuracyCaseReport: | ||
| """Decode a click-track WAV with TemporalAnalyzer and score Acc1. | ||
|
|
||
| Args: | ||
| audio_path: On-disk WAV written by ``write_pcm_wav``. | ||
| expected_sha256: Registered digest. Mismatch fails closed. | ||
| true_bpm: Known click tempo. | ||
|
|
||
| Returns: | ||
| A case report whose metric is 1.0 on Acc1 pass and 0.0 on Acc1 fail. | ||
| """ | ||
| assert_fixture_checksum(audio_path, expected_sha256) | ||
| features = TemporalAnalyzer().analyze(audio_path) | ||
| passed = tempo_acc1(features["bpm"], true_bpm) | ||
| return build_case_report( | ||
| case_id="click-120-bpm", | ||
| audio_sha256=expected_sha256, | ||
| metric_name="tempo_acc1", | ||
| metric_value=1.0 if passed else 0.0, | ||
| passed=passed, | ||
| true_label=f"{true_bpm:g} bpm", | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This helper scores the in-memory array it is given. The default digest is 64 zeros and is never checked against bytes on disk. The headline acceptance case writes a WAV, then still calls this with the pre-write buffer, so a corrupt or empty file would still pass.
Add a file-path entry that checksums, decodes (
sf.readorlibrosa.load), then scores. Keep this PCM helper for the silence unit case only.