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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
feat: add audible SongBook drum previews (#3222)
[._.]/ Adam Eivy7d9cc5963489
diff --git a/client/src/components/songbook/DrumPreview.jsx b/client/src/components/songbook/DrumPreview.jsxnew file mode 100644index 0000000000..fa69c103ae--- /dev/null+++ b/client/src/components/songbook/DrumPreview.jsx@@ -0,0 +1,89 @@+import { useCallback, useEffect, useMemo, useState } from 'react';+import useDrumPlayer from '../../hooks/useDrumPlayer.js';+import { chartHasMusic, parseDrumChart } from '../../lib/drumNotation.js';+import DrumSheetView from './DrumSheetView.jsx';+import DrumTransportBar from './DrumTransportBar.jsx';++/**+ * Audible drum-chart preview shared by the SongBook editor and importer.+ *+ * Playback uses a snapshot captured when Play is pressed. The live source can+ * therefore keep changing without rebuilding the Web Audio schedule mid-run.+ * While that happens the sheet stays on the sounding snapshot too, keeping its+ * playhead aligned with what the user hears.+ */+export default function DrumPreview({+ text,+ songId,+ fontSizeRem,+ sheetClassName = '',+}) {+ const [snapshot, setSnapshot] = useState(text);+ const [playAfterReload, setPlayAfterReload] = useState(false);+ const player = useDrumPlayer(snapshot, { songId });+ const chartChanged = text !== snapshot;+ const liveHasMusic = useMemo(() => chartHasMusic(parseDrumChart(text)), [text]);++ const toggle = useCallback(() => {+ if (player.playing) {+ player.toggle();+ return;+ }+ if (text !== snapshot) {+ setSnapshot(text);+ setPlayAfterReload(true);+ return;+ }+ player.toggle();+ }, [player.playing, player.toggle, snapshot, text]);++ // A snapshot update rebuilds the idle player. Start only after that render so+ // toggle() cannot accidentally launch the stale chart from the click render.+ useEffect(() => {+ if (!playAfterReload || snapshot !== text) return;+ setPlayAfterReload(false);+ player.toggle();+ }, [playAfterReload, player.toggle, snapshot, text]);++ const displayedText = player.playing ? snapshot : text;++ return (+ <div className="min-w-0">+ <DrumTransportBar+ playing={player.playing}+ onToggle={toggle}+ hasMusic={chartChanged ? liveHasMusic : player.hasMusic}+ bpm={player.bpm}+ onBpmChange={player.setBpm}+ onPercent={player.setBpmPercent}+ writtenTempo={player.writtenTempo}+ countInBars={player.countInBars}+ onCountInChange={player.setCountInBars}+ loopEnabled={player.loopEnabled}+ onLoopToggle={player.setLoopEnabled}+ loopFrom={player.loopFrom}+ loopTo={player.loopTo}+ onLoopRangeChange={player.setLoopRange}+ barCount={player.barCount}+ clickEnabled={player.clickEnabled}+ onClickToggle={player.setClickEnabled}+ beatsPerBar={player.beatsPerBar}+ pulse={player.pulse}+ currentBar={player.currentBar}+ />+ {chartChanged && (+ <p className="px-3 py-1.5 text-xs text-port-warning bg-port-warning/10" role="status">+ Chart changed — press Play to reload.+ </p>+ )}+ <div className={sheetClassName}>+ <DrumSheetView+ text={displayedText}+ fontSizeRem={fontSizeRem}+ getPlayhead={player.getPlayhead}+ playing={player.playing}+ />+ </div>+ </div>+ );+}diff --git a/client/src/components/songbook/DrumPreview.test.jsx b/client/src/components/songbook/DrumPreview.test.jsxnew file mode 100644index 0000000000..7a682f83c6--- /dev/null+++ b/client/src/components/songbook/DrumPreview.test.jsx@@ -0,0 +1,75 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';+import { fireEvent, render, screen, waitFor } from '@testing-library/react';++const mockPlayer = vi.hoisted(() => ({+ playing: false,+ toggle: vi.fn(),+ stop: vi.fn(),+ hasMusic: true,+ bpm: 96,+ setBpm: vi.fn(),+ setBpmPercent: vi.fn(),+ writtenTempo: 96,+ countInBars: 1,+ setCountInBars: vi.fn(),+ loopEnabled: false,+ setLoopEnabled: vi.fn(),+ loopFrom: 1,+ loopTo: 1,+ setLoopRange: vi.fn(),+ barCount: 1,+ clickEnabled: true,+ setClickEnabled: vi.fn(),+ beatsPerBar: 4,+ pulse: null,+ currentBar: 1,+ getPlayhead: vi.fn(),+}));++const useDrumPlayer = vi.hoisted(() => vi.fn(() => mockPlayer));+vi.mock('../../hooks/useDrumPlayer.js', () => ({ default: useDrumPlayer }));+vi.mock('./DrumTransportBar.jsx', () => ({+ default: ({ onToggle, playing, hasMusic }) => (+ <button type="button" onClick={onToggle} disabled={!hasMusic}>+ {playing ? 'Stop preview' : 'Play preview'}+ </button>+ ),+}));+vi.mock('./DrumSheetView.jsx', () => ({+ default: ({ text, playing }) => <div data-testid="drum-sheet" data-playing={playing}>{text}</div>,+}));++import DrumPreview from './DrumPreview.jsx';++const CHART_A = 'tempo: 96\nHH: x---\nK: o---';+const CHART_B = 'tempo: 96\nHH: xxxx\nK: o-o-';++describe('DrumPreview', () => {+ beforeEach(() => {+ mockPlayer.playing = false;+ mockPlayer.toggle.mockReset();+ mockPlayer.stop.mockReset();+ useDrumPlayer.mockClear();+ });++ it('freezes the sounding chart and sheet while live text changes', () => {+ const { rerender } = render(<DrumPreview text={CHART_A} />);+ mockPlayer.playing = true;+ rerender(<DrumPreview text={CHART_B} />);++ expect(useDrumPlayer.mock.calls.at(-1)[0]).toBe(CHART_A);+ expect(screen.getByTestId('drum-sheet').textContent).toBe(CHART_A);+ expect(screen.getByText('Chart changed — press Play to reload.')).toBeTruthy();+ });++ it('rebuilds from the latest snapshot before starting after a change', async () => {+ const { rerender } = render(<DrumPreview text={CHART_A} />);+ rerender(<DrumPreview text={CHART_B} />);+ fireEvent.click(screen.getByRole('button', { name: 'Play preview' }));++ await waitFor(() => expect(useDrumPlayer.mock.calls.at(-1)[0]).toBe(CHART_B));+ expect(mockPlayer.toggle).toHaveBeenCalledTimes(1);+ expect(screen.queryByText('Chart changed — press Play to reload.')).toBeNull();+ });++});diff --git a/client/src/pages/SongBookImport.jsx b/client/src/pages/SongBookImport.jsxindex c3f191a740..c68895f3ce 100644--- a/client/src/pages/SongBookImport.jsx+++ b/client/src/pages/SongBookImport.jsx@@ -22,7 +22,7 @@ import toast from '../components/ui/Toast';import PageHeader from '../components/PageHeader';import TabPills from '../components/ui/TabPills';import TabSheetView from '../components/songbook/TabSheetView';-import DrumSheetView from '../components/songbook/DrumSheetView';+import DrumPreview from '../components/songbook/DrumPreview';import {SONG_STAGES, INSTRUMENTS, DRUM_FORMAT, DRUM_INSTRUMENT,inputClass, labelClass, btnClass,@@ -279,9 +279,12 @@ export default function SongBookImport() {{contentText && (<div className="mb-4"><div className="text-xs text-gray-400 mb-1">Preview</div>- <div className="bg-port-card border border-port-border rounded-lg p-3 max-h-[50vh] overflow-y-auto overflow-x-auto">+ <div className={`bg-port-card border border-port-border rounded-lg max-h-[50vh] overflow-y-auto overflow-x-auto ${previewIsDrum ? '' : 'p-3'}`}>{previewIsDrum ? (- <DrumSheetView text={contentText} />+ <DrumPreview+ text={contentText}+ sheetClassName="p-3"+ />) : (<TabSheetViewtext={contentText}diff --git a/client/src/pages/SongBookImport.test.jsx b/client/src/pages/SongBookImport.test.jsxindex 03b5656723..1fdd60635f 100644--- a/client/src/pages/SongBookImport.test.jsx+++ b/client/src/pages/SongBookImport.test.jsx@@ -119,6 +119,7 @@ describe('SongBookImport', () => {expect(await screen.findByText('drum')).toBeTruthy();expect(screen.getByLabelText(/^Drum chart —/)).toBeTruthy();expect(screen.getAllByText('Hi-Hat').length).toBeGreaterThan(0);+ expect(screen.getByLabelText('Play along')).toBeTruthy();});it('saves the drum format with the chart text', async () => {diff --git a/client/src/pages/SongBookViewer.jsx b/client/src/pages/SongBookViewer.jsxindex 4ffb2e65fc..45ede44592 100644--- a/client/src/pages/SongBookViewer.jsx+++ b/client/src/pages/SongBookViewer.jsx@@ -43,6 +43,7 @@ import AutoSizeTextarea from '../components/ui/AutoSizeTextarea';import TabPills from '../components/ui/TabPills';import TabSheetView from '../components/songbook/TabSheetView';import DrumSheetView from '../components/songbook/DrumSheetView';+import DrumPreview from '../components/songbook/DrumPreview';import DrumTransportBar from '../components/songbook/DrumTransportBar';import {SONG_STAGES, SONG_STAGE_COLORS, INSTRUMENTS, SONG_FORMATS, DRUM_FORMAT,@@ -239,7 +240,8 @@ export default function SongBookViewer() {// a non-drum song, so it stands up no player and touches no audio.const drum = useDrumPlayer(isDrum ? contentText : '', { songId: id });- // The wake lock holds while EITHER hands-free surface is running.+ // The wake lock holds while either play-mode hands-free surface is running.+ // Edit-preview audio owns its lifecycle inside DrumPreview.useWakeLock(playing || drum.playing);// Leaving play mode (Edit) or unmounting must not leave the kit sounding — the@@ -539,9 +541,14 @@ export default function SongBookViewer() {</div><div><div className="text-xs text-gray-400 mb-1">Preview</div>- <div className="bg-port-card border border-port-border rounded-lg p-3 overflow-x-auto">+ <div className={`bg-port-card border border-port-border rounded-lg overflow-hidden ${draftIsDrum ? '' : 'p-3 overflow-x-auto'}`}>{draftIsDrum ? (- <DrumSheetView text={draft.text} fontSizeRem={fontSize} />+ <DrumPreview+ text={draft.text}+ songId={id}+ fontSizeRem={fontSize}+ sheetClassName="p-3"+ />) : (<TabSheetViewtext={draft.text}diff --git a/client/src/pages/SongBookViewer.test.jsx b/client/src/pages/SongBookViewer.test.jsxindex 3f31bd4225..f6ff4f67f4 100644--- a/client/src/pages/SongBookViewer.test.jsx+++ b/client/src/pages/SongBookViewer.test.jsx@@ -315,6 +315,17 @@ K: o - - - - - o -`;expect(await screen.findByLabelText('Title')).toBeTruthy();expect(screen.getByLabelText('Format').value).toBe('drum');expect(screen.getByLabelText(/^Drum chart —/)).toBeTruthy();+ expect(screen.getByLabelText('Play along')).toBeTruthy();+ });++ it('keeps the edit preview on its sounding snapshot while the draft changes', async () => {+ api.getSong.mockResolvedValue(drumSong());+ renderPage('/songbook/abc?mode=edit');+ const editor = await screen.findByLabelText('Content');+ fireEvent.change(editor, { target: { value: `${DRUM_CHART}\nC: x - x -` } });++ expect(screen.getByText('Chart changed — press Play to reload.')).toBeTruthy();+ expect(screen.getByLabelText('Play along')).toBeTruthy();});it('keeps an unknown stored instrument/format selectable and preserves it on save', async () => {