1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*
* melisma - extensible and scriptable audio engine
* Copyright (C) 2026 Ludvig Gunne Lindström
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <lauxlib.h>
#include "melisma.h"
#define TIMER_TYPENAME "timer"
struct timer
{
bool on;
uint64_t t;
};
static void proc(const ml_proc_args *args)
{
struct timer *tmr = args->userp;
if (tmr->on)
{
tmr->t += args->nframes;
}
}
static int l_init(lua_State *s)
{
struct timer *tmr = calloc(1,sizeof(*tmr));
tmr->on = false;
tmr->t = 0;
ml_node *node = ml_create_node(proc,tmr);
ml_channel *src = ml_add_output(node);
ml_channel *dst = ml_add_input(ml_get_root_node());
ml_connect(src,dst);
*(void**) lua_newuserdata(s,sizeof(void*)) = tmr;
luaL_setmetatable(s,TIMER_TYPENAME);
return 1;
}
static int l_start(lua_State *s)
{
struct timer *tmr = *(void**) luaL_checkudata(s,1,TIMER_TYPENAME);
tmr->on = true;
return 0;
}
static int l_stop(lua_State *s)
{
struct timer *tmr = *(void**) luaL_checkudata(s,1,TIMER_TYPENAME);
tmr->on = false;
return 0;
}
static int l_reset(lua_State *s)
{
struct timer *tmr = *(void**) luaL_checkudata(s,1,TIMER_TYPENAME);
tmr->t = 0;
return 0;
}
static int l_read(lua_State *s)
{
struct timer *tmr = *(void**) luaL_checkudata(s,1,TIMER_TYPENAME);
double t = (double) tmr->t / (double) ml_sample_rate();
lua_pushnumber(s,t);
return 1;
}
int luaopen_melisma_timer(lua_State *s)
{
/*** Time measurement.
local mtimer = require 'melisma.timer'
local mkeycallback = require 'melisma.keycallback'
local timer = mtimer.init()
timer:start()
mkeycallback.set(function()
print(timer:read())
timer:reset()
end)
@classmod timer
*/
lua_newtable(s);
int tbl = lua_gettop(s);
/*** Create a new timer.
@static
@function init
@treturn timer
*/
lua_pushcfunction(s,l_init);
lua_setfield(s,tbl,"init");
/*** Start timer.
@function start
*/
lua_pushcfunction(s,l_start);
lua_setfield(s,tbl,"start");
/*** Stop timer.
@function stop
*/
lua_pushcfunction(s,l_stop);
lua_setfield(s,tbl,"stop");
/*** Reset timer.
@function reset
*/
lua_pushcfunction(s,l_reset);
lua_setfield(s,tbl,"reset");
/*** Read timer.
@function read
@treturn number Elapsed seconds.
*/
lua_pushcfunction(s,l_read);
lua_setfield(s,tbl,"read");
luaL_newmetatable(s,TIMER_TYPENAME);
int mt = lua_gettop(s);
lua_pushvalue(s,tbl);
lua_setfield(s,mt,"__index");
lua_settop(s,tbl);
return 1;
}
|