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 typedef struct OPENAL_STREAM
120 ALuint bid; // buffer id.
125 static size_t num_channels = 0;
126 static OPENAL_Channels *channels = NULL;
127 static bool initialized = false;
128 static float listener_position[3];
130 static void set_channel_position(const int channel, const float x,
131 const float y, const float z)
133 OPENAL_Channels *chan = &channels[channel];
135 chan->position[0] = x;
136 chan->position[1] = y;
137 chan->position[2] = z;
139 OPENAL_SAMPLE *sptr = chan->sample;
143 const ALuint sid = chan->sid;
144 const bool no_attenuate = sptr->is2d;
148 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
149 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
153 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
154 alSource3f(sid, AL_POSITION, x, y, z);
159 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)
161 if (!initialized) return;
164 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
165 listener_position[0] = pos[0];
166 listener_position[1] = pos[1];
167 listener_position[2] = -pos[2];
170 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
171 alListenerfv(AL_ORIENTATION, vec);
173 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
175 // adjust existing positions...
176 for (int i = 0; i < num_channels; i++)
178 const float *p = channels[i].position;
179 set_channel_position(i, p[0], p[1], p[2]);
183 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
185 if (!initialized) return false;
186 if ((channel < 0) || (channel >= num_channels)) return false;
189 set_channel_position(channel, pos[0], pos[1], -pos[2]);
191 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
196 AL_API void OPENAL_3D_SetDopplerFactor(float scale)
198 if (!initialized) return;
199 // unimplemented...looks like init routines just call this with scale == 0.0f anyhow.
202 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
204 if (initialized) return false;
205 if (maxsoftwarechannels == 0) return false;
207 if (flags != 0) // unsupported.
210 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
212 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
214 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
219 ALCdevice *dev = alcOpenDevice(NULL);
223 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
224 ALCcontext *ctx = alcCreateContext(dev, caps);
231 alcMakeContextCurrent(ctx);
232 alcProcessContext(ctx);
234 bool cmdline(const char *cmd);
235 if (cmdline("openalinfo"))
237 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
238 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
239 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
240 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
243 num_channels = maxsoftwarechannels;
244 channels = new OPENAL_Channels[maxsoftwarechannels];
245 memset(channels, '\0', sizeof (OPENAL_Channels) * num_channels);
246 for (int i = 0; i < num_channels; i++)
247 alGenSources(1, &channels[i].sid); // !!! FIXME: verify this didn't fail!
253 AL_API void OPENAL_Close()
255 if (!initialized) return;
257 ALCcontext *ctx = alcGetCurrentContext();
260 for (int i = 0; i < num_channels; i++)
262 alSourceStop(channels[i].sid);
263 alSourcei(channels[i].sid, AL_BUFFER, 0);
264 alDeleteSources(1, &channels[i].sid);
266 ALCdevice *dev = alcGetContextsDevice(ctx);
267 alcMakeContextCurrent(NULL);
268 alcSuspendContext(ctx);
269 alcDestroyContext(ctx);
281 AL_API OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
283 if (!initialized) return NULL;
284 if ((channel < 0) || (channel >= num_channels)) return NULL;
285 return channels[channel].sample;
288 AL_API signed char OPENAL_GetPaused(int channel)
290 if (!initialized) return false;
291 if ((channel < 0) || (channel >= num_channels)) return false;
292 if (channels[channel].startpaused)
296 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
297 return((state == AL_PAUSED) ? true : false);
300 AL_API unsigned int OPENAL_GetLoopMode(int channel)
302 if (!initialized) return 0;
303 if ((channel < 0) || (channel >= num_channels)) return 0;
305 alGetSourceiv(channels[channel].sid, AL_LOOPING, &loop);
307 return(OPENAL_LOOP_NORMAL);
308 return OPENAL_LOOP_OFF;
311 AL_API signed char OPENAL_IsPlaying(int channel)
313 if (!initialized) return false;
314 if ((channel < 0) || (channel >= num_channels)) return false;
316 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
317 return((state == AL_PLAYING) ? true : false);
320 AL_API int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
322 if (!initialized) return -1;
323 if (sptr == NULL) return -1;
324 if (dsp != NULL) return -1;
325 if (channel == OPENAL_FREE)
327 for (int i = 0; i < num_channels; i++)
330 alGetSourceiv(channels[i].sid, AL_SOURCE_STATE, &state);
331 if ((state != AL_PLAYING) && (state != AL_PAUSED))
339 if ((channel < 0) || (channel >= num_channels)) return -1;
340 alSourceStop(channels[channel].sid);
341 channels[channel].sample = sptr;
342 alSourcei(channels[channel].sid, AL_BUFFER, sptr->bid);
343 alSourcei(channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
344 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
346 channels[channel].startpaused = ((startpaused) ? true : false);
348 alSourcePlay(channels[channel].sid);
353 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
356 const int bigendian = 1;
358 const int bigendian = 0;
361 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
362 char *fname = (char *) alloca(strlen(_fname) + 16);
363 strcpy(fname, _fname);
364 char *ptr = strchr(fname, '.');
365 if (ptr) *ptr = '\0';
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"))
380 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
382 fseek(io, 0, SEEK_END);
384 fseek(io, 0, SEEK_SET);
385 retval = (ALubyte *) malloc(size);
386 size_t rc = fread(retval, size, 1, io);
397 // Uncompress and feed to the AL.
399 memset(&vf, '\0', sizeof (vf));
400 if (ov_open(io, &vf, NULL, 0) == 0)
403 vorbis_info *info = ov_info(&vf, -1);
405 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
408 if ((info->channels != 1) && (info->channels != 2))
416 size_t allocated = 64 * 1024;
417 retval = (ALubyte *) malloc(allocated);
418 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
423 if (size >= allocated)
426 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
435 memcpy(retval + (size - rc), buf, rc);
447 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
449 if (!initialized) return NULL;
450 if (index != OPENAL_FREE) return NULL; // this is all the game does...
451 if (offset != 0) return NULL; // this is all the game does...
452 if (length != 0) return NULL; // this is all the game does...
453 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) return NULL; // this is all the game does...
455 OPENAL_SAMPLE *retval = NULL;
456 ALuint bufferName = 0;
457 ALenum format = AL_NONE;
459 ALuint frequency = 0;
460 void *data = decode_to_pcm(name_or_data, format, size, frequency);
466 alGenBuffers(1, &bid);
467 if (alGetError() == AL_NO_ERROR)
469 alBufferData(bid, format, data, size, frequency);
470 retval = new OPENAL_SAMPLE;
472 retval->mode = OPENAL_LOOP_OFF;
473 retval->is2d = (mode == OPENAL_2D);
474 retval->name = new char[strlen(name_or_data) + 1];
476 strcpy(retval->name, name_or_data);
483 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
485 if (!initialized) return;
488 for (int i = 0; i < num_channels; i++)
490 if (channels[i].sample == sptr)
492 alSourceStop(channels[i].sid);
493 alSourcei(channels[i].sid, AL_BUFFER, 0);
494 channels[i].sample = NULL;
497 alDeleteBuffers(1, &sptr->bid);
503 AL_API signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
505 if (!initialized) return false;
506 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF)) return false;
507 if (!sptr) return false;
512 AL_API signed char OPENAL_Sample_SetMinMaxDistance(OPENAL_SAMPLE *sptr, float mindist, float maxdist)
514 if (!initialized) return false;
515 if (sptr == NULL) return false;
516 // we ignore maxdist. It's not really important to this game, and the
517 // FMOD docs suggest that it's worthless anyhow.
519 // recalc sources to see if we need to adjust attenuation.
520 for (int i = 0; i < num_channels; i++)
522 if (channels[i].sample == sptr)
524 const float *p = channels[i].position;
525 set_channel_position(i, p[0], p[1], p[2]);
532 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
534 if (!initialized) return false;
535 if (channel == OPENAL_ALL)
537 for (int i = 0; i < num_channels; i++)
538 OPENAL_SetFrequency(i, freq);
542 if ((channel < 0) || (channel >= num_channels)) return false;
543 if (freq == 8012) // hack
544 alSourcef(channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
546 alSourcef(channels[channel].sid, AL_PITCH, 1.0f);
550 AL_API signed char OPENAL_SetVolume(int channel, int vol)
552 if (!initialized) return false;
554 if (channel == OPENAL_ALL)
556 for (int i = 0; i < num_channels; i++)
557 OPENAL_SetVolume(i, vol);
561 if ((channel < 0) || (channel >= num_channels)) return false;
563 if (vol < 0) vol = 0;
564 else if (vol > 255) vol = 255;
565 ALfloat gain = ((ALfloat) vol) / 255.0f;
566 alSourcef(channels[channel].sid, AL_GAIN, gain);
570 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
572 if (!initialized) return false;
574 if (channel == OPENAL_ALL)
576 for (int i = 0; i < num_channels; i++)
577 OPENAL_SetPaused(i, paused);
581 if ((channel < 0) || (channel >= num_channels)) return false;
584 if (channels[channel].startpaused)
587 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
589 if ((paused) && (state == AL_PLAYING))
590 alSourcePause(channels[channel].sid);
591 else if ((!paused) && (state == AL_PAUSED))
593 alSourcePlay(channels[channel].sid);
594 channels[channel].startpaused = false;
599 AL_API void OPENAL_SetSFXMasterVolume(int volume)
601 if (!initialized) return;
602 ALfloat gain = ((ALfloat) volume) / 255.0f;
603 alListenerf(AL_GAIN, gain);
606 AL_API signed char OPENAL_StopSound(int channel)
608 if (!initialized) return false;
610 if (channel == OPENAL_ALL)
612 for (int i = 0; i < num_channels; i++)
617 if ((channel < 0) || (channel >= num_channels)) return false;
618 alSourceStop(channels[channel].sid);
619 channels[channel].startpaused = false;
623 AL_API OPENAL_STREAM *OPENAL_Stream_Open(const char *name_or_data, unsigned int mode, int offset, int length)
625 return (OPENAL_STREAM *) OPENAL_Sample_Load(OPENAL_FREE, name_or_data, mode, offset, length);
628 AL_API void OPENAL_Stream_Close(OPENAL_STREAM *stream)
630 OPENAL_Sample_Free((OPENAL_SAMPLE *) stream);
633 AL_API OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
635 if (!initialized) return NULL;
636 return (OPENAL_SAMPLE *) stream;
639 AL_API int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
641 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
644 AL_API signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
646 if (!initialized) return false;
647 for (int i = 0; i < num_channels; i++)
649 if (channels[i].sample == (OPENAL_SAMPLE *) stream)
651 alSourceStop(channels[i].sid);
652 channels[i].startpaused = false;
658 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
660 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
663 AL_API void OPENAL_Update()
665 if (!initialized) return;
666 alcProcessContext(alcGetCurrentContext());
669 AL_API signed char OPENAL_SetOutput(int outputtype)