diff options
Diffstat (limited to 'plat.c')
-rw-r--r-- | plat.c | 93 |
1 files changed, 92 insertions, 1 deletions
@@ -1,5 +1,6 @@ -#include "plat.h" #include "config.h" +#include "memory.h" +#include "plat.h" #ifdef plat_posix #define _POSIX_SOURCE @@ -10,6 +11,7 @@ #include <stdlib.h> #include <dirent.h> #include <string.h> +#include <pthread.h> extern int fileno(FILE*); @@ -91,11 +93,100 @@ void iter_dir(const char* path, Dir_Iter fn, void* u) { closedir(di); } +pthread_t audio_thread; +pthread_mutex_t audio_mutex; + +#define inline /* erm sir we are using C90 here... */ +#include <pulse/simple.h> + +struct { + unsigned char* buf; + pa_simple* dev; + int chan, r; +} audio; + +extern int sound_mix(void*, unsigned char*, int); + +void* audio_worker(void* arg) { + const int s = audio_buffer_size; + int c = 1, as; + unsigned char* buf = audio.buf; + pa_simple* dev = audio.dev; + (void)arg; + while (c) { + lock_audio(); + if (!audio.r) c = 0; + as = sound_mix(arg, buf, s); + if (!as) audio.r = c = 0; + unlock_audio(); + pa_simple_write(dev, buf, as, 0); + } + return 0; +} + +void stop_audio(void) { + lock_audio(); + audio.r = 0; + unlock_audio(); +} + +void wait_audio(void) { + pthread_mutex_destroy(&audio_mutex); + pthread_join(audio_thread, 0); +} + +void init_audio(void* uptr, int sample, int channels) { + int r; + if (audio.dev) { + wait_audio(); + gfree(audio.buf); + } + audio.buf = galloc(audio_buffer_size * channels); + pa_sample_spec sp = { 0 }; + sp.format = PA_SAMPLE_S32LE; + sp.channels = channels; + sp.rate = sample; + audio.dev = pa_simple_new( + 0, + app_name, + PA_STREAM_PLAYBACK, + 0, + "Twink Player audio", + &sp, + 0, + 0, + 0 + ); + audio.r = 0; + if (!audio.dev) { + print_err("Failed to create audio device.\n"); + return; + } + audio.r = 1; + audio.chan = channels; + r = pthread_create( + &audio_thread, + 0, + audio_worker, + uptr + ); + pthread_mutex_init(&audio_mutex, 0); +} + +void lock_audio() { + pthread_mutex_lock(&audio_mutex); +} + +void unlock_audio() { + pthread_mutex_unlock(&audio_mutex); +} + extern int prog_main(void*); int main() { int r; void* mem; + audio.dev = 0; mem = malloc(memory_size); r = prog_main(mem); free(mem); |