-
Notifications
You must be signed in to change notification settings - Fork 0
/
mopenal.m
295 lines (252 loc) · 10.5 KB
/
mopenal.m
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
:- module mopenal.
%==============================================================================%
% Note that this is not entirely equal to OpenAL, but rather contains some of
% the limitations imposed by Cinnamon to make other backends simpler (see the
% loader type, for instance).
:- interface.
%==============================================================================%
:- use_module io.
:- use_module buffer.
:- use_module vector.
:- type context.
:- type device.
:- type loader.
:- type sound.
:- type listener_ctl ---> position ; velocity ; orientation.
:- type format ---> mono_16 ; stereo_16 ; mono_float ; stereo_float.
:- pred open_device(io.res(device), io.io, io.io).
:- mode open_device(uo, di, uo) is det.
:- pred create_context(device, io.res(context), io.io, io.io).
:- mode create_context(in, uo, di, uo) is det.
:- pred supports_float(device::in) is semidet.
:- pred context_supports_float(context::in) is semidet.
:- pred make_current(context::in, io.io::di, io.io::uo) is det.
:- pred listener_ctl(listener_ctl, vector.vector3, io.io, io.io).
:- mode listener_ctl(in, in, di, uo) is det.
:- pred listener_ctl(listener_ctl, float, float, float, io.io, io.io).
:- mode listener_ctl(in, in, in, in, di, uo) is det.
:- pred create_loader(format, int, context, loader, io.io, io.io).
:- mode create_loader(in, in, in, uo, di, uo) is det.
:- pred put_data(loader::di, loader::uo, buffer.buffer::in) is det.
:- pred finalize(loader::di, sound::uo) is det.
:- pred play(sound::in, io.io::di, io.io::uo) is det.
:- pred play_looping(sound::in, io.io::di, io.io::uo) is det.
:- pred set_looping(sound::in, io.io::di, io.io::uo) is det.
:- pred unset_looping(sound::in, io.io::di, io.io::uo) is det.
:- pred stop(sound::in, io.io::di, io.io::uo) is det.
:- pred pause(sound::in, io.io::di, io.io::uo) is det.
%==============================================================================%
:- implementation.
%==============================================================================%
:- pragma foreign_decl("C",
"
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
#endif
").
:- pragma foreign_import_module("C", buffer).
:- pragma foreign_type("C", context, "ALCcontext*const*").
:- pragma foreign_type("C", device, "ALCdevice*const*").
:- pragma foreign_type("C", sound, "ALuint*"). % Size of 1.
:- pragma foreign_type("C", loader, "ALuint*"). % { Snd, Format, Rate }
:- pragma foreign_enum("C", format/0,
[
mono_16 - "AL_FORMAT_MONO16",
stereo_16 - "AL_FORMAT_STEREO16",
mono_float - "0x10010", % Can't trust these will really exist.
stereo_float - "0x10011"
]).
:- pragma foreign_enum("C", listener_ctl/0,
[
position - "AL_POSITION",
velocity - "AL_VELOCITY",
orientation - "AL_ORIENTATION"
]).
:- pragma foreign_decl("C", "void MOpenAL_DevFinalizer(void *dev, void *arg);").
:- pragma foreign_code("C",
"
void MOpenAL_DevFinalizer(void *dev, void *arg){
(void)arg;
alcCloseDevice(*((ALCdevice **)dev));
}
").
:- pragma foreign_decl("C", "void MOpenAL_CtxFinalizer(void *ctx, void *arg);").
:- pragma foreign_code("C",
"
void MOpenAL_CtxFinalizer(void *ctx, void *arg){
alcDestroyContext(*((ALCcontext **)ctx));
}
").
:- pragma foreign_decl("C", "void MOpenAL_SndFinalizer(void *snd, void *arg);").
:- pragma foreign_code("C",
"
void MOpenAL_SndFinalizer(void *snd, void *arg){
alDeleteSources(1, (ALuint*)snd);
}
").
:- pragma foreign_decl("C", "int MOpenAL_CleanSound(ALuint snd, ALuint *out);").
:- pragma foreign_code("C",
"
int MOpenAL_CleanSound(ALuint snd, ALuint *out){
ALint i;
alGetSourcei(snd, AL_BUFFERS_PROCESSED, &i);
if(i > 0){
if(out != NULL){
i--;
alSourceUnqueueBuffers(snd, 1, out);
}
ALuint buffers[16];
do{
const unsigned to_delete = (i >= 16) ? 16 : i;
alSourceUnqueueBuffers(snd, to_delete, buffers);
alDeleteBuffers(to_delete, buffers);
i -= to_delete;
}while(i > 0);
return 1;
} /* if(i > 0) */
return 0;
}
").
% Wrappers for use inside the foreign procs.
:- func create_dev_error(string) = io.res(device).
create_dev_error(Err) = io.error(io.make_io_error(Err)).
:- pragma foreign_export("C", create_dev_error(in) = (out), "MOpenAL_CreateDevError").
:- func create_dev_ok(device) = io.res(device).
create_dev_ok(Dev) = io.ok(Dev).
:- pragma foreign_export("C", create_dev_ok(in) = (out), "MOpenAL_CreateDevOK").
:- func create_ctx_error(string) = io.res(context).
create_ctx_error(Err) = io.error(io.make_io_error(Err)).
:- pragma foreign_export("C", create_ctx_error(in) = (out), "MOpenAL_CreateCtxError").
:- func create_ctx_ok(context) = io.res(context).
create_ctx_ok(Ctx) = io.ok(Ctx).
:- pragma foreign_export("C", create_ctx_ok(in) = (out), "MOpenAL_CreateCtxOK").
:- pragma foreign_proc("C", open_device(Out::uo, IO0::di, IO1::uo),
[will_not_throw_exception, promise_pure, thread_safe, tabled_for_io],
"
IO1 = IO0;
ALCdevice *const device = alcOpenDevice(NULL);
if(device){
ALCdevice **const dev_ptr = MR_GC_malloc_atomic(sizeof(void*));
dev_ptr[0] = device;
Out = MOpenAL_CreateDevOK(dev_ptr);
MR_GC_register_finalizer(dev_ptr, MOpenAL_DevFinalizer, NULL);
}
else{
const char err[] = ""Could not open device"";
char *const m_err = MR_GC_malloc_atomic(sizeof(err));
memcpy(m_err, err, sizeof(err));
Out = MOpenAL_CreateDevError(m_err);
}
").
:- pragma foreign_proc("C", create_context(Dev::in, Out::uo, IO0::di, IO1::uo),
[will_not_throw_exception, promise_pure, thread_safe, tabled_for_io],
"
IO1 = IO0;
ALCcontext *const ctx = alcCreateContext(*Dev, NULL);
if(ctx){
ALCcontext **const ctx_ptr = MR_GC_malloc_atomic(sizeof(void*));
ctx_ptr[0] = ctx;
Out = MOpenAL_CreateCtxOK(ctx_ptr);
MR_GC_register_finalizer(ctx_ptr, MOpenAL_CtxFinalizer, (void*)Dev);
}
else{
const char err[] = ""Could not open context"";
char *const m_err = MR_GC_malloc_atomic(sizeof(err));
memcpy(m_err, err, sizeof(err));
Out = MOpenAL_CreateCtxError(m_err);
}
").
:- pragma foreign_proc("C", supports_float(Dev::in),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe],
"
const ALboolean exists = alcIsExtensionPresent(*Dev, ""AL_EXT_FLOAT32"");
SUCCESS_INDICATOR = (exists == AL_TRUE);
").
:- pragma foreign_proc("C", context_supports_float(Ctx::in),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe],
"
ALCdevice *const dev = alcGetContextsDevice(*Ctx);
const ALboolean exists = alcIsExtensionPresent(dev, ""AL_EXT_FLOAT32"");
SUCCESS_INDICATOR = (exists == AL_TRUE);
").
:- pragma foreign_proc("C", make_current(Ctx::in, IO0::di, IO1::uo),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe, tabled_for_io],
"
IO1 = IO0;
alcMakeContextCurrent(*Ctx);
").
listener_ctl(Ctl, vector.vector(X, Y, Z), !IO) :- listener_ctl(Ctl, X, Y, Z, !IO).
:- pragma foreign_proc("C",
listener_ctl(Ctl::in, X::in, Y::in, Z::in, IO0::di, IO1::uo),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe, tabled_for_io],
"
IO1 = IO0;
alListener3f(Ctl, X, Y, Z);
").
:- pragma foreign_proc("C",
create_loader(Format::in, Rate::in, Ctx::in, Out::uo, IO0::di, IO1::uo),
[will_not_throw_exception, promise_pure, thread_safe],
"
IO1 = IO0;
Out = MR_GC_malloc_atomic(sizeof(ALuint) * 3);
alcMakeContextCurrent(*Ctx);
alGenSources(1, Out);
Out[1] = Format;
Out[2] = Rate;
MR_GC_register_finalizer(Out, MOpenAL_SndFinalizer, (void*)Ctx);
").
:- pragma foreign_proc("C", put_data(In::di, Out::uo, Buffer::in),
[will_not_throw_exception, promise_pure, thread_safe, tabled_for_io],
"
Out = In;
ALuint buffer;
if(!MOpenAL_CleanSound(*In, &buffer)){
alGenBuffers(1, &buffer);
}
alBufferData(buffer, In[1], Buffer->data, Buffer->size, In[2]);
alSourceQueueBuffers(*In, 1, &buffer);
").
:- pragma foreign_proc("C", finalize(Loader::di, Sound::uo),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe],
"
Sound = Loader;
alSourcef(*Sound, AL_PITCH, 1.0f);
alSourcef(*Sound, AL_GAIN, 1.0f);
alSource3f(*Sound, AL_POSITION, 1.0f, 1.0f, 1.0f);
alSource3f(*Sound, AL_VELOCITY, 1.0f, 1.0f, 1.0f);
alSourcei(*Sound, AL_LOOPING, AL_FALSE);
").
:- pragma foreign_proc("C", play(Snd::in, IO0::di, IO1::uo),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe, tabled_for_io],
"
IO1 = IO0;
alSourcei(*Snd, AL_LOOPING, AL_FALSE);
alSourcePlay(*Snd);
").
:- pragma foreign_proc("C", stop(Snd::in, IO0::di, IO1::uo),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe, tabled_for_io],
" IO1 = IO0; alSourceStop(*Snd); ").
:- pragma foreign_proc("C", pause(Snd::in, IO0::di, IO1::uo),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe, tabled_for_io],
" IO1 = IO0; alSourcePause(*Snd); ").
:- pragma foreign_proc("C", set_looping(Snd::in, IO0::di, IO1::uo),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe, tabled_for_io],
" IO1 = IO0; alSourcei(*Snd, AL_LOOPING, AL_TRUE); ").
:- pragma foreign_proc("C", unset_looping(Snd::in, IO0::di, IO1::uo),
[will_not_call_mercury, does_not_affect_liveness, will_not_throw_exception,
promise_pure, thread_safe, tabled_for_io],
" IO1 = IO0; alSourcei(*Snd, AL_LOOPING, AL_FALSE); ").
play_looping(Snd, !IO) :- stop(Snd, !IO), set_looping(Snd, !IO), play(Snd, !IO).