Skip to content

How It Works

Niraj Khetre edited this page Nov 25, 2026 · 1 revision

How it works

flowchart LR
  P["Spotify / YouTube<br/>any media player"] -->|Windows media session| N["nowplaying.ps1"]
  N -->|title, artist, position + timestamp| M["main process"]
  M -->|artist, title, length| L["lyrics.js<br/>Kugou KRC, then LRCLIB"]
  M -->|artist, title| A["iTunes Search<br/>album art"]
  S["system audio<br/>(loopback)"] --> B["beat-worklet.js<br/>AudioWorklet"]
  M --> C["live.js<br/>shared song clock"]
  L --> C
  A --> C
  B --> C
  C --> O["Overlay<br/>engine.js + styles.css"]
  C --> F["Listen mode<br/>scenes.js, WebGL2"]
Loading

1. What's playing

nowplaying.ps1 runs in Windows PowerShell 5.1 and reads Windows' Global System Media Transport Controls session: the same data the media flyout shows. It prints one JSON line whenever something changes, and exits when the app does.

The key detail is the timestamp. Each position report comes with the player's own time for that position (at), so the song clock between reports is exactly position + (now − at) while playing. That makes the clock smooth and accurate without polling fast.

control.ps1 sends play, pause and skip to the same session for listen mode's controls.

2. Lyrics

lyrics.js looks up lyrics by artist, title and length:

  1. Kugou KRC first. KRC lyrics time every word. They arrive XOR-encoded and zlib-compressed; mondegreen decodes them.
  2. LRCLIB as the fallback, with line timing. Words inside a line are spread by syllable count, so they still light up one by one.

Both must match the track's length within a few seconds, so a live or extended version never gets studio timing. YouTube titles like Artist - Song (Official Video) [4K] are cleaned up before searching. Results are cached in %APPDATA%\mondegreen\lyrics, so a song you have played before shows its lyrics instantly, even offline. A song with no lyrics is tried again after 6 hours.

3. Album art and colour

The iTunes Search API supplies the cover. The loudest colour on it becomes the accent; its deepest shade and three strongest hues paint listen mode.

4. The beat

beat-worklet.js runs on the audio thread, fed by system-audio loopback:

  • It builds an onset envelope from several frequency bands.
  • It finds the tempo by autocorrelation, with a gentle preference around 120 BPM.
  • It phase-locks a beat grid to it with a comb filter, and keeps it locked with a phase-locked loop.
  • Half or double tempo never resets the grid: a challenger tempo must win several checks in a row before it takes over.

Animations land on the predicted beat instead of reacting after it. With no steady tempo (intros, ballads), mondegreen reacts to hits instead. In tests it locks within a few milliseconds and follows tempo changes.

5. One shared clock

live.js holds the song clock, lyrics, colours and beat for both windows, so the overlay and listen mode always agree.

6. Drawing

  • Overlay (engine.js, styles.css, overlay.js): one stage with twelve looks. A capped loop (60 or 30 fps) drives all motion through a few CSS variables per word. Nothing animates at the screen's full refresh rate, since a transparent window costs a full copy per frame, and the loop stops completely while hidden.
  • Listen mode (listen.html, listen.js, scenes.js): one WebGL2 fragment shader per scene, drawn at 60% resolution and scaled up, with objects in HTML and SVG on top. Late Night Texts is an SVG sketch with ink, watercolour and hatching filters.

Performance

Measured on a laptop with a 144 Hz screen: the overlay uses about 40–70% of one core while animating and around 6% while hidden. Listen mode's heaviest scenes use about 60% in the GPU process plus 30% in the renderer.

Clone this wiki locally