diff options
author | sanine <sanine.not@pm.me> | 2022-08-31 14:34:38 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-08-31 14:34:38 -0500 |
commit | 10a652728f2bc8e5f01aedf97ccc849b8ac686ba (patch) | |
tree | 6a5c5960fc22bdd30a37cc6d9aacb02a5539f54f | |
parent | 48e6520f6517f196a0a8f39bf86159075ad44c6b (diff) |
update example.c and add panning example
-rw-r--r-- | examples/CMakeLists.txt | 5 | ||||
-rw-r--r-- | examples/example.c | 18 | ||||
-rw-r--r-- | examples/panning.c | 41 |
3 files changed, 60 insertions, 4 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 49dd924..46e02d4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -3,3 +3,8 @@ project(mossrose) add_executable(example ${CMAKE_CURRENT_LIST_DIR}/example.c) target_link_libraries(example mossrose) add_dependencies(examples example) + + +add_executable(panning ${CMAKE_CURRENT_LIST_DIR}/panning.c) +target_link_libraries(panning mossrose) +add_dependencies(examples panning) diff --git a/examples/example.c b/examples/example.c index 9ae3acc..5e65d82 100644 --- a/examples/example.c +++ b/examples/example.c @@ -20,22 +20,32 @@ int main() sin1[i] = sin(2*PI*f1*time); sin2[i] = sin(2*PI*f2*time); } - mossrose_init(SAMPLE_RATE, N_CHANNELS); + + struct mossrose_sound_t sound_440 = { + .left = sin1, .right = NULL, .mono = true, .len = SAMPLE_RATE + }; + struct mossrose_sound_t sound_512 = { + .left = sin2, .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"); Pa_Sleep(500); printf("play 1\n"); - mossrose_play(sin1, NULL, SAMPLE_RATE, -1); + mossrose_play(&sound_512, -1); printf("wait\n"); Pa_Sleep(1000); printf("play 2\n"); - mossrose_play(sin2, NULL, SAMPLE_RATE, -1); + mossrose_play(&sound_440, -1); printf("wait\n"); Pa_Sleep(500); printf("play 3\n"); - mossrose_play(NULL, sin1, SAMPLE_RATE, -1); + mossrose_play(&sound_512, -1); printf("wait\n"); Pa_Sleep(1000); diff --git a/examples/panning.c b/examples/panning.c new file mode 100644 index 0000000..7ee9a76 --- /dev/null +++ b/examples/panning.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#include <math.h> +#include <mossrose.h> +#include <portaudio.h> + +#define PI 3.14159 + + +#define SAMPLE_RATE 44100 +#define N_CHANNELS 8 + +int main() +{ + float sine_wave[8*SAMPLE_RATE]; + const int frequency = 440; + for (long i=0; i<8*SAMPLE_RATE; i++) { + float time = ((float)i)/SAMPLE_RATE; + sine_wave[i] = sin(2*PI*frequency*time); + } + + struct mossrose_sound_t sound = { + .left = sine_wave, .right = NULL, .mono = true, .len = 8*SAMPLE_RATE + }; + + int err = mossrose_init(SAMPLE_RATE, N_CHANNELS, true); + if (err != 0) + fprintf(stderr, "FAILED TO INITIALIZE MOSSROSE\n"); + + Pa_Sleep(500); + + int chan = mossrose_play(&sound, -1); + + for (int i=0; i<800; i++) { + float time = ((float)i)/100; + mossrose_channel_set_pan(chan, cos(2*PI*time), 0); + Pa_Sleep(10); + } + + mossrose_terminate(); + return 0; +} |