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"
31 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
32 // one...so we just need to flip the sign on the Z axis when appropriate.
34 #define DYNAMIC_LOAD_OPENAL 0
36 #if DYNAMIC_LOAD_OPENAL
40 #define AL_FUNC(t,ret,fn,params,call,rt) \
42 static ret ALAPIENTRY (*p##fn) params = NULL; \
43 ret ALAPIENTRY fn params { rt p##fn call; } \
48 static void *aldlhandle = NULL;
50 static bool lookup_alsym(const char *funcname, void **func, const char *libname)
55 *func = dlsym(aldlhandle, funcname);
58 fprintf(stderr, "Failed to find OpenAL symbol \"%s\" in \"%s\"\n",
65 static void unload_alsyms(void)
67 #define AL_FUNC(t,ret,fn,params,call,rt) p##fn = NULL;
77 static bool lookup_all_alsyms(const char *libname)
81 if ( (aldlhandle = dlopen(libname, RTLD_GLOBAL | RTLD_NOW)) == NULL )
86 #define AL_FUNC(t,ret,fn,params,call,rt) \
87 if (!lookup_alsym(#fn, (void **) &p##fn, libname)) retval = false;
97 #define lookup_all_alsyms(x) (true)
98 #define unload_alsyms()
104 OPENAL_SAMPLE *sample;
109 typedef struct OPENAL_SAMPLE
112 ALuint bid; // buffer id.
117 static size_t num_channels = 0;
118 static OPENAL_Channels *impl_channels = NULL;
119 static bool initialized = false;
120 static float listener_position[3];
122 static void set_channel_position(const int channel, const float x,
123 const float y, const float z)
125 OPENAL_Channels *chan = &impl_channels[channel];
127 chan->position[0] = x;
128 chan->position[1] = y;
129 chan->position[2] = z;
131 OPENAL_SAMPLE *sptr = chan->sample;
135 const ALuint sid = chan->sid;
136 const bool no_attenuate = sptr->is2d;
140 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
141 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
145 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
146 alSource3f(sid, AL_POSITION, x, y, z);
151 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)
153 if (!initialized) return;
156 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
157 listener_position[0] = pos[0];
158 listener_position[1] = pos[1];
159 listener_position[2] = -pos[2];
162 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
163 alListenerfv(AL_ORIENTATION, vec);
165 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
167 // adjust existing positions...
168 for (int i = 0; i < num_channels; i++)
170 const float *p = impl_channels[i].position;
171 set_channel_position(i, p[0], p[1], p[2]);
175 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
177 if (!initialized) return false;
178 if ((channel < 0) || (channel >= num_channels)) return false;
181 set_channel_position(channel, pos[0], pos[1], -pos[2]);
183 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
188 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
190 if (initialized) return false;
191 if (maxsoftwarechannels == 0) return false;
193 if (flags != 0) // unsupported.
196 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
198 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
200 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
205 ALCdevice *dev = alcOpenDevice(NULL);
209 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
210 ALCcontext *ctx = alcCreateContext(dev, caps);
217 alcMakeContextCurrent(ctx);
218 alcProcessContext(ctx);
220 bool cmdline(const char *cmd);
221 if (cmdline("openalinfo"))
223 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
224 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
225 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
226 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
229 num_channels = maxsoftwarechannels;
230 impl_channels = new OPENAL_Channels[maxsoftwarechannels];
231 memset(impl_channels, '\0', sizeof (OPENAL_Channels) * num_channels);
232 for (int i = 0; i < num_channels; i++)
233 alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail!
239 AL_API void OPENAL_Close()
241 if (!initialized) return;
243 ALCcontext *ctx = alcGetCurrentContext();
246 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 AL_API OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
269 if (!initialized) return NULL;
270 if ((channel < 0) || (channel >= num_channels)) return NULL;
271 return impl_channels[channel].sample;
274 AL_API signed char OPENAL_GetPaused(int channel)
276 if (!initialized) return false;
277 if ((channel < 0) || (channel >= num_channels)) return false;
278 if (impl_channels[channel].startpaused)
282 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
283 return((state == AL_PAUSED) ? true : false);
286 AL_API unsigned int OPENAL_GetLoopMode(int channel)
288 if (!initialized) return 0;
289 if ((channel < 0) || (channel >= num_channels)) return 0;
291 alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
293 return(OPENAL_LOOP_NORMAL);
294 return OPENAL_LOOP_OFF;
297 AL_API signed char OPENAL_IsPlaying(int channel)
299 if (!initialized) return false;
300 if ((channel < 0) || (channel >= num_channels)) return false;
302 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
303 return((state == AL_PLAYING) ? true : false);
306 AL_API int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
308 if (!initialized) return -1;
309 if (sptr == NULL) return -1;
310 if (dsp != NULL) return -1;
311 if (channel == OPENAL_FREE)
313 for (int i = 0; i < num_channels; i++)
316 alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
317 if ((state != AL_PLAYING) && (state != AL_PAUSED))
325 if ((channel < 0) || (channel >= num_channels)) return -1;
326 alSourceStop(impl_channels[channel].sid);
327 impl_channels[channel].sample = sptr;
328 alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
329 alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
330 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
332 impl_channels[channel].startpaused = ((startpaused) ? true : false);
334 alSourcePlay(impl_channels[channel].sid);
339 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
342 const int bigendian = 1;
344 const int bigendian = 0;
347 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
348 char *fname = (char *) alloca(strlen(_fname) + 16);
349 strcpy(fname, _fname);
350 char *ptr = strchr(fname, '.');
351 if (ptr) *ptr = '\0';
352 strcat(fname, ".ogg");
356 FILE *io = fopen(fname, "rb");
360 ALubyte *retval = NULL;
362 #if 0 // untested, so disable this!
363 // Can we just feed it to the AL compressed?
364 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
366 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
368 fseek(io, 0, SEEK_END);
370 fseek(io, 0, SEEK_SET);
371 retval = (ALubyte *) malloc(size);
372 size_t rc = fread(retval, size, 1, io);
383 // Uncompress and feed to the AL.
385 memset(&vf, '\0', sizeof (vf));
386 if (ov_open(io, &vf, NULL, 0) == 0)
389 vorbis_info *info = ov_info(&vf, -1);
391 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
394 if ((info->channels != 1) && (info->channels != 2))
402 size_t allocated = 64 * 1024;
403 retval = (ALubyte *) malloc(allocated);
404 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
409 if (size >= allocated)
412 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
421 memcpy(retval + (size - rc), buf, rc);
433 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
435 if (!initialized) return NULL;
436 if (index != OPENAL_FREE) return NULL; // this is all the game does...
437 if (offset != 0) return NULL; // this is all the game does...
438 if (length != 0) return NULL; // this is all the game does...
439 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) return NULL; // this is all the game does...
441 OPENAL_SAMPLE *retval = NULL;
442 ALuint bufferName = 0;
443 ALenum format = AL_NONE;
445 ALuint frequency = 0;
446 void *data = decode_to_pcm(name_or_data, format, size, frequency);
452 alGenBuffers(1, &bid);
453 if (alGetError() == AL_NO_ERROR)
455 alBufferData(bid, format, data, size, frequency);
456 retval = new OPENAL_SAMPLE;
458 retval->mode = OPENAL_LOOP_OFF;
459 retval->is2d = (mode == OPENAL_2D);
460 retval->name = new char[strlen(name_or_data) + 1];
462 strcpy(retval->name, name_or_data);
469 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
471 if (!initialized) return;
474 for (int i = 0; i < num_channels; i++)
476 if (impl_channels[i].sample == sptr)
478 alSourceStop(impl_channels[i].sid);
479 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
480 impl_channels[i].sample = NULL;
483 alDeleteBuffers(1, &sptr->bid);
489 AL_API signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
491 if (!initialized) return false;
492 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF)) return false;
493 if (!sptr) return false;
498 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
500 if (!initialized) return false;
501 if (channel == OPENAL_ALL)
503 for (int i = 0; i < num_channels; i++)
504 OPENAL_SetFrequency(i, freq);
508 if ((channel < 0) || (channel >= num_channels)) return false;
509 if (freq == 8012) // hack
510 alSourcef(impl_channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
512 alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
516 AL_API signed char OPENAL_SetVolume(int channel, int vol)
518 if (!initialized) return false;
520 if (channel == OPENAL_ALL)
522 for (int i = 0; i < num_channels; i++)
523 OPENAL_SetVolume(i, vol);
527 if ((channel < 0) || (channel >= num_channels)) return false;
529 if (vol < 0) vol = 0;
530 else if (vol > 255) vol = 255;
531 ALfloat gain = ((ALfloat) vol) / 255.0f;
532 alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
536 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
538 if (!initialized) return false;
540 if (channel == OPENAL_ALL)
542 for (int i = 0; i < num_channels; i++)
543 OPENAL_SetPaused(i, paused);
547 if ((channel < 0) || (channel >= num_channels)) return false;
550 if (impl_channels[channel].startpaused)
553 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
555 if ((paused) && (state == AL_PLAYING))
556 alSourcePause(impl_channels[channel].sid);
557 else if ((!paused) && (state == AL_PAUSED))
559 alSourcePlay(impl_channels[channel].sid);
560 impl_channels[channel].startpaused = false;
565 AL_API void OPENAL_SetSFXMasterVolume(int volume)
567 if (!initialized) return;
568 ALfloat gain = ((ALfloat) volume) / 255.0f;
569 alListenerf(AL_GAIN, gain);
572 AL_API signed char OPENAL_StopSound(int channel)
574 if (!initialized) return false;
576 if (channel == OPENAL_ALL)
578 for (int i = 0; i < num_channels; i++)
583 if ((channel < 0) || (channel >= num_channels)) return false;
584 alSourceStop(impl_channels[channel].sid);
585 impl_channels[channel].startpaused = false;
589 AL_API void OPENAL_Stream_Close(OPENAL_STREAM *stream)
591 OPENAL_Sample_Free((OPENAL_SAMPLE *) stream);
594 AL_API OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
596 if (!initialized) return NULL;
597 return (OPENAL_SAMPLE *) stream;
600 AL_API int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
602 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
605 AL_API signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
607 if (!initialized) return false;
608 for (int i = 0; i < num_channels; i++)
610 if (impl_channels[i].sample == (OPENAL_SAMPLE *) stream)
612 alSourceStop(impl_channels[i].sid);
613 impl_channels[i].startpaused = false;
619 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
621 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
624 AL_API void OPENAL_Update()
626 if (!initialized) return;
627 alcProcessContext(alcGetCurrentContext());
630 AL_API signed char OPENAL_SetOutput(int outputtype)