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.
26 #include "Quaternions.h"
27 #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);
57 fprintf(stderr, "Failed to find OpenAL symbol \"%s\" in \"%s\"\n",
64 static void unload_alsyms(void)
66 #define AL_FUNC(t,ret,fn,params,call,rt) p##fn = NULL;
75 static bool lookup_all_alsyms(const char *libname)
78 if ( (aldlhandle = dlopen(libname, RTLD_GLOBAL | RTLD_NOW)) == NULL )
83 #define AL_FUNC(t,ret,fn,params,call,rt) \
84 if (!lookup_alsym(#fn, (void **) &p##fn, libname)) retval = false;
94 #define lookup_all_alsyms(x) (true)
95 #define unload_alsyms()
100 OPENAL_SAMPLE *sample;
105 typedef struct OPENAL_SAMPLE {
107 ALuint bid; // buffer id.
112 static size_t num_channels = 0;
113 static OPENAL_Channels *impl_channels = NULL;
114 static bool initialized = false;
115 static float listener_position[3];
117 static void set_channel_position(const int channel, const float x,
118 const float y, const float z)
120 OPENAL_Channels *chan = &impl_channels[channel];
122 chan->position[0] = x;
123 chan->position[1] = y;
124 chan->position[2] = z;
126 OPENAL_SAMPLE *sptr = chan->sample;
130 const ALuint sid = chan->sid;
131 const bool no_attenuate = sptr->is2d;
134 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
135 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
137 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
138 alSource3f(sid, AL_POSITION, x, y, z);
143 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)
148 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
149 listener_position[0] = pos[0];
150 listener_position[1] = pos[1];
151 listener_position[2] = -pos[2];
154 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
155 alListenerfv(AL_ORIENTATION, vec);
157 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
159 // adjust existing positions...
160 for (int i = 0; i < num_channels; i++) {
161 const float *p = impl_channels[i].position;
162 set_channel_position(i, p[0], p[1], p[2]);
166 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
170 if ((channel < 0) || (channel >= num_channels))
174 set_channel_position(channel, pos[0], pos[1], -pos[2]);
176 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
181 AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ &pos, const float *vel)
185 if ((channel < 0) || (channel >= num_channels))
188 set_channel_position(channel, pos.x, pos.y, -pos.z);
193 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
197 if (maxsoftwarechannels == 0)
200 if (flags != 0) // unsupported.
203 if (!lookup_all_alsyms("./openal.so")) { // !!! FIXME: linux specific lib name
204 if (!lookup_all_alsyms("openal.so.1")) { // !!! FIXME: linux specific lib name
205 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
210 ALCdevice *dev = alcOpenDevice(NULL);
214 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
215 ALCcontext *ctx = alcCreateContext(dev, caps);
221 alcMakeContextCurrent(ctx);
222 alcProcessContext(ctx);
224 bool cmdline(const char * cmd);
225 if (cmdline("openalinfo")) {
226 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
227 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
228 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
229 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
232 num_channels = maxsoftwarechannels;
233 impl_channels = new OPENAL_Channels[maxsoftwarechannels];
234 memset(impl_channels, '\0', sizeof (OPENAL_Channels) * num_channels);
235 for (int i = 0; i < num_channels; i++)
236 alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail!
242 AL_API void OPENAL_Close()
247 ALCcontext *ctx = alcGetCurrentContext();
249 for (int i = 0; i < num_channels; i++) {
250 alSourceStop(impl_channels[i].sid);
251 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
252 alDeleteSources(1, &impl_channels[i].sid);
254 ALCdevice *dev = alcGetContextsDevice(ctx);
255 alcMakeContextCurrent(NULL);
256 alcSuspendContext(ctx);
257 alcDestroyContext(ctx);
262 delete[] impl_channels;
263 impl_channels = NULL;
269 static OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
273 if ((channel < 0) || (channel >= num_channels))
275 return impl_channels[channel].sample;
278 static signed char OPENAL_GetPaused(int channel)
282 if ((channel < 0) || (channel >= num_channels))
284 if (impl_channels[channel].startpaused)
288 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
289 return((state == AL_PAUSED) ? true : false);
292 static unsigned int OPENAL_GetLoopMode(int channel)
296 if ((channel < 0) || (channel >= num_channels))
299 alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
301 return(OPENAL_LOOP_NORMAL);
302 return OPENAL_LOOP_OFF;
305 static signed char OPENAL_IsPlaying(int channel)
309 if ((channel < 0) || (channel >= num_channels))
312 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
313 return((state == AL_PLAYING) ? true : false);
316 static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
324 if (channel == OPENAL_FREE) {
325 for (int i = 0; i < num_channels; i++) {
327 alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
328 if ((state != AL_PLAYING) && (state != AL_PAUSED)) {
335 if ((channel < 0) || (channel >= num_channels))
337 alSourceStop(impl_channels[channel].sid);
338 impl_channels[channel].sample = sptr;
339 alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
340 alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
341 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
343 impl_channels[channel].startpaused = ((startpaused) ? true : false);
345 alSourcePlay(impl_channels[channel].sid);
350 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
353 const int bigendian = 1;
355 const int bigendian = 0;
358 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
359 char *fname = (char *) alloca(strlen(_fname) + 16);
360 strcpy(fname, _fname);
361 char *ptr = strchr(fname, '.');
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")) {
377 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
379 fseek(io, 0, SEEK_END);
381 fseek(io, 0, SEEK_SET);
382 retval = (ALubyte *) malloc(size);
383 size_t rc = fread(retval, size, 1, io);
393 // Uncompress and feed to the AL.
395 memset(&vf, '\0', sizeof (vf));
396 if (ov_open(io, &vf, NULL, 0) == 0) {
398 vorbis_info *info = ov_info(&vf, -1);
400 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
403 if ((info->channels != 1) && (info->channels != 2)) {
410 size_t allocated = 64 * 1024;
411 retval = (ALubyte *) malloc(allocated);
412 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 ) {
415 if (size >= allocated) {
417 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
425 memcpy(retval + (size - rc), buf, rc);
437 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
441 if (index != OPENAL_FREE)
442 return NULL; // this is all the game does...
444 return NULL; // this is all the game does...
446 return NULL; // this is all the game does...
447 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D))
448 return NULL; // this is all the game does...
450 OPENAL_SAMPLE *retval = NULL;
451 ALuint bufferName = 0;
452 ALenum format = AL_NONE;
454 ALuint frequency = 0;
455 void *data = decode_to_pcm(name_or_data, format, size, frequency);
461 alGenBuffers(1, &bid);
462 if (alGetError() == AL_NO_ERROR) {
463 alBufferData(bid, format, data, size, frequency);
464 retval = new OPENAL_SAMPLE;
466 retval->mode = OPENAL_LOOP_OFF;
467 retval->is2d = (mode == OPENAL_2D);
468 retval->name = new char[strlen(name_or_data) + 1];
470 strcpy(retval->name, name_or_data);
477 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
482 for (int i = 0; i < num_channels; i++) {
483 if (impl_channels[i].sample == sptr) {
484 alSourceStop(impl_channels[i].sid);
485 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
486 impl_channels[i].sample = NULL;
489 alDeleteBuffers(1, &sptr->bid);
495 static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
499 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF))
507 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
511 if (channel == OPENAL_ALL) {
512 for (int i = 0; i < num_channels; i++)
513 OPENAL_SetFrequency(i, freq);
517 if ((channel < 0) || (channel >= num_channels))
521 alSourcef(impl_channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
523 alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
527 AL_API signed char OPENAL_SetVolume(int channel, int vol)
532 if (channel == OPENAL_ALL) {
533 for (int i = 0; i < num_channels; i++)
534 OPENAL_SetVolume(i, vol);
538 if ((channel < 0) || (channel >= num_channels))
545 ALfloat gain = ((ALfloat) vol) / 255.0f;
546 alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
550 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
555 if (channel == OPENAL_ALL) {
556 for (int i = 0; i < num_channels; i++)
557 OPENAL_SetPaused(i, paused);
561 if ((channel < 0) || (channel >= num_channels))
565 if (impl_channels[channel].startpaused)
568 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
570 if ((paused) && (state == AL_PLAYING))
571 alSourcePause(impl_channels[channel].sid);
572 else if ((!paused) && (state == AL_PAUSED)) {
573 alSourcePlay(impl_channels[channel].sid);
574 impl_channels[channel].startpaused = false;
579 AL_API void OPENAL_SetSFXMasterVolume(int volume)
583 ALfloat gain = ((ALfloat) volume) / 255.0f;
584 alListenerf(AL_GAIN, gain);
587 AL_API signed char OPENAL_StopSound(int channel)
592 if (channel == OPENAL_ALL) {
593 for (int i = 0; i < num_channels; i++)
598 if ((channel < 0) || (channel >= num_channels))
600 alSourceStop(impl_channels[channel].sid);
601 impl_channels[channel].startpaused = false;
605 AL_API void OPENAL_Stream_Close(OPENAL_STREAM *stream)
607 OPENAL_Sample_Free((OPENAL_SAMPLE *) stream);
610 static OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
614 return (OPENAL_SAMPLE *) stream;
617 static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
619 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
622 static signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
626 for (int i = 0; i < num_channels; i++) {
627 if (impl_channels[i].sample == (OPENAL_SAMPLE *) stream) {
628 alSourceStop(impl_channels[i].sid);
629 impl_channels[i].startpaused = false;
635 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
637 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
640 AL_API void OPENAL_Update()
644 alcProcessContext(alcGetCurrentContext());
647 AL_API signed char OPENAL_SetOutput(int outputtype)
652 extern int channels[];
654 extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
656 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
657 if (currSample && currSample == samp[chan]) {
658 if (OPENAL_GetPaused(channels[chan])) {
659 OPENAL_StopSound(channels[chan]);
660 channels[chan] = OPENAL_FREE;
661 } else if (OPENAL_IsPlaying(channels[chan])) {
662 int loop_mode = OPENAL_GetLoopMode(channels[chan]);
663 if (loop_mode & OPENAL_LOOP_OFF) {
664 channels[chan] = OPENAL_FREE;
668 channels[chan] = OPENAL_FREE;
671 channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
672 if (channels[chan] < 0) {
673 channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
677 extern "C" void PlayStreamEx(int chan, OPENAL_STREAM *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
679 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
680 if (currSample && currSample == OPENAL_Stream_GetSample(sptr)) {
681 OPENAL_StopSound(channels[chan]);
682 OPENAL_Stream_Stop(sptr);
684 OPENAL_Stream_Stop(sptr);
685 channels[chan] = OPENAL_FREE;
688 channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
689 if (channels[chan] < 0) {
690 channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);