mirror of
https://github.com/9x/sheetmusictrainer.git
synced 2026-11-03 22:03:54 +01:00
Overhaul pitch detection and note matching
- Pick detector per instrument range: ACF2PLUS (robust down to ~40 Hz) for normal instruments, YIN only for the whistle register. pitchfinder's YIN latches onto tau=2 (~20 kHz garbage) below ~100 Hz and for quiet input; the old fixed 4096 window masked this, the smaller window exposed it. - RMS-normalize analysis input for the YIN path only; ACF2PLUS keeps the raw signal so its internal unpitched gate rejects quiet decayed audio instead of amplifying noise into confident wrong pitches (tuner stuck on 'a note'). - Adaptive analysis window sized to the instrument's lowest note (~2 periods): half the latency, a quarter of the CPU for guitar/piano/voice; bass keeps the full window for E1. - Reject detections implausibly below the instrument floor (mains hum). - Clear stale pitch readings after 200 ms instead of keeping them forever. - Shared AudioEngine singleton for all playback (was: two AudioContexts); tracks audibility so the app stops hearing its own speaker (ear-training self-scoring) and gates matching plus display during playback. - MatchTracker: grace-period based note hold instead of reset-on-every-flicker (YIN flickers during attacks/decays; the old hard reset made the 50 ms threshold fire unreliably). - Display damping in the producer (EMA for cents/level, 3-frame note-name hysteresis); display components render directly without CSS transitions. - Regression tests: synthetic signals through the exact pipeline helpers (MatchTracker + pitchDetection test suites).
This commit is contained in:
@@ -1,61 +1,18 @@
|
||||
import { useRef, useEffect, useCallback } from 'react';
|
||||
import { midiToFrequency } from '../music/NoteUtils';
|
||||
import { useCallback } from 'react';
|
||||
import { audioEngine } from '../audio/AudioEngine';
|
||||
|
||||
interface AudioPlayerHandle {
|
||||
playNote: (midi: number, duration?: number, volume?: number) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Thin hook wrapper around the shared AudioEngine singleton.
|
||||
* All playback goes through one AudioContext and is tracked there, so pitch
|
||||
* detection can gate itself while the app's own speaker output is audible.
|
||||
*/
|
||||
export function useAudioPlayer(): AudioPlayerHandle {
|
||||
const audioContext = useRef<AudioContext | null>(null);
|
||||
|
||||
// Initialize AudioContext lazily or on user interaction if possible,
|
||||
// but here we just ensure it exists when we try to play.
|
||||
|
||||
const playNote = useCallback((midi: number, duration: number = 0.5, volume: number = 0.3) => {
|
||||
if (!audioContext.current) {
|
||||
audioContext.current = new (window.AudioContext || (window as any).webkitAudioContext)();
|
||||
}
|
||||
|
||||
const ctx = audioContext.current;
|
||||
if (ctx.state === 'suspended') {
|
||||
ctx.resume();
|
||||
}
|
||||
|
||||
const osc = ctx.createOscillator();
|
||||
const gainNode = ctx.createGain();
|
||||
|
||||
const freq = midiToFrequency(midi);
|
||||
|
||||
osc.frequency.value = freq;
|
||||
// Triangle wave is often nicer than sine for music training
|
||||
osc.type = 'triangle';
|
||||
|
||||
osc.connect(gainNode);
|
||||
gainNode.connect(ctx.destination);
|
||||
|
||||
const now = ctx.currentTime;
|
||||
|
||||
// Attack
|
||||
gainNode.gain.setValueAtTime(0, now);
|
||||
gainNode.gain.linearRampToValueAtTime(volume, now + 0.02);
|
||||
|
||||
// Sustain -> Release
|
||||
// Note: For a "pluck" or single hit, we just ramp down.
|
||||
gainNode.gain.exponentialRampToValueAtTime(0.001, now + duration);
|
||||
|
||||
osc.start(now);
|
||||
osc.stop(now + duration + 0.1); // Stop slightly after fade out
|
||||
|
||||
}, []);
|
||||
|
||||
// Cleanup
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (audioContext.current) {
|
||||
audioContext.current.close();
|
||||
audioContext.current = null;
|
||||
}
|
||||
};
|
||||
audioEngine.playNote(midi, duration, volume);
|
||||
}, []);
|
||||
|
||||
return { playNote };
|
||||
|
||||
Reference in New Issue
Block a user