TILT!Audio features a lua runtime 5.2 to control additional extensions like shakers, LED-lights and also servos.
To make this possible TILT!Audio firmware embeds a lua runtime (lua virtual machine) and loads a “init.lua” script from the sound directory.
The init.lua script already has access to all core lua language features and also the TILT!Audio bindings see lua reference. The binding allows access to the sound engine, communicate via i2c, logging messages and get information about the current installation.
Lua in its language ecosystem also offers modules and package managers that allows installing and managing modules that extend lua features. One of this package managers is luarocks.
Using luarocks as package manager to install a new module together with TILT!Audio is not so straight forward as you can read it on most websites, because the linux running on a TILT!Audio card is tailored down to a minimum to run as efficient as it should to produce sound and nothing else.
What is missing for example is a c compiler that most lua modules require as they come as a combination of lua code and c code.
Also luarocks requires a certain directory structure that is aware of the different lua versions and different places in the system where the lua runtime should look for “modules” (which is *.so and *.lua files).
The TILT!Audio start script does set the required environment variables to let lua runtime find modules, but still not in that way that a module installed by luarocks would expect.
The new version of TILT!Audio firmware features better support for altsound sound packs and lua scripting support.
What is lua scripting?
In previous version there were already some command attached to a sound effect, either to control an attached shaker or the new port extender starting with 1.28 to control up to 16 additional GPIO ports directly with the TILT!Audio board.
Now lua scripting takes this to the next level. A command attached to a sound effect is per default simply passed to a lua script interpreter (see https://www.lua.org/). Lua script is a super easy to learn script language that lets you “program” what should happen when a certain sound effect gets played.
So everything you can do with special commands (control shaker, control GPIO, control serial media server, control various i2c devices e.g. ProMiniExtender) you can now also do with lua functions. Addtionally you can play additional sound effects or change volume all controlled by your lua script.
In order to make this work, TILT!Audio loads a lua script at startup (init.lua) that is located at the root directory of your sound pack, where you define all functions your going to use with your sound effects.
Lets make an example:
-- this is the init.lua file with the sound pack
-- get shaker pin from main config
-- define a function that activates shaker (on / off sequence)
-- only if a certain background music is playing
Now in the webUI choose the sound effect that should trigger the shaking …
As you can see the shakeOnMultiball checks which background music is playing and activates the shaker only if a certain music is playing. But this is just an example.
From lua you also can control custom sound playback:
Example: use a dummy sound file that actually contains only a second silence, but triggers a lua function:
-- init random generator
-- remember if drain sound was played
-- lua function to do custom sound handling
In this example we use addtional sound effect ids 1000-1004 and 1010 that the game normally don’t use (choose an arbitray high number). We create a matching row in altsound.csv (or directory in classic dir structure) and put the WAV files there.
The we call the lua functions like shown above and we will hear a random sound with the first function while the drain ball counter is increased. If playLooser sound triggers we play a “looser” sound if the was to many drained balls.
Of course this example requires the game to send the right trigger sounds when ball passes e.g. the outlane.
I recorded a short video to demonstrate, what you can do with lua scripting. This will also be shown with light controller in my MSF later this month.
Lua reference (all TILT!Audio lua functions and variables) can be found here.