From 1d19e014a3d129a2fb5f8ebea2610a785d78648e Mon Sep 17 00:00:00 2001 From: tgiachi Date: Fri, 13 Nov 2026 21:32:10 +0100 Subject: [PATCH] feat(web): choose how the player draws the sound: bars, oscilloscope, Milkdrop Settings has one choice, kept on the device, in place of the spectrogram switch: Spectrogram, Bars, Oscilloscope, Milkdrop or None. Whoever had turned the spectrogram off stays on None. - Bars: the classic spectrum, with a cap on each bar that waits and falls. - Oscilloscope: the wave of the sound as it plays. - Milkdrop: butterchurn and its presets fill the background of the full-screen player behind a veil. They are loaded only when chosen, change by themselves every 25 seconds or with a button, and give way to the spectrogram where WebGL is missing or the system asks for less motion. - The bar of the player keeps a small picture; for Milkdrop it is the spectrogram. - The analyser also hands over the wave and the audio node. --- README.md | 2 +- web/package-lock.json | 59 +++++++ web/package.json | 2 + web/src/butterchurn.d.ts | 19 +++ web/src/components/Bars.test.tsx | 56 +++++++ web/src/components/Bars.tsx | 81 +++++++++ web/src/components/Milkdrop.test.tsx | 132 +++++++++++++++ web/src/components/Milkdrop.tsx | 194 ++++++++++++++++++++++ web/src/components/Scope.test.tsx | 66 ++++++++ web/src/components/Scope.tsx | 75 +++++++++ web/src/components/Spectrogram.test.tsx | 8 - web/src/components/Spectrogram.tsx | 7 +- web/src/components/Visualizer.test.tsx | 40 +++++ web/src/components/Visualizer.tsx | 31 ++++ web/src/components/canvas.ts | 30 ++++ web/src/components/layout/NowPlaying.tsx | 24 ++- web/src/components/layout/Player.test.tsx | 55 ++++++ web/src/components/layout/PlayerBar.tsx | 4 +- web/src/components/milkdropLib.test.ts | 27 +++ web/src/components/milkdropLib.ts | 14 ++ web/src/i18n/locales/en.json | 13 +- web/src/i18n/locales/it.json | 13 +- web/src/pages/SettingsPage.test.tsx | 26 +-- web/src/pages/SettingsPage.tsx | 19 +-- web/src/player/analyser.test.ts | 59 +++++++ web/src/player/analyser.ts | 17 +- web/src/player/bars.test.ts | 56 +++++++ web/src/player/bars.ts | 42 +++++ web/src/player/milkdrop.ts | 12 ++ web/src/player/prefs.test.ts | 48 ++++-- web/src/player/prefs.ts | 40 +++-- web/src/player/scope.test.ts | 21 +++ web/src/player/scope.ts | 12 ++ 33 files changed, 1233 insertions(+), 71 deletions(-) create mode 100644 web/src/butterchurn.d.ts create mode 100644 web/src/components/Bars.test.tsx create mode 100644 web/src/components/Bars.tsx create mode 100644 web/src/components/Milkdrop.test.tsx create mode 100644 web/src/components/Milkdrop.tsx create mode 100644 web/src/components/Scope.test.tsx create mode 100644 web/src/components/Scope.tsx create mode 100644 web/src/components/Visualizer.test.tsx create mode 100644 web/src/components/Visualizer.tsx create mode 100644 web/src/components/canvas.ts create mode 100644 web/src/components/milkdropLib.test.ts create mode 100644 web/src/components/milkdropLib.ts create mode 100644 web/src/player/analyser.test.ts create mode 100644 web/src/player/bars.test.ts create mode 100644 web/src/player/bars.ts create mode 100644 web/src/player/milkdrop.ts create mode 100644 web/src/player/scope.test.ts create mode 100644 web/src/player/scope.ts diff --git a/README.md b/README.md index 084da88..1e31d6b 100644 --- a/README.md +++ b/README.md @@ -343,7 +343,7 @@ Back up the database and `sonda.toml`. Your music stays wherever you keep it. ## Web app -A web client lives in `web/` (React, Vite and shadcn/ui). It talks only to the Sonda server: no CDN, no external fonts, no analytics. With the app built into the binary (`make build`, or the Docker image) `sondad` serves it: open the address of the server, for example `http://127.0.0.1:8080`, and sign in. The page carries a strict Content-Security-Policy (only resources of the server itself, no inline scripts), cannot be framed and sends no referrer, and a link inside the app (`/admin/users`) works when you reload it. The player draws a live spectrogram of what is playing, in the browser, from the sound itself (Web Audio): the last seconds, low notes at the bottom, in the colour of this server or, for a friend's track, in the colour of the friend. It listens to the sound and changes nothing of it, and it is drawn only while something plays. +A web client lives in `web/` (React, Vite and shadcn/ui). It talks only to the Sonda server: no CDN, no external fonts, no analytics. With the app built into the binary (`make build`, or the Docker image) `sondad` serves it: open the address of the server, for example `http://127.0.0.1:8080`, and sign in. The page carries a strict Content-Security-Policy (only resources of the server itself, no inline scripts), cannot be framed and sends no referrer, and a link inside the app (`/admin/users`) works when you reload it. The player draws a live picture of what is playing, in the browser, from the sound itself (Web Audio). Settings has a choice for it, kept on the device: a spectrogram (the last seconds, low notes at the bottom, the default), the classic bars with falling caps, an oscilloscope, Milkdrop, or nothing. The bars, the oscilloscope and the spectrogram take the colour of this server or, for a friend's track, the colour of the friend. Milkdrop fills the background of the full-screen player with the pictures Winamp made famous, drawn on the device with [butterchurn](https://github.com/jberg/butterchurn) (MIT) and its presets, which are part of the app: nothing is fetched from outside. It is loaded only when chosen, and anyone who asked the system for less motion gets the spectrogram instead, because the pictures flash. The player listens to the sound and changes nothing of it, and draws only while something plays. To work on the interface itself, run the Vite dev server next to a running `sondad` (set `SONDA_URL` to where it listens: the default is `http://127.0.0.1:8080`): diff --git a/web/package-lock.json b/web/package-lock.json index 5d9eb13..91fec3b 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -11,6 +11,8 @@ "@base-ui/react": "^1.9.0", "@fontsource/figtree": "^5.3.0", "@tanstack/react-query": "^5.107.1", + "butterchurn": "^2.6.7", + "butterchurn-presets": "^2.4.7", "class-variance-authority": "^0.7.1", "cn": "^0.5.2", "i18next": "^27.1.1", @@ -2742,6 +2744,16 @@ "node": ">=10.12.0" } }, + "node_modules/babel-runtime": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz", + "integrity": "sha512-ITKNuq2wKlW1fJg9sSW52eepoYgZBggvOAHC0u/CYu/qxQ9EVzThCgR69BnSXLHjy2f7SY5zaQ4yt7H9ZVxY2g==", + "license": "MIT", + "dependencies": { + "core-js": "^2.4.0", + "regenerator-runtime": "^0.11.0" + } + }, "node_modules/balanced-match": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.4.tgz", @@ -2862,6 +2874,27 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/butterchurn": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/butterchurn/-/butterchurn-2.6.7.tgz", + "integrity": "sha512-BJiRA8L0L2+84uoG2SSfkp0kclBuN+vQKf217pK7pMlwEO2ZEg3MtO2/o+l8Qpr8Nbejg8tmL1ZHD1jmhiaaqg==", + "license": "MIT", + "dependencies": { + "@babel/runtime": "^7.0.0", + "ecma-proposal-math-extensions": "0.0.2" + } + }, + "node_modules/butterchurn-presets": { + "version": "2.4.7", + "resolved": "https://registry.npmjs.org/butterchurn-presets/-/butterchurn-presets-2.4.7.tgz", + "integrity": "sha512-4MdM8ripz/VfH1BCldrIKdAc/1ryJFBDvqlyow6Ivo1frwj0H3duzvSMFC7/wIjAjxb1QpwVHVqGqS9uAFKhpg==", + "license": "MIT", + "dependencies": { + "babel-runtime": "^6.26.0", + "ecma-proposal-math-extensions": "0.0.2", + "lodash": "^4.17.4" + } + }, "node_modules/caniuse-lite": { "version": "1.0.30001826", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001826.tgz", @@ -3034,6 +3067,14 @@ "url": "https://opencollective.com/express" } }, + "node_modules/core-js": { + "version": "2.6.12", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.12.tgz", + "integrity": "sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==", + "deprecated": "core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.", + "hasInstallScript": true, + "license": "MIT" + }, "node_modules/cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", @@ -3263,6 +3304,12 @@ "url": "https://dotenvx.com" } }, + "node_modules/ecma-proposal-math-extensions": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/ecma-proposal-math-extensions/-/ecma-proposal-math-extensions-0.0.2.tgz", + "integrity": "sha512-80BnDp2Fn7RxXlEr5HHZblniY4aQ97MOAicdWWpSo0vkQiISSE9wLR4SqxKsu4gCtXFBIPPzy8JMhay4NWRg/Q==", + "license": "MIT" + }, "node_modules/electron-to-chromium": { "version": "1.5.483", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.483.tgz", @@ -4303,6 +4350,12 @@ "node": ">=6" } }, + "node_modules/lodash": { + "version": "4.18.1", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.18.1.tgz", + "integrity": "sha512-dMInicTPVE8d1e5otfwmmjlxkZoUpiVLwyeTdUsi/Caj/gfzzblBcCE5sRHV/AsjuCmxWrte2TNGSYuCeCq+0Q==", + "license": "MIT" + }, "node_modules/log-symbols": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-6.0.0.tgz", @@ -5085,6 +5138,12 @@ "node": ">=8" } }, + "node_modules/regenerator-runtime": { + "version": "0.11.1", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", + "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==", + "license": "MIT" + }, "node_modules/require-from-string": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", diff --git a/web/package.json b/web/package.json index b8762b1..5ea6aed 100644 --- a/web/package.json +++ b/web/package.json @@ -15,6 +15,8 @@ "@base-ui/react": "^1.9.0", "@fontsource/figtree": "^5.3.0", "@tanstack/react-query": "^5.107.1", + "butterchurn": "^2.6.7", + "butterchurn-presets": "^2.4.7", "class-variance-authority": "^0.7.1", "cn": "^0.5.2", "i18next": "^27.1.1", diff --git a/web/src/butterchurn.d.ts b/web/src/butterchurn.d.ts new file mode 100644 index 0000000..14f2378 --- /dev/null +++ b/web/src/butterchurn.d.ts @@ -0,0 +1,19 @@ +// butterchurn ships no types. Only what Sonda uses is described. +declare module 'butterchurn/lib/butterchurn.min.js' { + export interface MilkdropVisualizer { + connectAudio(node: AudioNode): void + disconnectAudio(node: AudioNode): void + loadPreset(preset: object, blendSeconds: number): void + setRendererSize(width: number, height: number): void + render(): void + } + const butterchurn: { + createVisualizer(context: AudioContext, canvas: HTMLCanvasElement, options: { width: number; height: number; pixelRatio?: number }): MilkdropVisualizer + } + export default butterchurn +} + +declare module 'butterchurn-presets/lib/butterchurnPresets.min.js' { + const presets: { getPresets(): Record } + export default presets +} diff --git a/web/src/components/Bars.test.tsx b/web/src/components/Bars.test.tsx new file mode 100644 index 0000000..f7516ba --- /dev/null +++ b/web/src/components/Bars.test.tsx @@ -0,0 +1,56 @@ +import { act, render, screen } from '@testing-library/react' +import { beforeEach, expect, test, vi } from 'vitest' +import { initI18n } from '@/i18n' +import { usePlayer } from '@/player/store' +import Bars from './Bars' + +vi.mock('@/player/analyser', () => ({ + hasAnalyser: () => true, + readBins: () => new Uint8Array(256).fill(200), +})) + +const ctx = { clearRect: vi.fn(), fillRect: vi.fn(), fillStyle: '' } +let frames: FrameRequestCallback[] = [] + +beforeEach(async () => { + vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') + await initI18n() + ctx.clearRect.mockClear() + ctx.fillRect.mockClear() + frames = [] + vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => frames.push(cb)) + vi.stubGlobal('cancelAnimationFrame', () => {}) + vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue(ctx as unknown as CanvasRenderingContext2D) + Object.defineProperty(HTMLCanvasElement.prototype, 'clientWidth', { configurable: true, value: 200 }) + Object.defineProperty(HTMLCanvasElement.prototype, 'clientHeight', { configurable: true, value: 80 }) + usePlayer.setState({ playing: false }) +}) + +test('it is a picture with a name, and a canvas to draw on', () => { + render() + const picture = screen.getByRole('img', { name: 'Bars showing how loud each pitch is' }) + expect(picture.querySelector('canvas')).not.toBeNull() +}) + +test('it takes the colour of this server, or of a friend', () => { + const { rerender } = render() + expect(screen.getByRole('img').className).toContain('text-primary') + rerender() + expect(screen.getByRole('img').className).toContain('text-friend') +}) + +test('it draws only while something plays: a bar and a cap for every pitch', () => { + render() + expect(frames).toHaveLength(0) // paused: no loop, only the empty picture + const drawnEmpty = ctx.fillRect.mock.calls.length + act(() => usePlayer.setState({ playing: true })) + expect(frames.length).toBeGreaterThan(0) + act(() => frames.shift()?.(1000)) + // 4 bars and 4 caps more than the empty picture had. + expect(ctx.fillRect.mock.calls.length - drawnEmpty).toBeGreaterThanOrEqual(8) +}) + +test('where the browser has no Web Audio it breaks nothing', () => { + vi.stubGlobal('AudioContext', undefined) + expect(() => render()).not.toThrow() +}) diff --git a/web/src/components/Bars.tsx b/web/src/components/Bars.tsx new file mode 100644 index 0000000..c7838ac --- /dev/null +++ b/web/src/components/Bars.tsx @@ -0,0 +1,81 @@ +import { useEffect, useRef } from 'react' +import { useTranslation } from 'react-i18next' +import { hasAnalyser, readBins } from '@/player/analyser' +import { BarMeter } from '@/player/bars' +import { currentTrack, usePlayer } from '@/player/store' +import { rowsFromBins } from '@/player/spectrum' +import { cn } from '@/lib/utils' +import { prefersReducedMotion, surfaceOf } from './canvas' + +/** A new picture every this many milliseconds (every quarter of a second with reduced motion). */ +const FRAME_MS = 40 +const REDUCED_FRAME_MS = 250 + +function draw(canvas: HTMLCanvasElement, meter: BarMeter) { + const s = surfaceOf(canvas) + if (!s) return + const [r, g, b] = s.rgb + const count = meter.bars.length + const gap = 2 * s.ratio + const barW = (s.width - gap * (count - 1)) / count + const cap = Math.max(2, 2 * s.ratio) + for (let i = 0; i < count; i++) { + const x = i * (barW + gap) + const top = s.height - Math.max(0, meter.bars[i]) * (s.height - cap - gap) + s.ctx.fillStyle = `rgba(${r}, ${g}, ${b}, 0.55)` + s.ctx.fillRect(x, top, barW, s.height - top) + const capTop = s.height - meter.caps[i] * (s.height - cap - gap) - cap - gap + s.ctx.fillStyle = `rgb(${r}, ${g}, ${b})` + s.ctx.fillRect(x, Math.max(0, capTop), barW, cap) + } +} + +interface BarsProps { + /** How many bars, from low notes on the left to high ones on the right. */ + bars?: number + /** The track is a friend's: the colour is the friend's. */ + friend?: boolean + className?: string +} + +/** + * The classic spectrum: a bar for each stretch of pitches, jumping with the sound and falling back slowly, with a cap + * that waits at the highest point before it drops. Drawn only while something plays and the page is in view. + */ +export default function Bars({ bars = 32, friend = false, className }: BarsProps) { + const { t } = useTranslation() + const canvas = useRef(null) + const playing = usePlayer((s) => s.playing) + const now = usePlayer(currentTrack) + const key = now ? `${now.friend ?? 0}:${now.id}` : '' + + useEffect(() => { + const el = canvas.current + if (!el) return + const meter = new BarMeter(bars) + draw(el, meter) + if (!playing) return + const every = prefersReducedMotion() ? REDUCED_FRAME_MS : FRAME_MS + let last = 0 + let frame = 0 + const tick = (at: number) => { + frame = requestAnimationFrame(tick) + if (document.hidden || at - last < every || !hasAnalyser()) return + const seconds = last === 0 ? every / 1000 : Math.min((at - last) / 1000, 0.25) + last = at + const bins = readBins() + if (!bins) return + meter.update(rowsFromBins(bins, bars), seconds) + draw(el, meter) + } + frame = requestAnimationFrame(tick) + return () => cancelAnimationFrame(frame) + // A new track starts from empty bars. + }, [playing, key, bars]) + + return ( +
+
+ ) +} diff --git a/web/src/components/Milkdrop.test.tsx b/web/src/components/Milkdrop.test.tsx new file mode 100644 index 0000000..97dc6b3 --- /dev/null +++ b/web/src/components/Milkdrop.test.tsx @@ -0,0 +1,132 @@ +import { act, render, screen, waitFor } from '@testing-library/react' +import { beforeEach, expect, test, vi } from 'vitest' +import { initI18n } from '@/i18n' +import { useMilkdrop } from '@/player/milkdrop' +import { usePlayer } from '@/player/store' +import Milkdrop from './Milkdrop' + +const lib = vi.hoisted(() => { + const visualizer = { + connectAudio: vi.fn(), + disconnectAudio: vi.fn(), + loadPreset: vi.fn(), + setRendererSize: vi.fn(), + render: vi.fn(), + } + return { visualizer, createVisualizer: vi.fn(() => visualizer), loaded: vi.fn() } +}) + +vi.mock('butterchurn/lib/butterchurn.min.js', () => { + lib.loaded('butterchurn') + return { default: { createVisualizer: lib.createVisualizer } } +}) +vi.mock('butterchurn-presets/lib/butterchurnPresets.min.js', () => { + lib.loaded('presets') + return { default: { getPresets: () => ({ One: { n: 1 }, Two: { n: 2 }, Three: { n: 3 } }) } } +}) +vi.mock('@/player/analyser', () => ({ + hasAnalyser: () => true, + listenTo: () => ({ context: { sampleRate: 44100 }, node: { name: 'analyser' } }), +})) + +let frames: FrameRequestCallback[] = [] +const fallback =

