/ Examples / MusicAPI / FirstLua

Right-click to copy examples to your workspace


Here's the simplest example of combining Lua with our musicAPI. Since our docs are generally focused on JavaScript, these working examples are the best way to understand the mapping between the docs and the conditions on the moon (Lua is Moon in Portuguese).

The Code

local Ascene = require("ascene")
Ascene:init(function(scene) -- initialize the audio engine, callback when ready.
    -- instantiate and connect some Anodes
    local inst = scene:newAnode("Hz.Syntho")
    local dac = scene:newAnode("dac")
    scene:connect(inst, dac)
    scene:begin() -- asynchronously wait for scene to be ready
    local baseNote = 30 -- a midi note number
    local waittime = scene:seconds(.25)
    for i=0,23 do -- generate some notes
        local note = baseNote + i
        inst:noteOn(note, .9) -- initiate the note  with velocity .9
        scene:wait(waittime) -- synchronously wait while note plays.
        inst:noteOff(note, 1)
    end
    Log.notice("done")
end)

More Examples

We've included a number of additional small examples in the firstlua subdirectory for your perusal. Some of them assume the presence of a MIDI input device and some assume the present of one or more third-party CLAP plugins.

home .. topics .. interface .. reference .. examples .. tipjar