SameBoy/SDL/audio.c

113 lines
2.5 KiB
C
Raw Normal View History

2022-06-24 11:18:53 +00:00
#include <stdbool.h>
#include <stddef.h>
#include <Core/gb.h>
#include <stdlib.h>
2022-06-24 11:18:53 +00:00
#include "audio/audio.h"
#include "configuration.h"
2022-06-24 11:18:53 +00:00
#define unlikely(x) __builtin_expect((bool)(x), 0)
static const GB_audio_driver_t *driver = NULL;
bool GB_audio_init(void)
{
const GB_audio_driver_t *drivers[] = {
#ifdef _WIN32
GB_AUDIO_DRIVER_REF(XAudio2),
GB_AUDIO_DRIVER_REF(XAudio2_7),
#endif
GB_AUDIO_DRIVER_REF(SDL),
#ifdef ENABLE_OPENAL
GB_AUDIO_DRIVER_REF(OpenAL),
#endif
2022-06-24 11:18:53 +00:00
};
// First try the preferred driver
for (unsigned i = 0; i < sizeof(drivers) / sizeof(drivers[0]); i++) {
driver = drivers[i];
if (strcmp(driver->name, configuration.audio_driver) != 0) {
continue;
}
if (driver->audio_init()) {
if (driver->audio_deinit) {
atexit(driver->audio_deinit);
}
return true;
}
}
// Else go by priority
2022-06-24 11:18:53 +00:00
for (unsigned i = 0; i < sizeof(drivers) / sizeof(drivers[0]); i++) {
driver = drivers[i];
if (driver->audio_init()) {
if (driver->audio_deinit) {
atexit(driver->audio_deinit);
}
2022-06-24 11:18:53 +00:00
return true;
}
}
driver = NULL;
return false;
}
bool GB_audio_is_playing(void)
{
if (unlikely(!driver)) return false;
return driver->audio_is_playing();
}
void GB_audio_set_paused(bool paused)
{
if (unlikely(!driver)) return;
return driver->audio_set_paused(paused);
}
void GB_audio_clear_queue(void)
{
if (unlikely(!driver)) return;
return driver->audio_clear_queue();
}
unsigned GB_audio_get_frequency(void)
{
if (unlikely(!driver)) return 0;
return driver->audio_get_frequency();
}
size_t GB_audio_get_queue_length(void)
{
if (unlikely(!driver)) return 0;
return driver->audio_get_queue_length();
}
void GB_audio_queue_sample(GB_sample_t *sample)
{
if (unlikely(!driver)) return;
return driver->audio_queue_sample(sample);
}
const char *GB_audio_driver_name(void)
{
if (unlikely(!driver)) return "None";
return driver->name;
}
const char *GB_audio_driver_name_at_index(unsigned index)
{
const GB_audio_driver_t *drivers[] = {
#ifdef _WIN32
GB_AUDIO_DRIVER_REF(XAudio2),
GB_AUDIO_DRIVER_REF(XAudio2_7),
#endif
GB_AUDIO_DRIVER_REF(SDL),
#ifdef ENABLE_OPENAL
GB_AUDIO_DRIVER_REF(OpenAL),
#endif
};
if (index >= sizeof(drivers) / sizeof(drivers[0])) {
return "";
}
return drivers[index]->name;
}