2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
24 #include "Quaternions.h"
25 #include "openal_wrapper.h"
29 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
30 // one...so we just need to flip the sign on the Z axis when appropriate.
32 #define DYNAMIC_LOAD_OPENAL 0
34 #if DYNAMIC_LOAD_OPENAL
38 #define AL_FUNC(t,ret,fn,params,call,rt) \
40 static ret ALAPIENTRY (*p##fn) params = NULL; \
41 ret ALAPIENTRY fn params { rt p##fn call; } \
46 static void *aldlhandle = NULL;
48 static bool lookup_alsym(const char *funcname, void **func, const char *libname)
53 *func = dlsym(aldlhandle, funcname);
55 fprintf(stderr, "Failed to find OpenAL symbol \"%s\" in \"%s\"\n",
62 static void unload_alsyms(void)
64 #define AL_FUNC(t,ret,fn,params,call,rt) p##fn = NULL;
73 static bool lookup_all_alsyms(const char *libname)
76 if ( (aldlhandle = dlopen(libname, RTLD_GLOBAL | RTLD_NOW)) == NULL )
81 #define AL_FUNC(t,ret,fn,params,call,rt) \
82 if (!lookup_alsym(#fn, (void **) &p##fn, libname)) retval = false;
92 #define lookup_all_alsyms(x) (true)
93 #define unload_alsyms()
98 OPENAL_SAMPLE *sample;
103 typedef struct OPENAL_SAMPLE {
105 ALuint bid; // buffer id.
110 static size_t num_channels = 0;
111 static OPENAL_Channels *impl_channels = NULL;
112 static bool initialized = false;
113 static float listener_position[3];
115 static void set_channel_position(const int channel, const float x,
116 const float y, const float z)
118 OPENAL_Channels *chan = &impl_channels[channel];
120 chan->position[0] = x;
121 chan->position[1] = y;
122 chan->position[2] = z;
124 OPENAL_SAMPLE *sptr = chan->sample;
128 const ALuint sid = chan->sid;
129 const bool no_attenuate = sptr->is2d;
132 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
133 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
135 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
136 alSource3f(sid, AL_POSITION, x, y, z);
141 AL_API void OPENAL_3D_Listener_SetAttributes(const float *pos, const float *vel, float fx, float fy, float fz, float tx, float ty, float tz)
146 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
147 listener_position[0] = pos[0];
148 listener_position[1] = pos[1];
149 listener_position[2] = -pos[2];
152 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
153 alListenerfv(AL_ORIENTATION, vec);
155 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
157 // adjust existing positions...
158 for (int i = 0; i < num_channels; i++) {
159 const float *p = impl_channels[i].position;
160 set_channel_position(i, p[0], p[1], p[2]);
164 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
168 if ((channel < 0) || (channel >= num_channels))
172 set_channel_position(channel, pos[0], pos[1], -pos[2]);
174 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
179 AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ &pos, const float *vel)
183 if ((channel < 0) || (channel >= num_channels))
186 set_channel_position(channel, pos.x, pos.y, -pos.z);
191 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
195 if (maxsoftwarechannels == 0)
198 if (flags != 0) // unsupported.
201 if (!lookup_all_alsyms("./openal.so")) { // !!! FIXME: linux specific lib name
202 if (!lookup_all_alsyms("openal.so.1")) { // !!! FIXME: linux specific lib name
203 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
208 ALCdevice *dev = alcOpenDevice(NULL);
212 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
213 ALCcontext *ctx = alcCreateContext(dev, caps);
219 alcMakeContextCurrent(ctx);
220 alcProcessContext(ctx);
222 bool cmdline(const char * cmd);
223 if (cmdline("openalinfo")) {
224 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
225 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
226 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
227 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
230 num_channels = maxsoftwarechannels;
231 impl_channels = new OPENAL_Channels[maxsoftwarechannels];
232 memset(impl_channels, '\0', sizeof (OPENAL_Channels) * num_channels);
233 for (int i = 0; i < num_channels; i++)
234 alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail!
240 AL_API void OPENAL_Close()
245 ALCcontext *ctx = alcGetCurrentContext();
247 for (int i = 0; i < num_channels; i++) {
248 alSourceStop(impl_channels[i].sid);
249 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
250 alDeleteSources(1, &impl_channels[i].sid);
252 ALCdevice *dev = alcGetContextsDevice(ctx);
253 alcMakeContextCurrent(NULL);
254 alcSuspendContext(ctx);
255 alcDestroyContext(ctx);
260 delete[] impl_channels;
261 impl_channels = NULL;
267 static OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
271 if ((channel < 0) || (channel >= num_channels))
273 return impl_channels[channel].sample;
276 static signed char OPENAL_GetPaused(int channel)
280 if ((channel < 0) || (channel >= num_channels))
282 if (impl_channels[channel].startpaused)
286 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
287 return((state == AL_PAUSED) ? true : false);
290 static unsigned int OPENAL_GetLoopMode(int channel)
294 if ((channel < 0) || (channel >= num_channels))
297 alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
299 return(OPENAL_LOOP_NORMAL);
300 return OPENAL_LOOP_OFF;
303 static signed char OPENAL_IsPlaying(int channel)
307 if ((channel < 0) || (channel >= num_channels))
310 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
311 return((state == AL_PLAYING) ? true : false);
314 static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
322 if (channel == OPENAL_FREE) {
323 for (int i = 0; i < num_channels; i++) {
325 alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
326 if ((state != AL_PLAYING) && (state != AL_PAUSED)) {
333 if ((channel < 0) || (channel >= num_channels))
335 alSourceStop(impl_channels[channel].sid);
336 impl_channels[channel].sample = sptr;
337 alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
338 alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
339 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
341 impl_channels[channel].startpaused = ((startpaused) ? true : false);
343 alSourcePlay(impl_channels[channel].sid);
348 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
351 const int bigendian = 1;
353 const int bigendian = 0;
356 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
357 char *fname = (char *) alloca(strlen(_fname) + 16);
358 strcpy(fname, _fname);
359 char *ptr = strchr(fname, '.');
362 strcat(fname, ".ogg");
366 FILE *io = fopen(fname, "rb");
370 ALubyte *retval = NULL;
372 #if 0 // untested, so disable this!
373 // Can we just feed it to the AL compressed?
374 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis")) {
375 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
377 fseek(io, 0, SEEK_END);
379 fseek(io, 0, SEEK_SET);
380 retval = (ALubyte *) malloc(size);
381 size_t rc = fread(retval, size, 1, io);
391 // Uncompress and feed to the AL.
393 memset(&vf, '\0', sizeof (vf));
394 if (ov_open(io, &vf, NULL, 0) == 0) {
396 vorbis_info *info = ov_info(&vf, -1);
398 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
401 if ((info->channels != 1) && (info->channels != 2)) {
408 size_t allocated = 64 * 1024;
409 retval = (ALubyte *) malloc(allocated);
410 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 ) {
413 if (size >= allocated) {
415 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
423 memcpy(retval + (size - rc), buf, rc);
435 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
439 if (index != OPENAL_FREE)
440 return NULL; // this is all the game does...
442 return NULL; // this is all the game does...
444 return NULL; // this is all the game does...
445 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D))
446 return NULL; // this is all the game does...
448 OPENAL_SAMPLE *retval = NULL;
449 ALuint bufferName = 0;
450 ALenum format = AL_NONE;
452 ALuint frequency = 0;
453 void *data = decode_to_pcm(name_or_data, format, size, frequency);
459 alGenBuffers(1, &bid);
460 if (alGetError() == AL_NO_ERROR) {
461 alBufferData(bid, format, data, size, frequency);
462 retval = new OPENAL_SAMPLE;
464 retval->mode = OPENAL_LOOP_OFF;
465 retval->is2d = (mode == OPENAL_2D);
466 retval->name = new char[strlen(name_or_data) + 1];
468 strcpy(retval->name, name_or_data);
475 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
480 for (int i = 0; i < num_channels; i++) {
481 if (impl_channels[i].sample == sptr) {
482 alSourceStop(impl_channels[i].sid);
483 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
484 impl_channels[i].sample = NULL;
487 alDeleteBuffers(1, &sptr->bid);
493 static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
497 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF))
505 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
509 if (channel == OPENAL_ALL) {
510 for (int i = 0; i < num_channels; i++)
511 OPENAL_SetFrequency(i, freq);
515 if ((channel < 0) || (channel >= num_channels))
519 alSourcef(impl_channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
521 alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
525 AL_API signed char OPENAL_SetVolume(int channel, int vol)
530 if (channel == OPENAL_ALL) {
531 for (int i = 0; i < num_channels; i++)
532 OPENAL_SetVolume(i, vol);
536 if ((channel < 0) || (channel >= num_channels))
543 ALfloat gain = ((ALfloat) vol) / 255.0f;
544 alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
548 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
553 if (channel == OPENAL_ALL) {
554 for (int i = 0; i < num_channels; i++)
555 OPENAL_SetPaused(i, paused);
559 if ((channel < 0) || (channel >= num_channels))
563 if (impl_channels[channel].startpaused)
566 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
568 if ((paused) && (state == AL_PLAYING))
569 alSourcePause(impl_channels[channel].sid);
570 else if ((!paused) && (state == AL_PAUSED)) {
571 alSourcePlay(impl_channels[channel].sid);
572 impl_channels[channel].startpaused = false;
577 AL_API void OPENAL_SetSFXMasterVolume(int volume)
581 ALfloat gain = ((ALfloat) volume) / 255.0f;
582 alListenerf(AL_GAIN, gain);
585 AL_API signed char OPENAL_StopSound(int channel)
590 if (channel == OPENAL_ALL) {
591 for (int i = 0; i < num_channels; i++)
596 if ((channel < 0) || (channel >= num_channels))
598 alSourceStop(impl_channels[channel].sid);
599 impl_channels[channel].startpaused = false;
603 AL_API void OPENAL_Stream_Close(OPENAL_STREAM *stream)
605 OPENAL_Sample_Free((OPENAL_SAMPLE *) stream);
608 static OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
612 return (OPENAL_SAMPLE *) stream;
615 static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
617 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
620 static signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
624 for (int i = 0; i < num_channels; i++) {
625 if (impl_channels[i].sample == (OPENAL_SAMPLE *) stream) {
626 alSourceStop(impl_channels[i].sid);
627 impl_channels[i].startpaused = false;
633 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
635 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
638 AL_API void OPENAL_Update()
642 alcProcessContext(alcGetCurrentContext());
645 AL_API signed char OPENAL_SetOutput(int outputtype)
650 extern int channels[];
652 extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
654 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
655 if (currSample && currSample == samp[chan]) {
656 if (OPENAL_GetPaused(channels[chan])) {
657 OPENAL_StopSound(channels[chan]);
658 channels[chan] = OPENAL_FREE;
659 } else if (OPENAL_IsPlaying(channels[chan])) {
660 int loop_mode = OPENAL_GetLoopMode(channels[chan]);
661 if (loop_mode & OPENAL_LOOP_OFF) {
662 channels[chan] = OPENAL_FREE;
666 channels[chan] = OPENAL_FREE;
669 channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
670 if (channels[chan] < 0) {
671 channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
675 extern "C" void PlayStreamEx(int chan, OPENAL_STREAM *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
677 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
678 if (currSample && currSample == OPENAL_Stream_GetSample(sptr)) {
679 OPENAL_StopSound(channels[chan]);
680 OPENAL_Stream_Stop(sptr);
682 OPENAL_Stream_Stop(sptr);
683 channels[chan] = OPENAL_FREE;
686 channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
687 if (channels[chan] < 0) {
688 channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);