diff options
author | sanine <sanine.not@pm.me> | 2022-08-28 11:25:44 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-08-28 11:25:44 -0500 |
commit | 0d6ece00397ebb9215ccf1af06cce22c3a94197e (patch) | |
tree | 022cc796822a998a59d8bf8896f02599ce6d3700 /src/mossrose.c | |
parent | a4dd0ad63c00f4dee3b86dfd3075d1d61b2b3180 (diff) |
begin plibsys refactor
Diffstat (limited to 'src/mossrose.c')
-rw-r--r-- | src/mossrose.c | 132 |
1 files changed, 2 insertions, 130 deletions
diff --git a/src/mossrose.c b/src/mossrose.c index 4f31711..40f4393 100644 --- a/src/mossrose.c +++ b/src/mossrose.c @@ -1,149 +1,21 @@ #include <stdio.h> #include <stdlib.h> +#include <plibsys.h> #include <portaudio.h> #include <mossrose.h> -#include "mutex.h" #include "channel.h" -/* ~~~~~~~~~~~~~~~~ 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; i<mossrose_global.n_channels; i++) { - chan = mossrose_global.channels + i; - if (mossrose_mutex_trylock(&(chan->mutex)) != 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, - unsigned long frame_count, - const PaStreamCallbackTimeInfo *time_info, - void *userdata) -{ - struct audio_output_t *out = output; - struct audio_output_t sample; - - for (int i=0; i<frame_count; i++) { - sample = build_sample(); - out[i].l = sample.l; - out[i].r = sample.r; - } - return 0; -} - - 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<n_channels; i++) { - mossrose_channel_init(mossrose_global.channels + i); - } - - PaError err; - - /* initialize portaudio */ - err = Pa_Initialize(); - if (err != paNoError) { - fprintf(stderr, "failed to initialize PortAudio!\n"); - return 1; - } - - /* open stream */ - err = Pa_OpenDefaultStream(&(mossrose_global.stream), 0, 2, paFloat32, sample_rate, 0, callback, NULL); - if (err != paNoError) { - fprintf(stderr, "failed to open audio stream!\n"); - return 1; - } - - /* start stream */ - err = Pa_StartStream(mossrose_global.stream); - if (err != paNoError) { - fprintf(stderr, "failed to start audio stream!\n"); - return 1; - } - - return 0; }; -int mossrose_play(float *left, float *right, size_t len, int channel) +int mossrose_play(struct mossrose_sound_t *sound, int channel) { - if (channel > 0) { - if (mossrose_channel_set(mossrose_global.channels+channel, left, right, len, 1) == 0) - return channel; - else - return -1; - } - else { - struct mossrose_channel_t *chan; - for (int i=0; i<mossrose_global.n_channels; i++) { - chan = mossrose_global.channels + i; - if (mossrose_channel_set(chan, left, right, len, 0) == 0) return i; - } - return -1; - } } int mossrose_terminate(double sample_rate, int n_channels) { - Pa_AbortStream(mossrose_global.stream); - Pa_CloseStream(mossrose_global.stream); - Pa_Terminate(); - return 0; } |