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.
29 #include "MacCompatibility.h"
36 #include "vorbis/vorbisfile.h"
39 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
40 // one...so we just need to flip the sign on the Z axis when appropriate.
43 #define DYNAMIC_LOAD_OPENAL 0
45 #if DYNAMIC_LOAD_OPENAL
49 #define AL_FUNC(t,ret,fn,params,call,rt) \
51 static ret ALAPIENTRY (*p##fn) params = NULL; \
52 ret ALAPIENTRY fn params { rt p##fn call; } \
57 static void *aldlhandle = NULL;
59 static bool lookup_alsym(const char *funcname, void **func, const char *libname)
64 *func = dlsym(aldlhandle, funcname);
67 fprintf(stderr, "Failed to find OpenAL symbol \"%s\" in \"%s\"\n",
74 static void unload_alsyms(void)
76 #define AL_FUNC(t,ret,fn,params,call,rt) p##fn = NULL;
86 static bool lookup_all_alsyms(const char *libname)
90 if ( (aldlhandle = dlopen(libname, RTLD_GLOBAL | RTLD_NOW)) == NULL )
95 #define AL_FUNC(t,ret,fn,params,call,rt) \
96 if (!lookup_alsym(#fn, (void **) &p##fn, libname)) retval = false;
106 #define lookup_all_alsyms(x) (true)
107 #define unload_alsyms()
114 FSOUND_SAMPLE *sample;
119 typedef struct FSOUND_SAMPLE
122 ALuint bid; // buffer id.
128 typedef struct FSOUND_STREAM
131 ALuint bid; // buffer id.
136 static size_t num_channels = 0;
137 static OPENAL_Channels *channels = NULL;
138 static bool initialized = false;
139 static float listener_position[3];
142 static inline bool source_too_close(const int channel)
144 const OPENAL_Channels *chan = &channels[channel];
145 const float *pos = chan->position;
146 const float distance = sqrtf(powf((pos[0] - listener_position[0]), 2.0f) +
147 powf((pos[1] - listener_position[1]), 2.0f) +
148 powf((pos[2] - listener_position[2]), 2.0f));
149 return (distance <= chan->sample->min_distance);
153 static void set_channel_position(const int channel, const float x,
154 const float y, const float z)
156 OPENAL_Channels *chan = &channels[channel];
158 chan->position[0] = x;
159 chan->position[1] = y;
160 chan->position[2] = z;
162 FSOUND_SAMPLE *sptr = chan->sample;
166 const ALuint sid = chan->sid;
167 const bool no_attenuate = ((sptr->is2d) || (source_too_close(channel)));
171 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
172 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
176 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
177 alSource3f(sid, AL_POSITION, x, y, z);
182 void F_API OPENAL_3D_Listener_SetAttributes(const float *pos, const float *vel, float fx, float fy, float fz, float tx, float ty, float tz)
184 if (!initialized) return;
187 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
188 listener_position[0] = pos[0];
189 listener_position[1] = pos[1];
190 listener_position[2] = -pos[2];
193 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
194 alListenerfv(AL_ORIENTATION, vec);
196 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
198 // adjust existing positions...
199 for (int i = 0; i < num_channels; i++)
201 const float *p = channels[i].position;
202 set_channel_position(i, p[0], p[1], p[2]);
206 signed char F_API OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
208 if (!initialized) return false;
209 if ((channel < 0) || (channel >= num_channels)) return false;
212 set_channel_position(channel, pos[0], pos[1], -pos[2]);
214 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
219 void F_API OPENAL_3D_SetDopplerFactor(float scale)
221 if (!initialized) return;
222 // unimplemented...looks like init routines just call this with scale == 0.0f anyhow.
225 signed char F_API OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
227 if (initialized) return false;
228 if (maxsoftwarechannels == 0) return false;
230 if (flags != 0) // unsupported.
233 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
235 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
237 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
242 ALCdevice *dev = alcOpenDevice(NULL);
246 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
247 ALCcontext *ctx = alcCreateContext(dev, caps);
254 alcMakeContextCurrent(ctx);
255 alcProcessContext(ctx);
257 bool cmdline(const char *cmd);
258 if (cmdline("openalinfo"))
260 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
261 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
262 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
263 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
266 num_channels = maxsoftwarechannels;
267 channels = new OPENAL_Channels[maxsoftwarechannels];
268 memset(channels, '\0', sizeof (OPENAL_Channels) * num_channels);
269 for (int i = 0; i < num_channels; i++)
270 alGenSources(1, &channels[i].sid); // !!! FIXME: verify this didn't fail!
276 void F_API OPENAL_Close()
278 if (!initialized) return;
280 ALCcontext *ctx = alcGetCurrentContext();
283 for (int i = 0; i < num_channels; i++)
285 alSourceStop(channels[i].sid);
286 alSourcei(channels[i].sid, AL_BUFFER, 0);
287 alDeleteSources(1, &channels[i].sid);
289 ALCdevice *dev = alcGetContextsDevice(ctx);
290 alcMakeContextCurrent(NULL);
291 alcSuspendContext(ctx);
292 alcDestroyContext(ctx);
304 FSOUND_SAMPLE *F_API OPENAL_GetCurrentSample(int channel)
306 if (!initialized) return NULL;
307 if ((channel < 0) || (channel >= num_channels)) return NULL;
308 return channels[channel].sample;
311 signed char F_API OPENAL_GetPaused(int channel)
313 if (!initialized) return false;
314 if ((channel < 0) || (channel >= num_channels)) return false;
315 if (channels[channel].startpaused)
319 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
320 return((state == AL_PAUSED) ? true : false);
323 unsigned int F_API OPENAL_GetLoopMode(int channel)
325 if (!initialized) return 0;
326 if ((channel < 0) || (channel >= num_channels)) return 0;
328 alGetSourceiv(channels[channel].sid, AL_LOOPING, &loop);
330 return(FSOUND_LOOP_NORMAL);
331 return FSOUND_LOOP_OFF;
334 signed char F_API OPENAL_IsPlaying(int channel)
336 if (!initialized) return false;
337 if ((channel < 0) || (channel >= num_channels)) return false;
339 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
340 return((state == AL_PLAYING) ? true : false);
343 int F_API OPENAL_PlaySoundEx(int channel, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
345 if (!initialized) return -1;
346 if (sptr == NULL) return -1;
347 if (dsp != NULL) return -1;
348 if (channel == FSOUND_FREE)
350 for (int i = 0; i < num_channels; i++)
353 alGetSourceiv(channels[i].sid, AL_SOURCE_STATE, &state);
354 if ((state != AL_PLAYING) && (state != AL_PAUSED))
362 if ((channel < 0) || (channel >= num_channels)) return -1;
363 alSourceStop(channels[channel].sid);
364 channels[channel].sample = sptr;
365 alSourcei(channels[channel].sid, AL_BUFFER, sptr->bid);
366 alSourcei(channels[channel].sid, AL_LOOPING, (sptr->mode == FSOUND_LOOP_OFF) ? AL_FALSE : AL_TRUE);
367 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
369 channels[channel].startpaused = ((startpaused) ? true : false);
371 alSourcePlay(channels[channel].sid);
376 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
379 const int bigendian = 1;
381 const int bigendian = 0;
384 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
385 char *fname = (char *) alloca(strlen(_fname) + 16);
386 strcpy(fname, _fname);
387 char *ptr = strchr(fname, '.');
388 if (ptr) *ptr = NULL;
389 strcat(fname, ".ogg");
393 FILE *io = fopen(fname, "rb");
397 ALubyte *retval = NULL;
399 #if 0 // untested, so disable this!
400 // Can we just feed it to the AL compressed?
401 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
403 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
405 fseek(io, 0, SEEK_END);
407 fseek(io, 0, SEEK_SET);
408 retval = (ALubyte *) malloc(size);
409 size_t rc = fread(retval, size, 1, io);
420 // Uncompress and feed to the AL.
422 memset(&vf, '\0', sizeof (vf));
423 if (ov_open(io, &vf, NULL, 0) == 0)
426 vorbis_info *info = ov_info(&vf, -1);
428 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
431 if ((info->channels != 1) && (info->channels != 2))
439 size_t allocated = 64 * 1024;
440 retval = (ALubyte *) malloc(allocated);
441 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
446 if (size >= allocated)
449 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
458 memcpy(retval + (size - rc), buf, rc);
470 FSOUND_SAMPLE * F_API OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
472 if (!initialized) return NULL;
473 if (index != FSOUND_FREE) return NULL; // this is all the game does...
474 if (offset != 0) return NULL; // this is all the game does...
475 if (length != 0) return NULL; // this is all the game does...
476 if ((mode != FSOUND_HW3D) && (mode != FSOUND_2D)) return NULL; // this is all the game does...
478 FSOUND_SAMPLE *retval = NULL;
479 ALuint bufferName = 0;
480 ALenum format = AL_NONE;
482 ALuint frequency = 0;
483 void *data = decode_to_pcm(name_or_data, format, size, frequency);
489 alGenBuffers(1, &bid);
490 if (alGetError() == AL_NO_ERROR)
492 alBufferData(bid, format, data, size, frequency);
493 retval = new FSOUND_SAMPLE;
495 retval->mode = FSOUND_LOOP_OFF;
496 retval->is2d = (mode == FSOUND_2D);
497 retval->name = new char[strlen(name_or_data) + 1];
499 strcpy(retval->name, name_or_data);
506 void F_API OPENAL_Sample_Free(FSOUND_SAMPLE *sptr)
508 if (!initialized) return;
511 for (int i = 0; i < num_channels; i++)
513 if (channels[i].sample == sptr)
515 alSourceStop(channels[i].sid);
516 alSourcei(channels[i].sid, AL_BUFFER, 0);
517 channels[i].sample = NULL;
520 alDeleteBuffers(1, &sptr->bid);
526 signed char F_API OPENAL_Sample_SetMode(FSOUND_SAMPLE *sptr, unsigned int mode)
528 if (!initialized) return false;
529 if ((mode != FSOUND_LOOP_NORMAL) && (mode != FSOUND_LOOP_OFF)) return false;
530 if (!sptr) return false;
535 signed char F_API OPENAL_Sample_SetMinMaxDistance(FSOUND_SAMPLE *sptr, float mindist, float maxdist)
537 if (!initialized) return false;
538 if (sptr == NULL) return false;
539 sptr->min_distance = mindist;
540 // we ignore maxdist. It's not really important to this game, and the
541 // FMOD docs suggest that it's worthless anyhow.
543 // recalc sources to see if we need to adjust attenuation.
544 for (int i = 0; i < num_channels; i++)
546 if (channels[i].sample == sptr)
548 const float *p = channels[i].position;
549 set_channel_position(i, p[0], p[1], p[2]);
556 signed char F_API OPENAL_SetFrequency(int channel, int freq)
558 if (!initialized) return false;
559 if (channel == FSOUND_ALL)
561 for (int i = 0; i < num_channels; i++)
562 OPENAL_SetFrequency(i, freq);
566 if ((channel < 0) || (channel >= num_channels)) return false;
567 if (freq == 8012) // hack
568 alSourcef(channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
570 alSourcef(channels[channel].sid, AL_PITCH, 1.0f);
574 signed char F_API OPENAL_SetVolume(int channel, int vol)
576 if (!initialized) return false;
578 if (channel == FSOUND_ALL)
580 for (int i = 0; i < num_channels; i++)
581 OPENAL_SetVolume(i, vol);
585 if ((channel < 0) || (channel >= num_channels)) return false;
587 if (vol < 0) vol = 0;
588 else if (vol > 255) vol = 255;
589 ALfloat gain = ((ALfloat) vol) / 255.0f;
590 alSourcef(channels[channel].sid, AL_GAIN, gain);
594 signed char F_API OPENAL_SetPaused(int channel, signed char paused)
596 if (!initialized) return false;
598 if (channel == FSOUND_ALL)
600 for (int i = 0; i < num_channels; i++)
601 OPENAL_SetPaused(i, paused);
605 if ((channel < 0) || (channel >= num_channels)) return false;
608 if (channels[channel].startpaused)
611 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
613 if ((paused) && (state == AL_PLAYING))
614 alSourcePause(channels[channel].sid);
615 else if ((!paused) && (state == AL_PAUSED))
617 alSourcePlay(channels[channel].sid);
618 channels[channel].startpaused = false;
623 void F_API OPENAL_SetSFXMasterVolume(int volume)
625 if (!initialized) return;
626 ALfloat gain = ((ALfloat) volume) / 255.0f;
627 alListenerf(AL_GAIN, gain);
630 signed char F_API OPENAL_StopSound(int channel)
632 if (!initialized) return false;
634 if (channel == FSOUND_ALL)
636 for (int i = 0; i < num_channels; i++)
641 if ((channel < 0) || (channel >= num_channels)) return false;
642 alSourceStop(channels[channel].sid);
643 channels[channel].startpaused = false;
647 FSOUND_STREAM * F_API OPENAL_Stream_Open(const char *name_or_data, unsigned int mode, int offset, int length)
649 return (FSOUND_STREAM *) OPENAL_Sample_Load(FSOUND_FREE, name_or_data, mode, offset, length);
652 signed char F_API OPENAL_Stream_Close(FSOUND_STREAM *stream)
654 OPENAL_Sample_Free((FSOUND_SAMPLE *) stream);
657 FSOUND_SAMPLE * F_API OPENAL_Stream_GetSample(FSOUND_STREAM *stream)
659 if (!initialized) return NULL;
660 return (FSOUND_SAMPLE *) stream;
663 int F_API OPENAL_Stream_PlayEx(int channel, FSOUND_STREAM *stream, FSOUND_DSPUNIT *dsp, signed char startpaused)
665 return OPENAL_PlaySoundEx(channel, (FSOUND_SAMPLE *) stream, dsp, startpaused);
668 signed char F_API OPENAL_Stream_Stop(FSOUND_STREAM *stream)
670 if (!initialized) return false;
671 for (int i = 0; i < num_channels; i++)
673 if (channels[i].sample == (FSOUND_SAMPLE *) stream)
675 alSourceStop(channels[i].sid);
676 channels[i].startpaused = false;
682 signed char F_API OPENAL_Stream_SetMode(FSOUND_STREAM *stream, unsigned int mode)
684 return OPENAL_Sample_SetMode((FSOUND_SAMPLE *) stream, mode);
687 void F_API OPENAL_Update()
689 if (!initialized) return;
690 alcProcessContext(alcGetCurrentContext());
693 signed char F_API OPENAL_SetOutput(int outputtype)