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
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program 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.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "openal_wrapper.h"
32 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
33 // one...so we just need to flip the sign on the Z axis when appropriate.
35 #define DYNAMIC_LOAD_OPENAL 0
37 #if DYNAMIC_LOAD_OPENAL
41 #define AL_FUNC(t,ret,fn,params,call,rt) \
43 static ret ALAPIENTRY (*p##fn) params = NULL; \
44 ret ALAPIENTRY fn params { rt p##fn call; } \
49 static void *aldlhandle = NULL;
51 static bool lookup_alsym(const char *funcname, void **func, const char *libname)
56 *func = dlsym(aldlhandle, funcname);
59 fprintf(stderr, "Failed to find OpenAL symbol \"%s\" in \"%s\"\n",
66 static void unload_alsyms(void)
68 #define AL_FUNC(t,ret,fn,params,call,rt) p##fn = NULL;
78 static bool lookup_all_alsyms(const char *libname)
82 if ( (aldlhandle = dlopen(libname, RTLD_GLOBAL | RTLD_NOW)) == NULL )
87 #define AL_FUNC(t,ret,fn,params,call,rt) \
88 if (!lookup_alsym(#fn, (void **) &p##fn, libname)) retval = false;
98 #define lookup_all_alsyms(x) (true)
99 #define unload_alsyms()
105 OPENAL_SAMPLE *sample;
110 typedef struct OPENAL_SAMPLE
113 ALuint bid; // buffer id.
118 static size_t num_channels = 0;
119 static OPENAL_Channels *impl_channels = NULL;
120 static bool initialized = false;
121 static float listener_position[3];
123 static void set_channel_position(const int channel, const float x,
124 const float y, const float z)
126 OPENAL_Channels *chan = &impl_channels[channel];
128 chan->position[0] = x;
129 chan->position[1] = y;
130 chan->position[2] = z;
132 OPENAL_SAMPLE *sptr = chan->sample;
136 const ALuint sid = chan->sid;
137 const bool no_attenuate = sptr->is2d;
141 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
142 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
146 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
147 alSource3f(sid, AL_POSITION, x, y, z);
152 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)
154 if (!initialized) return;
157 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
158 listener_position[0] = pos[0];
159 listener_position[1] = pos[1];
160 listener_position[2] = -pos[2];
163 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
164 alListenerfv(AL_ORIENTATION, vec);
166 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
168 // adjust existing positions...
169 for (int i = 0; i < num_channels; i++)
171 const float *p = impl_channels[i].position;
172 set_channel_position(i, p[0], p[1], p[2]);
176 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
178 if (!initialized) return false;
179 if ((channel < 0) || (channel >= num_channels)) return false;
182 set_channel_position(channel, pos[0], pos[1], -pos[2]);
184 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
189 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
191 if (initialized) return false;
192 if (maxsoftwarechannels == 0) return false;
194 if (flags != 0) // unsupported.
197 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
199 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
201 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
206 ALCdevice *dev = alcOpenDevice(NULL);
210 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
211 ALCcontext *ctx = alcCreateContext(dev, caps);
218 alcMakeContextCurrent(ctx);
219 alcProcessContext(ctx);
221 bool cmdline(const char *cmd);
222 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()
242 if (!initialized) return;
244 ALCcontext *ctx = alcGetCurrentContext();
247 for (int i = 0; i < num_channels; i++)
249 alSourceStop(impl_channels[i].sid);
250 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
251 alDeleteSources(1, &impl_channels[i].sid);
253 ALCdevice *dev = alcGetContextsDevice(ctx);
254 alcMakeContextCurrent(NULL);
255 alcSuspendContext(ctx);
256 alcDestroyContext(ctx);
261 delete[] impl_channels;
262 impl_channels = NULL;
268 static OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
270 if (!initialized) return NULL;
271 if ((channel < 0) || (channel >= num_channels)) return NULL;
272 return impl_channels[channel].sample;
275 static signed char OPENAL_GetPaused(int channel)
277 if (!initialized) return false;
278 if ((channel < 0) || (channel >= num_channels)) return false;
279 if (impl_channels[channel].startpaused)
283 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
284 return((state == AL_PAUSED) ? true : false);
287 static unsigned int OPENAL_GetLoopMode(int channel)
289 if (!initialized) return 0;
290 if ((channel < 0) || (channel >= num_channels)) return 0;
292 alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
294 return(OPENAL_LOOP_NORMAL);
295 return OPENAL_LOOP_OFF;
298 static signed char OPENAL_IsPlaying(int channel)
300 if (!initialized) return false;
301 if ((channel < 0) || (channel >= num_channels)) return false;
303 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
304 return((state == AL_PLAYING) ? true : false);
307 static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
309 if (!initialized) return -1;
310 if (sptr == NULL) return -1;
311 if (dsp != NULL) return -1;
312 if (channel == OPENAL_FREE)
314 for (int i = 0; i < num_channels; i++)
317 alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
318 if ((state != AL_PLAYING) && (state != AL_PAUSED))
326 if ((channel < 0) || (channel >= num_channels)) return -1;
327 alSourceStop(impl_channels[channel].sid);
328 impl_channels[channel].sample = sptr;
329 alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
330 alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
331 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
333 impl_channels[channel].startpaused = ((startpaused) ? true : false);
335 alSourcePlay(impl_channels[channel].sid);
340 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
343 const int bigendian = 1;
345 const int bigendian = 0;
348 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
349 char *fname = (char *) alloca(strlen(_fname) + 16);
350 strcpy(fname, _fname);
351 char *ptr = strchr(fname, '.');
352 if (ptr) *ptr = '\0';
353 strcat(fname, ".ogg");
357 FILE *io = fopen(fname, "rb");
361 ALubyte *retval = NULL;
363 #if 0 // untested, so disable this!
364 // Can we just feed it to the AL compressed?
365 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
367 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
369 fseek(io, 0, SEEK_END);
371 fseek(io, 0, SEEK_SET);
372 retval = (ALubyte *) malloc(size);
373 size_t rc = fread(retval, size, 1, io);
384 // Uncompress and feed to the AL.
386 memset(&vf, '\0', sizeof (vf));
387 if (ov_open(io, &vf, NULL, 0) == 0)
390 vorbis_info *info = ov_info(&vf, -1);
392 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
395 if ((info->channels != 1) && (info->channels != 2))
403 size_t allocated = 64 * 1024;
404 retval = (ALubyte *) malloc(allocated);
405 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
410 if (size >= allocated)
413 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
422 memcpy(retval + (size - rc), buf, rc);
434 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
436 if (!initialized) return NULL;
437 if (index != OPENAL_FREE) return NULL; // this is all the game does...
438 if (offset != 0) return NULL; // this is all the game does...
439 if (length != 0) return NULL; // this is all the game does...
440 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) return NULL; // this is all the game does...
442 OPENAL_SAMPLE *retval = NULL;
443 ALuint bufferName = 0;
444 ALenum format = AL_NONE;
446 ALuint frequency = 0;
447 void *data = decode_to_pcm(name_or_data, format, size, frequency);
453 alGenBuffers(1, &bid);
454 if (alGetError() == AL_NO_ERROR)
456 alBufferData(bid, format, data, size, frequency);
457 retval = new OPENAL_SAMPLE;
459 retval->mode = OPENAL_LOOP_OFF;
460 retval->is2d = (mode == OPENAL_2D);
461 retval->name = new char[strlen(name_or_data) + 1];
463 strcpy(retval->name, name_or_data);
470 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
472 if (!initialized) return;
475 for (int i = 0; i < num_channels; i++)
477 if (impl_channels[i].sample == sptr)
479 alSourceStop(impl_channels[i].sid);
480 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
481 impl_channels[i].sample = NULL;
484 alDeleteBuffers(1, &sptr->bid);
490 static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
492 if (!initialized) return false;
493 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF)) return false;
494 if (!sptr) return false;
499 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
501 if (!initialized) return false;
502 if (channel == OPENAL_ALL)
504 for (int i = 0; i < num_channels; i++)
505 OPENAL_SetFrequency(i, freq);
509 if ((channel < 0) || (channel >= num_channels)) return false;
510 if (freq == 8012) // hack
511 alSourcef(impl_channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
513 alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
517 AL_API signed char OPENAL_SetVolume(int channel, int vol)
519 if (!initialized) return false;
521 if (channel == OPENAL_ALL)
523 for (int i = 0; i < num_channels; i++)
524 OPENAL_SetVolume(i, vol);
528 if ((channel < 0) || (channel >= num_channels)) return false;
530 if (vol < 0) vol = 0;
531 else if (vol > 255) vol = 255;
532 ALfloat gain = ((ALfloat) vol) / 255.0f;
533 alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
537 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
539 if (!initialized) return false;
541 if (channel == OPENAL_ALL)
543 for (int i = 0; i < num_channels; i++)
544 OPENAL_SetPaused(i, paused);
548 if ((channel < 0) || (channel >= num_channels)) return false;
551 if (impl_channels[channel].startpaused)
554 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
556 if ((paused) && (state == AL_PLAYING))
557 alSourcePause(impl_channels[channel].sid);
558 else if ((!paused) && (state == AL_PAUSED))
560 alSourcePlay(impl_channels[channel].sid);
561 impl_channels[channel].startpaused = false;
566 AL_API void OPENAL_SetSFXMasterVolume(int volume)
568 if (!initialized) return;
569 ALfloat gain = ((ALfloat) volume) / 255.0f;
570 alListenerf(AL_GAIN, gain);
573 AL_API signed char OPENAL_StopSound(int channel)
575 if (!initialized) return false;
577 if (channel == OPENAL_ALL)
579 for (int i = 0; i < num_channels; i++)
584 if ((channel < 0) || (channel >= num_channels)) return false;
585 alSourceStop(impl_channels[channel].sid);
586 impl_channels[channel].startpaused = false;
590 AL_API void OPENAL_Stream_Close(OPENAL_STREAM *stream)
592 OPENAL_Sample_Free((OPENAL_SAMPLE *) stream);
595 static OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
597 if (!initialized) return NULL;
598 return (OPENAL_SAMPLE *) stream;
601 static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
603 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
606 static signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
608 if (!initialized) return false;
609 for (int i = 0; i < num_channels; i++)
611 if (impl_channels[i].sample == (OPENAL_SAMPLE *) stream)
613 alSourceStop(impl_channels[i].sid);
614 impl_channels[i].startpaused = false;
620 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
622 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
625 AL_API void OPENAL_Update()
627 if (!initialized) return;
628 alcProcessContext(alcGetCurrentContext());
631 AL_API signed char OPENAL_SetOutput(int outputtype)
636 extern int channels[100];
638 extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
640 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
641 if (currSample && currSample == samp[chan])
643 if (OPENAL_GetPaused(channels[chan]))
645 OPENAL_StopSound(channels[chan]);
646 channels[chan] = OPENAL_FREE;
648 else if (OPENAL_IsPlaying(channels[chan]))
650 int loop_mode = OPENAL_GetLoopMode(channels[chan]);
651 if (loop_mode & OPENAL_LOOP_OFF)
653 channels[chan] = OPENAL_FREE;
659 channels[chan] = OPENAL_FREE;
662 channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
663 if (channels[chan] < 0)
665 channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
669 extern "C" void PlayStreamEx(int chan, OPENAL_STREAM *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
671 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
672 if (currSample && currSample == OPENAL_Stream_GetSample(sptr))
674 OPENAL_StopSound(channels[chan]);
675 OPENAL_Stream_Stop(sptr);
679 OPENAL_Stream_Stop(sptr);
680 channels[chan] = OPENAL_FREE;
683 channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
684 if (channels[chan] < 0)
686 channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);