aboutsummaryrefslogtreecommitdiff
path: root/src/audio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio.c')
-rw-r--r--src/audio.c24
1 files changed, 11 insertions, 13 deletions
diff --git a/src/audio.c b/src/audio.c
index cbe5be3..b2ba701 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -3,6 +3,11 @@
#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:
@@ -20,19 +25,12 @@ struct pw_audio *pw_audio_init(void) {
.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 = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK,
+ &spec, NULL, NULL);
+ if (!audio->stream) {
+ SDL_LogWarn(0, "starting without audio");
}
- audio->stream = stream;
return audio;
}
@@ -196,8 +194,8 @@ bool pw_audio_unlock(struct pw_audio *audio) {
return SDL_InvalidParamError("audio");
}
- size_t i = audio->lock / PW_AUDIO_BUFFER_MAX;
- size_t len = audio->lock % PW_AUDIO_BUFFER_MAX;
+ 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;