diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/CMakeLists.txt | 5 | ||||
-rw-r--r-- | examples/callback.c | 55 |
2 files changed, 60 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 7fe2030..aea5060 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -13,3 +13,8 @@ add_dependencies(examples panning) add_executable(loop ${CMAKE_CURRENT_LIST_DIR}/loop.c) target_link_libraries(loop mossrose) add_dependencies(examples loop) + + +add_executable(callback ${CMAKE_CURRENT_LIST_DIR}/callback.c) +target_link_libraries(callback mossrose) +add_dependencies(examples callback) 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; +} |