rework audio system to be able to call it from anywhere, rather than having to have access to the audio data.

This commit is contained in:
2025-06-27 12:39:04 +02:00
parent c4905f2da3
commit 21aca01cba
3 changed files with 95 additions and 83 deletions

View File

@@ -11,22 +11,28 @@
#include "../error.h" #include "../error.h"
#include "../util/compat.h" #include "../util/compat.h"
#include "../util/types.h"
struct audioplayer { struct audioplayer {
uint8_t const* buf; u8 const* buf;
int len; int len;
}; };
struct audiodevice { static struct audiodevice {
struct audioplayer audio[AUDIO_MAX]; struct audioplayer audio[AUDIO_MAX];
unsigned id; u32 id;
int freq; SDL_AudioSpec spec;
SDL_AudioFormat fmt;
uint8_t ch; // audio channels
} dev; } dev;
struct audiodata audio_dat[AUDIO_ID_COUNT] = {0}; // contains pointers to audio buffers.
static char const* const audio_path[AUDIO_ID_COUNT] = {
"korobeiniki.wav",
"place.wav",
};
/* mixes the audio output stream, using the different audio as sources */ /* mixes the audio output stream, using the different audio as sources */
static void audiomixer(void* const userdata, uint8_t* const stream, int const len) { static void audiomixer(void* const userdata, u8* const stream, int const len) {
(void)userdata; (void)userdata;
memset(stream, 0, len); // clear the playing audio memset(stream, 0, len); // clear the playing audio
@@ -34,7 +40,7 @@ static void audiomixer(void* const userdata, uint8_t* const stream, int const le
for (unsigned i = 0; i < AUDIO_MAX; i++) { for (unsigned i = 0; i < AUDIO_MAX; i++) {
if (dev.audio[i].len > 0) { if (dev.audio[i].len > 0) {
unsigned mixlen = SDL_min(len, dev.audio[i].len); unsigned mixlen = SDL_min(len, dev.audio[i].len);
SDL_MixAudioFormat(stream, dev.audio[i].buf, dev.fmt, mixlen, SDL_MIX_MAXVOLUME); SDL_MixAudioFormat(stream, dev.audio[i].buf, dev.spec.format, mixlen, SDL_MIX_MAXVOLUME);
dev.audio[i].len -= mixlen; dev.audio[i].len -= mixlen;
dev.audio[i].buf += mixlen; dev.audio[i].buf += mixlen;
} }
@@ -45,12 +51,12 @@ static void audiomixer(void* const userdata, uint8_t* const stream, int const le
* `len` is a pointer to the current size, the new size will be written to this location. * `len` is a pointer to the current size, the new size will be written to this location.
* returns the pointer to the audio buffer to use, or NULL, when something went wrong. * returns the pointer to the audio buffer to use, or NULL, when something went wrong.
* NULL will never be returned after the conversion */ * NULL will never be returned after the conversion */
static uint8_t* audio_cvt(SDL_AudioSpec const* spec, uint8_t* bufptr, unsigned* len) { static u8* audio_cvt(SDL_AudioSpec const* spec, u8* bufptr, unsigned* len) {
if (!bufptr) return NULL; if (!bufptr) return NULL;
// init the converter // init the converter
SDL_AudioCVT cvt; SDL_AudioCVT cvt;
if (SDL_BuildAudioCVT(&cvt, spec->format, spec->channels, spec->freq, dev.fmt, dev.ch, dev.freq) < 0) { if (SDL_BuildAudioCVT(&cvt, spec->format, spec->channels, spec->freq, dev.spec.format, dev.spec.channels, dev.spec.freq) < 0) {
error("could not build the audio converter! SDL Error: %s", SDL_GetError()); error("could not build the audio converter! SDL Error: %s", SDL_GetError());
return NULL; return NULL;
} }
@@ -78,72 +84,68 @@ static uint8_t* audio_cvt(SDL_AudioSpec const* spec, uint8_t* bufptr, unsigned*
return bufptr ? bufptr : cvt.buf; return bufptr ? bufptr : cvt.buf;
} }
int audio_device_init(int freq, SDL_AudioFormat fmt, uint8_t channels, uint16_t samples) { /* computes the time in milliseconds of the audio fragment by dividing
// define the audio specification * the audio byte length by the format's bit size, which is divided by
SDL_AudioSpec spec = {freq, fmt, channels, 0, samples, 0, 0, NULL, NULL}; * the audio device's channels and frequency */
spec.callback = audiomixer; static inline u32 audio_btoms(u32 len) {
spec.userdata = NULL; return (((1000 * len) / (SDL_AUDIO_BITSIZE(dev.spec.format) / 8)) / dev.spec.channels / dev.spec.freq);
unsigned id = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0);
// create the audio device
dev = (struct audiodevice){
{0},
id,
freq,
fmt,
channels,
};
if (id) {
// default state of the device is paused, so we unpause it here
SDL_PauseAudioDevice(id, 0);
return 0;
} else return 1;
} }
void audio_device_free(void) { /* loads a `struct audiodata` from `fpat` to `out`. */
SDL_CloseAudioDevice(dev.id); static void audio_wav_load(char const* restrict fpat, struct audiodata* restrict out) {
dev = (struct audiodevice){0}; debug("loading audio file '%s'...", fpat);
} if (faccess(fpat, FA_R)) {
error("audio file either isn't readable or doesn't exist. path: '%s'!", fpat);
void audio_play(audiodata const* audio) { return;
if (!dev.id) return;
if (!audio->buf) return;
unsigned i = 0;
while (i < AUDIO_MAX && dev.audio[i].len > 0) i++;
if (i < AUDIO_MAX)
dev.audio[i] = (struct audioplayer){audio->buf, audio->len};
}
audiodata audio_wav_load(char const* fpath) {
debug("loading audio file '%s'...", fpath);
if (faccess(fpath, FA_R)) {
error("%s:%u audio file either isn't readable or doesn't exist. path: '%s'!", __FILE_NAME__, __LINE__, fpath);
return (struct audiodata){0};
} }
// load the audio // load the audio
u32 len;
u8 *ptr, *tmp;
SDL_AudioSpec spec; SDL_AudioSpec spec;
audiodata audio; SDL_LoadWAV(fpat, &spec, &ptr, &len);
SDL_LoadWAV(fpath, &spec, &audio.buf, &audio.len);
if (!audio.buf) return audio;
// convert the audio data to the format reflecting dev // convert the audio data to the format reflecting dev
void* ptr = audio_cvt(&spec, audio.buf, &audio.len); tmp = audio_cvt(&spec, ptr, &len);
if (!ptr) free(audio.buf); // free the buffer if NULL was returned; failure if (!tmp) free(ptr); // free the buffer if NULL was returned; failure
audio.buf = ptr;
// calculate the time in milliseconds of the audio fragment *out = (struct audiodata){tmp, len, audio_btoms(len)};
// by dividing the audio bytelength by the format's bitsize, by the audio device's channels and the audio device's frequency
audio.ms = (((1000 * audio.len) / (SDL_AUDIO_BITSIZE(dev.fmt) / 8)) / dev.ch / dev.freq);
return audio;
} }
void audio_wav_unload(audiodata* audio) { /* loads the audio data into the buffer */
free(audio->buf); static inline void audio_load(void) {
*audio = (audiodata){0}; // zero out all audio data for (size_t i = 0; i < AUDIO_ID_COUNT; i++)
audio_wav_load(audio_path[i], &audio_dat[i]);
}
void audio_play(u32 audio_id) {
if (!dev.id) return;
size_t i = 0;
while (i < AUDIO_MAX && dev.audio[i].len > 0) i++;
if (i >= AUDIO_MAX) return;
dev.audio[i] = (struct audioplayer){
audio_dat[audio_id].buf,
audio_dat[audio_id].len,
};
}
int audio_init(int freq, SDL_AudioFormat fmt, u8 ch, u16 samples) {
// initialise the audio device + specification
SDL_AudioSpec spec = {freq, fmt, ch, 0, samples, 0, 0, audiomixer, NULL};
unsigned id = SDL_OpenAudioDevice(NULL, 0, &spec, &spec, 0);
dev = (struct audiodevice){{0}, id, spec};
if (!id) return 1;
SDL_PauseAudioDevice(id, 0);
audio_load();
return 0;
}
void audio_free(void) {
SDL_CloseAudioDevice(dev.id);
dev = (struct audiodevice){0};
for (size_t i = 0; i < AUDIO_ID_COUNT; i++)
free((void*)audio_dat[i].buf);
} }

View File

@@ -3,18 +3,31 @@
#include <SDL_audio.h> #include <SDL_audio.h>
#include <stdint.h> #include <stdint.h>
#include "../util/types.h"
#define AUDIO_MAX 4 // maximum number of sound effects that are allowed to play at once #define AUDIO_MAX 4 // maximum number of sound effects that are allowed to play at once
struct audiodata { struct audiodata {
uint8_t* buf; // pointer to the audio buffer u8 const* buf; // pointer to the audio buffer
uint32_t len; // length in bytes of the audio buffer u32 len; // length in bytes of the audio buffer
uint32_t ms; // length in miliseconds of the audio buffer u32 ms; // length in miliseconds of the audio buffer
}; };
typedef struct audiodata audiodata;
int audio_device_init(int, SDL_AudioFormat, uint8_t, uint16_t); enum audio_id {
void audio_device_free(void); AUDIO_ID_MUSIC,
void audio_play(audiodata const*); AUDIO_ID_PLACE,
audiodata audio_wav_load(char const*); // leave at end, will contain count
void audio_wav_unload(audiodata*); AUDIO_ID_COUNT,
};
extern struct audiodata audio_dat[AUDIO_ID_COUNT];
/* loads the audio to be played, unless `AUDIO_MAX` has been reached, then this call will fail */
void audio_play(u32);
/* initialises the audio device, then loads the audio data */
int audio_init(int freq, SDL_AudioFormat fmt, u8 ch, u16 samples);
/* frees up resources held by the audio device and audio buffers */
void audio_free(void);

View File

@@ -17,7 +17,6 @@
static SDL_Window* win = NULL; static SDL_Window* win = NULL;
static bool close = false; static bool close = false;
static audiodata music;
void window_init(struct gamedata const* gdat) { void window_init(struct gamedata const* gdat) {
assert(!win && !close); assert(!win && !close);
@@ -31,8 +30,7 @@ void window_init(struct gamedata const* gdat) {
fatal(ERROR_SDL_RENDERING_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError()); fatal(ERROR_SDL_RENDERING_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError());
render_init(win, gdat); render_init(win, gdat);
audio_device_init(32000, AUDIO_S16, 1, 4096); audio_init(32000, AUDIO_S16, 1, 4096);
music = audio_wav_load("korobeiniki.wav");
} }
void window_free(void) { void window_free(void) {
@@ -42,8 +40,7 @@ void window_free(void) {
win = NULL; win = NULL;
close = false; close = false;
audio_wav_unload(&music); audio_free();
audio_device_free();
} }
void window_open(void) { void window_open(void) {
@@ -53,8 +50,8 @@ void window_open(void) {
render_update(); render_update();
static time_t timeout = 0; static time_t timeout = 0;
if (time_poll(time, music.ms, &timeout)) if (time_poll(time, audio_dat[AUDIO_ID_MUSIC].ms, &timeout))
audio_play(&music); audio_play(AUDIO_ID_MUSIC);
} }
} }