#include #include #include "audio.h" struct pw_audio *pw_audio_init(void) { struct pw_audio *audio = SDL_calloc(1, sizeof(*audio)); if (!audio) { return NULL; } // 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, }; audio->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL); if (!audio->stream) { SDL_LogWarn(0, "starting without audio"); } 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_buffer *buffer = userdata; if (buffer->data != buf || !buffer->inuse) { SDL_LogWarn(0, "unexpected audio buffer completion"); return; } buffer->inuse = false; } 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 - 1) / PW_AUDIO_BUFFER_MAX; size_t len = (audio->lock - 1) % PW_AUDIO_BUFFER_MAX + 1; 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); #if SDL_VERSION_ATLEAST(3, 4, 0) ok = SDL_PutAudioStreamDataNoCopy(audio->stream, buffer->data, sz, data_complete, buffer); #else ok = SDL_PutAudioStreamData(audio->stream, buffer->data, sz); if (ok) { data_complete(buffer, buffer->data, sz); } #endif if (!ok) { buffer->inuse = false; } cleanup: audio->lock = 0; return unlock_stream(audio) && ok; }