Merge pull request 'The player can draw the sound as bars, an oscilloscope or Milkdrop' (#163) from feat/visualizers into main
This commit is contained in:
commit
7aa822f6bb
33 changed files with 1230 additions and 68 deletions
|
|
@ -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`):
|
||||
|
||||
|
|
|
|||
59
web/package-lock.json
generated
59
web/package-lock.json
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
19
web/src/butterchurn.d.ts
vendored
Normal file
19
web/src/butterchurn.d.ts
vendored
Normal file
|
|
@ -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<string, object> }
|
||||
export default presets
|
||||
}
|
||||
56
web/src/components/Bars.test.tsx
Normal file
56
web/src/components/Bars.test.tsx
Normal file
|
|
@ -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(<Bars className="h-10 w-40" />)
|
||||
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(<Bars />)
|
||||
expect(screen.getByRole('img').className).toContain('text-primary')
|
||||
rerender(<Bars friend />)
|
||||
expect(screen.getByRole('img').className).toContain('text-friend')
|
||||
})
|
||||
|
||||
test('it draws only while something plays: a bar and a cap for every pitch', () => {
|
||||
render(<Bars bars={4} />)
|
||||
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(<Bars className="h-10 w-40" />)).not.toThrow()
|
||||
})
|
||||
81
web/src/components/Bars.tsx
Normal file
81
web/src/components/Bars.tsx
Normal file
|
|
@ -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<HTMLCanvasElement>(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 (
|
||||
<div role="img" aria-label={t('player.bars')} className={cn(friend ? 'text-friend' : 'text-primary', className)}>
|
||||
<canvas ref={canvas} aria-hidden="true" className="size-full" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
132
web/src/components/Milkdrop.test.tsx
Normal file
132
web/src/components/Milkdrop.test.tsx
Normal file
|
|
@ -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 = <p>spectrogram instead</p>
|
||||
|
||||
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(<Milkdrop fallback={fallback} className="h-40 w-full" />)
|
||||
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(<Milkdrop fallback={fallback} />)
|
||||
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(<Milkdrop fallback={fallback} />)
|
||||
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(<Milkdrop fallback={fallback} />)
|
||||
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(<Milkdrop fallback={fallback} />)
|
||||
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(<Milkdrop fallback={fallback} />)
|
||||
act(() => usePlayer.setState({ playing: true }))
|
||||
await waitFor(() => expect(lib.createVisualizer).toHaveBeenCalled())
|
||||
unmount()
|
||||
expect(lib.visualizer.disconnectAudio).toHaveBeenCalledWith({ name: 'analyser' })
|
||||
})
|
||||
194
web/src/components/Milkdrop.tsx
Normal file
194
web/src/components/Milkdrop.tsx
Normal file
|
|
@ -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<string, object>
|
||||
}
|
||||
|
||||
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<HTMLCanvasElement>(null)
|
||||
const [library, setLibrary] = useState<Library | null>(null)
|
||||
const [failed, setFailed] = useState(false)
|
||||
const running = useRef<Running | null>(null)
|
||||
const order = useRef<string[]>([])
|
||||
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>(engine, 'createVisualizer'),
|
||||
presets: holderOf<{ getPresets(): Record<string, object> }>(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 (
|
||||
<div role="img" aria-label={t('player.milkdrop')} className={cn(className)}>
|
||||
<canvas ref={canvas} aria-hidden="true" className="size-full" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
66
web/src/components/Scope.test.tsx
Normal file
66
web/src/components/Scope.test.tsx
Normal file
|
|
@ -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(<Scope className="h-10 w-40" />)
|
||||
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(<Scope />)
|
||||
expect(screen.getByRole('img').className).toContain('text-primary')
|
||||
rerender(<Scope friend />)
|
||||
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(<Scope />)
|
||||
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(<Scope className="h-10 w-40" />)).not.toThrow()
|
||||
})
|
||||
75
web/src/components/Scope.tsx
Normal file
75
web/src/components/Scope.tsx
Normal file
|
|
@ -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<HTMLCanvasElement>(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<number>(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 (
|
||||
<div role="img" aria-label={t('player.scope')} className={cn(friend ? 'text-friend' : 'text-primary', className)}>
|
||||
<canvas ref={canvas} aria-hidden="true" className="size-full" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -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(<Spectrogram friend />)
|
||||
expect(screen.getByRole('img').className).toContain('text-friend')
|
||||
})
|
||||
|
||||
test('turned off in Settings, it is not there at all', () => {
|
||||
useSpectrogramPref.setState({ on: false })
|
||||
render(<Spectrogram className="h-10 w-40" />)
|
||||
expect(screen.queryByRole('img')).not.toBeInTheDocument()
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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<HTMLCanvasElement>(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 (
|
||||
<div role="img" aria-label={t('player.spectrogram')} className={cn(friend ? 'text-friend' : 'text-primary', className)}>
|
||||
<canvas ref={canvas} aria-hidden="true" className="size-full" />
|
||||
|
|
|
|||
40
web/src/components/Visualizer.test.tsx
Normal file
40
web/src/components/Visualizer.test.tsx
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { render, screen } from '@testing-library/react'
|
||||
import { beforeEach, expect, test, vi } from 'vitest'
|
||||
import { initI18n } from '@/i18n'
|
||||
import { useVisualizerPref } from '@/player/prefs'
|
||||
import Visualizer from './Visualizer'
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
||||
await initI18n()
|
||||
useVisualizerPref.setState({ mode: 'spectrogram' })
|
||||
})
|
||||
|
||||
test('it draws what this device chose', () => {
|
||||
const names = {
|
||||
spectrogram: 'Spectrogram of what is playing',
|
||||
bars: 'Bars showing how loud each pitch is',
|
||||
scope: 'Wave of what is playing',
|
||||
} as const
|
||||
for (const [mode, name] of Object.entries(names)) {
|
||||
useVisualizerPref.setState({ mode: mode as keyof typeof names })
|
||||
const { unmount } = render(<Visualizer size="bar" />)
|
||||
expect(screen.getByRole('img', { name })).toBeInTheDocument()
|
||||
unmount()
|
||||
}
|
||||
})
|
||||
|
||||
test('chosen None, it is not there at all', () => {
|
||||
useVisualizerPref.setState({ mode: 'off' })
|
||||
render(<Visualizer size="full" />)
|
||||
expect(screen.queryByRole('img')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('Milkdrop fills the whole full-screen player, so where the picture goes the spectrogram stays, in the bar and in the player', () => {
|
||||
useVisualizerPref.setState({ mode: 'milkdrop' })
|
||||
for (const size of ['bar', 'full'] as const) {
|
||||
const { unmount } = render(<Visualizer size={size} />)
|
||||
expect(screen.getByRole('img', { name: 'Spectrogram of what is playing' })).toBeInTheDocument()
|
||||
unmount()
|
||||
}
|
||||
})
|
||||
31
web/src/components/Visualizer.tsx
Normal file
31
web/src/components/Visualizer.tsx
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { useVisualizerPref } from '@/player/prefs'
|
||||
import Bars from './Bars'
|
||||
import Scope from './Scope'
|
||||
import Spectrogram from './Spectrogram'
|
||||
|
||||
interface VisualizerProps {
|
||||
/** `bar` is the small place in the bar of the player, `full` the big one in the full-screen player. */
|
||||
size: 'bar' | 'full'
|
||||
/** The track is a friend's: the colour is the friend's. */
|
||||
friend?: boolean
|
||||
className?: string
|
||||
}
|
||||
|
||||
/** The picture of the sound this device chose in Settings, or nothing when it chose None. */
|
||||
export default function Visualizer({ size, friend = false, className }: VisualizerProps) {
|
||||
const mode = useVisualizerPref((s) => s.mode)
|
||||
const big = size === 'full'
|
||||
const spectrogram = <Spectrogram columns={big ? 72 : 28} rows={big ? 16 : 6} friend={friend} className={className} />
|
||||
switch (mode) {
|
||||
case 'off':
|
||||
return null
|
||||
case 'bars':
|
||||
return <Bars bars={big ? 48 : 20} friend={friend} className={className} />
|
||||
case 'scope':
|
||||
return <Scope friend={friend} className={className} />
|
||||
// Milkdrop is drawn behind the whole full-screen player (see NowPlaying): in the place of the picture, as in the bar,
|
||||
// the spectrogram stays.
|
||||
default:
|
||||
return spectrogram
|
||||
}
|
||||
}
|
||||
30
web/src/components/canvas.ts
Normal file
30
web/src/components/canvas.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/** What a picture drawn on a canvas needs: the canvas made as sharp as the screen, and the colour of its text. */
|
||||
|
||||
export interface Surface {
|
||||
ctx: CanvasRenderingContext2D
|
||||
width: number
|
||||
height: number
|
||||
ratio: number
|
||||
/** The colour the canvas inherits (this server's, or a friend's), as red, green and blue. */
|
||||
rgb: [number, number, number]
|
||||
}
|
||||
|
||||
/** Sizes the canvas to what is on screen and clears it. Null where the browser cannot draw. */
|
||||
export function surfaceOf(canvas: HTMLCanvasElement): Surface | null {
|
||||
const ctx = canvas.getContext('2d')
|
||||
if (!ctx) return null
|
||||
const ratio = window.devicePixelRatio || 1
|
||||
const width = Math.round(canvas.clientWidth * ratio)
|
||||
const height = Math.round(canvas.clientHeight * ratio)
|
||||
if (canvas.width !== width) canvas.width = width
|
||||
if (canvas.height !== height) canvas.height = height
|
||||
ctx.clearRect(0, 0, width, height)
|
||||
const match = /rgba?\(\s*(\d+)[,\s]+(\d+)[,\s]+(\d+)/.exec(getComputedStyle(canvas).color)
|
||||
const rgb: [number, number, number] = match ? [Number(match[1]), Number(match[2]), Number(match[3])] : [255, 255, 255]
|
||||
return { ctx, width, height, ratio, rgb }
|
||||
}
|
||||
|
||||
/** True when the person asked the system for less motion. */
|
||||
export function prefersReducedMotion(): boolean {
|
||||
return typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { ChevronDown, ListMusic, X } from 'lucide-react'
|
||||
import { ChevronDown, ListMusic, Sparkles, X } from 'lucide-react'
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Link } from 'react-router-dom'
|
||||
|
|
@ -8,13 +8,16 @@ import Cover from '@/components/Cover'
|
|||
import FriendChip from '@/components/FriendChip'
|
||||
import FullscreenButton from '@/components/FullscreenButton'
|
||||
import Scrubber from '@/components/Scrubber'
|
||||
import Spectrogram from '@/components/Spectrogram'
|
||||
import Milkdrop from '@/components/Milkdrop'
|
||||
import Visualizer from '@/components/Visualizer'
|
||||
import TrackRow from '@/components/TrackRow'
|
||||
import VolumeControl from '@/components/VolumeControl'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Dialog, DialogContent, DialogDescription, DialogTitle } from '@/components/ui/dialog'
|
||||
import { formatTime } from '@/lib/time'
|
||||
import { albumHref, artistHref, type QueuedTrack } from '@/player/media'
|
||||
import { useMilkdrop } from '@/player/milkdrop'
|
||||
import { useVisualizerPref } from '@/player/prefs'
|
||||
import { currentTrack, usePlayer } from '@/player/store'
|
||||
import { useNowPlayingUi } from '@/player/ui'
|
||||
import PlayerControls from './PlayerControls'
|
||||
|
|
@ -55,6 +58,9 @@ export default function NowPlaying() {
|
|||
const index = usePlayer((s) => s.index)
|
||||
const jumpTo = usePlayer((s) => s.jumpTo)
|
||||
const upNextRef = useRef<HTMLElement>(null)
|
||||
const milkdrop = useVisualizerPref((s) => s.mode) === 'milkdrop'
|
||||
const milkdropRunning = useMilkdrop((s) => s.running)
|
||||
const nextPicture = useMilkdrop((s) => s.skip)
|
||||
|
||||
// Nothing left to show (the queue was emptied): close.
|
||||
useEffect(() => {
|
||||
|
|
@ -71,6 +77,13 @@ export default function NowPlaying() {
|
|||
>
|
||||
{track && (
|
||||
<>
|
||||
{/* Milkdrop fills the whole player, behind everything. A veil keeps the words easy to read over it. */}
|
||||
{milkdrop && (
|
||||
<div className="pointer-events-none absolute inset-0 -z-10">
|
||||
<Milkdrop fallback={null} className="size-full" />
|
||||
<div className="absolute inset-0 bg-background/45" />
|
||||
</div>
|
||||
)}
|
||||
{/* On a phone: close on the left, the friend in the middle, the queue on the right. Wide: the buttons at the corner. */}
|
||||
<div data-phone-top className="-mx-3 -mt-3 flex items-center justify-between gap-2 sm:absolute sm:top-4 sm:right-4 sm:m-0 sm:flex-row-reverse sm:justify-start sm:gap-1">
|
||||
<Button variant="ghost" className="size-11" aria-label={t('player.close')} onClick={() => setOpen(false)}>
|
||||
|
|
@ -78,6 +91,11 @@ export default function NowPlaying() {
|
|||
<X className="max-sm:hidden" />
|
||||
</Button>
|
||||
{track.friend !== undefined && <FriendChip friend={track.friend} className="px-3.5 text-sm sm:hidden" />}
|
||||
{milkdrop && milkdropRunning && (
|
||||
<Button variant="ghost" className="size-11 sm:order-last" aria-label={t('player.nextPicture')} onClick={nextPicture}>
|
||||
<Sparkles />
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="size-11 sm:hidden"
|
||||
|
|
@ -124,7 +142,7 @@ export default function NowPlaying() {
|
|||
)}
|
||||
</div>
|
||||
|
||||
<Spectrogram columns={72} rows={16} friend={track.friend !== undefined} className="h-20 w-full sm:h-36" />
|
||||
<Visualizer size="full" friend={track.friend !== undefined} className="h-20 w-full sm:h-36" />
|
||||
|
||||
<NowPlayingSeek track={track} />
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ import Scrubber from '@/components/Scrubber'
|
|||
import TrackList from '@/components/TrackList'
|
||||
import TrackRow from '@/components/TrackRow'
|
||||
import { initI18n } from '@/i18n'
|
||||
import { useMilkdrop } from '@/player/milkdrop'
|
||||
import { useVisualizerPref } from '@/player/prefs'
|
||||
import { usePlayer } from '@/player/store'
|
||||
import { useNowPlayingUi } from '@/player/ui'
|
||||
import { fakeFullscreen } from '@/test/fullscreen'
|
||||
|
|
@ -33,9 +35,13 @@ function track(n: number, over: Partial<Track> = {}): Track {
|
|||
|
||||
const [a, b, c, d, e] = [1, 2, 3, 4, 5].map((n) => track(n))
|
||||
|
||||
vi.mock('@/components/Milkdrop', () => ({ default: () => <div role="img" aria-label="Milkdrop picture of what is playing" /> }))
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
||||
await initI18n()
|
||||
useVisualizerPref.setState({ mode: 'spectrogram' })
|
||||
useMilkdrop.setState({ running: false, skips: 0 })
|
||||
sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: false } })
|
||||
usePlayer.setState({
|
||||
queue: [],
|
||||
|
|
@ -426,6 +432,55 @@ test('the bar and the full-screen player both show the spectrogram of what plays
|
|||
expect(within(screen.getByRole('dialog')).getAllByRole('img', { name: 'Spectrogram of what is playing' })).toHaveLength(1)
|
||||
})
|
||||
|
||||
test('the picture of the sound is the one this device chose, in the bar and in the full-screen player', async () => {
|
||||
useVisualizerPref.setState({ mode: 'bars' })
|
||||
act(() => usePlayer.getState().playTracks([a]))
|
||||
renderBar()
|
||||
expect(screen.getByRole('img', { name: 'Bars showing how loud each pitch is' })).toBeInTheDocument()
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Open the full-screen player' }))
|
||||
expect(within(screen.getByRole('dialog')).getByRole('img', { name: 'Bars showing how loud each pitch is' })).toBeInTheDocument()
|
||||
expect(screen.queryByRole('img', { name: 'Spectrogram of what is playing' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('chosen Milkdrop, it fills the full-screen player and the spectrogram stays where the picture was', async () => {
|
||||
useVisualizerPref.setState({ mode: 'milkdrop' })
|
||||
act(() => usePlayer.getState().playTracks([a]))
|
||||
renderBar()
|
||||
expect(screen.queryByRole('img', { name: 'Milkdrop picture of what is playing' })).not.toBeInTheDocument()
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Open the full-screen player' }))
|
||||
const dialog = within(screen.getByRole('dialog'))
|
||||
expect(dialog.getByRole('img', { name: 'Milkdrop picture of what is playing' })).toBeInTheDocument()
|
||||
expect(dialog.getByRole('img', { name: 'Spectrogram of what is playing' })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('the button for the next Milkdrop picture is there only while Milkdrop runs, and asks for the next one', async () => {
|
||||
useVisualizerPref.setState({ mode: 'milkdrop' })
|
||||
act(() => usePlayer.getState().playTracks([a]))
|
||||
renderBar()
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Open the full-screen player' }))
|
||||
const dialog = within(screen.getByRole('dialog'))
|
||||
expect(dialog.queryByRole('button', { name: 'Next picture' })).not.toBeInTheDocument()
|
||||
act(() => useMilkdrop.getState().setRunning(true))
|
||||
await userEvent.click(dialog.getByRole('button', { name: 'Next picture' }))
|
||||
expect(useMilkdrop.getState().skips).toBe(1)
|
||||
})
|
||||
|
||||
test('with any other picture there is no button for a next one', async () => {
|
||||
useMilkdrop.setState({ running: true })
|
||||
act(() => usePlayer.getState().playTracks([a]))
|
||||
renderBar()
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Open the full-screen player' }))
|
||||
expect(within(screen.getByRole('dialog')).queryByRole('button', { name: 'Next picture' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('chosen None, the player draws no picture of the sound', async () => {
|
||||
useVisualizerPref.setState({ mode: 'off' })
|
||||
act(() => usePlayer.getState().playTracks([a]))
|
||||
renderBar()
|
||||
await screen.findByText('Song 1')
|
||||
expect(screen.queryByRole('img', { name: /Spectrogram|Bars|Wave|Milkdrop/ })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('the full-screen player says whose music a friend’s track is', async () => {
|
||||
act(() => usePlayer.getState().playTracks([theirs(1)]))
|
||||
renderBar()
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import Cover from '@/components/Cover'
|
|||
import FriendChip from '@/components/FriendChip'
|
||||
import FullscreenButton from '@/components/FullscreenButton'
|
||||
import Scrubber from '@/components/Scrubber'
|
||||
import Spectrogram from '@/components/Spectrogram'
|
||||
import Visualizer from '@/components/Visualizer'
|
||||
import VolumeControl from '@/components/VolumeControl'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { usePeaks } from '@/api/peaks'
|
||||
|
|
@ -107,7 +107,7 @@ export default function PlayerBar() {
|
|||
<PlayerSeek track={track} />
|
||||
|
||||
<div className="player-extras flex items-center justify-end max-sm:hidden">
|
||||
<Spectrogram columns={28} rows={6} friend={track?.friend !== undefined} className="mr-3 h-10 w-36 max-lg:hidden" />
|
||||
<Visualizer size="bar" friend={track?.friend !== undefined} className="mr-3 h-10 w-36 max-lg:hidden" />
|
||||
<VolumeControl />
|
||||
<FullscreenButton />
|
||||
<Button
|
||||
|
|
|
|||
27
web/src/components/milkdropLib.test.ts
Normal file
27
web/src/components/milkdropLib.test.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { expect, test } from 'vitest'
|
||||
import { holderOf } from './milkdropLib'
|
||||
|
||||
const create = () => 'made'
|
||||
|
||||
test('it finds the library whether it is the module, its default, or the default of its default', () => {
|
||||
const lib = { createVisualizer: create }
|
||||
expect(holderOf({ ...lib }, 'createVisualizer')).toMatchObject(lib)
|
||||
expect(holderOf({ default: lib }, 'createVisualizer')).toBe(lib)
|
||||
expect(holderOf({ default: { default: lib } }, 'createVisualizer')).toBe(lib)
|
||||
})
|
||||
|
||||
test('a class with static functions is found too, and is the thing to call them on', () => {
|
||||
class Engine {
|
||||
static createVisualizer() {
|
||||
return this === Engine
|
||||
}
|
||||
}
|
||||
const holder = holderOf<typeof Engine>({ default: { __esModule: true, default: Engine } }, 'createVisualizer')
|
||||
expect(holder).toBe(Engine)
|
||||
expect(holder.createVisualizer()).toBe(true)
|
||||
})
|
||||
|
||||
test('where it is not there it says so, and does not loop', () => {
|
||||
expect(() => holderOf({ default: { default: {} } }, 'createVisualizer')).toThrow(/createVisualizer/)
|
||||
expect(() => holderOf(undefined, 'createVisualizer')).toThrow()
|
||||
})
|
||||
14
web/src/components/milkdropLib.ts
Normal file
14
web/src/components/milkdropLib.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
* The butterchurn packages are old CommonJS code compiled from modules: depending on who loads them (the browser build,
|
||||
* the test runner) what an import gives is the library itself, or an object that holds it under `default`, or one more
|
||||
* object inside that. The library is a class (a function) whose functions are static, so it has to be called as itself,
|
||||
* not taken apart. This finds the object that has `key`, the one to call it on.
|
||||
*/
|
||||
export function holderOf<T extends object>(module: unknown, key: string): T {
|
||||
let found: unknown = module
|
||||
for (let depth = 0; depth < 4 && found && (typeof found === 'object' || typeof found === 'function'); depth++) {
|
||||
if (key in found) return found as T
|
||||
found = (found as { default?: unknown }).default
|
||||
}
|
||||
throw new Error(`butterchurn: ${key} not found`)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue