AI CHOREOGRAPHY / STUDY / LEVEL 6 OF 7

Beat-locked
music.

In a dance game the music is the clock. AI Choreography does not play a recording and hope it lines up: it builds the music from the same clock that picks the pose (Level 1), one sixteenth of a bar at a time. That makes it a step sequencer, the kind of grid drum machines use. The grid below is recorded from the game's own music.js.

This level plays music; headphones help. Sound starts only when you press play and stops when you leave the page.

One bar of the pattern

chords

How this grid was made: a copy of music.js was asked to schedule one bar into a silent, fake audio system that writes down every sound instead of playing it. So the grid is exactly what the game plays.

Change the feel

the two extra inputs to schedule()

A longer combo opens the filter on the arpeggio (Dance) or the bass (Fitness): the music gets brighter as you do well.

One-off sounds

cue() and ding()

The real code

music.js

playStep decides what sounds on step s of the bar with simple remainders (s % 4 == 0 is every beat). schedule is the look-ahead loop: every frame it queues the steps due in the next 0.15 s.


      

Where the scene calls it

coach.js

      
Full files: music.js ↗ · coach.js ↗ · WebAudio basics (oscillators, filters, envelopes) are explained in Haptic Fishing level 6 ↗

What you should be able to explain

  1. A bar is 16 steps. One beat is 4 steps (sixteenth notes), so a step lasts beat ÷ 4. The step number comes straight from the clock: n = clock ÷ step, s = n % 16, and the bar Math.floor(n ÷ 16) % 4 picks the chord.
  2. Patterns are remainders. s % 4 == 0: on every beat (a "four on the floor" kick). s % 8 == 4: beats 2 and 4 (the clap or snare, the backbeat). s % 2 == 1: every offbeat sixteenth (hi-hats).
  3. Why the music stays in sync. The pose, the controller ticks and the music all compute from one clock, and the notes are handed to WebAudio slightly ahead with exact times (a lookahead scheduler). In multiplayer, the clock is shared (Level 7), so everyone hears the same beat.
  4. Music reacts to play. hype brightens the sound as the combo grows; quiet ducks it while someone records a routine; cues mark the countdown, a miss, a win.

Exercises: ① In the Dance grid, which rows play on every beat, and which only on beats 2 and 4? Find the matching if in playStep. ② The Fitness kick has one extra hit that is not on a beat. Which step is it, and which condition adds it? ③ Play the music, then drag hype from 0 to 1. What changes in the sound?