-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsound_openal.cpp
526 lines (454 loc) · 12 KB
/
sound_openal.cpp
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#ifdef linux
#include "dx_linux.h"
#ifdef USE_SDL2
#include <SDL2/SDL.h>
#else
#include <SDL/SDL.h>
#endif
#include <AL/al.h>
#include <AL/alc.h>
#include <math.h>
bool Sound3D = false; // enable 3d sound
struct {
int buffers;
int sources;
int playing;
} stats;
struct sound_buffer_t {
ALuint id;
int freq;
};
struct sound_source_t {
ALuint id;
ALuint buffer;
sound_buffer_t* buff;
bool playing;
};
//
// sound system load / unload
//
static ALCdevice* Device = NULL;
static ALCcontext* Context = NULL;
int sound_minimum_volume;
int sound_initialized = 0;
static void print_info ( void )
{
ALint version_major, version_minor;
ALenum error;
ALCdevice *device;
printf("openal: info start\n");
// Check for EAX 2.0 support
printf("EAX2.0 support = %s\n",
alIsExtensionPresent("EAX2.0")?"true":"false");
if(alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT") == AL_TRUE)
{
printf("default playback: %s\n",alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER));
printf("default capture: %s\n",alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));
{
// print all playback devices
const char * s = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
while(s && *s != '\0')
{
printf("playback: %s\n", s);
s += strlen(s)+1;
}
// print all capture devices
s = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
while(s && *s != '\0')
{
printf("capture: %s\n", s);
s += strlen(s)+1;
}
}
}
else
printf("No device enumeration available\n");
printf("Default device: %s\n", alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER));
printf("Default capture device: %s\n", (alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER)));
device = alcGetContextsDevice(alcGetCurrentContext());
if ((error = alGetError()) != AL_NO_ERROR)
printf("error: :%s\n", alGetString(error));
alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &version_major);
alcGetIntegerv(device, ALC_MINOR_VERSION, 1, &version_minor);
if ((error = alGetError()) != AL_NO_ERROR)
printf("error: :%s\n", alGetString(error));
printf("ALC version: %d.%d\n", (int)version_major, (int)version_minor);
printf("ALC extensions: %s\n", alcGetString(device, ALC_EXTENSIONS));
if ((error = alGetError()) != AL_NO_ERROR)
printf("error: :%s\n", alGetString(error));
printf("OpenAL vendor string: %s\n", alGetString(AL_VENDOR));
printf("OpenAL renderer string: %s\n", alGetString(AL_RENDERER));
printf("OpenAL version string: %s\n", alGetString(AL_VERSION));
printf("OpenAL extensions: %s\n", alGetString(AL_EXTENSIONS));
if ((error = alGetError()) != AL_NO_ERROR)
printf("error: :%s\n", alGetString(error));
printf("openal: info end\n");
}
bool sound_init( void )
{
// TODO - disabling the ability to re-init sound system
// on ubuntu using "drivers = pulse" will handle in alcOpenDevice
// is there really even a reason we need to re-init sound?
static int initialized = 0;
if(initialized)
return true;
initialized = 1;
Device = alcOpenDevice(NULL); // preferred device
if(!Device)
return false;
Context = alcCreateContext(Device,NULL);
if(!Context) {
alcCloseDevice(Device);
Device = NULL;
return false;
}
alcMakeContextCurrent(Context);
// since the game was based around dsound
// it performs some volume calculations using this value
// dsound defines DSBVOLUME_MIN as -10000
// and forsaken defined min volume as 1/3 of that
sound_minimum_volume = -10000 / 3;
// global listener sound set to 50% to reduce crackling
// TODO - we should probably have a global sound level setting
alListenerf(AL_GAIN, 0.5f);
{
ALfloat f;
ALfloat pos[3];
alGetListenerf(AL_GAIN,&f);
printf("listener gain: %f\n",f);
alGetListenerfv(AL_POSITION,pos);
printf("listener position: %f %f %f\n",pos[0],pos[1],pos[2]);
}
print_info();
sound_initialized = 1;
return true;
}
void sound_destroy( void )
{
if(!sound_initialized)
return;
ALCcontext * Context = alcGetCurrentContext();
ALCdevice * Device = alcGetContextsDevice(Context);
alcMakeContextCurrent(NULL);
alcDestroyContext(Context);
alcCloseDevice(Device);
}
//
// 3d routines
//
bool sound_listener_position( float x, float y, float z )
{
if(!sound_initialized)
return false;
alListener3f(AL_POSITION, x, y, -z);
return true;
}
bool sound_listener_velocity( float x, float y, float z )
{
if(!sound_initialized)
return false;
alListener3f(AL_VELOCITY, x, y, -z);
return true;
}
bool sound_listener_orientation(
float fx, float fy, float fz, // forward vector
float ux, float uy, float uz // up vector
)
{
if(!sound_initialized)
return false;
float vector[6] = {fx,fy,-fz,ux,uy,-uz};
alListenerfv(AL_ORIENTATION, &vector[0]);
return true;
}
// TODO - we'll want to set velocity as well
void sound_position( sound_source_t * source, float x, float y, float z, float min, float max )
{
if(!sound_initialized)
return;
alSource3f(source->id, AL_POSITION, x, y, -z);
alSourcef(source->id, AL_MAX_DISTANCE, max);
alSourcef(source->id, AL_REFERENCE_DISTANCE, min); // is this right?
}
//
// 2d routines
//
void sound_set_pitch( sound_source_t * source, float pitch )
{
if(!sound_initialized)
return;
ALfloat f = pitch ? pitch : 1.0f ; // 1.0f is default
alSourcef( source->id, AL_PITCH, f );
//printf("sound_pitch: %f\n",f);
}
void sound_set_frequency( sound_source_t * source, long frequency )
{
if(!sound_initialized)
return;
ALfloat f = (float)frequency/source->buff->freq ; // 1.0f is default
alSourcef( source->id, AL_PITCH, f );
//printf("sound_pitch: %f\n",f);
}
void sound_volume( sound_source_t * source, long millibels )
{
if(!sound_initialized)
return;
ALfloat f;
millibels = ( millibels > 0 ) ? 0 : millibels;
// gain is scaled to (silence) 0.0f through (no change) 1.0f
// millibels = hundredths of decibels (dB)
// defined in Dsound.h as (no change) 0 and (silence) -10,000
f = (ALfloat) pow(10.0, millibels/2000.0);
alSourcef(source->id, AL_GAIN, f);
//printf("sound_volume: %ld\n",millibels);
}
void sound_pan( sound_source_t * source, long _pan )
{
if(!sound_initialized)
return;
// where pan is -1 (left) to +1 (right)
// must be scaled from -1 <-> +1
// probably need to scale down by 10,000, since dsound goes from -10000 to +10000
// so:
float pan = (float) _pan / 10000.0f;
float pan2 = (float) sqrt(1 - pan*pan);
//printf("sound_pan: %f - %f\n",pan,pan2);
alSource3f(source->id, AL_POSITION, pan, pan2, 0.0f);
}
//
// play / stop
//
void sound_play( sound_source_t * source )
{
if(!sound_initialized)
return;
if(!source->playing)
stats.playing++;
source->playing = true;
//
alSourcePlay( source->id );
//
//printf("sound_play: playing sound='%s' count=%d source=%d\n",
// source->path, stats.playing, source);
}
void sound_play_looping( sound_source_t * source )
{
if(!sound_initialized)
return;
if(!source->playing)
stats.playing++;
source->playing = true;
//
alSourcei( source->id, AL_LOOPING, AL_TRUE );
sound_play( source );
//
//printf("sound_play_looping: playing %d\n",stats.playing);
}
void sound_stop( sound_source_t * source )
{
if(!sound_initialized)
return;
if(source->playing)
stats.playing--;
source->playing = false;
//
alSourceStop( source->id );
//
//printf("sound_stop: playing %d\n",stats.playing);
}
bool sound_is_playing( sound_source_t * source )
{
if(!sound_initialized)
return false;
if(!source->playing)
return false;
ALint state;
alGetSourcei( source->id, AL_SOURCE_STATE, &state );
return (state == AL_PLAYING);
}
void sound_set_position( sound_source_t * source, long newpos )
{
if(!sound_initialized)
return;
if(!source->playing)
return;
alSourcei( source->id, AL_BYTE_OFFSET, newpos );
}
long sound_get_position( sound_source_t * source )
{
if(!sound_initialized)
return 0;
if(!source->playing)
return 0;
ALint offset;
alGetSourcei( source->id, AL_BYTE_OFFSET, &offset);
return offset;
}
//
// load resources
//
sound_buffer_t * sound_load(void* data, int size, int bits, int sign, int channels, int freq)
{
if(!sound_initialized)
return NULL;
ALenum error;
ALenum format;
u_int8_t *wav_buffer;
sound_buffer_t * buffer;
// create the buffer
buffer = (sound_buffer_t*)malloc(sizeof(sound_buffer_t));
if(!buffer)
{
printf("sound_load: failed to malloc buffer\n");
return NULL;
}
// clear error code
alGetError();
// Generate Buffers
alGenBuffers(1, &(buffer->id));
if ((error = alGetError()) != AL_NO_ERROR)
{
printf("alGenBuffers: %s\n", alGetString(error));
free(buffer);
return NULL;
}
wav_buffer = (u_int8_t*)data;
if(bits == 8) // 8 bit
{
// openal only supports unsigned 8bit
if(sign)
{
int i;
for(i = 0; i < (int) size; i++)
wav_buffer[i] ^= 0x80; // converts S8 to U8
printf("sound_buffer: converted s8 to u8\n");
}
if(channels == 1)
format = AL_FORMAT_MONO8;
else
format = AL_FORMAT_STEREO8;
printf("sound_buffer: format = %s\n",
format == AL_FORMAT_MONO8 ? "mono8" : "stereo8" );
}
else // 16 bit
{
// openal only supports signed 16bit
if(!sign)
{
int i;
for(i = 0;i < (int) size/2;i++)
((u_int16_t*)wav_buffer)[i] ^= 0x8000; // converts U16 to S16
printf("sound_buffer: converted u16 to s16\n");
}
if(channels == 1)
format = AL_FORMAT_MONO16;
else
format = AL_FORMAT_STEREO16;
//printf("sound_buffer: format = %s\n",
// format == AL_FORMAT_MONO16 ? "mono16" : "stereo16" );
}
// Copy data into AL Buffer 0
alBufferData(buffer->id,format,wav_buffer,size,freq);
if ((error = alGetError()) != AL_NO_ERROR)
{
printf("alBufferData: %s\n", alGetString(error));
alDeleteBuffers(1, &buffer->id);
free(buffer);
SDL_FreeWAV(wav_buffer);
return NULL;
}
stats.buffers++;
//printf("sound_load: buffers %d sources %d playing %d buffer %d\n",
// stats.buffers,stats.sources,stats.playing,buffer);
buffer->freq = freq;
return buffer;
}
sound_source_t * sound_source( sound_buffer_t * buffer )
{
if(!sound_initialized)
return NULL;
ALenum error;
sound_source_t * source;
if(!buffer)
{
printf("sound_source: null buffer given\n");
return NULL;
}
source = (sound_source_t*)malloc(sizeof(sound_source_t));
if(!source)
{
printf("sound_source: failed to malloc source\n");
return NULL;
}
source->playing = false;
source->buffer = buffer->id;
source->buff = buffer;
// clear errors
alGetError();
// generate a new source id
alGenSources(1,&source->id);
if ((error = alGetError()) != AL_NO_ERROR)
{
printf("alGenSources: %s\n", alGetString(error));
free(source);
source = NULL;
return NULL;
}
// attach the buffer to the source
alSourcei(source->id, AL_BUFFER, source->buffer);
if ((error = alGetError()) != AL_NO_ERROR)
{
printf("alSourcei AL_BUFFER: %s\n", alGetString(error));
alDeleteSources(1,&source->id);
free(source);
source = NULL;
return NULL;
}
alSourcei(source->id,AL_SOURCE_RELATIVE,
Sound3D ? AL_FALSE : AL_TRUE);
stats.sources++;
//printf("sound_source: sources %d buffers %d playing %d source %d\n",
// stats.sources,stats.buffers,stats.playing,source);
return source;
}
//
// release resources
//
void sound_release_source( sound_source_t * source )
{
if(!source)
return;
if(source->playing)
stats.playing--;
source->playing = false;
// deleting source implicitly detaches buffer
alDeleteSources( 1, &source->id );
// clean up resources
free(source);
// show stats
stats.sources--;
//printf("sound_release_source: buffers %d sources %d playing %d source %d\n",
// stats.buffers,stats.sources,stats.playing,source);
source = NULL;
}
void sound_release_buffer( sound_buffer_t * buffer )
{
if(!buffer)
return;
// buffers will not delete if they are attached to other sources
alGetError(); // clear error so we can see if buffer is attached elsewhere
alDeleteBuffers( 1, &buffer->id );
// if buffer attached elsewhere than error is generated
if(alGetError() == AL_NO_ERROR)
stats.buffers--;
else
printf("sound_release_buffer: error buffer %d still attached to a source.\n",
buffer->id);
// clean up resources
free(buffer);
buffer = NULL;
// show stats
//printf("sound_release_buffer: buffers %d sources %d playing %d\n",
// stats.buffers,stats.sources,stats.playing);
}
#endif // SOUND_OPENAL