summaryrefslogtreecommitdiff
path: root/examples/callback.c
blob: bf7e18791be14ce5968690377f3714e48060599d (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
#include <stdio.h>
#include <math.h>
#include <mossrose.h>
#include <portaudio.h>

#define PI 3.14159


#define SAMPLE_RATE 44100
#define N_CHANNELS 8

float f(float t)
{
	const int f0 = 440;
	const int f1 = 880;
	return ( t*f1 ) + ( (1-t)*f0 );
}


void callback(int chan, void *d)
{
	bool *loop = d;
	*loop = false;
	printf("channel %d waves goodnight!\n", chan);
}


int main()
{
	float data[SAMPLE_RATE];
	for (long i=0; i<SAMPLE_RATE; i++) {
		float time = ((float)i)/SAMPLE_RATE;
		data[i] = sin(2*PI*f(time)*time);
	}

	struct mossrose_sound_t sound = {
		.left = data, .right = NULL, .mono = true, .len = SAMPLE_RATE
	};

	int err = mossrose_init(SAMPLE_RATE, N_CHANNELS, true);
	if (err != 0)
		fprintf(stderr, "FAILED TO INITIALIZE MOSSROSE\n");

	int chan = mossrose_play(&sound, -1, 1);

	bool loop = true;
	mossrose_channel_set_callback(chan, callback, &loop);

	while(loop) {
		mossrose_poll_callbacks();
	}

	mossrose_terminate();
	return 0;
}