summaryrefslogtreecommitdiff
path: root/examples/callback.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/callback.c')
-rw-r--r--examples/callback.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/examples/callback.c b/examples/callback.c
new file mode 100644
index 0000000..bf7e187
--- /dev/null
+++ b/examples/callback.c
@@ -0,0 +1,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;
+}