aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorcbdev <cb@cbcdn.com>2020-03-21 19:00:28 +0100
committercbdev <cb@cbcdn.com>2020-03-21 19:00:28 +0100
commitd1baab6ce4b13c562867147ed60906dfe651ae81 (patch)
tree6966f70aa2bf0e0dc33c575949dd1475798780db
parent5cc2ea308632c335f347d7a1002123c7152fd851 (diff)
downloadmidimonster-d1baab6ce4b13c562867147ed60906dfe651ae81.tar.gz
midimonster-d1baab6ce4b13c562867147ed60906dfe651ae81.tar.bz2
midimonster-d1baab6ce4b13c562867147ed60906dfe651ae81.zip
Enable setting channel values at load time for lua
-rw-r--r--backends/lua.c29
-rw-r--r--backends/lua.h1
2 files changed, 20 insertions, 10 deletions
diff --git a/backends/lua.c b/backends/lua.c
index b71ca97..7f80cc7 100644
--- a/backends/lua.c
+++ b/backends/lua.c
@@ -17,9 +17,8 @@ static lua_timer* timer = NULL;
uint64_t timer_interval = 0;
#ifdef MMBACKEND_LUA_TIMERFD
static int timer_fd = -1;
-#else
-static uint64_t last_timestamp;
#endif
+static uint64_t last_timestamp = 0;
static size_t threads = 0;
static lua_thread* thread = NULL;
@@ -224,7 +223,6 @@ static int lua_callback_output(lua_State* interpreter){
size_t n = 0;
channel_value val;
const char* channel_name = NULL;
- channel* channel = NULL;
instance* inst = NULL;
lua_instance_data* data = NULL;
@@ -243,15 +241,21 @@ static int lua_callback_output(lua_State* interpreter){
channel_name = lua_tostring(interpreter, 1);
val.normalised = clamp(luaL_checknumber(interpreter, 2), 1.0, 0.0);
+ //if not started yet, create any requested channels so scripts may set them at load time
+ if(!last_timestamp && channel_name){
+ lua_channel(inst, (char*) channel_name, mmchannel_output);
+ }
+
//find correct channel & output value
for(n = 0; n < data->channels; n++){
if(!strcmp(channel_name, data->channel[n].name)){
- channel = mm_channel(inst, n, 0);
- if(!channel){
- return 0;
- }
- mm_channel_event(channel, val);
data->channel[n].out = val.normalised;
+ if(!last_timestamp){
+ data->channel[n].mark = 1;
+ }
+ else{
+ mm_channel_event(mm_channel(inst, n, 0), val);
+ }
return 0;
}
}
@@ -589,6 +593,7 @@ static int lua_start(size_t n, instance** inst){
size_t u, p;
lua_instance_data* data = NULL;
int default_handler;
+ channel_value v;
//resolve channels to their handler functions
for(u = 0; u < n; u++){
@@ -608,6 +613,11 @@ static int lua_start(size_t n, instance** inst){
if(!data->default_handler){
data->channel[p].reference = lua_resolve_symbol(data->interpreter, data->channel[p].name);
}
+ //push initial values
+ if(data->channel[p].mark){
+ v.normalised = data->channel[p].out;
+ mm_channel_event(mm_channel(inst[u], p, 0), v);
+ }
}
}
@@ -617,9 +627,8 @@ static int lua_start(size_t n, instance** inst){
if(mm_manage_fd(timer_fd, BACKEND_NAME, 1, NULL)){
return 1;
}
- #else
- last_timestamp = mm_timestamp();
#endif
+ last_timestamp = mm_timestamp();
return 0;
}
diff --git a/backends/lua.h b/backends/lua.h
index 3c7568b..4583dfe 100644
--- a/backends/lua.h
+++ b/backends/lua.h
@@ -27,6 +27,7 @@ typedef struct /*_lua_channel*/ {
int reference;
double in;
double out;
+ uint8_t mark;
} lua_channel_data;
typedef struct /*_lua_instance_data*/ {