Faun 0.2.5
A high-level C audio library
Loading...
Searching...
No Matches
feed.c

This example shows how to feed a stream with a callback function.

/*
* Feed stream example.
*/
#include <math.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#define sleep(sec) Sleep((sec)*1000)
#else
#include <unistd.h>
#endif
#include "faun.h"
typedef struct {
float wform, pan;
} Clocks;
int feedAudio(float* samples, int frames, void* user)
{
Clocks* clocks = (Clocks*) user;
const float winc = 3.141592653f * 0.01f;
const float pinc = 3.141592653f * 0.00001f;
float v, p;
//printf("feedAudio %d\n", frames);
for (int i = 0; i < frames; ++i) {
v = sinf(clocks->wform) * 0.5f;
clocks->wform += winc;
p = cosf(clocks->pan);
clocks->pan += pinc;
*samples++ = v * p;
*samples++ = v * (1.0f - p);
}
return 0;
}
int main(int argc, char** argv)
{
Clocks clocks;
const int sourceCount = 0;
const int st0 = sourceCount;
const char* error;
uint32_t pid;
int rc = 0;
if ((error = faun_startup(0, sourceCount, 2, 0, "Faun Test"))) {
fprintf(stderr, "faun_startup: %s\n", error);
return 1;
}
clocks.wform = clocks.pan = 0.0f;
pid = faun_feedStream(st0, feedAudio, &clocks);
if (pid) {
sleep(10);
faun_control(FAUN_PID_SOURCE(pid), 1, FC_STOP);
sleep(1);
} else {
fprintf(stderr, "faun_feedStream failed\n");
rc = 1;
}
faun_shutdown();
return rc;
}
The Faun programmer interface.
#define FAUN_PID_SOURCE(pid)
Get the source index from a playback identifier.
Definition faun.h:82
@ FC_STOP
Halt playback and deactivate source.
Definition faun.h:24