2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_audio_c.h"
|
|
|
|
#include "twn_config.h"
|
|
|
|
#include "twn_engine_context_c.h"
|
|
|
|
#include "twn_util.h"
|
2024-07-08 06:46:12 +00:00
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <stb_ds.h>
|
|
|
|
#include <physfs.h>
|
|
|
|
|
2024-09-27 15:04:16 +00:00
|
|
|
#define STB_VORBIS_NO_PUSHDATA_API
|
2024-07-08 06:46:12 +00:00
|
|
|
#define STB_VORBIS_HEADER_ONLY
|
|
|
|
#include <stb_vorbis.c>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-10-01 09:59:01 +00:00
|
|
|
static const char *audio_exts[AUDIO_FILE_TYPE_COUNT] = {
|
|
|
|
".ogg", /* AUDIO_FILE_TYPE_OGG */
|
|
|
|
".xm", /* AUDIO_FILE_TYPE_XM */
|
2024-07-08 06:46:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* TODO: count frames without use, free the memory when threshold is met */
|
2024-07-08 13:58:23 +00:00
|
|
|
/* TODO: count repeated usages for sound effect cases with rendering to ram? */
|
2024-07-08 06:46:12 +00:00
|
|
|
/* stores path to data hash, useful for sound effects */
|
2024-09-23 17:43:16 +00:00
|
|
|
static struct AudioFileCache {
|
2024-07-08 06:46:12 +00:00
|
|
|
char *key;
|
2024-09-23 17:43:16 +00:00
|
|
|
struct AudioFileCacheValue {
|
2024-07-08 06:46:12 +00:00
|
|
|
unsigned char *data;
|
|
|
|
size_t len;
|
|
|
|
} value;
|
|
|
|
} *audio_file_cache;
|
|
|
|
|
|
|
|
|
|
|
|
static int64_t get_audio_data(const char *path, unsigned char **data) {
|
2024-09-23 17:43:16 +00:00
|
|
|
const struct AudioFileCache *cache = shgetp_null(audio_file_cache, path);
|
2024-07-08 06:46:12 +00:00
|
|
|
if (!cache) {
|
|
|
|
unsigned char *file;
|
|
|
|
int64_t len = file_to_bytes(path, &file);
|
|
|
|
if (len == -1) {
|
|
|
|
CRY("Audio error", "Error reading file");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
const struct AudioFileCacheValue value = { file, (size_t)len };
|
2024-07-08 06:46:12 +00:00
|
|
|
shput(audio_file_cache, path, value);
|
|
|
|
|
|
|
|
*data = file;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data = cache->value.data;
|
|
|
|
return (int64_t)cache->value.len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void audio_play(const char *path, const char *channel) {
|
|
|
|
const AudioChannelItem *pair = shgetp_null(ctx.audio_channels, channel);
|
2024-07-08 06:46:12 +00:00
|
|
|
if (!pair)
|
2024-09-23 17:43:16 +00:00
|
|
|
audio_play_ex(path, channel, audio_get_default_args());
|
2024-07-08 06:46:12 +00:00
|
|
|
else
|
2024-09-23 17:43:16 +00:00
|
|
|
audio_play_ex(path, channel, pair->value.args);
|
2024-07-08 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static AudioFileType infer_audio_file_type(const char *path) {
|
2024-09-25 22:42:34 +00:00
|
|
|
size_t path_len = SDL_strlen(path);
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-10-01 09:59:01 +00:00
|
|
|
for (int i = 0; i < AUDIO_FILE_TYPE_COUNT; ++i) {
|
2024-09-25 22:42:34 +00:00
|
|
|
size_t ext_length = SDL_strlen(audio_exts[i]);
|
2024-07-08 06:46:12 +00:00
|
|
|
if (path_len <= ext_length)
|
|
|
|
continue;
|
|
|
|
|
2024-09-25 22:42:34 +00:00
|
|
|
if (SDL_strcmp(&path[path_len - ext_length], audio_exts[i]) == 0)
|
2024-09-23 17:43:16 +00:00
|
|
|
return (AudioFileType)i;
|
2024-07-08 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
2024-10-01 09:59:01 +00:00
|
|
|
return AUDIO_FILE_TYPE_UNKNOWN;
|
2024-07-08 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* TODO: error propagation and clearing of resources on partial success? */
|
|
|
|
/* or should we expect things to simply fail? */
|
2024-09-23 17:43:16 +00:00
|
|
|
static union AudioContext init_audio_context(const char *path, AudioFileType type) {
|
2024-07-08 06:46:12 +00:00
|
|
|
switch (type) {
|
2024-10-01 09:59:01 +00:00
|
|
|
case AUDIO_FILE_TYPE_OGG: {
|
2024-07-08 06:46:12 +00:00
|
|
|
unsigned char *data;
|
|
|
|
int64_t len = get_audio_data(path, &data);
|
|
|
|
if (len == -1) {
|
|
|
|
CRY("Audio error", "Error reading file");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int error = 0;
|
|
|
|
stb_vorbis* handle = stb_vorbis_open_memory(data, (int)len, &error, NULL);
|
|
|
|
if (error != 0) {
|
|
|
|
CRY("Audio error", "Error reading .ogg file");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
stb_vorbis_info info = stb_vorbis_get_info(handle);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
return (union AudioContext) {
|
2024-07-08 06:46:12 +00:00
|
|
|
.vorbis = {
|
|
|
|
.data = data,
|
|
|
|
.handle = handle,
|
|
|
|
.frequency = info.sample_rate,
|
|
|
|
.channel_count = (uint8_t)info.channels,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-10-01 09:59:01 +00:00
|
|
|
case AUDIO_FILE_TYPE_XM: {
|
2024-07-08 13:58:23 +00:00
|
|
|
unsigned char *data;
|
|
|
|
int64_t len = get_audio_data(path, &data);
|
|
|
|
if (len == -1) {
|
|
|
|
CRY("Audio error", "Error reading file");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xm_context_t *handle;
|
|
|
|
int response = xm_create_context_safe(&handle,
|
|
|
|
(const char *)data,
|
|
|
|
(size_t)len,
|
|
|
|
AUDIO_FREQUENCY);
|
|
|
|
if (response != 0) {
|
|
|
|
CRY("Audio error", "Error loading xm module");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-07-08 15:00:38 +00:00
|
|
|
xm_set_max_loop_count(handle, 1);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
return (union AudioContext) {
|
2024-07-08 13:58:23 +00:00
|
|
|
.xm = { .handle = handle }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-08 06:46:12 +00:00
|
|
|
default:
|
|
|
|
CRY("Audio error", "Unhandled audio format (in init)");
|
2024-09-23 17:43:16 +00:00
|
|
|
return (union AudioContext){0};
|
2024-07-08 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
return (union AudioContext){0};
|
2024-07-08 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static void repeat_audio(AudioChannel *channel) {
|
2024-07-08 13:58:23 +00:00
|
|
|
switch (channel->file_type) {
|
2024-10-01 09:59:01 +00:00
|
|
|
case AUDIO_FILE_TYPE_OGG: {
|
2024-07-08 13:58:23 +00:00
|
|
|
stb_vorbis_seek_start(channel->context.vorbis.handle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-10-01 09:59:01 +00:00
|
|
|
case AUDIO_FILE_TYPE_XM: {
|
2024-07-08 20:46:47 +00:00
|
|
|
xm_restart(channel->context.xm.handle);
|
2024-07-08 13:58:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-07-08 13:58:23 +00:00
|
|
|
default:
|
|
|
|
CRY("Audio error", "Unhandled audio format (in repeat)");
|
|
|
|
break;
|
|
|
|
}
|
2024-07-08 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void audio_play_ex(const char *path, const char *channel, PlayAudioArgs args) {
|
|
|
|
AudioChannelItem *pair = shgetp_null(ctx.audio_channels, channel);
|
2024-07-08 06:46:12 +00:00
|
|
|
|
|
|
|
/* create a channel if it doesn't exist */
|
|
|
|
if (!pair) {
|
2024-09-23 17:43:16 +00:00
|
|
|
AudioFileType file_type = infer_audio_file_type(path);
|
|
|
|
AudioChannel new_channel = {
|
2024-07-08 06:46:12 +00:00
|
|
|
.args = args,
|
|
|
|
.file_type = file_type,
|
|
|
|
.context = init_audio_context(path, file_type),
|
|
|
|
.path = path,
|
2024-07-08 15:22:40 +00:00
|
|
|
.name = channel,
|
2024-07-08 06:46:12 +00:00
|
|
|
};
|
|
|
|
shput(ctx.audio_channels, channel, new_channel);
|
|
|
|
pair = shgetp_null(ctx.audio_channels, channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: destroy and create new context when channel is reused for different file */
|
|
|
|
|
|
|
|
/* works for both restarts and new audio */
|
|
|
|
if (strcmp(pair->value.path, path) == 0)
|
|
|
|
repeat_audio(&pair->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
PlayAudioArgs *audio_get_args(const char *channel) {
|
|
|
|
AudioChannelItem *pair = shgetp_null(ctx.audio_channels, channel);
|
2024-07-08 15:13:33 +00:00
|
|
|
if (!pair)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return &pair->value.args;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
PlayAudioArgs audio_get_default_args(void) {
|
|
|
|
return (PlayAudioArgs){
|
2024-07-08 06:46:12 +00:00
|
|
|
.repeat = false,
|
|
|
|
.crossfade = false,
|
|
|
|
.volume = 1.0f,
|
|
|
|
.panning = 0.0f,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-01 09:59:01 +00:00
|
|
|
/* this assumes float based streams */
|
2024-09-23 17:43:16 +00:00
|
|
|
static void audio_mixin_streams(const AudioChannel *channel,
|
2024-07-08 13:58:23 +00:00
|
|
|
uint8_t *restrict a,
|
|
|
|
uint8_t *restrict b,
|
|
|
|
size_t frames)
|
|
|
|
{
|
2024-10-01 09:59:01 +00:00
|
|
|
float *const sa = (float *)a;
|
|
|
|
float *const sb = (float *)b;
|
2024-07-08 13:58:23 +00:00
|
|
|
|
|
|
|
const float left_panning = fminf(fabsf(channel->args.panning - 1.0f), 1.0f);
|
|
|
|
const float right_panning = fminf(fabsf(channel->args.panning + 1.0f), 1.0f);
|
|
|
|
|
|
|
|
for (size_t s = 0; s < frames; s += 2) {
|
|
|
|
/* left channel */
|
2024-10-01 09:59:01 +00:00
|
|
|
sa[s] += (float)(sb[s] * channel->args.volume * left_panning);
|
2024-07-08 13:58:23 +00:00
|
|
|
|
|
|
|
/* right channel */
|
2024-10-01 09:59:01 +00:00
|
|
|
sa[s + 1] += (float)(sb[s + 1] * channel->args.volume * right_panning);
|
2024-07-08 13:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* remember: sample is data for all channels where frame is a part of it */
|
2024-09-23 17:43:16 +00:00
|
|
|
static void audio_sample_and_mixin_channel(const AudioChannel *channel,
|
2024-07-08 06:46:12 +00:00
|
|
|
uint8_t *stream,
|
2024-07-08 13:58:23 +00:00
|
|
|
int len)
|
|
|
|
{
|
2024-07-08 06:46:12 +00:00
|
|
|
static uint8_t buffer[16384];
|
2024-07-08 13:58:23 +00:00
|
|
|
const int float_buffer_frames = sizeof (buffer) / sizeof (float);
|
2024-10-01 09:59:01 +00:00
|
|
|
const int stream_frames = len / (int)(sizeof (float));
|
2024-07-08 06:46:12 +00:00
|
|
|
|
|
|
|
switch (channel->file_type) {
|
2024-10-01 09:59:01 +00:00
|
|
|
case AUDIO_FILE_TYPE_OGG: {
|
2024-07-08 06:46:12 +00:00
|
|
|
/* feed stream for needed conversions */
|
2024-07-08 13:58:23 +00:00
|
|
|
for (int i = 0; i < stream_frames; ) {
|
2024-10-01 09:59:01 +00:00
|
|
|
const int n_frames = (stream_frames - i) > float_buffer_frames ?
|
|
|
|
float_buffer_frames : stream_frames - i;
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-10-01 09:59:01 +00:00
|
|
|
const int samples_per_channel = stb_vorbis_get_samples_float_interleaved(
|
2024-07-08 06:46:12 +00:00
|
|
|
channel->context.vorbis.handle,
|
|
|
|
channel->context.vorbis.channel_count,
|
2024-10-01 09:59:01 +00:00
|
|
|
(float *)buffer,
|
2024-07-08 06:46:12 +00:00
|
|
|
n_frames);
|
|
|
|
|
2024-07-08 06:57:04 +00:00
|
|
|
/* handle end of file */
|
|
|
|
if (samples_per_channel == 0) {
|
2024-07-08 15:00:38 +00:00
|
|
|
if (channel->args.repeat) {
|
2024-07-08 06:57:04 +00:00
|
|
|
/* seek to start and try sampling some more */
|
|
|
|
stb_vorbis_seek_start(channel->context.vorbis.handle);
|
2024-07-08 15:00:38 +00:00
|
|
|
continue;
|
|
|
|
} else
|
2024-07-08 06:57:04 +00:00
|
|
|
/* leave silence */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-07-08 06:46:12 +00:00
|
|
|
/* panning and mixing */
|
2024-07-08 15:00:38 +00:00
|
|
|
audio_mixin_streams(channel,
|
2024-10-01 09:59:01 +00:00
|
|
|
&stream[i * sizeof(float)], buffer,
|
2024-07-08 15:00:38 +00:00
|
|
|
samples_per_channel * 2);
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-07-08 15:00:38 +00:00
|
|
|
i += samples_per_channel * 2;
|
2024-07-08 13:58:23 +00:00
|
|
|
}
|
2024-07-08 06:57:29 +00:00
|
|
|
|
2024-07-08 13:58:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-10-01 09:59:01 +00:00
|
|
|
case AUDIO_FILE_TYPE_XM: {
|
2024-07-08 13:58:23 +00:00
|
|
|
for (int i = 0; i < stream_frames; ) {
|
|
|
|
const int n_frames = (stream_frames - i) > float_buffer_frames ?
|
|
|
|
float_buffer_frames : stream_frames - i;
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-07-08 15:00:38 +00:00
|
|
|
const int samples_per_channel = xm_generate_samples(channel->context.xm.handle,
|
|
|
|
(float *)buffer,
|
|
|
|
n_frames / 2);
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-07-08 15:00:38 +00:00
|
|
|
/* handle end of file */
|
|
|
|
if (samples_per_channel == 0) {
|
|
|
|
if (channel->args.repeat) {
|
|
|
|
/* seek to start and try sampling some more */
|
|
|
|
xm_restart(channel->context.xm.handle);
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
/* leave silence */
|
|
|
|
break;
|
2024-07-08 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 13:58:23 +00:00
|
|
|
/* panning and mixing */
|
2024-07-08 15:00:38 +00:00
|
|
|
audio_mixin_streams(channel,
|
2024-10-01 09:59:01 +00:00
|
|
|
&stream[i * sizeof(float)],
|
2024-07-08 15:00:38 +00:00
|
|
|
buffer,
|
|
|
|
samples_per_channel * 2);
|
2024-07-08 13:58:23 +00:00
|
|
|
|
2024-07-08 15:00:38 +00:00
|
|
|
i += samples_per_channel * 2;
|
2024-07-08 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
CRY("Audio error", "Unhandled audio format (in sampling)");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static void sanity_check_channel(const AudioChannel *channel) {
|
2024-07-08 15:22:40 +00:00
|
|
|
if (channel->args.volume < 0.0f || channel->args.volume > 1.0f)
|
|
|
|
log_warn("Volume argument is out of range for channel (%s)", channel->name);
|
|
|
|
|
|
|
|
if (channel->args.panning < -1.0f || channel->args.panning > 1.0f)
|
|
|
|
log_warn("panning argument is out of range for channel (%s)", channel->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-08 06:46:12 +00:00
|
|
|
void audio_callback(void *userdata, uint8_t *stream, int len) {
|
|
|
|
(void)userdata;
|
|
|
|
|
|
|
|
/* prepare for mixing */
|
2024-07-08 19:37:58 +00:00
|
|
|
SDL_memset(stream, 0, len);
|
2024-07-08 06:46:12 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < shlen(ctx.audio_channels); ++i) {
|
2024-07-08 15:22:40 +00:00
|
|
|
sanity_check_channel(&ctx.audio_channels[i].value);
|
2024-07-08 06:46:12 +00:00
|
|
|
audio_sample_and_mixin_channel(&ctx.audio_channels[i].value, stream, len);
|
|
|
|
}
|
|
|
|
}
|