"""Minimal usage example for hv-note. If you have ESC-50 or FSDD locally, edit the paths below. Otherwise this file synthesizes a small WAV and scores it. """ import math import os import wave from array import array from hv_note import HVNote OUTDIR = "./hv_note_example" WAV_PATH = os.path.join(OUTDIR, "example.wav") def _synth(path: str, sr: int = 16000) -> None: os.makedirs(os.path.dirname(path), exist_ok=True) sig = [] for f, dur_ms in [(440, 150), (660, 150), (880, 250)]: n = int(sr * dur_ms / 1000) for i in range(n): t = i / sr env = math.sin(math.pi * i / n) ** 0.5 sig.append(0.5 * env * math.sin(2 * math.pi * f * t)) sig.extend([0.0] * int(sr * 0.1)) ints = array( "h", (max(-32768, min(32767, int(x * 32767))) for x in sig), ) with wave.open(path, "wb") as w: w.setnchannels(1) w.setsampwidth(2) w.setframerate(sr) w.writeframes(ints.tobytes()) def main() -> None: _synth(WAV_PATH) model = HVNote() report = model.score_file(WAV_PATH) print("=" * 60) print("hv-note") print("=" * 60) print(model.render_file(report)) print() print("=" * 60) print("Numeric summary") print("=" * 60) print(f" file : {report.path}") print(f" duration : {report.duration_s:.3f} s") print(f" sample rate : {report.sample_rate} Hz") print(f" events : {len(report.events)}") print(f" unique sym : {len(report.histogram)}") print(f" entropy : {report.entropy_bits:.4f} bits") print(f" score : {report.score}") print(f" fingerprint : {model.fingerprint(WAV_PATH, k=8)}") print() if __name__ == "__main__": main()