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 "Quaternions.h"
29 #include "openal_wrapper.h"
33 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
34 // one...so we just need to flip the sign on the Z axis when appropriate.
36 #define DYNAMIC_LOAD_OPENAL 0
38 #if DYNAMIC_LOAD_OPENAL
42 #define AL_FUNC(t,ret,fn,params,call,rt) \
44 static ret ALAPIENTRY (*p##fn) params = NULL; \
45 ret ALAPIENTRY fn params { rt p##fn call; } \
50 static void *aldlhandle = NULL;
52 static bool lookup_alsym(const char *funcname, void **func, const char *libname)
57 *func = dlsym(aldlhandle, funcname);
60 fprintf(stderr, "Failed to find OpenAL symbol \"%s\" in \"%s\"\n",
67 static void unload_alsyms(void)
69 #define AL_FUNC(t,ret,fn,params,call,rt) p##fn = NULL;
79 static bool lookup_all_alsyms(const char *libname)
83 if ( (aldlhandle = dlopen(libname, RTLD_GLOBAL | RTLD_NOW)) == NULL )
88 #define AL_FUNC(t,ret,fn,params,call,rt) \
89 if (!lookup_alsym(#fn, (void **) &p##fn, libname)) retval = false;
99 #define lookup_all_alsyms(x) (true)
100 #define unload_alsyms()
106 OPENAL_SAMPLE *sample;
111 typedef struct OPENAL_SAMPLE
114 ALuint bid; // buffer id.
119 static size_t num_channels = 0;
120 static OPENAL_Channels *impl_channels = NULL;
121 static bool initialized = false;
122 static float listener_position[3];
124 static void set_channel_position(const int channel, const float x,
125 const float y, const float z)
127 OPENAL_Channels *chan = &impl_channels[channel];
129 chan->position[0] = x;
130 chan->position[1] = y;
131 chan->position[2] = z;
133 OPENAL_SAMPLE *sptr = chan->sample;
137 const ALuint sid = chan->sid;
138 const bool no_attenuate = sptr->is2d;
142 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
143 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
147 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
148 alSource3f(sid, AL_POSITION, x, y, z);
153 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)
155 if (!initialized) return;
158 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
159 listener_position[0] = pos[0];
160 listener_position[1] = pos[1];
161 listener_position[2] = -pos[2];
164 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
165 alListenerfv(AL_ORIENTATION, vec);
167 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
169 // adjust existing positions...
170 for (int i = 0; i < num_channels; i++)
172 const float *p = impl_channels[i].position;
173 set_channel_position(i, p[0], p[1], p[2]);
177 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
179 if (!initialized) return false;
180 if ((channel < 0) || (channel >= num_channels)) return false;
183 set_channel_position(channel, pos[0], pos[1], -pos[2]);
185 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
190 AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ &pos, const float *vel)
192 if (!initialized) return false;
193 if ((channel < 0) || (channel >= num_channels)) return false;
195 set_channel_position(channel, pos.x, pos.y, -pos.z);
200 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
202 if (initialized) return false;
203 if (maxsoftwarechannels == 0) return false;
205 if (flags != 0) // unsupported.
208 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
210 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
212 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
217 ALCdevice *dev = alcOpenDevice(NULL);
221 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
222 ALCcontext *ctx = alcCreateContext(dev, caps);
229 alcMakeContextCurrent(ctx);
230 alcProcessContext(ctx);
232 bool cmdline(const char *cmd);
233 if (cmdline("openalinfo"))
235 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
236 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
237 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
238 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
241 num_channels = maxsoftwarechannels;
242 impl_channels = new OPENAL_Channels[maxsoftwarechannels];
243 memset(impl_channels, '\0', sizeof (OPENAL_Channels) * num_channels);
244 for (int i = 0; i < num_channels; i++)
245 alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail!
251 AL_API void OPENAL_Close()
253 if (!initialized) return;
255 ALCcontext *ctx = alcGetCurrentContext();
258 for (int i = 0; i < num_channels; i++)
260 alSourceStop(impl_channels[i].sid);
261 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
262 alDeleteSources(1, &impl_channels[i].sid);
264 ALCdevice *dev = alcGetContextsDevice(ctx);
265 alcMakeContextCurrent(NULL);
266 alcSuspendContext(ctx);
267 alcDestroyContext(ctx);
272 delete[] impl_channels;
273 impl_channels = NULL;
279 static OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
281 if (!initialized) return NULL;
282 if ((channel < 0) || (channel >= num_channels)) return NULL;
283 return impl_channels[channel].sample;
286 static signed char OPENAL_GetPaused(int channel)
288 if (!initialized) return false;
289 if ((channel < 0) || (channel >= num_channels)) return false;
290 if (impl_channels[channel].startpaused)
294 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
295 return((state == AL_PAUSED) ? true : false);
298 static unsigned int OPENAL_GetLoopMode(int channel)
300 if (!initialized) return 0;
301 if ((channel < 0) || (channel >= num_channels)) return 0;
303 alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
305 return(OPENAL_LOOP_NORMAL);
306 return OPENAL_LOOP_OFF;
309 static signed char OPENAL_IsPlaying(int channel)
311 if (!initialized) return false;
312 if ((channel < 0) || (channel >= num_channels)) return false;
314 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
315 return((state == AL_PLAYING) ? true : false);
318 static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
320 if (!initialized) return -1;
321 if (sptr == NULL) return -1;
322 if (dsp != NULL) return -1;
323 if (channel == OPENAL_FREE)
325 for (int i = 0; i < num_channels; i++)
328 alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
329 if ((state != AL_PLAYING) && (state != AL_PAUSED))
337 if ((channel < 0) || (channel >= num_channels)) return -1;
338 alSourceStop(impl_channels[channel].sid);
339 impl_channels[channel].sample = sptr;
340 alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
341 alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
342 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
344 impl_channels[channel].startpaused = ((startpaused) ? true : false);
346 alSourcePlay(impl_channels[channel].sid);
351 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
354 const int bigendian = 1;
356 const int bigendian = 0;
359 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
360 char *fname = (char *) alloca(strlen(_fname) + 16);
361 strcpy(fname, _fname);
362 char *ptr = strchr(fname, '.');
363 if (ptr) *ptr = '\0';
364 strcat(fname, ".ogg");
368 FILE *io = fopen(fname, "rb");
372 ALubyte *retval = NULL;
374 #if 0 // untested, so disable this!
375 // Can we just feed it to the AL compressed?
376 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
378 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
380 fseek(io, 0, SEEK_END);
382 fseek(io, 0, SEEK_SET);
383 retval = (ALubyte *) malloc(size);
384 size_t rc = fread(retval, size, 1, io);
395 // Uncompress and feed to the AL.
397 memset(&vf, '\0', sizeof (vf));
398 if (ov_open(io, &vf, NULL, 0) == 0)
401 vorbis_info *info = ov_info(&vf, -1);
403 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
406 if ((info->channels != 1) && (info->channels != 2))
414 size_t allocated = 64 * 1024;
415 retval = (ALubyte *) malloc(allocated);
416 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
421 if (size >= allocated)
424 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
433 memcpy(retval + (size - rc), buf, rc);
445 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
447 if (!initialized) return NULL;
448 if (index != OPENAL_FREE) return NULL; // this is all the game does...
449 if (offset != 0) return NULL; // this is all the game does...
450 if (length != 0) return NULL; // this is all the game does...
451 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) return NULL; // this is all the game does...
453 OPENAL_SAMPLE *retval = NULL;
454 ALuint bufferName = 0;
455 ALenum format = AL_NONE;
457 ALuint frequency = 0;
458 void *data = decode_to_pcm(name_or_data, format, size, frequency);
464 alGenBuffers(1, &bid);
465 if (alGetError() == AL_NO_ERROR)
467 alBufferData(bid, format, data, size, frequency);
468 retval = new OPENAL_SAMPLE;
470 retval->mode = OPENAL_LOOP_OFF;
471 retval->is2d = (mode == OPENAL_2D);
472 retval->name = new char[strlen(name_or_data) + 1];
474 strcpy(retval->name, name_or_data);
481 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
483 if (!initialized) return;
486 for (int i = 0; i < num_channels; i++)
488 if (impl_channels[i].sample == sptr)
490 alSourceStop(impl_channels[i].sid);
491 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
492 impl_channels[i].sample = NULL;
495 alDeleteBuffers(1, &sptr->bid);
501 static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
503 if (!initialized) return false;
504 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF)) return false;
505 if (!sptr) return false;
510 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
512 if (!initialized) return false;
513 if (channel == OPENAL_ALL)
515 for (int i = 0; i < num_channels; i++)
516 OPENAL_SetFrequency(i, freq);
520 if ((channel < 0) || (channel >= num_channels)) return false;
521 if (freq == 8012) // hack
522 alSourcef(impl_channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
524 alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
528 AL_API signed char OPENAL_SetVolume(int channel, int vol)
530 if (!initialized) return false;
532 if (channel == OPENAL_ALL)
534 for (int i = 0; i < num_channels; i++)
535 OPENAL_SetVolume(i, vol);
539 if ((channel < 0) || (channel >= num_channels)) return false;
541 if (vol < 0) vol = 0;
542 else if (vol > 255) vol = 255;
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)
550 if (!initialized) return false;
552 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)) return false;
562 if (impl_channels[channel].startpaused)
565 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
567 if ((paused) && (state == AL_PLAYING))
568 alSourcePause(impl_channels[channel].sid);
569 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)
579 if (!initialized) return;
580 ALfloat gain = ((ALfloat) volume) / 255.0f;
581 alListenerf(AL_GAIN, gain);
584 AL_API signed char OPENAL_StopSound(int channel)
586 if (!initialized) return false;
588 if (channel == OPENAL_ALL)
590 for (int i = 0; i < num_channels; i++)
595 if ((channel < 0) || (channel >= num_channels)) return false;
596 alSourceStop(impl_channels[channel].sid);
597 impl_channels[channel].startpaused = false;
601 AL_API void OPENAL_Stream_Close(OPENAL_STREAM *stream)
603 OPENAL_Sample_Free((OPENAL_SAMPLE *) stream);
606 static OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
608 if (!initialized) return NULL;
609 return (OPENAL_SAMPLE *) stream;
612 static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
614 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
617 static signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
619 if (!initialized) return false;
620 for (int i = 0; i < num_channels; i++)
622 if (impl_channels[i].sample == (OPENAL_SAMPLE *) stream)
624 alSourceStop(impl_channels[i].sid);
625 impl_channels[i].startpaused = false;
631 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
633 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
636 AL_API void OPENAL_Update()
638 if (!initialized) return;
639 alcProcessContext(alcGetCurrentContext());
642 AL_API signed char OPENAL_SetOutput(int outputtype)
647 extern int channels[100];
649 extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
651 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
652 if (currSample && currSample == samp[chan])
654 if (OPENAL_GetPaused(channels[chan]))
656 OPENAL_StopSound(channels[chan]);
657 channels[chan] = OPENAL_FREE;
659 else if (OPENAL_IsPlaying(channels[chan]))
661 int loop_mode = OPENAL_GetLoopMode(channels[chan]);
662 if (loop_mode & OPENAL_LOOP_OFF)
664 channels[chan] = OPENAL_FREE;
670 channels[chan] = OPENAL_FREE;
673 channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
674 if (channels[chan] < 0)
676 channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
680 extern "C" void PlayStreamEx(int chan, OPENAL_STREAM *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
682 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
683 if (currSample && currSample == OPENAL_Stream_GetSample(sptr))
685 OPENAL_StopSound(channels[chan]);
686 OPENAL_Stream_Stop(sptr);
690 OPENAL_Stream_Stop(sptr);
691 channels[chan] = OPENAL_FREE;
694 channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
695 if (channels[chan] < 0)
697 channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);