aboutsummaryrefslogtreecommitdiffhomepage
path: root/backends/lua.md
diff options
context:
space:
mode:
authorcbdev <cb@cbcdn.com>2020-03-20 21:50:33 +0100
committercbdev <cb@cbcdn.com>2020-03-20 21:50:33 +0100
commit2d66d5cee9bf3ed5779f65d8a99b40ee5181bf30 (patch)
tree9fbf00807fd06a9a48871d05de26625e6c1fb9fd /backends/lua.md
parent9718e10c7f4151cea895f515c785c14e0021d967 (diff)
downloadmidimonster-2d66d5cee9bf3ed5779f65d8a99b40ee5181bf30.tar.gz
midimonster-2d66d5cee9bf3ed5779f65d8a99b40ee5181bf30.tar.bz2
midimonster-2d66d5cee9bf3ed5779f65d8a99b40ee5181bf30.zip
Implement Lua threading
Diffstat (limited to 'backends/lua.md')
-rw-r--r--backends/lua.md30
1 files changed, 24 insertions, 6 deletions
diff --git a/backends/lua.md b/backends/lua.md
index 96e53c8..05509b6 100644
--- a/backends/lua.md
+++ b/backends/lua.md
@@ -19,24 +19,41 @@ The following functions are provided within the Lua interpreter for interaction
| `output_value(string)` | `output_value("bar")` | Get the last output value on a channel |
| `input_channel()` | `print(input_channel())` | Returns the name of the input channel whose handler function is currently running or `nil` if in an `interval`'ed function (or the initial parse step) |
| `timestamp()` | `print(timestamp())` | Returns the core timestamp for this iteration with millisecond resolution. This is not a performance timer, but intended for timeouting, etc |
+| `thread(function)` | `thread(run_show)` | Run a function as a Lua thread (see below) |
+| `sleep(number)` | `sleep(100)` | Suspend current thread for time specified in milliseconds |
Example script:
-```
+```lua
function bar(value)
- output("foo", value / 2)
+ output("foo", value / 2);
end
step = 0
function toggle()
- output("bar", step * 1.0)
+ output("bar", step * 1.0);
step = (step + 1) % 2;
end
+function run_show()
+ while(true) do
+ sleep(1000);
+ output("narf", 0);
+ sleep(1000);
+ output("narf", 1.0);
+ end
+end
+
interval(toggle, 1000)
+thread(run_show)
```
Input values range between 0.0 and 1.0, output values are clamped to the same range.
+Threads are implemented as Lua coroutines, not operating system threads. This means that
+cooperative multithreading is required, which can be achieved by calling the `sleep(number)`
+function from within a running thread. Calling that function from any other context is
+not supported.
+
#### Global configuration
The `lua` backend does not take any global configuration.
@@ -61,9 +78,10 @@ lua1.foo > lua2.bar
#### Known bugs / problems
-Using any of the interface functions (`output`, `interval`, `input_value`, `output_value`, `input_channel`,
-`timestamp`) as an input channel name to a Lua instance will not call any handler functions.
-Using these names as arguments to the output and value interface functions works as intended.
+Using any of the interface functions (`output`, `interval`, etc.) as an input channel name to a
+Lua instance will not call any handler functions. Using these names as arguments to the output and
+value interface functions works as intended. When using a default handler, the default handler will
+be called.
Output values will not trigger corresponding input event handlers unless the channel is mapped
back in the MIDIMonster configuration. This is intentional.