Sound and music
Crash-tan’s Tip: Browsers keep a page quiet until you’ve clicked on it. Press Try it or Run and I’m allowed to make noise.
Beeps and tones
BEEP is the classic. SOUND plays a tone: how high, in hertz, and for how
many seconds.
SCREEN 320, 240
BEEP
SOUND 440, 0.2
SOUND 660, 0.2
SOUND 880, 0.4
PRINT "ta-da!"
PAUSE
ta-da!
When a program ends, its sounds stop with it, so PAUSE at the end keeps it
going until you press a key. A game never needs one: it’s still running.
Tunes
PLAY plays a tune written as notes, the way the old BASICs did it: T sets
the tempo, O the octave, L how long a note is (4 is a quarter note, 8 an
eighth), and then the notes themselves.
SCREEN 320, 240
PLAY "T160 O4 L8 C D E F G4 G4 A A A A G2"
PRINT "done"
PAUSE
done
PLAY waits for its tune to finish. In a game you’d rather it didn’t, so the
game goes on while it plays: !! background.
SCREEN 320, 240
PLAY "T120 O3 L4 C E G > C < G E C2" !! background
x = 0
DO
IF COOLDOWN(1)
MEMORYBUFFER "scene"
CIRCLE (x, 120), 10, 13, 13
END MEMORYBUFFER
FRAME
CLS
PUTIMAGE (0, 0), "scene"
END FRAME
x = (x + 3) MOD 320
END IF
LOOP
Music
For a whole song with several instruments, describe it in a MUSIC block:
the patterns, the tracks in each one, and the order they play in. Then
PLAYMUSIC plays it, in the background, for as long as you like.
SCREEN 320, 240
MUSIC "theme"
TEMPO 140
PATTERN "verse"
TRACK !! channel(1) notes("o4 l8 c e g e c e g e")
TRACK !! channel(2) notes("o2 l2 c g")
END PATTERN
PATTERN "chorus"
TRACK !! channel(1) notes("o4 l8 f a > c < a g b > d < b")
TRACK !! channel(2) notes("o2 l2 f g")
END PATTERN
ARRANGEMENT
PLAY "verse" !! repeat(2)
PLAY "chorus"
END ARRANGEMENT
END MUSIC
PLAYMUSIC "theme"
PRINT "playing", musicplaying()
PAUSE
There’s a lot more where that came from: instruments you design yourself, envelopes and filters, samples, fading one song into another. The reference has every word, and the music lessons will come later in this book.