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 *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 = &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 = 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 void OPENAL_3D_SetDopplerFactor(float scale)
190 if (!initialized) return;
191 // unimplemented...looks like init routines just call this with scale == 0.0f anyhow.
194 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
196 if (initialized) return false;
197 if (maxsoftwarechannels == 0) return false;
199 if (flags != 0) // unsupported.
202 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
204 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
206 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
211 ALCdevice *dev = alcOpenDevice(NULL);
215 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
216 ALCcontext *ctx = alcCreateContext(dev, caps);
223 alcMakeContextCurrent(ctx);
224 alcProcessContext(ctx);
226 bool cmdline(const char *cmd);
227 if (cmdline("openalinfo"))
229 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
230 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
231 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
232 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
235 num_channels = maxsoftwarechannels;
236 channels = new OPENAL_Channels[maxsoftwarechannels];
237 memset(channels, '\0', sizeof (OPENAL_Channels) * num_channels);
238 for (int i = 0; i < num_channels; i++)
239 alGenSources(1, &channels[i].sid); // !!! FIXME: verify this didn't fail!
245 AL_API void OPENAL_Close()
247 if (!initialized) return;
249 ALCcontext *ctx = alcGetCurrentContext();
252 for (int i = 0; i < num_channels; i++)
254 alSourceStop(channels[i].sid);
255 alSourcei(channels[i].sid, AL_BUFFER, 0);
256 alDeleteSources(1, &channels[i].sid);
258 ALCdevice *dev = alcGetContextsDevice(ctx);
259 alcMakeContextCurrent(NULL);
260 alcSuspendContext(ctx);
261 alcDestroyContext(ctx);
273 AL_API OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
275 if (!initialized) return NULL;
276 if ((channel < 0) || (channel >= num_channels)) return NULL;
277 return channels[channel].sample;
280 AL_API signed char OPENAL_GetPaused(int channel)
282 if (!initialized) return false;
283 if ((channel < 0) || (channel >= num_channels)) return false;
284 if (channels[channel].startpaused)
288 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
289 return((state == AL_PAUSED) ? true : false);
292 AL_API unsigned int OPENAL_GetLoopMode(int channel)
294 if (!initialized) return 0;
295 if ((channel < 0) || (channel >= num_channels)) return 0;
297 alGetSourceiv(channels[channel].sid, AL_LOOPING, &loop);
299 return(OPENAL_LOOP_NORMAL);
300 return OPENAL_LOOP_OFF;
303 AL_API signed char OPENAL_IsPlaying(int channel)
305 if (!initialized) return false;
306 if ((channel < 0) || (channel >= num_channels)) return false;
308 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
309 return((state == AL_PLAYING) ? true : false);
312 AL_API int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
314 if (!initialized) return -1;
315 if (sptr == NULL) return -1;
316 if (dsp != NULL) return -1;
317 if (channel == OPENAL_FREE)
319 for (int i = 0; i < num_channels; i++)
322 alGetSourceiv(channels[i].sid, AL_SOURCE_STATE, &state);
323 if ((state != AL_PLAYING) && (state != AL_PAUSED))
331 if ((channel < 0) || (channel >= num_channels)) return -1;
332 alSourceStop(channels[channel].sid);
333 channels[channel].sample = sptr;
334 alSourcei(channels[channel].sid, AL_BUFFER, sptr->bid);
335 alSourcei(channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
336 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
338 channels[channel].startpaused = ((startpaused) ? true : false);
340 alSourcePlay(channels[channel].sid);
345 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
348 const int bigendian = 1;
350 const int bigendian = 0;
353 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
354 char *fname = (char *) alloca(strlen(_fname) + 16);
355 strcpy(fname, _fname);
356 char *ptr = strchr(fname, '.');
357 if (ptr) *ptr = '\0';
358 strcat(fname, ".ogg");
362 FILE *io = fopen(fname, "rb");
366 ALubyte *retval = NULL;
368 #if 0 // untested, so disable this!
369 // Can we just feed it to the AL compressed?
370 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
372 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
374 fseek(io, 0, SEEK_END);
376 fseek(io, 0, SEEK_SET);
377 retval = (ALubyte *) malloc(size);
378 size_t rc = fread(retval, size, 1, io);
389 // Uncompress and feed to the AL.
391 memset(&vf, '\0', sizeof (vf));
392 if (ov_open(io, &vf, NULL, 0) == 0)
395 vorbis_info *info = ov_info(&vf, -1);
397 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
400 if ((info->channels != 1) && (info->channels != 2))
408 size_t allocated = 64 * 1024;
409 retval = (ALubyte *) malloc(allocated);
410 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
415 if (size >= allocated)
418 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)
441 if (!initialized) return NULL;
442 if (index != OPENAL_FREE) return NULL; // this is all the game does...
443 if (offset != 0) return NULL; // this is all the game does...
444 if (length != 0) return NULL; // this is all the game does...
445 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) return NULL; // this is all the game does...
447 OPENAL_SAMPLE *retval = NULL;
448 ALuint bufferName = 0;
449 ALenum format = AL_NONE;
451 ALuint frequency = 0;
452 void *data = decode_to_pcm(name_or_data, format, size, frequency);
458 alGenBuffers(1, &bid);
459 if (alGetError() == AL_NO_ERROR)
461 alBufferData(bid, format, data, size, frequency);
462 retval = new OPENAL_SAMPLE;
464 retval->mode = OPENAL_LOOP_OFF;
465 retval->is2d = (mode == OPENAL_2D);
466 retval->name = new char[strlen(name_or_data) + 1];
468 strcpy(retval->name, name_or_data);
475 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
477 if (!initialized) return;
480 for (int i = 0; i < num_channels; i++)
482 if (channels[i].sample == sptr)
484 alSourceStop(channels[i].sid);
485 alSourcei(channels[i].sid, AL_BUFFER, 0);
486 channels[i].sample = NULL;
489 alDeleteBuffers(1, &sptr->bid);
495 AL_API signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
497 if (!initialized) return false;
498 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF)) return false;
499 if (!sptr) return false;
504 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
506 if (!initialized) return false;
507 if (channel == OPENAL_ALL)
509 for (int i = 0; i < num_channels; i++)
510 OPENAL_SetFrequency(i, freq);
514 if ((channel < 0) || (channel >= num_channels)) return false;
515 if (freq == 8012) // hack
516 alSourcef(channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
518 alSourcef(channels[channel].sid, AL_PITCH, 1.0f);
522 AL_API signed char OPENAL_SetVolume(int channel, int vol)
524 if (!initialized) return false;
526 if (channel == OPENAL_ALL)
528 for (int i = 0; i < num_channels; i++)
529 OPENAL_SetVolume(i, vol);
533 if ((channel < 0) || (channel >= num_channels)) return false;
535 if (vol < 0) vol = 0;
536 else if (vol > 255) vol = 255;
537 ALfloat gain = ((ALfloat) vol) / 255.0f;
538 alSourcef(channels[channel].sid, AL_GAIN, gain);
542 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
544 if (!initialized) return false;
546 if (channel == OPENAL_ALL)
548 for (int i = 0; i < num_channels; i++)
549 OPENAL_SetPaused(i, paused);
553 if ((channel < 0) || (channel >= num_channels)) return false;
556 if (channels[channel].startpaused)
559 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
561 if ((paused) && (state == AL_PLAYING))
562 alSourcePause(channels[channel].sid);
563 else if ((!paused) && (state == AL_PAUSED))
565 alSourcePlay(channels[channel].sid);
566 channels[channel].startpaused = false;
571 AL_API void OPENAL_SetSFXMasterVolume(int volume)
573 if (!initialized) return;
574 ALfloat gain = ((ALfloat) volume) / 255.0f;
575 alListenerf(AL_GAIN, gain);
578 AL_API signed char OPENAL_StopSound(int channel)
580 if (!initialized) return false;
582 if (channel == OPENAL_ALL)
584 for (int i = 0; i < num_channels; i++)
589 if ((channel < 0) || (channel >= num_channels)) return false;
590 alSourceStop(channels[channel].sid);
591 channels[channel].startpaused = false;
595 AL_API void OPENAL_Stream_Close(OPENAL_STREAM *stream)
597 OPENAL_Sample_Free((OPENAL_SAMPLE *) stream);
600 AL_API OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
602 if (!initialized) return NULL;
603 return (OPENAL_SAMPLE *) stream;
606 AL_API int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
608 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
611 AL_API signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
613 if (!initialized) return false;
614 for (int i = 0; i < num_channels; i++)
616 if (channels[i].sample == (OPENAL_SAMPLE *) stream)
618 alSourceStop(channels[i].sid);
619 channels[i].startpaused = false;
625 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
627 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
630 AL_API void OPENAL_Update()
632 if (!initialized) return;
633 alcProcessContext(alcGetCurrentContext());
636 AL_API signed char OPENAL_SetOutput(int outputtype)