aboutsummaryrefslogtreecommitdiff
path: root/preset_work.c
blob: 0d25cfd2dc015ff4f25497d2d2714d2b53f8d3c1 (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
#include <stddef.h>
#include <stdlib.h>

#include <jit_api.h>
#include <jit_util.h>

static void
_produce_val(jit_t *const jit)
{
	float *val;
	const size_t len = sizeof(float);

	while( (val = jit_work_write_request(jit, len)) )
	{
		*val = (float)rand() / RAND_MAX - 0.5f;

		jit_work_write_advance(jit, len);
	}
}

static void *
init(jit_t *const jit)
{
	// pre-fill ringbuffer with noise
	_produce_val(jit);

	return NULL;
}

static float
_drain_val(jit_t *const jit)
{
	float const *val;
	size_t len;
	float noise = 0.f;

	if( (val = jit_work_read_request(jit, &len)) )
	{
		noise = *val;

		jit_work_read_advance(jit);
	}
	else
	{
		jit_printf(jit, "[%s] buffer underrun", __func__);
	}

	return noise;
}

static void
_wake_worker(jit_t *const jit)
{
	if(jit_work_write_request(jit, 0))
	{
		jit_work_write_advance(jit, 0);
	}
	else
	{
		jit_printf(jit, "[%s] buffer overrun", __func__);
	}
}

static void
run(jit_t *const jit, void *const data JIT_UNUSED)
{
	_wake_worker(jit);

	for(uint32_t n = 0; n < jit->nchannels; n++)
	{
		for(uint32_t i = 0; i < jit->nsamples; i++)
		{
			float const noise = _drain_val(jit);

			jit->audio_out[n][i] = noise;
		}
	}
}

static void
_drain_dummy(jit_t *const jit)
{
	size_t len;

	while(jit_work_read_request(jit, &len))
	{
		jit_work_read_advance(jit);
	}
}

static void
work(jit_t *const jit, void *const data JIT_UNUSED)
{
	_drain_dummy(jit);
	// re-fill ringbuffer with noise
	_produce_val(jit);
}

JIT_EXPORT jit_desc_t const jit_desc = {
	.init = init,
	.run = run,
	.work = work
};

// vim: set syntax=c:
// jit: gcc -std=c11 -ffast-math -fvisibility=hidden -O3 -shared -fpic -fPIC -Wall -Wextra -Wpedantic -Wl,-z,defs -I %b -o %o -xc %i -lm 2>&1: