Files
sdl_template/src/window/audio.c
2025-03-25 11:11:11 +01:00

198 lines
5.9 KiB
C

#include "audio.h"
#include <SDL_audio.h>
#include <SDL_error.h>
#include <SDL_stdinc.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef __unix__
# include <unistd.h>
# define fexists(fname) (access(fname, F_OK) == 0)
#elif _WIN32
# include <io.h>
# define fexists(fname) (_access(fname, 0) == 0)
#else
# error platform not supported!
#endif
#include "../error.h"
static void audiomixer(void* const userdata, uint8_t* const stream, int32_t const len) {
memset(stream, 0, len); // clear the playing audio
audiodevice* const dev = userdata; // retrieve the callback data
// return if dev is null, since it can fail to initialize
if (dev == NULL) return;
struct audioplayer* prev = NULL;
struct audioplayer* curr = dev->audio_players;
while (curr != NULL) {
// if the current audio fragment has reached the end of their data
if (curr->len == 0) {
struct audioplayer* ncurr = curr->nxt;
// free the memory allocated to it and assign the next to to the currently playing
free(curr);
curr = ncurr;
// write to the audio device if prev hasn't been set yet
if (prev == NULL)
dev->audio_players = curr;
else
prev->nxt = curr;
// continue so if curr is now NULL, the loop stops
continue;
}
// calculate how much of the current audio player we should mix into the stream
uint32_t const mixlen = SDL_min(curr->len, (uint32_t)len);
// mix the current buffer into the stream, and update the audio player values accordingly
SDL_MixAudioFormat(stream, curr->buf, dev->fmt, mixlen, SDL_MIX_MAXVOLUME);
curr->buf += mixlen;
curr->len -= mixlen;
// increment the current node
prev = curr;
curr = curr->nxt;
}
}
// converts the inputted audio to the format of dev
// returns 1 upon failure, 0 upon success. When 1 is returned *bufptr will be freed. Otherwise *bufptr is reallocated
static int8_t audio_cvt(audiodevice const* dev, SDL_AudioSpec const* spec, uint8_t** bufptr, uint32_t* len) {
// init the converter
SDL_AudioCVT cvt;
if (SDL_BuildAudioCVT(&cvt, spec->format, spec->channels, spec->freq, dev->fmt, dev->channels, dev->freq) < 0) {
error("%s:%u could not build the audio converter! SDL Error: %s", __FILE_NAME__, __LINE__, SDL_GetError());
free(*bufptr); // free the buffer upon an error, as we won't be using this
return 1;
} else if (!cvt.needed) { // ensure the conversion is necessary
return 0;
}
cvt.len = (*len) * spec->channels; // calculate the size of the source data in bytes by multiplying the length by the amount of channels (warn: uint32_t -> int32_t)
cvt.buf = realloc(*bufptr, cvt.len * cvt.len_mult); // grow the inputted buffer for the conversion
// ensure the conversion buffer reallocation goes correctly
if (cvt.buf == NULL) {
error("%s:%u failed to reallocate the audio buffer to the new size of %u bytes!", __FILE_NAME__, __LINE__, cvt.len);
free(*bufptr);
return 1;
}
// converts the audio to the new format
if (SDL_ConvertAudio(&cvt)) {
error("%s:%u something went wrong when loading/converting an audio buffer! SDL Error: %s", __FILE_NAME__, __LINE__, SDL_GetError());
free(cvt.buf);
return 1;
}
*len = cvt.len;
*bufptr = realloc(cvt.buf, cvt.len_cvt);
if (*bufptr == NULL) {
warn("%s:%u something went wrong whilst shrinking the audio buffer whilst converting!", __FILE_NAME__, __LINE__);
*bufptr = cvt.buf; // use the conversion buffer, as this one will be valid if realloc fails
}
return 0;
}
audiodevice* audio_device_init(int32_t freq, SDL_AudioFormat fmt, uint8_t channels, uint16_t samples) {
audiodevice* dev = malloc(sizeof(audiodevice));
if (dev == NULL) {
error("%s:%u null pointer when allocating memory for the audio device!", __FILE_NAME__, __LINE__);
return NULL;
}
// define the audio specification
SDL_AudioSpec spec = {freq, fmt, channels, 0, samples, 0, 0, NULL, NULL};
spec.callback = audiomixer;
spec.userdata = dev;
// create the audio device
*dev = (audiodevice){
NULL,
SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0),
freq,
fmt,
channels,
};
if (dev->id < 1) {
error("%s:%u audio device failed to open! SDL Error: %s", __FILE_NAME__, __LINE__, SDL_GetError());
free(dev);
return NULL;
}
// default state of the device is paused, so we unpause it here
SDL_PauseAudioDevice(dev->id, 0);
return dev;
}
void audio_play(audiodevice* dev, audiodata const* audio) {
if (dev == NULL) return; // dev might fail to initialize
if (audio->len == 0) return; // audio might fail to initialize
// create an audio player
struct audioplayer* player = malloc(sizeof(struct audioplayer));
*player = (struct audioplayer){
dev->audio_players, // set nxt to the first item in dev (can be NULL, this is fine)
audio->buf,
audio->len,
};
// assign ourselves to the first item
dev->audio_players = player;
}
void audio_device_free(audiodevice* dev) {
if (dev == NULL) return;
SDL_CloseAudioDevice(dev->id);
struct audioplayer* curr = dev->audio_players;
// free all audio players
while (curr != NULL) {
dev->audio_players = curr->nxt; // use audio_players in dev as a cache
free(curr);
curr = dev->audio_players;
}
// free the audio device itself
free(dev);
}
audiodata audio_wav_load(audiodevice const* dev, char const* fpath) {
if (dev == NULL) return (audiodata){0};
SDL_AudioSpec spec;
audiodata audio;
debug("loading audio file '%s'...", fpath);
if (!fexists(fpath)) {
error("%s:%u couldn't find audio file '%s'!", __FILE_NAME__, __LINE__, fpath);
return (audiodata){0};
}
// load and parse the audio to the correct format
SDL_LoadWAV(fpath, &spec, &audio.buf, &audio.len);
if (audio_cvt(dev, &spec, &audio.buf, &audio.len)) {
return (audiodata){0};
}
// calculate the time in miliseconds of the audio fragment
audio.ms = 1000 * (((audio.len) / (SDL_AUDIO_BITSIZE(dev->fmt) / 8)) / spec.channels / dev->freq);
return audio;
}
void audio_wav_unload(audiodata* audio) {
free(audio->buf);
*audio = (audiodata){0}; // zero out all audio data
}