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);
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;
77 static bool lookup_all_alsyms(const char *libname)
80 if ( (aldlhandle = dlopen(libname, RTLD_GLOBAL | RTLD_NOW)) == NULL )
85 #define AL_FUNC(t,ret,fn,params,call,rt) \
86 if (!lookup_alsym(#fn, (void **) &p##fn, libname)) retval = false;
96 #define lookup_all_alsyms(x) (true)
97 #define unload_alsyms()
102 OPENAL_SAMPLE *sample;
107 typedef struct OPENAL_SAMPLE {
109 ALuint bid; // buffer id.
114 static size_t num_channels = 0;
115 static OPENAL_Channels *impl_channels = NULL;
116 static bool initialized = false;
117 static float listener_position[3];
119 static void set_channel_position(const int channel, const float x,
120 const float y, const float z)
122 OPENAL_Channels *chan = &impl_channels[channel];
124 chan->position[0] = x;
125 chan->position[1] = y;
126 chan->position[2] = z;
128 OPENAL_SAMPLE *sptr = chan->sample;
132 const ALuint sid = chan->sid;
133 const bool no_attenuate = sptr->is2d;
136 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
137 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
139 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
140 alSource3f(sid, AL_POSITION, x, y, z);
145 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)
150 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
151 listener_position[0] = pos[0];
152 listener_position[1] = pos[1];
153 listener_position[2] = -pos[2];
156 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
157 alListenerfv(AL_ORIENTATION, vec);
159 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
161 // adjust existing positions...
162 for (int i = 0; i < num_channels; i++) {
163 const float *p = impl_channels[i].position;
164 set_channel_position(i, p[0], p[1], p[2]);
168 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
172 if ((channel < 0) || (channel >= num_channels))
176 set_channel_position(channel, pos[0], pos[1], -pos[2]);
178 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
183 AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ &pos, const float *vel)
187 if ((channel < 0) || (channel >= num_channels))
190 set_channel_position(channel, pos.x, pos.y, -pos.z);
195 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
199 if (maxsoftwarechannels == 0)
202 if (flags != 0) // unsupported.
205 if (!lookup_all_alsyms("./openal.so")) { // !!! FIXME: linux specific lib name
206 if (!lookup_all_alsyms("openal.so.1")) { // !!! FIXME: linux specific lib name
207 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
212 ALCdevice *dev = alcOpenDevice(NULL);
216 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
217 ALCcontext *ctx = alcCreateContext(dev, caps);
223 alcMakeContextCurrent(ctx);
224 alcProcessContext(ctx);
226 bool cmdline(const char * cmd);
227 if (cmdline("openalinfo")) {
228 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
229 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
230 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
231 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
234 num_channels = maxsoftwarechannels;
235 impl_channels = new OPENAL_Channels[maxsoftwarechannels];
236 memset(impl_channels, '\0', sizeof (OPENAL_Channels) * num_channels);
237 for (int i = 0; i < num_channels; i++)
238 alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail!
244 AL_API void OPENAL_Close()
249 ALCcontext *ctx = alcGetCurrentContext();
251 for (int i = 0; i < num_channels; i++) {
252 alSourceStop(impl_channels[i].sid);
253 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
254 alDeleteSources(1, &impl_channels[i].sid);
256 ALCdevice *dev = alcGetContextsDevice(ctx);
257 alcMakeContextCurrent(NULL);
258 alcSuspendContext(ctx);
259 alcDestroyContext(ctx);
264 delete[] impl_channels;
265 impl_channels = NULL;
271 static OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
275 if ((channel < 0) || (channel >= num_channels))
277 return impl_channels[channel].sample;
280 static signed char OPENAL_GetPaused(int channel)
284 if ((channel < 0) || (channel >= num_channels))
286 if (impl_channels[channel].startpaused)
290 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
291 return((state == AL_PAUSED) ? true : false);
294 static unsigned int OPENAL_GetLoopMode(int channel)
298 if ((channel < 0) || (channel >= num_channels))
301 alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
303 return(OPENAL_LOOP_NORMAL);
304 return OPENAL_LOOP_OFF;
307 static signed char OPENAL_IsPlaying(int channel)
311 if ((channel < 0) || (channel >= num_channels))
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)
326 if (channel == OPENAL_FREE) {
327 for (int i = 0; i < num_channels; i++) {
329 alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
330 if ((state != AL_PLAYING) && (state != AL_PAUSED)) {
337 if ((channel < 0) || (channel >= num_channels))
339 alSourceStop(impl_channels[channel].sid);
340 impl_channels[channel].sample = sptr;
341 alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
342 alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
343 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
345 impl_channels[channel].startpaused = ((startpaused) ? true : false);
347 alSourcePlay(impl_channels[channel].sid);
352 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
355 const int bigendian = 1;
357 const int bigendian = 0;
360 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
361 char *fname = (char *) alloca(strlen(_fname) + 16);
362 strcpy(fname, _fname);
363 char *ptr = strchr(fname, '.');
366 strcat(fname, ".ogg");
370 FILE *io = fopen(fname, "rb");
374 ALubyte *retval = NULL;
376 #if 0 // untested, so disable this!
377 // Can we just feed it to the AL compressed?
378 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis")) {
379 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
381 fseek(io, 0, SEEK_END);
383 fseek(io, 0, SEEK_SET);
384 retval = (ALubyte *) malloc(size);
385 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) {
400 vorbis_info *info = ov_info(&vf, -1);
402 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
405 if ((info->channels != 1) && (info->channels != 2)) {
412 size_t allocated = 64 * 1024;
413 retval = (ALubyte *) malloc(allocated);
414 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 ) {
417 if (size >= allocated) {
419 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
427 memcpy(retval + (size - rc), buf, rc);
439 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
443 if (index != OPENAL_FREE)
444 return NULL; // this is all the game does...
446 return NULL; // this is all the game does...
448 return NULL; // this is all the game does...
449 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D))
450 return NULL; // this is all the game does...
452 OPENAL_SAMPLE *retval = NULL;
453 ALuint bufferName = 0;
454 ALenum format = AL_NONE;
456 ALuint frequency = 0;
457 void *data = decode_to_pcm(name_or_data, format, size, frequency);
463 alGenBuffers(1, &bid);
464 if (alGetError() == AL_NO_ERROR) {
465 alBufferData(bid, format, data, size, frequency);
466 retval = new OPENAL_SAMPLE;
468 retval->mode = OPENAL_LOOP_OFF;
469 retval->is2d = (mode == OPENAL_2D);
470 retval->name = new char[strlen(name_or_data) + 1];
472 strcpy(retval->name, name_or_data);
479 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
484 for (int i = 0; i < num_channels; i++) {
485 if (impl_channels[i].sample == sptr) {
486 alSourceStop(impl_channels[i].sid);
487 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
488 impl_channels[i].sample = NULL;
491 alDeleteBuffers(1, &sptr->bid);
497 static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
501 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF))
509 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
513 if (channel == OPENAL_ALL) {
514 for (int i = 0; i < num_channels; i++)
515 OPENAL_SetFrequency(i, freq);
519 if ((channel < 0) || (channel >= num_channels))
523 alSourcef(impl_channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
525 alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
529 AL_API signed char OPENAL_SetVolume(int channel, int vol)
534 if (channel == OPENAL_ALL) {
535 for (int i = 0; i < num_channels; i++)
536 OPENAL_SetVolume(i, vol);
540 if ((channel < 0) || (channel >= num_channels))
547 ALfloat gain = ((ALfloat) vol) / 255.0f;
548 alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
552 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
557 if (channel == OPENAL_ALL) {
558 for (int i = 0; i < num_channels; i++)
559 OPENAL_SetPaused(i, paused);
563 if ((channel < 0) || (channel >= num_channels))
567 if (impl_channels[channel].startpaused)
570 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
572 if ((paused) && (state == AL_PLAYING))
573 alSourcePause(impl_channels[channel].sid);
574 else if ((!paused) && (state == AL_PAUSED)) {
575 alSourcePlay(impl_channels[channel].sid);
576 impl_channels[channel].startpaused = false;
581 AL_API void OPENAL_SetSFXMasterVolume(int volume)
585 ALfloat gain = ((ALfloat) volume) / 255.0f;
586 alListenerf(AL_GAIN, gain);
589 AL_API signed char OPENAL_StopSound(int channel)
594 if (channel == OPENAL_ALL) {
595 for (int i = 0; i < num_channels; i++)
600 if ((channel < 0) || (channel >= num_channels))
602 alSourceStop(impl_channels[channel].sid);
603 impl_channels[channel].startpaused = false;
607 AL_API void OPENAL_Stream_Close(OPENAL_STREAM *stream)
609 OPENAL_Sample_Free((OPENAL_SAMPLE *) stream);
612 static OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
616 return (OPENAL_SAMPLE *) stream;
619 static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
621 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
624 static signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
628 for (int i = 0; i < num_channels; i++) {
629 if (impl_channels[i].sample == (OPENAL_SAMPLE *) stream) {
630 alSourceStop(impl_channels[i].sid);
631 impl_channels[i].startpaused = false;
637 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
639 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
642 AL_API void OPENAL_Update()
646 alcProcessContext(alcGetCurrentContext());
649 AL_API signed char OPENAL_SetOutput(int outputtype)
654 extern int channels[];
656 extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
658 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
659 if (currSample && currSample == samp[chan]) {
660 if (OPENAL_GetPaused(channels[chan])) {
661 OPENAL_StopSound(channels[chan]);
662 channels[chan] = OPENAL_FREE;
663 } else if (OPENAL_IsPlaying(channels[chan])) {
664 int loop_mode = OPENAL_GetLoopMode(channels[chan]);
665 if (loop_mode & OPENAL_LOOP_OFF) {
666 channels[chan] = OPENAL_FREE;
670 channels[chan] = OPENAL_FREE;
673 channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
674 if (channels[chan] < 0) {
675 channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
679 extern "C" void PlayStreamEx(int chan, OPENAL_STREAM *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
681 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
682 if (currSample && currSample == OPENAL_Stream_GetSample(sptr)) {
683 OPENAL_StopSound(channels[chan]);
684 OPENAL_Stream_Stop(sptr);
686 OPENAL_Stream_Stop(sptr);
687 channels[chan] = OPENAL_FREE;
690 channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
691 if (channels[chan] < 0) {
692 channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);