From 8ec3f8e82acd70410515550fd1790ee5827aafdb Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 26 Aug 2022 12:17:51 -0500 Subject: make sound --- src/mossrose.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 93 insertions(+), 17 deletions(-) (limited to 'src/mossrose.c') diff --git a/src/mossrose.c b/src/mossrose.c index fdaa10c..02f063a 100644 --- a/src/mossrose.c +++ b/src/mossrose.c @@ -1,17 +1,67 @@ #include +#include #include #include "mossrose.h" +#include "mossrose-mutex.h" +#include "mossrose-channel.h" -PaStream *stream; - +/* ~~~~~~~~~~~~~~~~ type definitions ~~~~~~~~~~~~~~~~ */ +/* audio output */ struct audio_output_t { float l; float r; }; + +/* ~~~~~~~~~~~~~~~~ globals ~~~~~~~~~~~~~~~~ */ + +struct mossrose_globals_t { + PaStream *stream; + struct mossrose_channel_t *channels; + int n_channels; +} mossrose_global; + + +struct audio_output_t build_sample() +{ + struct audio_output_t out; + out.l = 0; out.r = 0; + + /* loop variables */ + struct mossrose_channel_t *chan; + float chan_l, chan_r; + + for (int i=0; imutex)) != 0) { + /* can't lock the mutex, this channel is being modified */ + printf("can't lock channel %d\n", i); + continue; + } + + if (chan->n_samples == 0) { + /* channel is not currently in use, skip */ + } + else { + if (mossrose_channel_advance(&chan_l, &chan_r, chan) != 0) + /* channel is done playing, reset */ + mossrose_channel_reset(chan); + else { + out.l += chan_l; + out.r += chan_r; + } + } + + mossrose_mutex_unlock(&(chan->mutex)); + } + + return out; +} + + static int callback( const void *input, void *output, @@ -19,39 +69,49 @@ static int callback( const PaStreamCallbackTimeInfo *time_info, void *userdata) { - static float left = 0; - static float right = 0; - struct audio_output_t *out = output; + struct audio_output_t sample; + for (int i=0; i 1) left -= 2; - if (right > 1) right -= 2; + sample = build_sample(); + out[i].l = sample.l; + out[i].r = sample.r; } return 0; } -int mr_init(double sample_rate, int n_channels) +int mossrose_init(double sample_rate, int n_channels) { + /* initialize channels */ + mossrose_global.n_channels = n_channels; + mossrose_global.channels = malloc(n_channels * sizeof(struct mossrose_channel_t)); + if (mossrose_global.channels == NULL) { + fprintf(stderr, "failed to allocate memory for %d channels", n_channels); + return 1; + } + for (int i=0; i 0) { + return mossrose_channel_set(mossrose_global.channels+channel, left, right, len, 1); + } + else { + struct mossrose_channel_t *chan; + for (int i=0; i