spectrogram instead

+ +beforeEach(async () => { + vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') + await initI18n() + for (const fn of [lib.createVisualizer, lib.loaded, ...Object.values(lib.visualizer)]) fn.mockClear() + lib.createVisualizer.mockImplementation(() => lib.visualizer) + frames = [] + vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => frames.push(cb)) + vi.stubGlobal('cancelAnimationFrame', () => {}) + vi.stubGlobal('matchMedia', () => ({ matches: false, addEventListener: () => {}, removeEventListener: () => {} })) + usePlayer.setState({ playing: false }) + useMilkdrop.setState({ running: false, skips: 0 }) +}) + +async function ready() { + render() + act(() => usePlayer.setState({ playing: true })) + await waitFor(() => expect(lib.createVisualizer).toHaveBeenCalled()) +} + +test('it is a picture with a name, a canvas to draw on, and it asks nothing of the library until it is shown', async () => { + expect(lib.loaded).not.toHaveBeenCalled() + render() + const picture = await screen.findByRole('img', { name: 'Milkdrop picture of what is playing' }) + expect(picture.querySelector('canvas')).not.toBeNull() +}) + +test('once something plays it listens to the sound and starts on one of the presets', async () => { + await ready() + expect(lib.visualizer.connectAudio).toHaveBeenCalledWith({ name: 'analyser' }) + expect(lib.visualizer.loadPreset).toHaveBeenCalledTimes(1) + expect((lib.createVisualizer.mock.calls as unknown[][])[0][0]).toEqual({ sampleRate: 44100 }) +}) + +test('it draws a frame at every turn of the loop while it plays', async () => { + await ready() + act(() => frames.shift()?.(1000)) + expect(lib.visualizer.render).toHaveBeenCalled() +}) + +test('asking for the next picture moves on to another preset', async () => { + await ready() + act(() => useMilkdrop.getState().skip()) + expect(lib.visualizer.loadPreset).toHaveBeenCalledTimes(2) + const [first, second] = lib.visualizer.loadPreset.mock.calls + expect(second[0]).not.toBe(first[0]) +}) + +test('it says when it is running, and when it is not any more', async () => { + const { unmount } = render() + expect(useMilkdrop.getState().running).toBe(false) + act(() => usePlayer.setState({ playing: true })) + await waitFor(() => expect(useMilkdrop.getState().running).toBe(true)) + unmount() + expect(useMilkdrop.getState().running).toBe(false) +}) + +test('the canvas is made as big as its place before it is drawn on', async () => { + Object.defineProperty(HTMLCanvasElement.prototype, 'clientWidth', { configurable: true, value: 400 }) + Object.defineProperty(HTMLCanvasElement.prototype, 'clientHeight', { configurable: true, value: 200 }) + await ready() + const canvas = screen.getByRole('img').querySelector('canvas') + expect(canvas?.width).toBe(400) + expect(canvas?.height).toBe(200) +}) + +test('it moves on by itself after a while', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }) + await ready() + act(() => vi.advanceTimersByTime(31_000)) + expect(lib.visualizer.loadPreset.mock.calls.length).toBeGreaterThanOrEqual(2) +}) + +test('a browser that cannot draw it shows the fallback and breaks nothing', async () => { + lib.createVisualizer.mockImplementation(() => { + throw new Error('no WebGL') + }) + render() + act(() => usePlayer.setState({ playing: true })) + expect(await screen.findByText('spectrogram instead')).toBeInTheDocument() + expect(screen.queryByRole('img', { name: 'Milkdrop picture of what is playing' })).not.toBeInTheDocument() +}) + +test('whoever asks for less motion gets the fallback, and the library is never loaded', async () => { + vi.stubGlobal('matchMedia', () => ({ matches: true, addEventListener: () => {}, removeEventListener: () => {} })) + render() + expect(screen.getByText('spectrogram instead')).toBeInTheDocument() + act(() => usePlayer.setState({ playing: true })) + expect(lib.loaded).not.toHaveBeenCalled() + expect(lib.createVisualizer).not.toHaveBeenCalled() +}) + +test('when it goes away it lets go of the sound', async () => { + const { unmount } = render() + act(() => usePlayer.setState({ playing: true })) + await waitFor(() => expect(lib.createVisualizer).toHaveBeenCalled()) + unmount() + expect(lib.visualizer.disconnectAudio).toHaveBeenCalledWith({ name: 'analyser' }) +}) diff --git a/web/src/components/Milkdrop.tsx b/web/src/components/Milkdrop.tsx new file mode 100644 index 0000000..d5c4e8a --- /dev/null +++ b/web/src/components/Milkdrop.tsx @@ -0,0 +1,194 @@ +import { type ReactNode, useEffect, useRef, useState } from 'react' +import { useTranslation } from 'react-i18next' +import type { MilkdropVisualizer } from 'butterchurn/lib/butterchurn.min.js' +import { listenTo } from '@/player/analyser' +import { useMilkdrop } from '@/player/milkdrop' +import { usePlayer } from '@/player/store' +import { cn } from '@/lib/utils' +import { prefersReducedMotion } from './canvas' +import { holderOf } from './milkdropLib' + +/** The pictures change by themselves this often, and cross over for a few seconds. */ +const PRESET_MS = 25_000 +const CROSSFADE_S = 2.7 +/** At most this many pictures a second: the sound is smooth enough and a phone stays cooler. */ +const FRAME_MS = 33 + +interface Engine { + createVisualizer(context: AudioContext, canvas: HTMLCanvasElement, options: { width: number; height: number; pixelRatio?: number }): MilkdropVisualizer +} + +interface Library { + engine: Engine + presets: Record +} + +interface Running { + visualizer: MilkdropVisualizer + node: AudioNode + canvas: HTMLCanvasElement +} + +function shuffled(names: string[]): string[] { + const out = [...names] + for (let i = out.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)) + ;[out[i], out[j]] = [out[j], out[i]] + } + return out +} + +interface MilkdropProps { + /** What is shown where Milkdrop cannot run: no WebGL, or the person asked for less motion. */ + fallback: ReactNode + className?: string +} + +/** + * The full-screen pictures Winamp made famous, drawn on this device by butterchurn from the sound itself. The library + * is big, so it is loaded only when this is shown. The next picture comes by itself, or when the person asks (the store in `player/milkdrop`). Where the browser cannot draw + * it, and for anyone who asked the system for less motion (the pictures flash), the fallback is shown instead. + */ +export default function Milkdrop({ fallback, className }: MilkdropProps) { + const { t } = useTranslation() + const reduced = prefersReducedMotion() + const playing = usePlayer((s) => s.playing) + const canvas = useRef(null) + const [library, setLibrary] = useState(null) + const [failed, setFailed] = useState(false) + const running = useRef(null) + const order = useRef([]) + const at = useRef(0) + const setRunning = useMilkdrop((s) => s.setRunning) + const skips = useMilkdrop((s) => s.skips) + const seenSkips = useRef(skips) + + function show(blend: number) { + const lib = library + const names = order.current + if (!lib || !running.current || names.length === 0) return + // A preset that fails to load is skipped: the next one is tried. + for (let tries = 0; tries < names.length; tries++) { + const preset = lib.presets[names[at.current++ % names.length]] + try { + running.current.visualizer.loadPreset(preset, blend) + return + } catch { + // try the next + } + } + } + + useEffect(() => { + if (reduced) return + let gone = false + Promise.all([import('butterchurn/lib/butterchurn.min.js'), import('butterchurn-presets/lib/butterchurnPresets.min.js')]) + .then(([engine, pack]) => { + if (gone) return + setLibrary({ + engine: holderOf(engine, 'createVisualizer'), + presets: holderOf<{ getPresets(): Record }>(pack, 'getPresets').getPresets(), + }) + }) + .catch(() => { + if (!gone) setFailed(true) + }) + return () => { + gone = true + } + }, [reduced]) + + // While something plays: make the picture once, then draw it and change it from time to time. + useEffect(() => { + const el = canvas.current + if (!library || !el || !playing || failed) return + if (!running.current) { + const ear = listenTo() + if (!ear) return + try { + const ratio = Math.min(window.devicePixelRatio || 1, 1.5) + const width = Math.max(1, Math.round(el.clientWidth * ratio)) + const height = Math.max(1, Math.round(el.clientHeight * ratio)) + // The canvas is as sharp as its place: left alone it would stay at the browser's small default. + el.width = width + el.height = height + const visualizer = library.engine.createVisualizer(ear.context, el, { width, height, pixelRatio: ratio }) + visualizer.connectAudio(ear.node) + running.current = { visualizer, node: ear.node, canvas: el } + setRunning(true) + order.current = shuffled(Object.keys(library.presets)) + show(0) + } catch { + setRunning(false) + setFailed(true) + return + } + } + const run = running.current + const timer = setInterval(() => show(CROSSFADE_S), PRESET_MS) + let last = 0 + let frame = 0 + const tick = (now: number) => { + frame = requestAnimationFrame(tick) + if (document.hidden || now - last < FRAME_MS) return + last = now + run.visualizer.render() + } + frame = requestAnimationFrame(tick) + return () => { + clearInterval(timer) + cancelAnimationFrame(frame) + } + // `show` reads the library and the running picture, which this effect already depends on. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [library, playing, failed]) + + // The person asked for the next picture. + useEffect(() => { + if (skips === seenSkips.current) return + seenSkips.current = skips + show(1) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [skips]) + + // The picture follows the size of its place (the phone turns, the window changes). + useEffect(() => { + const el = canvas.current + if (!el || typeof ResizeObserver === 'undefined') return + const watch = new ResizeObserver(() => { + if (!running.current) return + const ratio = Math.min(window.devicePixelRatio || 1, 1.5) + const width = Math.max(1, Math.round(el.clientWidth * ratio)) + const height = Math.max(1, Math.round(el.clientHeight * ratio)) + el.width = width + el.height = height + running.current.visualizer.setRendererSize(width, height) + }) + watch.observe(el) + return () => watch.disconnect() + }, [library]) + + // When it goes away it lets go of the sound and of the graphics card. + useEffect( + () => () => { + const run = running.current + running.current = null + setRunning(false) + if (!run) return + try { + run.visualizer.disconnectAudio(run.node) + run.canvas.getContext('webgl2')?.getExtension('WEBGL_lose_context')?.loseContext() + } catch { + // nothing left to let go of + } + }, + [setRunning], + ) + + if (reduced || failed) return <>{fallback} + return ( +
+
+ ) +} diff --git a/web/src/components/Scope.test.tsx b/web/src/components/Scope.test.tsx new file mode 100644 index 0000000..f40d535 --- /dev/null +++ b/web/src/components/Scope.test.tsx @@ -0,0 +1,66 @@ +import { act, render, screen } from '@testing-library/react' +import { beforeEach, expect, test, vi } from 'vitest' +import { initI18n } from '@/i18n' +import { usePlayer } from '@/player/store' +import Scope from './Scope' + +vi.mock('@/player/analyser', () => ({ + hasAnalyser: () => true, + readWave: () => new Uint8Array(512).fill(200), +})) + +const ctx = { + clearRect: vi.fn(), + beginPath: vi.fn(), + moveTo: vi.fn(), + lineTo: vi.fn(), + stroke: vi.fn(), + strokeStyle: '', + lineWidth: 0, + lineJoin: '', + lineCap: '', +} +let frames: FrameRequestCallback[] = [] + +beforeEach(async () => { + vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') + await initI18n() + for (const fn of [ctx.clearRect, ctx.beginPath, ctx.moveTo, ctx.lineTo, ctx.stroke]) fn.mockClear() + frames = [] + vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => frames.push(cb)) + vi.stubGlobal('cancelAnimationFrame', () => {}) + vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue(ctx as unknown as CanvasRenderingContext2D) + Object.defineProperty(HTMLCanvasElement.prototype, 'clientWidth', { configurable: true, value: 200 }) + Object.defineProperty(HTMLCanvasElement.prototype, 'clientHeight', { configurable: true, value: 80 }) + usePlayer.setState({ playing: false }) +}) + +test('it is a picture with a name, and a canvas to draw on', () => { + render() + const picture = screen.getByRole('img', { name: 'Wave of what is playing' }) + expect(picture.querySelector('canvas')).not.toBeNull() +}) + +test('it takes the colour of this server, or of a friend', () => { + const { rerender } = render() + expect(screen.getByRole('img').className).toContain('text-primary') + rerender() + expect(screen.getByRole('img').className).toContain('text-friend') +}) + +test('paused it is a flat line; playing it draws the wave, one line through many points', () => { + render() + expect(frames).toHaveLength(0) + expect(ctx.stroke).toHaveBeenCalled() // the flat line + act(() => usePlayer.setState({ playing: true })) + ctx.stroke.mockClear() + ctx.lineTo.mockClear() + act(() => frames.shift()?.(1000)) + expect(ctx.lineTo.mock.calls.length).toBeGreaterThan(10) + expect(ctx.stroke).toHaveBeenCalledTimes(1) +}) + +test('where the browser has no Web Audio it breaks nothing', () => { + vi.stubGlobal('AudioContext', undefined) + expect(() => render()).not.toThrow() +}) diff --git a/web/src/components/Scope.tsx b/web/src/components/Scope.tsx new file mode 100644 index 0000000..093c0b6 --- /dev/null +++ b/web/src/components/Scope.tsx @@ -0,0 +1,75 @@ +import { useEffect, useRef } from 'react' +import { useTranslation } from 'react-i18next' +import { hasAnalyser, readWave } from '@/player/analyser' +import { currentTrack, usePlayer } from '@/player/store' +import { wavePoints } from '@/player/scope' +import { cn } from '@/lib/utils' +import { prefersReducedMotion, surfaceOf } from './canvas' + +const FRAME_MS = 33 +const REDUCED_FRAME_MS = 250 +/** Music is mostly quieter than the full range: the wave is drawn this much taller, and cut where it would leave the picture. */ +const GAIN = 2 +/** How many points the line passes through. */ +const POINTS = 96 + +function draw(canvas: HTMLCanvasElement, points: number[]) { + const s = surfaceOf(canvas) + if (!s) return + const [r, g, b] = s.rgb + const middle = s.height / 2 + const reach = (s.height / 2) * 0.9 + s.ctx.strokeStyle = `rgb(${r}, ${g}, ${b})` + s.ctx.lineWidth = Math.max(2, 2 * s.ratio) + s.ctx.lineJoin = 'round' + s.ctx.lineCap = 'round' + s.ctx.beginPath() + points.forEach((p, i) => { + const x = (i / (points.length - 1)) * s.width + const y = middle - Math.max(-1, Math.min(1, p * GAIN)) * reach + if (i === 0) s.ctx.moveTo(x, y) + else s.ctx.lineTo(x, y) + }) + s.ctx.stroke() +} + +interface ScopeProps { + /** The track is a friend's: the colour is the friend's. */ + friend?: boolean + className?: string +} + +/** The oscilloscope: the wave of the sound as it plays, in one line. A flat line when nothing plays. */ +export default function Scope({ friend = false, className }: ScopeProps) { + const { t } = useTranslation() + const canvas = useRef(null) + const playing = usePlayer((s) => s.playing) + const now = usePlayer(currentTrack) + const key = now ? `${now.friend ?? 0}:${now.id}` : '' + + useEffect(() => { + const el = canvas.current + if (!el) return + draw(el, new Array(POINTS).fill(0)) + if (!playing) return + const every = prefersReducedMotion() ? REDUCED_FRAME_MS : FRAME_MS + let last = 0 + let frame = 0 + const tick = (at: number) => { + frame = requestAnimationFrame(tick) + if (document.hidden || at - last < every || !hasAnalyser()) return + last = at + const wave = readWave() + if (!wave) return + draw(el, wavePoints(wave, POINTS)) + } + frame = requestAnimationFrame(tick) + return () => cancelAnimationFrame(frame) + }, [playing, key]) + + return ( +
+
+ ) +} diff --git a/web/src/components/Spectrogram.test.tsx b/web/src/components/Spectrogram.test.tsx index a908604..162248e 100644 --- a/web/src/components/Spectrogram.test.tsx +++ b/web/src/components/Spectrogram.test.tsx @@ -1,13 +1,11 @@ import { render, screen } from '@testing-library/react' import { beforeEach, expect, test, vi } from 'vitest' import { initI18n } from '@/i18n' -import { useSpectrogramPref } from '@/player/prefs' import Spectrogram from './Spectrogram' beforeEach(async () => { vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') await initI18n() - useSpectrogramPref.setState({ on: true }) }) test('it is a picture with a name, and a canvas where the browser can draw', () => { @@ -27,9 +25,3 @@ test('it takes the colour of this server, or of a friend', () => { rerender() expect(screen.getByRole('img').className).toContain('text-friend') }) - -test('turned off in Settings, it is not there at all', () => { - useSpectrogramPref.setState({ on: false }) - render() - expect(screen.queryByRole('img')).not.toBeInTheDocument() -}) diff --git a/web/src/components/Spectrogram.tsx b/web/src/components/Spectrogram.tsx index c9c78e2..fc25ed5 100644 --- a/web/src/components/Spectrogram.tsx +++ b/web/src/components/Spectrogram.tsx @@ -1,7 +1,6 @@ import { useEffect, useRef } from 'react' import { useTranslation } from 'react-i18next' import { hasAnalyser, readBins } from '@/player/analyser' -import { useSpectrogramPref } from '@/player/prefs' import { currentTrack, usePlayer } from '@/player/store' import { ColumnBuffer, rowsFromBins } from '@/player/spectrum' import { cn } from '@/lib/utils' @@ -56,14 +55,13 @@ interface SpectrogramProps { export default function Spectrogram({ columns = 48, rows = 12, friend = false, className }: SpectrogramProps) { const { t } = useTranslation() const canvas = useRef(null) - const wanted = useSpectrogramPref((s) => s.on) const playing = usePlayer((s) => s.playing) const now = usePlayer(currentTrack) const key = now ? `${now.friend ?? 0}:${now.id}` : '' useEffect(() => { const el = canvas.current - if (!el || !wanted) return + if (!el) return const buffer = new ColumnBuffer(columns, rows) draw(el, buffer.columns(), rows) if (!playing) return @@ -83,9 +81,8 @@ export default function Spectrogram({ columns = 48, rows = 12, friend = false, c frame = requestAnimationFrame(tick) return () => cancelAnimationFrame(frame) // A new track starts from an empty picture. - }, [wanted, playing, key, columns, rows]) + }, [playing, key, columns, rows]) - if (!wanted) return null return (