aboutsummaryrefslogtreecommitdiff
path: root/src/audio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio.c')
-rw-r--r--src/audio.c223
1 files changed, 223 insertions, 0 deletions
diff --git a/src/audio.c b/src/audio.c
new file mode 100644
index 0000000..cbe5be3
--- /dev/null
+++ b/src/audio.c
@@ -0,0 +1,223 @@
+#include <SDL3/SDL_log.h>
+
+#include "audio.h"
+
+struct pw_audio *pw_audio_init(void) {
+ // PICO-8 exposes a `_mix(len0, len1)` function for copying audio data
+ // from an internal buffer to the host's audio context. From the
+ // Emscripten bundle, we see a few things:
+ //
+ // - It takes two lengths, one is the number of samples at the internal
+ // sample rate, and the other at the external sample rate
+ // - The internal sample rate is always 22050 Hz and uses 16-bit signed
+ // integer samples
+ //
+ // For us, this means we can let SDL do all the resampling for us and
+ // just choose a format and sample rate that matches PICO-8, meaning no
+ // resampling is done by the engine, and we can just call `_mix(l, l)`.
+ SDL_AudioSpec spec = {
+ .format = SDL_AUDIO_S16,
+ .channels = 1,
+ .freq = 22050,
+ };
+ SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(
+ SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL);
+ if (!stream) {
+ return NULL;
+ }
+
+ struct pw_audio *audio = SDL_calloc(1, sizeof(*audio));
+ if (!audio) {
+ SDL_DestroyAudioStream(stream);
+ return NULL;
+ }
+
+ audio->stream = stream;
+ return audio;
+}
+
+void pw_audio_free(struct pw_audio *audio) {
+ if (!audio) {
+ SDL_InvalidParamError("error");
+ return;
+ }
+
+ if (audio->stream) {
+ SDL_DestroyAudioStream(audio->stream);
+ }
+ SDL_free(audio);
+}
+
+bool pw_audio_start(struct pw_audio *audio, uint32_t size) {
+ if (!audio || !audio->stream || audio->size) {
+ return SDL_InvalidParamError("audio");
+ }
+
+ bool ok = SDL_ResumeAudioStreamDevice(audio->stream);
+ if (ok) {
+ audio->size = size;
+ }
+
+ return ok;
+}
+
+static void destroy_stream(struct pw_audio *audio) {
+ SDL_DestroyAudioStream(audio->stream);
+ audio->stream = NULL;
+ audio->size = 0;
+}
+
+bool pw_audio_stop(struct pw_audio *audio) {
+ if (!audio || !audio->stream) {
+ return SDL_InvalidParamError("audio");
+ }
+
+ if (!audio->size) {
+ return true;
+ }
+
+ // If we fail to pause and clear the stream, then there is clearly
+ // something wrong with it, so mark the stream as destroyed meaning
+ // our audio device is dead.
+ if (!SDL_PauseAudioStreamDevice(audio->stream) ||
+ !SDL_ClearAudioStream(audio->stream)) {
+ SDL_LogCritical(0, "audio stream cannot be stopped");
+ destroy_stream(audio);
+ return false;
+ }
+
+ audio->size = 0;
+ return true;
+}
+
+bool pw_audio_ready(struct pw_audio *audio) {
+ if (!audio) {
+ return SDL_InvalidParamError("error");
+ }
+
+ return audio->stream && audio->size;
+}
+
+static bool unlock_stream(struct pw_audio *audio) {
+ bool ok = SDL_UnlockAudioStream(audio->stream);
+
+ // If we get in this state, something is seriously weird, and means
+ // we will likely not recover audio. Log and close the audio device
+ // to prevent further use.
+ if (!ok) {
+ SDL_LogCritical(0, "audio stream mutex poisoned: %s", SDL_GetError());
+ destroy_stream(audio);
+ }
+
+ return ok;
+}
+
+static int16_t no_samples[] = {0};
+
+int16_t *pw_audio_lock(struct pw_audio *audio, uint32_t *len) {
+ if (!audio || !audio->stream || !audio->size || audio->lock) {
+ SDL_InvalidParamError("audio");
+ return NULL;
+ }
+ if (!len) {
+ SDL_InvalidParamError("len");
+ return NULL;
+ }
+
+ // Pick a buffer to use for collecting samples, since the `inuse` flag
+ // is reset by an audio callback, we need to make sure to hold a lock
+ // to the audio device to prevent threading issues.
+ if (!SDL_LockAudioStream(audio->stream)) {
+ return false;
+ }
+
+ int16_t *data = NULL;
+ uint32_t request = 0;
+
+ int queued = SDL_GetAudioStreamQueued(audio->stream);
+ if (queued < 0) {
+ goto cleanup;
+ }
+
+ uint32_t q =
+ (uint32_t)SDL_min(queued, INT32_MAX) / sizeof(*audio->buffers->data);
+ if (q >= audio->size) {
+ data = no_samples;
+ goto cleanup;
+ }
+
+ size_t i = 0;
+ struct pw_audio_buffer *buffer = NULL;
+ for (i = 0; i < SDL_arraysize(audio->buffers); ++i) {
+ if (!audio->buffers[i].inuse) {
+ buffer = &audio->buffers[i];
+ break;
+ }
+ }
+ if (!buffer) {
+ SDL_SetError("no remaining audio buffers");
+ goto cleanup;
+ }
+
+ data = buffer->data;
+ request = SDL_min(audio->size - q, PW_AUDIO_BUFFER_MAX);
+
+ audio->lock = i * PW_AUDIO_BUFFER_MAX + request;
+ buffer->inuse = true;
+
+cleanup:
+ if (!unlock_stream(audio)) {
+ return NULL;
+ }
+
+ if (data) {
+ *len = request;
+ }
+ return data;
+}
+
+static void data_complete(void *userdata, const void *buf, int buflen) {
+ (void)buflen;
+
+ struct pw_audio *audio = userdata;
+ for (size_t i = 0; i < SDL_arraysize(audio->buffers); ++i) {
+ struct pw_audio_buffer *b = audio->buffers + i;
+ if (b->data == buf && b->inuse) {
+ b->inuse = false;
+ return;
+ }
+ }
+
+ SDL_LogWarn(0, "could not find completed in-use buffer");
+}
+
+bool pw_audio_unlock(struct pw_audio *audio) {
+ if (!audio || !audio->stream || !audio->size || !audio->lock) {
+ return SDL_InvalidParamError("audio");
+ }
+
+ size_t i = audio->lock / PW_AUDIO_BUFFER_MAX;
+ size_t len = audio->lock % PW_AUDIO_BUFFER_MAX;
+
+ if (!SDL_LockAudioStream(audio->stream)) {
+ return false;
+ }
+
+ bool ok = false;
+
+ struct pw_audio_buffer *buffer = audio->buffers + i;
+ if (!buffer->inuse) {
+ goto cleanup;
+ }
+
+ size_t sz = len * sizeof(*buffer->data);
+ ok = SDL_PutAudioStreamDataNoCopy(audio->stream, buffer->data, sz,
+ data_complete, audio);
+ if (!ok) {
+ buffer->inuse = false;
+ }
+
+cleanup:
+ audio->lock = 0;
+ return unlock_stream(audio) && ok;
+}