forked from LIJI32/SameBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
109 lines (95 loc) · 2.38 KB
/
audio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <Core/gb.h>
#include "audio/audio.h"
#include "configuration.h"
#define likely(x) GB_likely(x)
#define unlikely(x) GB_unlikely(x)
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),
#endif
GB_AUDIO_DRIVER_REF(SDL),
#ifdef ENABLE_OPENAL
GB_AUDIO_DRIVER_REF(OpenAL),
#endif
};
// 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
for (unsigned i = 0; i < sizeof(drivers) / sizeof(drivers[0]); i++) {
driver = drivers[i];
if (driver->audio_init()) {
atexit(driver->audio_deinit);
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),
#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;
}