HAPTIC FISHING / STUDY / LEVEL 6 OF 7

Music that follows
the game.

The game has no audio files. Every sound is built live with the browser's WebAudio: an oscillator vibrates to make a tone, a gain (volume) node makes it rise and fade, and a filter softens it. Combine a few of these parts and you get a kalimba, a bass, a shaker. Split them into layers, set each layer's volume from the fishing state, and the music follows the game. This is called adaptive music.

This level is about sound; headphones help. Sound plays only when you press a button, and stops when you leave the page.

A. How one note is made

oscilloscope: the waveform you are hearing
Press "Play a note".

The parts

osc, env and pluck in fishingMusic.js

B. The real game music

76 BPM

This runs the game's own createFishingMusic(). Press play, then the state buttons below, and listen to the music change. The five bars are the target volume of each layer, from mixFor.

Game state

playing
One-off phrases: music.cue(...)

The music code

fishingMusic.js

Whatever you press, the matching code is highlighted here.


      
Full file: fishingMusic.js ↗ · where the scene calls it every frame: fishing.js ↗ (search for music.update)

What you should be able to explain

  1. A note = oscillator → filter → envelope. This chain is the audio graph. The waveform sets the timbre; the pitch is how fast it vibrates (frequency, in hertz). The envelope makes it sound plucked: full volume within a few milliseconds (attack), then a slow fade (decay).
  2. Pitch as a MIDI number. 60 is middle C. +1 is one semitone; +12 is one octave (double the frequency). The formula is 440 × 2^((m − 69) / 12), with note 69, the A at 440 Hz, as the reference pitch.
  3. Schedule ahead. Browser frame timing is uneven (jitter). So each frame, update does one thing: it hands WebAudio every note due in the next 0.2 s, each with an exact start time on WebAudio's own accurate clock. This is a lookahead scheduler, and it keeps the rhythm steady.
  4. Layers and mixing. Melody, bass, shaker, ambience and the fight layer each have their own gain, like faders on a mixing desk. mixFor(state) sets each fader's target, and setTargetAtTime glides it there: 0.06 s to duck when a fish tests the bait or bites (ducking), 0.4 s to recover.

Exercises: ① In part A, play the same note with all four waveforms. Which sounds most like a kalimba? Which like an old console? ② Drag the attack to 300 ms. Does it still sound plucked? What does it sound like now? ③ In part B, play the music and switch from waiting to nibbling: how quickly does it go quiet? Switch back: does it recover quickly or slowly? Find both speeds in the code. ④ Set tension to 1 and tick surging. How many semitones does the fight bass rise? (Look for surging ? 2 : 0 in playStep.)