summaryrefslogtreecommitdiff
path: root/src/libs/sig/sig.c
blob: 203a37a973b073fb0ae5682f6221640e14800b6e (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>

#include "sig.h"

/* This is to try ensure ctxt's alignment accomodates all the base type sizes,
 * it may waste some space in sig_t but since the caller supplies just a size
 * via the supplied sig_ops_t.size(), we know nothing of the alignment reqs.
 *
 * XXX: If callers start using other types like xmmintrinsics __m128, this
 * struct will have to get those added.
 */
typedef union sig_context_t {
	float		f;
	double		d;
	long double	ld;
	char		c;
	short		s;
	int		i;
	long		l;
	long long	ll;
	void		*p;
} sig_context_t;

typedef struct sig_t {
	const sig_ops_t	*ops;
	unsigned	refcount;
	sig_context_t	ctxt[];
} sig_t;


/* return a new signal generator of ops type, configured according to va_list */
sig_t * sig_new(const sig_ops_t *ops, ...)
{
	static const sig_ops_t	null_ops;
	size_t			ctxt_size = 0;
	sig_t			*sig;
	va_list			ap;

	if (!ops)
		ops = &null_ops;

	va_start(ap, ops);
	if (ops->size)
		ctxt_size = ops->size(ap);
	va_end(ap);

	sig = calloc(1, sizeof(sig_t) + ctxt_size);
	if (!sig)
		return NULL;

	va_start(ap, ops);
	if (ops->init)
		ops->init(&sig->ctxt, ap);
	va_end(ap);

	sig->ops = ops;
	sig->refcount = 1;

	return sig;
}


/* add a reference to an existing signal generator,
 * free/unref using sig_free() just like it had been returned by sig_new()
 */
sig_t * sig_ref(sig_t *sig)
{
	assert(sig);

	sig->refcount++;

	return sig;
}


/* free a signal generator, returns sig if sig still has references,
 * otherwise returns NULL
 */
sig_t * sig_free(sig_t *sig)
{
	if (sig) {
		assert(sig->refcount > 0);

		sig->refcount--;
		if (sig->refcount)
			return sig;

		if (sig->ops->destroy)
			sig->ops->destroy(&sig->ctxt);

		free(sig);
	}

	return NULL;
}


/* produce the value for time ticks_ms from the supplied signal generator,
 * the returned value should always be kept in the range 0-1.
 */
float sig_output(sig_t *sig, unsigned ticks_ms)
{
	assert(sig);
	assert(sig->ops);

	if (sig->ops->output)
		return sig->ops->output(sig->ctxt, ticks_ms);

	return 0;
}


/* What follows is a bunch of convenience wrappers around the bundled
 * sig_ops implementations.  One may call sig_new() directly supplying
 * the sig_ops_$op and appropriate va_args, which is important for
 * supporting custom caller-implemented sig_ops in particular.
 * However, it's desirable to use these helpers when possible, as
 * they offer type checking of the arguments, as well less verbosity.
 */

sig_t * sig_new_abs(sig_t *x)
{
	return sig_new(&sig_ops_abs, x);
}


sig_t * sig_new_add(sig_t *a, sig_t *b)
{
	return sig_new(&sig_ops_add, a, b);
}


sig_t * sig_new_ceil(sig_t *x)
{
	return sig_new(&sig_ops_ceil, x);
}


sig_t * sig_new_clamp(sig_t *x, sig_t *min, sig_t *max)
{
	return sig_new(&sig_ops_clamp, x, min, max);
}


sig_t * sig_new_const(float x)
{
	return sig_new(&sig_ops_const, x);
}


sig_t * sig_new_div(sig_t *a, sig_t *b)
{
	return sig_new(&sig_ops_div, a, b);
}


sig_t * sig_new_expand(sig_t *x)
{
	return sig_new(&sig_ops_expand, x);
}


sig_t * sig_new_floor(sig_t *x)
{
	return sig_new(&sig_ops_floor, x);
}


sig_t * sig_new_inv(sig_t *x)
{
	return sig_new(&sig_ops_inv, x);
}


sig_t * sig_new_lerp(sig_t *a, sig_t *b, sig_t *t)
{
	return sig_new(&sig_ops_lerp, a, b, t);
}


sig_t * sig_new_max(sig_t *a, sig_t *b)
{
	return sig_new(&sig_ops_max, a, b);
}


sig_t * sig_new_min(sig_t *a, sig_t *b)
{
	return sig_new(&sig_ops_min, a, b);
}


sig_t * sig_new_mult(sig_t *a, sig_t *b)
{
	return sig_new(&sig_ops_mult, a, b);
}


sig_t * sig_new_neg(sig_t *x)
{
	return sig_new(&sig_ops_neg, x);
}


sig_t * sig_new_pow(sig_t *x, sig_t *y)
{
	return sig_new(&sig_ops_pow, x, y);
}


sig_t * sig_new_rand(void)
{
	return sig_new(&sig_ops_rand);
}


sig_t * sig_new_round(sig_t *x)
{
	return sig_new(&sig_ops_round, x);
}


sig_t * sig_new_scale(sig_t *x, sig_t *min, sig_t *max)
{
	return sig_new(&sig_ops_scale, x, min, max);
}


sig_t * sig_new_sin(sig_t *hz)
{
	return sig_new(&sig_ops_sin, hz);
}


sig_t * sig_new_sqr(sig_t *hz)
{
	return sig_new(&sig_ops_sqr, hz);
}


sig_t * sig_new_tri(sig_t *hz)
{
	return sig_new(&sig_ops_tri, hz);
}


sig_t * sig_new_sub(sig_t *a, sig_t *b)
{
	return sig_new(&sig_ops_sub, a, b);
}


#ifdef TESTING

#include <stdio.h>

int main(int argc, char *argv[])
{
	sig_t	*sig;

	sig = sig_new(NULL);
	printf("null output=%f\n", sig_output(sig, 0));
	sig = sig_free(sig);

	sig = sig_new(&sig_ops_rand);
	for (unsigned j = 0; j < 2; j++) {
		for (unsigned i = 0; i < 10; i++)
			printf("rand j=%u i=%u output=%f\n", j, i, sig_output(sig, i));
	}

	sig = sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 2.f));
	for (unsigned i = 0; i < 1000; i++)
		printf("sin 2hz output %i=%f\n", i, sig_output(sig, i));
	sig = sig_free(sig);

	sig = sig_new(&sig_ops_mult,
		      sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 1.f)),	/* LFO @ 1hz */
		      sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 100.f))	/* oscillator @ 100hz */
		);
	for (unsigned i = 0; i < 1000; i++)
		printf("sin 100hz * 1hz output %i=%f\n", i, sig_output(sig, i));
	sig = sig_free(sig);

	sig =	sig_new(&sig_ops_pow,								/* raise an ... */
			sig_new(&sig_ops_sin,							/* oscillator ... */
				sig_new(&sig_ops_const, 10.f)),					/* @ 10hz, */
			sig_new(&sig_ops_round,							/* to a rounded .. */
				sig_new(&sig_ops_mult, sig_new(&sig_ops_const, 50.f),		/* 50 X ... */
					sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 1.f))	/* 1hz oscillator */
				)
			)
		);
	for (unsigned i = 0; i < 1000; i++)
		printf("sin 10hz ^ (sin 1hz * 50) output %i=%f\n", i, sig_output(sig, i));
	sig = sig_free(sig);

	sig =	sig_new(&sig_ops_scale,								/* scale a */
			sig_new(&sig_ops_lerp,							/* linear interpolation */
				sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 10.f)),		/* between one 10hz oscillator */
				sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 33.f)),		/* and another 33hz oscillator */
				sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 2.f))		/* weighted by a 2hz oscillator */
			),
			sig_new(&sig_ops_const, -100.f), sig_new(&sig_ops_const, 100.f)		/* to the range -100 .. +100 */
		);
	for (unsigned i = 0; i < 1000; i++)
		printf("scale(lerp(sin(10hz), sin(33hz), sin(2hz)), -100, +100)  output %i=%f\n", i, sig_output(sig, i));
	sig = sig_free(sig);

}

#endif
© All Rights Reserved