feat(api-server): expose real-time audio spectrum over REST and WebSocket - #4612
feat(api-server): expose real-time audio spectrum over REST and WebSocket#4612stranzero wants to merge 3 commits into
Conversation
…cket Hook the player Web Audio graph with an AnalyserNode and publish log-spaced bands (0-255) via GET /api/v1/spectrum and an opt-in SPECTRUM WebSocket stream. Main-process polling keeps sampling responsive when the window is backgrounded.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe API server adds configurable audio spectrum capture, renderer-side FFT sampling, backend IPC polling, an HTTP spectrum endpoint, and opt-in WebSocket spectrum broadcasts. ChangesAudio spectrum streaming
Estimated code review effort: 4 (Complex) | ~60 minutes Sequence Diagram(s)sequenceDiagram
participant RendererAudioGraph
participant BackendSpectrumPolling
participant HTTPClient
participant WebSocketClient
BackendSpectrumPolling->>RendererAudioGraph: request-spectrum
RendererAudioGraph-->>BackendSpectrumPolling: audio-spectrum frame
BackendSpectrumPolling->>HTTPClient: return latest spectrum
BackendSpectrumPolling-->>WebSocketClient: broadcast subscribed spectrum frame
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/plugins/api-server/backend/main.ts`:
- Around line 90-102: Update startSpectrumPolling to clear this.spectrum
whenever spectrumEnabled is false, before returning after stopping the polling
timer. Ensure the getSpectrum route observes the cleared cache and returns 204
when spectrum streaming is disabled, while preserving the existing polling
behavior when enabled.
In `@src/plugins/api-server/renderer.ts`:
- Around line 130-145: Update onRendererLoad and onRendererUnload to store the
named peard:request-spectrum callback, then remove that callback from the IPC
object during unload before clearing rendererIpc. Preserve the existing
audio-can-play listener cleanup and state reset while ensuring each renderer
reload leaves no spectrum IPC listener behind.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 7b89fdbf-305e-4019-bbca-9dc84cdf8768
📒 Files selected for processing (7)
src/plugins/api-server/backend/main.tssrc/plugins/api-server/backend/routes/control.tssrc/plugins/api-server/backend/routes/websocket.tssrc/plugins/api-server/backend/types.tssrc/plugins/api-server/config.tssrc/plugins/api-server/index.tssrc/plugins/api-server/renderer.ts
Return 204 after spectrumEnabled is turned off by dropping the cached frame, and remove the peard:request-spectrum listener on renderer unload so reloads do not stack handlers.
|
Addressed the review notes:
|
Cap log bands around 4.2 kHz, lift treble with a stronger shelf, and prefer peak/loud-bin energy on upper bands so sparse highs aren't RMS-diluted into silence. Deepen the analyser floor slightly for quiet HF.
|
Pushed a spectrum balance tweak so the right-side bands actually move on typical tracks:
|
Summary
peard:audio-can-play) with anAnalyserNodeand publish log-spaced frequency bands (0–255).GET /api/v1/spectrumand an opt-in WebSocketSPECTRUMstream (SUBSCRIBE_SPECTRUM/UNSUBSCRIBE_SPECTRUM).API
GET /api/v1/spectrum→{ bands, peak, timestamp }(204 if no data yet){ type: SUBSCRIBE_SPECTRUM }to receive{ type: SPECTRUM, bands, peak, timestamp }framesspectrumEnabled(default true),spectrumBands(16),spectrumFps(20)Test plan
GET /api/v1/spectrumreturns non-empty bands that move with the trackSUBSCRIBE_SPECTRUM, confirm frames at ~configured fpsUNSUBSCRIBE_SPECTRUMand confirm frames stop for that socketspectrumEnabledand confirm REST returns 204 / WS stops emittingSummary by CodeRabbit
GET /api/{API_VERSION}/spectrumto fetch the latest spectrum frame; returns no content when disabled/unavailable.