-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenal.c
165 lines (142 loc) · 3.87 KB
/
openal.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "openal.h"
#include "sound.h"
#include "../candle/systems/sauces.h"
#include "../candle/components/camera.h"
#include "../candle/components/node.h"
#include <stdio.h>
#include <stdlib.h>
#include "alw.h"
void *sound_loader(const char *bytes, size_t bytes_num, const char *name,
uint32_t ext)
{
sound_t *sound = sound_new();
sound_load(sound, bytes, bytes_num);
return sound;
}
void _check_al_error(const char *file, int line)
{
ALCenum err;
int got_error = 0;
char message[512];
static int count = 0;
static char last_error[512] = "";
err = alwGetError();
while (err != AL_NO_ERROR) {
char *error = NULL;
switch(err)
{
case AL_INVALID_OPERATION: error="AL_INVALID_OPERATION"; break;
case AL_INVALID_NAME: error="AL_INVALID_NAME"; break;
case AL_INVALID_ENUM: error="AL_INVALID_ENUM"; break;
case AL_INVALID_VALUE: error="AL_INVALID_VALUE"; break;
case AL_OUT_OF_MEMORY: error = "AL_OUT_OF_MEMORY"; break;
}
snprintf(message, sizeof(message), "GL_%s - %s:%d", error, file, line);
if(!strncmp(last_error, message, sizeof(last_error)))
{
printf("\b\b\r%s%6d\n", message, ++count);
}
else
{
count = 0;
printf("%s\n", message);
strncpy(last_error, message, sizeof(last_error));
}
err = alwGetError();
got_error = 1;
}
if(got_error)
{
printf("Exiting due to al error\n");
exit(1);
}
}
void c_openal_init(c_openal_t *self)
{
if (!alwcOpenDevice)
alw_init();
self->device = alwcOpenDevice(NULL);
if(!self->device)
{
printf("Could not open al device.\n");
return;
}
self->context = alwcCreateContext(self->device, NULL);
if (!alwcMakeContextCurrent(self->context))
{
printf("Could not create al context.\n");
return;
}
alwDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
#ifndef WIN32
#ifndef __EMSCRIPTEN__
if(!alwcIsExtensionPresent(self->device, "ALC_SOFT_HRTF"))
{
fprintf(stderr, "Error: ALC_SOFT_HRTF not supported\n");
exit(1);
}
/* Enumerate available HRTFs, and reset the device using one. */
ALCint num_hrtf;
/* const char *hrtfname = "default-48000"; */
const char *hrtfname = NULL;
ALCint hrtf_state;
alwcGetIntegerv(self->device, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtf);
if(!num_hrtf) printf("No HRTFs found\n");
else
{
ALCint attr[5];
ALCint index = -1;
ALCint i;
printf("Available HRTFs:\n");
for(i = 0; i < num_hrtf; i++)
{
const ALCchar *name = alwcGetStringiSOFT(self->device, ALC_HRTF_SPECIFIER_SOFT, i);
printf(" %d: %s\n", i, name);
if(hrtfname && strcmp(name, hrtfname) == 0) index = i;
}
i = 0;
attr[i++] = ALC_HRTF_SOFT;
attr[i++] = ALC_TRUE;
if(index == -1)
{
if(hrtfname) printf("HRTF \"%s\" not found\n", hrtfname);
printf("Using default HRTF...\n");
}
else
{
printf("Selecting HRTF %d...\n", index);
attr[i++] = ALC_HRTF_ID_SOFT;
attr[i++] = index;
}
attr[i] = 0;
if(!alwcResetDeviceSOFT(self->device, attr))
printf("Failed to reset device: %s\n", alwcGetString(self->device, alwcGetError(self->device)));
}
alwcGetIntegerv(self->device, ALC_HRTF_SOFT, 1, &hrtf_state);
if(!hrtf_state) printf("HRTF not enabled!\n");
else
{
const ALchar *name = alwcGetString(self->device, ALC_HRTF_SPECIFIER_SOFT);
printf("HRTF enabled, using %s\n", name);
}
#endif
#endif
sauces_loader(ref("wav"), sound_loader);
}
c_openal_t *c_openal_new()
{
c_openal_t *self = component_new(ct_openal);
return self;
}
void c_openal_destroy(c_openal_t *self)
{
alwcMakeContextCurrent(NULL);
alwcDestroyContext(self->context);
alwcCloseDevice(self->device);
}
void ct_openal(ct_t *self)
{
ct_init(self, "openal", sizeof(c_openal_t));
ct_set_init(self, (init_cb)c_openal_init);
ct_set_destroy(self, (destroy_cb)c_openal_destroy);
}