Lua: Tools Module
Description
The optional tools.lua module adds helper functions and classes for mlys.lua scripts. Load it at the beginning of a script:
require("tools")
Math Helpers
local s = math.sign(-3) -- -1
local r = math.round(1.234, 2) -- 1.23
local p = math.Pow(-2, 3) -- 8
dump
print(dump(myTableOrController))
dump returns a string representation of a Lua table or Modalys controller. For a controller, it queries the current value first.
Average
local average = Average:new{ size=100, factor=0.1, method="linear" }
function update()
return average:update(inlet(1))
end
Average stores recent values and returns a scaled moving average. If method is not "linear", it computes an RMS-like average.
InletChangeObserver
inlet_observer:set_notification_function(function(index, value)
print("inlet " .. index .. " changed to " .. value)
end)
function update()
inlet_observer:update()
end
InletChangeObserver watches mlys.lua inlets and calls a function when an inlet value changes. A default observer is available as modalys.inlet_observer and inlet_observer.
Parametric Curves and Surfaces
ParametricCurve2D, ParametricCurve3D, and ParametricSurface3D help discretize mathematical shapes and create Modalys meshes.
local curve = ParametricCurve2D:new{
scale = 0.1,
resolution = 20,
f = function(t)
return math.cos(2 * math.pi * t), math.sin(2 * math.pi * t)
end
}
local mesh = curve:createMesh()
For surfaces, provide f(s, t) returning x, y, z coordinates. The surface helper builds quadrilateral meshes through create_mesh.
get_pitched_finite_element_object
local object = get_pitched_finite_element_object{
mesh = mesh,
block = hold,
midinote = 60,
modes = 160
}
This helper scales a finite-element mesh until a selected mode approaches a target frequency, then creates a finite-element object from the scaled mesh.
★