aboutsummaryrefslogtreecommitdiff
path: root/src/audio.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio.h')
-rw-r--r--src/audio.h68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/audio.h b/src/audio.h
new file mode 100644
index 0000000..2f70e8f
--- /dev/null
+++ b/src/audio.h
@@ -0,0 +1,68 @@
+#ifndef PW_AUDIO_H
+#define PW_AUDIO_H
+
+#include <SDL3/SDL_audio.h>
+
+#define PW_AUDIO_BUFFER_COUNT 8
+#define PW_AUDIO_BUFFER_MAX 4096
+
+/**
+ * An audio buffer that we pass to the SDL audio stream.
+ *
+ * This allows buffer reuse and avoid double copying from Web Assembly
+ * linear memory (where a copy is required in order to be portable on
+ * big-endian platforms where data is written backwards).
+ */
+struct pw_audio_buffer {
+ bool inuse;
+ int16_t data[PW_AUDIO_BUFFER_MAX];
+};
+
+/**
+ * PICO-8 audio stream.
+ */
+struct pw_audio {
+ SDL_AudioStream *stream;
+ uint32_t size;
+ uint32_t lock;
+ struct pw_audio_buffer buffers[PW_AUDIO_BUFFER_COUNT];
+};
+
+/**
+ * Initializes the PICO-8 audio stream.
+ */
+struct pw_audio *pw_audio_init(void);
+
+/**
+ * Frees the PICO-8 audio stream and all of its resources.
+ */
+void pw_audio_free(struct pw_audio *audio);
+
+/**
+ * Starts playing audio from the stream with the specified buffer size.
+ */
+bool pw_audio_start(struct pw_audio *audio, uint32_t size);
+
+/**
+ * Stops playing audio from the stream.
+ */
+bool pw_audio_stop(struct pw_audio *audio);
+
+/**
+ * Whether or not the audio system is ready.
+ */
+bool pw_audio_ready(struct pw_audio *audio);
+
+/**
+ * Locks the audio device returning a buffer to write samples to.
+ *
+ * The audio stream expects `len` 16-bit samples to be written.
+ */
+int16_t *pw_audio_lock(struct pw_audio *audio, uint32_t *len);
+
+/**
+ * Unlocks the audio device.
+ */
+bool pw_audio_unlock(struct pw_audio *audio);
+
+#endif