#ifndef PW_AUDIO_H #define PW_AUDIO_H #include #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