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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/*
Copyright (C) 2010 Andrey Nazarov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "sound.h"
#if USE_FIXED_LIBAL
#include "qal/fixed.h"
#else
#include "qal/dynamic.h"
#endif
// translates from AL coordinate system to quake
#define AL_UnpackVector(v) -v[1],v[2],-v[0]
#define AL_CopyVector(a,b) ((b)[0]=-(a)[1],(b)[1]=(a)[2],(b)[2]=-(a)[0])
// OpenAL implementation should support at least this number of sources
#define MIN_CHANNELS 16
static ALuint s_srcnums[MAX_CHANNELS];
static int s_framecount;
void AL_SoundInfo(void)
{
Com_Printf("AL_VENDOR: %s\n", qalGetString(AL_VENDOR));
Com_Printf("AL_RENDERER: %s\n", qalGetString(AL_RENDERER));
Com_Printf("AL_VERSION: %s\n", qalGetString(AL_VERSION));
Com_Printf("AL_EXTENSIONS: %s\n", qalGetString(AL_EXTENSIONS));
Com_Printf("Number of sources: %d\n", s_numchannels);
}
qboolean AL_Init(void)
{
int i;
Com_DPrintf("Initializing OpenAL\n");
if (!QAL_Init()) {
goto fail0;
}
// check for linear distance extension
if (!qalIsExtensionPresent("AL_EXT_LINEAR_DISTANCE")) {
Com_SetLastError("AL_EXT_LINEAR_DISTANCE extension is missing");
goto fail1;
}
// generate source names
qalGetError();
for (i = 0; i < MAX_CHANNELS; i++) {
qalGenSources(1, &s_srcnums[i]);
if (qalGetError() != AL_NO_ERROR) {
break;
}
}
Com_DPrintf("Got %d AL sources\n", i);
if (i < MIN_CHANNELS) {
Com_SetLastError("Insufficient number of AL sources");
goto fail1;
}
s_numchannels = i;
Com_Printf("OpenAL initialized.\n");
return qtrue;
fail1:
QAL_Shutdown();
fail0:
Com_EPrintf("Failed to initialize OpenAL: %s\n", Com_GetLastError());
return qfalse;
}
void AL_Shutdown(void)
{
Com_Printf("Shutting down OpenAL.\n");
if (s_numchannels) {
// delete source names
qalDeleteSources(s_numchannels, s_srcnums);
memset(s_srcnums, 0, sizeof(s_srcnums));
s_numchannels = 0;
}
QAL_Shutdown();
}
sfxcache_t *AL_UploadSfx(sfx_t *s)
{
sfxcache_t *sc;
ALsizei size = s_info.samples * s_info.width;
ALenum format = s_info.width == 2 ? AL_FORMAT_MONO16 : AL_FORMAT_MONO8;
ALuint name;
if (!size) {
s->error = Q_ERR_TOO_FEW;
return NULL;
}
qalGetError();
qalGenBuffers(1, &name);
qalBufferData(name, format, s_info.data, size, s_info.rate);
if (qalGetError() != AL_NO_ERROR) {
s->error = Q_ERR_LIBRARY_ERROR;
return NULL;
}
#if 0
// specify OpenAL-Soft style loop points
if (s_info.loopstart > 0 && qalIsExtensionPresent("AL_SOFT_loop_points")) {
ALint points[2] = { s_info.loopstart, s_info.samples };
qalBufferiv(name, AL_LOOP_POINTS_SOFT, points);
}
#endif
// allocate placeholder sfxcache
sc = s->cache = S_Malloc(sizeof(*sc));
sc->length = s_info.samples * 1000 / s_info.rate; // in msec
sc->loopstart = s_info.loopstart;
sc->width = s_info.width;
sc->size = size;
sc->bufnum = name;
return sc;
}
void AL_DeleteSfx(sfx_t *s)
{
sfxcache_t *sc;
ALuint name;
sc = s->cache;
if (!sc) {
return;
}
name = sc->bufnum;
qalDeleteBuffers(1, &name);
}
static void AL_Spatialize(channel_t *ch)
{
vec3_t origin;
// anything coming from the view entity will always be full volume
// no attenuation = no spatialization
if (ch->entnum == -1 || ch->entnum == listener_entnum || !ch->dist_mult) {
VectorCopy(listener_origin, origin);
} else if (ch->fixed_origin) {
VectorCopy(ch->origin, origin);
} else {
CL_GetEntitySoundOrigin(ch->entnum, origin);
}
qalSource3f(ch->srcnum, AL_POSITION, AL_UnpackVector(origin));
}
void AL_StopChannel(channel_t *ch)
{
#ifdef _DEBUG
if (s_show->integer > 1)
Com_Printf("%s: %s\n", __func__, ch->sfx->name);
#endif
// stop it
qalSourceStop(ch->srcnum);
qalSourcei(ch->srcnum, AL_BUFFER, AL_NONE);
memset(ch, 0, sizeof(*ch));
}
void AL_PlayChannel(channel_t *ch)
{
sfxcache_t *sc = ch->sfx->cache;
#ifdef _DEBUG
if (s_show->integer > 1)
Com_Printf("%s: %s\n", __func__, ch->sfx->name);
#endif
ch->srcnum = s_srcnums[ch - channels];
qalGetError();
qalSourcei(ch->srcnum, AL_BUFFER, sc->bufnum);
if (ch->autosound /*|| sc->loopstart >= 0*/) {
qalSourcei(ch->srcnum, AL_LOOPING, AL_TRUE);
} else {
qalSourcei(ch->srcnum, AL_LOOPING, AL_FALSE);
}
qalSourcef(ch->srcnum, AL_GAIN, ch->master_vol);
qalSourcef(ch->srcnum, AL_REFERENCE_DISTANCE, SOUND_FULLVOLUME);
qalSourcef(ch->srcnum, AL_MAX_DISTANCE, 8192);
qalSourcef(ch->srcnum, AL_ROLLOFF_FACTOR, ch->dist_mult * (8192 - SOUND_FULLVOLUME));
AL_Spatialize(ch);
// play it
qalSourcePlay(ch->srcnum);
if (qalGetError() != AL_NO_ERROR) {
AL_StopChannel(ch);
}
}
static void AL_IssuePlaysounds(void)
{
playsound_t *ps;
// start any playsounds
while (1) {
ps = s_pendingplays.next;
if (ps == &s_pendingplays)
break; // no more pending sounds
if (ps->begin > paintedtime)
break;
S_IssuePlaysound(ps);
}
}
void AL_StopAllChannels(void)
{
int i;
channel_t *ch;
ch = channels;
for (i = 0; i < s_numchannels; i++, ch++) {
if (!ch->sfx)
continue;
AL_StopChannel(ch);
}
}
static channel_t *AL_FindLoopingSound(int entnum, sfx_t *sfx)
{
int i;
channel_t *ch;
ch = channels;
for (i = 0; i < s_numchannels; i++, ch++) {
if (!ch->sfx)
continue;
if (!ch->autosound)
continue;
if (entnum && ch->entnum != entnum)
continue;
if (ch->sfx != sfx)
continue;
return ch;
}
return NULL;
}
static void AL_AddLoopSounds(void)
{
int i;
int sounds[MAX_EDICTS];
channel_t *ch, *ch2;
sfx_t *sfx;
sfxcache_t *sc;
int num;
entity_state_t *ent;
if (cls.state != ca_active || sv_paused->integer || !s_ambient->integer) {
return;
}
S_BuildSoundList(sounds);
for (i = 0; i < cl.frame.numEntities; i++) {
if (!sounds[i])
continue;
sfx = S_SfxForHandle(cl.sound_precache[sounds[i]]);
if (!sfx)
continue; // bad sound effect
sc = sfx->cache;
if (!sc)
continue;
num = (cl.frame.firstEntity + i) & PARSE_ENTITIES_MASK;
ent = &cl.entityStates[num];
ch = AL_FindLoopingSound(ent->number, sfx);
if (ch) {
ch->autoframe = s_framecount;
ch->end = paintedtime + sc->length;
continue;
}
// allocate a channel
ch = S_PickChannel(0, 0);
if (!ch)
continue;
ch2 = AL_FindLoopingSound(0, sfx);
ch->autosound = qtrue; // remove next frame
ch->autoframe = s_framecount;
ch->sfx = sfx;
ch->entnum = ent->number;
ch->master_vol = 1;
ch->dist_mult = SOUND_LOOPATTENUATE;
ch->end = paintedtime + sc->length;
AL_PlayChannel(ch);
// attempt to synchronize with existing sounds of the same type
if (ch2) {
ALint offset;
qalGetSourcei(ch2->srcnum, AL_SAMPLE_OFFSET, &offset);
qalSourcei(ch->srcnum, AL_SAMPLE_OFFSET, offset);
}
}
}
void AL_Update(void)
{
int i;
channel_t *ch;
vec_t orientation[6];
if (!s_active) {
return;
}
paintedtime = cl.time;
// set listener parameters
qalListener3f(AL_POSITION, AL_UnpackVector(listener_origin));
AL_CopyVector(listener_forward, orientation);
AL_CopyVector(listener_up, orientation + 3);
qalListenerfv(AL_ORIENTATION, orientation);
qalListenerf(AL_GAIN, s_volume->value);
qalDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);
// update spatialization for dynamic sounds
ch = channels;
for (i = 0; i < s_numchannels; i++, ch++) {
if (!ch->sfx)
continue;
if (ch->autosound) {
// autosounds are regenerated fresh each frame
if (ch->autoframe != s_framecount) {
AL_StopChannel(ch);
continue;
}
} else {
ALenum state;
qalGetError();
qalGetSourcei(ch->srcnum, AL_SOURCE_STATE, &state);
if (qalGetError() != AL_NO_ERROR || state == AL_STOPPED) {
AL_StopChannel(ch);
continue;
}
}
#ifdef _DEBUG
if (s_show->integer) {
Com_Printf("%.1f %s\n", ch->master_vol, ch->sfx->name);
// total++;
}
#endif
AL_Spatialize(ch); // respatialize channel
}
s_framecount++;
// add loopsounds
AL_AddLoopSounds();
AL_IssuePlaysounds();
}
|