aboutsummaryrefslogtreecommitdiff
path: root/mod/interp.c
blob: c5bbefb0582c94fdb612b5c0c8bb087ba71bf57f (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * 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 <string.h>
#include <lauxlib.h>
#include "melisma.h"
#include "mutil.h"

#define INTERP_TYPENAME "interp"
#define DEFAULT_METHOD "linear"

struct interp;
typedef float (*smp_fn)(float);

enum {
	LINEAR,
	EASEIN,
	EASEOUT,
	SMOOTHSTEP,
};

struct interp
{
	ml_channel *out;
	smp_fn      smp;
	float       src;
	float       dst;
	size_t      t0;
	size_t      t;
	int         method;
};

static float sample(struct interp *ip)
{
	float x = ip->t0 == 0 ? 0 : (float)ip->t / (float)ip->t0;
	switch (ip->method)
	{
	case LINEAR:
		break;
	case EASEIN:
		x = x*x;
		break;
	case EASEOUT:
		x = 1 -x;
		x = 1 - x*x;
		break;
	case SMOOTHSTEP:
		x = x*x*(3 - 2*x);
		break;
	}
	return ip->src + x*(ip->dst - ip->src);
}

static void proc(const ml_proc_args *args)
{
	struct interp *ip = args->userp;

	float *out = args->out[0];

	for (size_t i = 0; i < args->nframes; i++)
	{
		out[i] = sample(ip);

		if (ip->t == ip->t0)
		{
			ip->t = 0;
			ip->t0 = 0;
			ip->src = ip->dst;
		}
		else
		{
			ip->t++;
		}
	}
}

static int l_init(lua_State *s)
{
	float v = luaL_checknumber(s,1);

	struct interp *ip = calloc(1,sizeof(*ip));
	ip->src = v;
	ip->dst = v;
	ip->method = LINEAR;

	ml_node *node = ml_create_node(proc,ip);
	ml_label_node(node,"interp");
	ip->out = ml_add_output(node);

	*(void**) lua_newuserdata(s,sizeof(void*)) = ip;
	luaL_setmetatable(s,INTERP_TYPENAME);

	return 1;
}

static int l_set(lua_State *s)
{
	struct interp *ip = *(void**) luaL_checkudata(s,1,INTERP_TYPENAME);
	float v = luaL_checknumber(s,2);

	ip->src = v;
	ip->dst = v;
	ip->t0 = 0;
	ip->t = 0;

	return 0;
}

static int l_target(lua_State *s)
{
	int method_i = lua_upvalueindex(1);
	int method = luaL_checkinteger(s,method_i);
	struct interp *ip = *(void**) luaL_checkudata(s,1,INTERP_TYPENAME);
	float v = luaL_checknumber(s,2);
	float t = luaL_checknumber(s,3);

	size_t sr = ml_sample_rate();

	ip->src = sample(ip);
	ip->dst = v;
	ip->t0 = sr*t;
	ip->t = 0;
	ip->method = method;

	return 0;
}

static int l_sample(lua_State *s)
{
	struct interp *ip = *(void**) luaL_checkudata(s,1,INTERP_TYPENAME);
	lua_pushnumber(s,sample(ip));
	return 1;
}

static int l_out(lua_State *s)
{
	struct interp *ip = *(void**) luaL_checkudata(s,1,INTERP_TYPENAME);
	*(void**) lua_newuserdata(s,sizeof(void*)) = ip->out;
	luaL_setmetatable(s,MELISMA_OUTPUT_CHANNEL_TYPENAME);
	return 1;
}

int luaopen_melisma_interp(lua_State *s)
{
	ml_check_version();

/*** Interpolation.

	local minterp = require 'melisma.interp'
	local mevent = require 'melisma.event'
	local mgain = require 'melisma.gain'

	local interp = minterp.init(0)

	interp:linear(1,1)

	mevent.schedule(0.5, function()
		print('current value:', interp:sample())
	end)

	local sig = ...
	local gain = mgain.linear(sig,interp)

@classmod interp
*/
	lua_newtable(s);
	int tbl = lua_gettop(s);

/*** Initialize an interpolation unit.
@static
@function init
@tparam number x0 Initial value.
@treturn interp The newly created interpolation unit.
*/
	lua_pushcfunction(s,l_init);
	lua_setfield(s,tbl,"init");

/*** Set value directly.
@function set
@tparam number x New value.
*/
	lua_pushcfunction(s,l_set);
	lua_setfield(s,tbl,"set");

/*** Interpolate using linear interpolation.
@function linear
@tparam number v Target value.
@tparam number t Interpolation time.
*/
	lua_pushnumber(s,LINEAR);
	lua_pushcclosure(s,l_target,1);
	lua_setfield(s,tbl,"linear");


/*** Interpolate using ease-in interpolation.
@function ease_in
@tparam number v Target value.
@tparam number t Interpolation time.
*/
	lua_pushnumber(s,EASEIN);
	lua_pushcclosure(s,l_target,1);
	lua_setfield(s,tbl,"ease_in");


/*** Interpolate using ease-out interpolation.
@function ease_out
@tparam number v Target value.
@tparam number t Interpolation time.
*/
	lua_pushnumber(s,EASEOUT);
	lua_pushcclosure(s,l_target,1);
	lua_setfield(s,tbl,"ease_out");

/*** Interpolate using smoothstep interpolation.
@function smoothstep
@tparam number v Target value.
@tparam number t Interpolation time.
*/
	lua_pushnumber(s,SMOOTHSTEP);
	lua_pushcclosure(s,l_target,1);
	lua_setfield(s,tbl,"smoothstep");

/*** Get the current value.
@function sample
@treturn number The current value.
*/
	lua_pushcfunction(s,l_sample);
	lua_setfield(s,tbl,"sample");

	lua_pushcfunction(s,l_out);
	lua_setfield(s,tbl,MUTIL_OUTPUT_CHANNEL_FUNCTION);

	luaL_newmetatable(s,INTERP_TYPENAME);
	int mt = lua_gettop(s);

	lua_pushvalue(s,tbl);
	lua_setfield(s,mt,"__index");

	lua_settop(s,tbl);
	return 1;
}