Instruments
Every note I play, I play on an instrument. So far that’s been instrument 0,
the piano. In a tune, i and a number picks another one, and it plays
every note after it until the next i.
SCREEN 320, 240
PLAY "t150 o4 l8 i0 c e g > c < i17 c e g > c < i7 o2 c e g > c"
PRINT instrumentname(0)
PRINT instrumentname(17)
PRINT instrumentname(7)
PAUSE
FM Piano
FM Vibraphone
FM Bass
I come with a whole bank of them, numbered from 0: pianos, strings, basses,
drums, and the sounds games need, like a coin pickup and a laser.
instrumentname(n) tells you what number n is.
A bank from a file
A .cbi file is a bank of instruments, and each instrument in it takes its
number, in place of the one I had there. A file called instruments.cbi in
your game’s folder is special: I load it by myself, before your program
starts. Every game in this book has one, so these are already its
instruments, not mine:
SCREEN 320, 240
PRINT instrumentname(51)
PLAY "t160 o5 l16 i51 c e g > c < g e c4"
PAUSE
MM2 Lead
In my own bank, number 51 is called “Futuristic Lead”. LOADINSTRUMENTS
loads any other bank, whenever you like: LOADINSTRUMENTS "spooky.cbi" for
the haunted level, say. With !! instrument(51) it loads only that one and
leaves the rest alone.
One of your own
INSTRUMENT and a number describes an instrument of your own. Any number
will do: I start mine at 200, so they don’t land on top of the bank’s.
SCREEN 320, 240
INSTRUMENT 200
NAME "Pluck"
VOLUME 0.8
ENVELOPE !! attack(0.005) decay(0.15) sustain(0) release(0.1)
LAYER "triangle"
END LAYER
END INSTRUMENT
INSTRUMENT 201
NAME "Pad"
VOLUME 0.6
ENVELOPE !! attack(0.4) decay(0.2) sustain(0.8) release(0.8)
LAYER "sawtooth"
END LAYER
END INSTRUMENT
PLAY "t120 o4 l4 i200 c e g e"
PLAY "t120 o4 l4 i201 c e g e"
PRINT instrumentname(200), "and", instrumentname(201)
PAUSE
Pluck and Pad
The same four notes, and two very different sounds. A LAYER is a wave:
"sine", "square", "triangle", "sawtooth", "pulse" or "noise". The
ENVELOPE is how each note goes:
attack: how many seconds it takes to get loud when the note starts;decay: how long it then takes to settle down to…sustain: the level it holds, from 0 to 1, for as long as the note lasts;release: how long it takes to fade once the note ends.
The pluck gets loud at once and dies away, like a plucked string. The pad swells in slowly and lingers.
Shaping the sound
A few more lines go in an instrument, a setting each:
FILTERtakes some of the sound away."lowpass"keeps the low part and muffles the highs above itscutoff, in hertz;resonancemakes it ring at the cutoff.FILTERENVELOPEmoves the cutoff over each note, the wayENVELOPEmoves the volume, up as far as itspeak.VIBRATOmakes the pitch waver, andTREMOLOthe volume:rateis how many times a second,depthhow much.PANsits it to the left (-1) or the right (1), andVOLUMEsays how loud.
SCREEN 320, 240
INSTRUMENT 202
NAME "Wah"
FILTER "lowpass" !! cutoff(400) resonance(3)
FILTERENVELOPE !! attack(0.1) decay(0.3) sustain(0.2) release(0.2) peak(2000)
LAYER "sawtooth"
END LAYER
END INSTRUMENT
INSTRUMENT 203
NAME "Singer"
VIBRATO !! rate(5) depth(0.2) delay(0.3)
TREMOLO !! rate(6) depth(0.3)
PAN -0.5
LAYER "triangle"
END LAYER
END INSTRUMENT
PLAY "t130 o3 l8 i202 c c g g a a g4"
PLAY "t90 o5 l2 i203 c g"
PRINT instrumentname(202), instrumentname(203), instrumentpan(203)
PAUSE
Wah Singer -0.5
VIBRATO’s delay waits a moment before it starts, the way a singer holds a
long note steady and then lets it shake.
Layers
An instrument can have several layers, and they all play every note
together. Each one can be tuned a little away from the note (detune, in
hundredths of a note), play octaves up or down (octave), or be quieter than
the rest (volume). Inside a layer, UNISON plays it as several copies at
once, each a little out of tune with the others, for a big, wide sound.
SCREEN 320, 240
INSTRUMENT 204
NAME "Big Lead"
ENVELOPE !! attack(0.01) decay(0.2) sustain(0.7) release(0.3)
LAYER "sawtooth"
UNISON !! voices(5) detune(15) spread(0.8)
END LAYER
LAYER "square" !! octave(-1) volume(0.4)
END LAYER
LAYER "pulse" !! duty(0.25) detune(7) volume(0.3)
END LAYER
END INSTRUMENT
PLAY "t140 o4 l8 i204 e g > c4 < b g a2"
PRINT instrumentname(204), "has", instrumentlayercount(204), "layers"
PAUSE
Big Lead has 3 layers
Drums
A drum is an instrument too. "noise" is a hiss, and a hiss that stops
almost at once is a snare. PERCUSSION says it’s a drum.
SCREEN 320, 240
INSTRUMENT 205
NAME "Snare"
PERCUSSION 1
ENVELOPE !! attack(0) decay(0.1) sustain(0) release(0.05)
LAYER "noise"
END LAYER
END INSTRUMENT
PLAY "t120 o4 l8 i205 c r c r c c c r"
PRINT instrumentpercussion(205)
PAUSE
1
Real drums sound better than any hiss, though, and a layer can play a
recording instead of a wave. SAMPLE gives it the file, and basenote the
note it was recorded at: 60 is o4 c, so that note plays the drum just as it
was recorded, and higher notes play it higher.
My book comes with three drums, kick.wav, snare.wav and hat.wav. Here
they are as a kit:
SCREEN 320, 240
INSTRUMENT 210
NAME "Kick"
PERCUSSION 1
LAYER "sine"
SAMPLE "kick.wav" !! basenote(60)
END LAYER
END INSTRUMENT
INSTRUMENT 211
NAME "Snare"
PERCUSSION 1
LAYER "sine"
SAMPLE "snare.wav" !! basenote(60)
END LAYER
END INSTRUMENT
INSTRUMENT 212
NAME "Hat"
PERCUSSION 1
LAYER "sine"
SAMPLE "hat.wav" !! basenote(60)
END LAYER
END INSTRUMENT
beat = "i210 c i212 c i211 c i212 c "
PLAY "t110 o4 l8 " + beat + beat + beat + beat
PRINT "boom tss crack tss"
PAUSE
boom tss crack tss
PLAY plays one note after another, so this beat is one drum at a time. In
the next lesson, a song plays the kick, the snare and the hats on tracks of
their own, all at once.
Crash-tan’s Tip: A sound you’ve already loaded with
LOADWAVcan go on an instrument too:SETINSTRUMENTWAV 210, "boom".
WAVETABLE is a layer’s other way to use a file: a file of many single
waves, one after another, and the layer plays the one at position, from 0
to 1. Moving the position while a note plays turns one sound into another.
There’s no such file in this book’s folder, so this one’s only to look at:
INSTRUMENT 206
NAME "Morph"
LAYER "sine"
WAVETABLE "waves.wav" !! framesize(256) position(0.5)
END LAYER
END INSTRUMENT
Changing one as you go
SET INSTRUMENT changes an instrument you’ve made, and only what you write
in it. Everything else stays as it was. Here the mouse opens and closes the
filter while a bass line plays: click the screen and move across it.
SCREEN 320, 240
INSTRUMENT 207
NAME "Bass"
FILTER "lowpass" !! cutoff(300) resonance(4)
ENVELOPE !! attack(0.01) decay(0.2) sustain(0.5) release(0.1)
LAYER "sawtooth"
END LAYER
END INSTRUMENT
cutoff = 300
DO
IF COOLDOWN(30)
cutoff = 200 + mousex * 10
SET INSTRUMENT 207
FILTER "lowpass" !! cutoff(cutoff) resonance(4)
END SET INSTRUMENT
PLAY "t120 o2 l16 i207 c c > c < c" !! background
END IF
IF COOLDOWN(1)
FRAME
CLS
LOCATE 1, 1
PRINT "cutoff", cutoff
END FRAME
END IF
LOOP
Every half a second the loop sets the filter where the mouse says and plays the next few notes with it.
Keeping them
When an instrument sounds right, SAVEINSTRUMENTS saves it in a bank of
your own (!! instrument(n) saves just that one), and LOADINSTRUMENTS
brings it back, in this game or the next.
SCREEN 320, 240
INSTRUMENT 208
NAME "Blip"
ENVELOPE !! attack(0) decay(0.1) sustain(0) release(0.05)
LAYER "square" !! volume(0.5)
END LAYER
END INSTRUMENT
SAVEINSTRUMENTS "mine.cbi" !! instrument(208)
DELETE INSTRUMENT 208
PRINT instrumentexists(208)
LOADINSTRUMENTS "mine.cbi"
PRINT instrumentexists(208), instrumentname(208)
PLAY "t200 o6 l16 i208 c e g"
PAUSE
0
1 Blip
Call your bank instruments.cbi and put it in the game’s folder, and I’ll
load it by myself every time the game starts.