Programming Macaw OS

Input and sound

Keys

function _update()
  if btn("left") then x = x - 1 end           -- held down
  if btnp("space") then sfx("jump") end       -- just pressed, this frame
end
CallWhat it does
btn(key)Returns true while key is held down
btnp(key)Returns true in the frame key goes down, then again every so often while it's held, the way a key repeats when you type
keysp()Returns a list of the names of every key that went down or repeated this frame
textin()Returns the characters typed this frame. Use it for text entry, not for game controls

The key names are left, right, up, down, enter, escape, space, backspace, tab, delete, home, end, pageup, pagedown, shift, ctrl, alt, cmd, f1 to f12, a to z and 0 to 9. Punctuation keys are named by their character (-, =, [, ], ;, ', ,, ., /, `).

Only the window in front gets key presses. Ctrl (Cmd on a Mac) with a letter is kept for menu shortcuts, so use the arrow keys, letters, Space and Enter for game controls.

The mouse

local mx, my, down, pressed, released, wheel = mouse()
if pressed & 1 ~= 0 then print("clicked at", mx, my) end

mouse() returns the pointer's position inside your window, then three button values. These say which buttons are held, which were pressed this frame and which were let go this frame. Each is a sum of 1 (left), 2 (right) and 4 (middle). wheel is how far the scroll wheel turned.

To make your own right-click menu, see Apps.

Sound effects

sfx("coin")

The built-in effects are blip, coin, jump, hit, lose, squawk, rawk (a shorter macaw call) and chime. In a cart, sfx(n) with a number plays the cart's own effects instead, the ones you made in the SFX editor.

Notes and tones

There are four effect voices, numbered 1 to 4. Each voice plays one sound at a time.

play(1, "C4", 200)                         -- voice 1, middle C, 200 ms
play(2, "E4", 200, "triangle", 0.3)        -- a softer wave, quieter
tone(4, 880, 300, "square", 0.4, -2000)    -- 880 Hz, falling 2000 Hz a second
CallWhat it does
play(voice, note, ms, [wave], [volume])Plays a note such as "C4", "F#3" or "Bb5" for ms milliseconds
tone(voice, hz, ms, [wave], [volume], [slide])Plays a frequency in Hz. slide is how many Hz per second the pitch rises by. A negative slide makes it fall
note(name)Returns the frequency of a note. note("A4") is 440
stop()Stops all four voices

The waves are square, pulse, triangle, saw, sine and noise. Volume goes from 0 to 1, and is 0.5 if you leave it out.

Music

You write songs in the Music app, which saves them as .song files. A song you play loops until you stop it:

function _init()
  playsong("/home/music/island.song")
end

function _close()
  stopsong()
end
CallWhat it does
playsong(path or song, [loop])Plays a song. Pass false for loop to play it once
stopsong()Stops the song
loadsong(path)Loads a song so you can change it before you play it
music.volume(v)Sets the music's volume, from 0 to 1
music.note(channel, instrument, n, [volume])Plays one note on the music chip straight away. instrument is 1-16, from the song that's playing
music.release(channel)Releases the note playing on channel
notenumber(name)Returns a note's number. notenumber("C-4") is 49
notename(n)Returns a note's name from its number. notename(49) is "C-4"

The music chip has 16 voices of its own, separate from the four effect voices, so music and sound effects can play at the same time. Each instrument is either a synth, which plays a wave you draw, or a recording of a real instrument. The recorded ones include piano, strings, cello, harp, flute, clarinet, horn, trumpet, marimba and drums. The example songs are in /home/music. Double-clicking one plays it in Jukebox, and the Music app opens it to change it. demo.song (synth) and island.song (recorded) are good songs to start from, and ambient.song is an ambient piece made with the pads below.

For ambient music, the synth has pads and textures that swell in slowly and take seconds to fade: warmpad, glasspad, darkpad, choirpad, swell, drone, air, wind, shimmer, softbell, musicbox and subbass. A note that's still fading keeps ringing when the next one starts, so chords wash into each other. Two settings make a synth sound move. detune adds a second copy of the wave a few cents higher, which makes it wider and richer. drift sweeps the filter down and back up again, slowly, at driftspeed times a second, so a long note breathes.

Programs can make songs too. newrollsong() makes a new song with six tracks. Each track has clips, and each clip holds notes like { t = step, len = steps, n = note, v = volume }, with 16 steps to a bar. The code that builds and plays songs is in /system/sound.lua, and the Music app that edits them is /system/apps/music.lua.