Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: address review — mirror chord practice settings, kill the phantom pre-roll count-in (#4104)

[._.]/ Adam Eivy82911599e1e1
diff --git a/client/src/components/songbook/ChordPreview.jsx b/client/src/components/songbook/ChordPreview.jsx
index 4d64ade4d3..deabff2ee4 100644
--- a/client/src/components/songbook/ChordPreview.jsx
+++ b/client/src/components/songbook/ChordPreview.jsx
@@ -1,4 +1,4 @@
-import { useLayoutEffect, useState } from 'react';
+import { useCallback, useLayoutEffect, useState } from 'react';
import useChordPlayer from '../../hooks/useChordPlayer.js';
import useWakeLock from '../../hooks/useWakeLock.js';
import ChordTransportBar from './ChordTransportBar.jsx';
@@ -13,6 +13,12 @@ import TabSheetView from './TabSheetView.jsx';
* therefore keep changing without tearing down the schedule mid-run, and the
* sheet stays on the sounding snapshot too so the highlight keeps pointing at
* the chord you're actually hearing.
+ *
+ * `settingsMirror` is a SECOND `useChordPlayer` (the viewer's play-mode one)
+ * that should follow the practice settings changed here. Both transports read
+ * the per-song tempo/beats from storage at MOUNT, so without the mirror a tempo
+ * set in the editor wouldn't reach the already-mounted play-mode bar until a
+ * reload. Same contract as `<DrumPreview>`'s.
*/
export default function ChordPreview({
text,
@@ -22,6 +28,7 @@ export default function ChordPreview({
instrumentView,
showChordStrip = false,
sheetClassName = '',
+ settingsMirror,
}) {
const [snapshot, setSnapshot] = useState(text);
// `plain` is the sheet's explicit opt-out of ALL notation UI, chord tokens
@@ -38,6 +45,29 @@ export default function ChordPreview({
if (!player.playing && snapshot !== text) setSnapshot(text);
}, [player.playing, snapshot, text]);
+ const setBpm = useCallback((next) => {
+ player.setBpm(next);
+ settingsMirror?.setBpm(next);
+ }, [player.setBpm, settingsMirror]);
+ const setBpmPercent = useCallback((percent) => {
+ player.setBpmPercent(percent);
+ settingsMirror?.setBpmPercent(percent);
+ }, [player.setBpmPercent, settingsMirror]);
+ const setBeatsPerBar = useCallback((next) => {
+ player.setBeatsPerBar(next);
+ settingsMirror?.setBeatsPerBar(next);
+ }, [player.setBeatsPerBar, settingsMirror]);
+ const setCountInBars = useCallback((next) => {
+ player.setCountInBars(next);
+ settingsMirror?.setCountInBars(next);
+ }, [player.setCountInBars, settingsMirror]);
+ // The click persists globally, but the viewer's transport already read it at
+ // mount — mirror the change so the two don't disagree until a reload.
+ const setClickEnabled = useCallback((enabled) => {
+ player.setClickEnabled(enabled);
+ settingsMirror?.setClickEnabled(enabled);
+ }, [player.setClickEnabled, settingsMirror]);
+
const displayedText = player.playing ? snapshot : text;
return (
@@ -50,15 +80,15 @@ export default function ChordPreview({
onToggle={player.toggle}
hasChords={player.hasChords}
bpm={player.bpm}
- onBpmChange={player.setBpm}
- onPercent={player.setBpmPercent}
+ onBpmChange={setBpm}
+ onPercent={setBpmPercent}
writtenTempo={player.writtenTempo}
beatsPerBar={player.beatsPerBar}
- onBeatsPerBarChange={player.setBeatsPerBar}
+ onBeatsPerBarChange={setBeatsPerBar}
countInBars={player.countInBars}
- onCountInChange={player.setCountInBars}
+ onCountInChange={setCountInBars}
clickEnabled={player.clickEnabled}
- onClickToggle={player.setClickEnabled}
+ onClickToggle={setClickEnabled}
chordCount={player.chordCount}
pulse={player.pulse}
/>
diff --git a/client/src/hooks/useChordPlayer.js b/client/src/hooks/useChordPlayer.js
index 5e088e1e15..952d05a01e 100644
--- a/client/src/hooks/useChordPlayer.js
+++ b/client/src/hooks/useChordPlayer.js
@@ -77,9 +77,13 @@ export default function useChordPlayer(text, { songId } = {}) {
setBpmState(clampBpm(bpmKey ? safeReadStorage(bpmKey) : null) ?? DEFAULT_CHORD_TEMPO);
}, [bpmKey]);
useEffect(() => {
- const stored = Number(beatsKey ? safeReadStorage(beatsKey) : null);
+ // `null` (never set) must not reach Number(), which would turn it into a 0
+ // that then has to be rescued by the range check — the sentinel is tested
+ // for explicitly instead.
+ const raw = beatsKey ? safeReadStorage(beatsKey) : null;
+ const stored = raw == null ? null : Number(raw);
setBeatsPerBarState(
- Number.isFinite(stored) && stored >= CHORD_BEATS_MIN && stored <= CHORD_BEATS_MAX
+ stored != null && Number.isFinite(stored) && stored >= CHORD_BEATS_MIN && stored <= CHORD_BEATS_MAX
? Math.trunc(stored)
: DEFAULT_CHORD_BEATS_PER_BAR,
);
@@ -209,9 +213,14 @@ export default function useChordPlayer(text, { songId } = {}) {
if (beatsKey) safeWriteStorage(beatsKey, String(n));
}, [stopIfPlaying, beatsKey]);
+ // Unparseable input is REJECTED rather than falling through to 0 — 0 is the
+ // legitimate "no count-in" choice, so a `|| 0` fallback would make a garbled
+ // value indistinguishable from that choice (and silently stop playback for it).
const setCountInBars = useCallback((next) => {
+ const n = Math.trunc(Number(next));
+ if (!Number.isFinite(n)) return;
stopIfPlaying();
- setCountInBarsState(Math.max(0, Math.min(CHORD_COUNT_IN_MAX, Math.trunc(Number(next)) || 0)));
+ setCountInBarsState(Math.max(0, Math.min(CHORD_COUNT_IN_MAX, n)));
}, [stopIfPlaying]);
return {
diff --git a/client/src/hooks/useChordPlayer.test.jsx b/client/src/hooks/useChordPlayer.test.jsx
index 05dd5bb0fb..095ea0e960 100644
--- a/client/src/hooks/useChordPlayer.test.jsx
+++ b/client/src/hooks/useChordPlayer.test.jsx
@@ -66,6 +66,18 @@ describe('useChordPlayer', () => {
expect(result.current.bpm).toBe(DEFAULT_CHORD_TEMPO);
act(() => { result.current.setCountInBars(99); });
expect(result.current.countInBars).toBe(4);
+ // Unparseable input is rejected, NOT collapsed into the legitimate "none"
+ // choice — 0 is a real count-in value here.
+ act(() => { result.current.setCountInBars('nonsense'); });
+ expect(result.current.countInBars).toBe(4);
+ act(() => { result.current.setCountInBars(0); });
+ expect(result.current.countInBars).toBe(0);
+ });
+
+ it('takes the defaults when nothing is stored, rather than coercing null to a number', () => {
+ window.localStorage.setItem('songbook:chordBeats:song-4', '');
+ const { result } = renderHook(() => useChordPlayer(SHEET, { songId: 'song-4' }));
+ expect(result.current.beatsPerBar).toBe(DEFAULT_CHORD_BEATS_PER_BAR);
});
it('sets the tempo from a percent of the reference tempo', () => {
diff --git a/client/src/lib/chordPlayback.js b/client/src/lib/chordPlayback.js
index e69b9c7b3b..67d623c7f6 100644
--- a/client/src/lib/chordPlayback.js
+++ b/client/src/lib/chordPlayback.js
@@ -170,9 +170,13 @@ export const resolveChordPlayhead = (schedule, posSec) => {
if (!events.length || !(beatSec > 0)) return null;
const pos = Number.isFinite(posSec) ? posSec : 0;
- if (pos < countInSec) {
- // The transport's pre-roll lead is negative — clamp it to the first beat
- // rather than counting backwards past the start of the count-in.
+ // `countInSec > 0` is load-bearing, not redundant: the transport's pre-roll
+ // lead makes `pos` briefly NEGATIVE, so a bare `pos < countInSec` would report
+ // "counting in" for the first ~80ms of a run that has no count-in at all —
+ // yellow beat dots and a null highlight before the first chord.
+ if (countInSec > 0 && pos < countInSec) {
+ // Clamp that negative lead to the first beat rather than counting backwards
+ // past the start of the count-in.
const elapsedBeats = Math.floor(Math.max(0, pos) / beatSec);
return { countIn: true, beat: (elapsedBeats % Math.max(1, beatsPerBar)) + 1 };
}
diff --git a/client/src/lib/chordPlayback.test.js b/client/src/lib/chordPlayback.test.js
index a1bf2e5261..c625e23523 100644
--- a/client/src/lib/chordPlayback.test.js
+++ b/client/src/lib/chordPlayback.test.js
@@ -159,6 +159,14 @@ describe('resolveChordPlayhead', () => {
expect(resolveChordPlayhead(schedule, 999).index).toBe(3);
});
+ it('never reports a count-in for a schedule that has none, pre-roll included', () => {
+ // The transport's lead makes `pos` briefly negative. With no count-in bars
+ // that must still read as bar 1 beat 1 — not as a phantom count-in.
+ const noCountIn = buildChordSchedule(lines(SHEET), { bpm: 120 });
+ expect(resolveChordPlayhead(noCountIn, -0.08)).toEqual({ countIn: false, index: 0, beat: 1 });
+ expect(resolveChordPlayhead(noCountIn, 0)).toEqual({ countIn: false, index: 0, beat: 1 });
+ });
+
it('returns null when there is nothing to place', () => {
expect(resolveChordPlayhead(buildChordSchedule(lines('words only')), 1)).toBeNull();
expect(resolveChordPlayhead(null, 1)).toBeNull();
diff --git a/client/src/pages/SongBookViewer.jsx b/client/src/pages/SongBookViewer.jsx
index 90d2bbbda6..b8f66a0be8 100644
--- a/client/src/pages/SongBookViewer.jsx
+++ b/client/src/pages/SongBookViewer.jsx
@@ -812,6 +812,7 @@ export default function SongBookViewer() {
fontSizeRem={fontSize}
instrumentView={toVoicingInstrument(draft.instrument)}
sheetClassName="p-3 overflow-x-auto"
+ settingsMirror={chord}
/>
)}
</div>