summaryrefslogtreecommitdiff
path: root/src/mossrose.c
blob: fdaa10c65f13229cda476d74f18001f5c43279db (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
#include <stdio.h>
#include <portaudio.h>
#include "mossrose.h"


PaStream *stream;


struct audio_output_t {
	float l;
	float r;
};


static int callback(
	const void *input,
	void *output,
	unsigned long frame_count,
	const PaStreamCallbackTimeInfo *time_info,
	void *userdata)
{
	static float left = 0;
	static float right = 0;

	struct audio_output_t *out = output;
	for (int i=0; i<frame_count; i++) {
		out[i].l = left;
		out[i].r = right;
		left += 0.01;
		right += 0.03;
		if (left > 1) left -= 2;
		if (right > 1) right -= 2;
	}
	return 0;
}


int mr_init(double sample_rate, int n_channels)
{
	PaError err;

	err = Pa_Initialize();
	if (err != paNoError) {
		fprintf(stderr, "failed to initialize PortAudio!\n");
		return 1;
	}

	err = Pa_OpenDefaultStream(&stream, 0, 2, paFloat32, sample_rate, 0, callback, NULL);
	if (err != paNoError) {
		fprintf(stderr, "failed to open audio stream!\n");
		return 1;
	}

	err = Pa_StartStream(stream);
	if (err != paNoError) {
		fprintf(stderr, "failed to start audio stream!\n");
		return 1;
	}

	return 0;
};


int mr_terminate(double sample_rate, int n_channels)
{
	Pa_AbortStream(stream);
	Pa_CloseStream(stream);
	Pa_Terminate();
	return 0;
}