From 6433511a6586a9d8477a5c5ef417731b8feaaf95 Mon Sep 17 00:00:00 2001 From: headshot2017 Date: Tue, 27 Dec 2022 21:29:50 -0400 Subject: [PATCH] add test/audio.c and test/audio.h --- test/audio.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ test/audio.h | 6 +++++ 2 files changed, 75 insertions(+) create mode 100644 test/audio.c create mode 100644 test/audio.h diff --git a/test/audio.c b/test/audio.c new file mode 100644 index 0000000..071d77f --- /dev/null +++ b/test/audio.c @@ -0,0 +1,69 @@ +#include "audio.h" + +#include +#include +#include +#include + +#include "context.h" + +static SDL_AudioDeviceID dev; + +pthread_t gSoundThread; +long long timeInMilliseconds(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + + return(((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000); +} + +void* audio_thread(void* keepAlive) +{ + // from https://github.com/ckosmic/libsm64/blob/audio/src/libsm64.c#L535-L555 + // except keepAlive is a null pointer here, so don't use it + + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); + pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL); + + long long currentTime = timeInMilliseconds(); + long long targetTime = 0; + while(1) + { + //if(!*((bool*)keepAlive)) return NULL; + + int16_t audioBuffer[544 * 2 * 2]; + uint32_t numSamples = sm64_audio_tick(SDL_GetQueuedAudioSize(dev)/4, 1100, audioBuffer); + if (SDL_GetQueuedAudioSize(dev)/4 < 6000) + SDL_QueueAudio(dev, audioBuffer, numSamples * 2 * 4); + + targetTime = currentTime + 33; + while (timeInMilliseconds() < targetTime) + { + usleep(100); + //if(!*((bool*)keepAlive)) return NULL; + } + currentTime = timeInMilliseconds(); + } +} + +void audio_init() +{ + SDL_AudioSpec want, have; + SDL_zero(want); + want.freq = 32000; + want.format = AUDIO_S16; + want.channels = 2; + want.samples = 512; + want.callback = NULL; + dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0); + if (dev == 0) { + fprintf(stderr, "SDL_OpenAudio error: %s\n", SDL_GetError()); + return; + } + SDL_PauseAudioDevice(dev, 0); + + // it's best to run audio in a separate thread + pthread_create(&gSoundThread, NULL, audio_thread, NULL); +} + diff --git a/test/audio.h b/test/audio.h new file mode 100644 index 0000000..9f2d934 --- /dev/null +++ b/test/audio.h @@ -0,0 +1,6 @@ +#ifndef AUDIO_H +#define AUDIO_H + +void audio_init(); + +#endif \ No newline at end of file