File size: 1,774 Bytes
c3cf9d9 | 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 | """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() |