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.
33 #include "MacCompatibility.h"
40 #include "vorbis/vorbisfile.h"
43 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
44 // one...so we just need to flip the sign on the Z axis when appropriate.
47 #define DYNAMIC_LOAD_OPENAL 0
49 #if DYNAMIC_LOAD_OPENAL
53 #define AL_FUNC(t,ret,fn,params,call,rt) \
55 static ret ALAPIENTRY (*p##fn) params = NULL; \
56 ret ALAPIENTRY fn params { rt p##fn call; } \
61 static void *aldlhandle = NULL;
63 static bool lookup_alsym(const char *funcname, void **func, const char *libname)
68 *func = dlsym(aldlhandle, funcname);
71 fprintf(stderr, "Failed to find OpenAL symbol \"%s\" in \"%s\"\n",
78 static void unload_alsyms(void)
80 #define AL_FUNC(t,ret,fn,params,call,rt) p##fn = NULL;
90 static bool lookup_all_alsyms(const char *libname)
94 if ( (aldlhandle = dlopen(libname, RTLD_GLOBAL | RTLD_NOW)) == NULL )
99 #define AL_FUNC(t,ret,fn,params,call,rt) \
100 if (!lookup_alsym(#fn, (void **) &p##fn, libname)) retval = false;
110 #define lookup_all_alsyms(x) (true)
111 #define unload_alsyms()
118 FSOUND_SAMPLE *sample;
123 typedef struct FSOUND_SAMPLE
126 ALuint bid; // buffer id.
132 typedef struct FSOUND_STREAM
135 ALuint bid; // buffer id.
140 static size_t num_channels = 0;
141 static OPENAL_Channels *channels = NULL;
142 static bool initialized = false;
143 static float listener_position[3];
146 static inline bool source_too_close(const int channel)
148 const OPENAL_Channels *chan = &channels[channel];
149 const float *pos = chan->position;
150 const float distance = sqrtf(powf((pos[0] - listener_position[0]), 2.0f) +
151 powf((pos[1] - listener_position[1]), 2.0f) +
152 powf((pos[2] - listener_position[2]), 2.0f));
153 return (distance <= chan->sample->min_distance);
157 static void set_channel_position(const int channel, const float x,
158 const float y, const float z)
160 OPENAL_Channels *chan = &channels[channel];
162 chan->position[0] = x;
163 chan->position[1] = y;
164 chan->position[2] = z;
166 FSOUND_SAMPLE *sptr = chan->sample;
170 const ALuint sid = chan->sid;
171 const bool no_attenuate = ((sptr->is2d) || (source_too_close(channel)));
175 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
176 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
180 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
181 alSource3f(sid, AL_POSITION, x, y, z);
186 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)
188 if (!initialized) return;
191 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
192 listener_position[0] = pos[0];
193 listener_position[1] = pos[1];
194 listener_position[2] = -pos[2];
197 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
198 alListenerfv(AL_ORIENTATION, vec);
200 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
202 // adjust existing positions...
203 for (int i = 0; i < num_channels; i++)
205 const float *p = channels[i].position;
206 set_channel_position(i, p[0], p[1], p[2]);
210 signed char F_API OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
212 if (!initialized) return false;
213 if ((channel < 0) || (channel >= num_channels)) return false;
216 set_channel_position(channel, pos[0], pos[1], -pos[2]);
218 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
223 void F_API OPENAL_3D_SetDopplerFactor(float scale)
225 if (!initialized) return;
226 // unimplemented...looks like init routines just call this with scale == 0.0f anyhow.
229 signed char F_API OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
231 if (initialized) return false;
232 if (maxsoftwarechannels == 0) return false;
234 if (flags != 0) // unsupported.
237 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
239 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
241 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
246 ALCdevice *dev = alcOpenDevice(NULL);
250 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
251 ALCcontext *ctx = alcCreateContext(dev, caps);
258 alcMakeContextCurrent(ctx);
259 alcProcessContext(ctx);
261 bool cmdline(const char *cmd);
262 if (cmdline("openalinfo"))
264 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
265 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
266 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
267 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
270 num_channels = maxsoftwarechannels;
271 channels = new OPENAL_Channels[maxsoftwarechannels];
272 memset(channels, '\0', sizeof (OPENAL_Channels) * num_channels);
273 for (int i = 0; i < num_channels; i++)
274 alGenSources(1, &channels[i].sid); // !!! FIXME: verify this didn't fail!
280 void F_API OPENAL_Close()
282 if (!initialized) return;
284 ALCcontext *ctx = alcGetCurrentContext();
287 for (int i = 0; i < num_channels; i++)
289 alSourceStop(channels[i].sid);
290 alSourcei(channels[i].sid, AL_BUFFER, 0);
291 alDeleteSources(1, &channels[i].sid);
293 ALCdevice *dev = alcGetContextsDevice(ctx);
294 alcMakeContextCurrent(NULL);
295 alcSuspendContext(ctx);
296 alcDestroyContext(ctx);
308 FSOUND_SAMPLE *F_API OPENAL_GetCurrentSample(int channel)
310 if (!initialized) return NULL;
311 if ((channel < 0) || (channel >= num_channels)) return NULL;
312 return channels[channel].sample;
315 signed char F_API OPENAL_GetPaused(int channel)
317 if (!initialized) return false;
318 if ((channel < 0) || (channel >= num_channels)) return false;
319 if (channels[channel].startpaused)
323 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
324 return((state == AL_PAUSED) ? true : false);
327 unsigned int F_API OPENAL_GetLoopMode(int channel)
329 if (!initialized) return 0;
330 if ((channel < 0) || (channel >= num_channels)) return 0;
332 alGetSourceiv(channels[channel].sid, AL_LOOPING, &loop);
334 return(FSOUND_LOOP_NORMAL);
335 return FSOUND_LOOP_OFF;
338 signed char F_API OPENAL_IsPlaying(int channel)
340 if (!initialized) return false;
341 if ((channel < 0) || (channel >= num_channels)) return false;
343 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
344 return((state == AL_PLAYING) ? true : false);
347 int F_API OPENAL_PlaySoundEx(int channel, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
349 if (!initialized) return -1;
350 if (sptr == NULL) return -1;
351 if (dsp != NULL) return -1;
352 if (channel == FSOUND_FREE)
354 for (int i = 0; i < num_channels; i++)
357 alGetSourceiv(channels[i].sid, AL_SOURCE_STATE, &state);
358 if ((state != AL_PLAYING) && (state != AL_PAUSED))
366 if ((channel < 0) || (channel >= num_channels)) return -1;
367 alSourceStop(channels[channel].sid);
368 channels[channel].sample = sptr;
369 alSourcei(channels[channel].sid, AL_BUFFER, sptr->bid);
370 alSourcei(channels[channel].sid, AL_LOOPING, (sptr->mode == FSOUND_LOOP_OFF) ? AL_FALSE : AL_TRUE);
371 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
373 channels[channel].startpaused = ((startpaused) ? true : false);
375 alSourcePlay(channels[channel].sid);
380 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
383 const int bigendian = 1;
385 const int bigendian = 0;
388 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
389 char *fname = (char *) alloca(strlen(_fname) + 16);
390 strcpy(fname, _fname);
391 char *ptr = strchr(fname, '.');
392 if (ptr) *ptr = NULL;
393 strcat(fname, ".ogg");
397 FILE *io = fopen(fname, "rb");
401 ALubyte *retval = NULL;
403 #if 0 // untested, so disable this!
404 // Can we just feed it to the AL compressed?
405 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
407 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
409 fseek(io, 0, SEEK_END);
411 fseek(io, 0, SEEK_SET);
412 retval = (ALubyte *) malloc(size);
413 size_t rc = fread(retval, size, 1, io);
424 // Uncompress and feed to the AL.
426 memset(&vf, '\0', sizeof (vf));
427 if (ov_open(io, &vf, NULL, 0) == 0)
430 vorbis_info *info = ov_info(&vf, -1);
432 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
435 if ((info->channels != 1) && (info->channels != 2))
443 size_t allocated = 64 * 1024;
444 retval = (ALubyte *) malloc(allocated);
445 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
450 if (size >= allocated)
453 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
462 memcpy(retval + (size - rc), buf, rc);
474 FSOUND_SAMPLE * F_API OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
476 if (!initialized) return NULL;
477 if (index != FSOUND_FREE) return NULL; // this is all the game does...
478 if (offset != 0) return NULL; // this is all the game does...
479 if (length != 0) return NULL; // this is all the game does...
480 if ((mode != FSOUND_HW3D) && (mode != FSOUND_2D)) return NULL; // this is all the game does...
482 FSOUND_SAMPLE *retval = NULL;
483 ALuint bufferName = 0;
484 ALenum format = AL_NONE;
486 ALuint frequency = 0;
487 void *data = decode_to_pcm(name_or_data, format, size, frequency);
493 alGenBuffers(1, &bid);
494 if (alGetError() == AL_NO_ERROR)
496 alBufferData(bid, format, data, size, frequency);
497 retval = new FSOUND_SAMPLE;
499 retval->mode = FSOUND_LOOP_OFF;
500 retval->is2d = (mode == FSOUND_2D);
501 retval->name = new char[strlen(name_or_data) + 1];
503 strcpy(retval->name, name_or_data);
510 void F_API OPENAL_Sample_Free(FSOUND_SAMPLE *sptr)
512 if (!initialized) return;
515 for (int i = 0; i < num_channels; i++)
517 if (channels[i].sample == sptr)
519 alSourceStop(channels[i].sid);
520 alSourcei(channels[i].sid, AL_BUFFER, 0);
521 channels[i].sample = NULL;
524 alDeleteBuffers(1, &sptr->bid);
530 signed char F_API OPENAL_Sample_SetMode(FSOUND_SAMPLE *sptr, unsigned int mode)
532 if (!initialized) return false;
533 if ((mode != FSOUND_LOOP_NORMAL) && (mode != FSOUND_LOOP_OFF)) return false;
534 if (!sptr) return false;
539 signed char F_API OPENAL_Sample_SetMinMaxDistance(FSOUND_SAMPLE *sptr, float mindist, float maxdist)
541 if (!initialized) return false;
542 if (sptr == NULL) return false;
543 sptr->min_distance = mindist;
544 // we ignore maxdist. It's not really important to this game, and the
545 // FMOD docs suggest that it's worthless anyhow.
547 // recalc sources to see if we need to adjust attenuation.
548 for (int i = 0; i < num_channels; i++)
550 if (channels[i].sample == sptr)
552 const float *p = channels[i].position;
553 set_channel_position(i, p[0], p[1], p[2]);
560 signed char F_API OPENAL_SetFrequency(int channel, int freq)
562 if (!initialized) return false;
563 if (channel == FSOUND_ALL)
565 for (int i = 0; i < num_channels; i++)
566 OPENAL_SetFrequency(i, freq);
570 if ((channel < 0) || (channel >= num_channels)) return false;
571 if (freq == 8012) // hack
572 alSourcef(channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
574 alSourcef(channels[channel].sid, AL_PITCH, 1.0f);
578 signed char F_API OPENAL_SetVolume(int channel, int vol)
580 if (!initialized) return false;
582 if (channel == FSOUND_ALL)
584 for (int i = 0; i < num_channels; i++)
585 OPENAL_SetVolume(i, vol);
589 if ((channel < 0) || (channel >= num_channels)) return false;
591 if (vol < 0) vol = 0;
592 else if (vol > 255) vol = 255;
593 ALfloat gain = ((ALfloat) vol) / 255.0f;
594 alSourcef(channels[channel].sid, AL_GAIN, gain);
598 signed char F_API OPENAL_SetPaused(int channel, signed char paused)
600 if (!initialized) return false;
602 if (channel == FSOUND_ALL)
604 for (int i = 0; i < num_channels; i++)
605 OPENAL_SetPaused(i, paused);
609 if ((channel < 0) || (channel >= num_channels)) return false;
612 if (channels[channel].startpaused)
615 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
617 if ((paused) && (state == AL_PLAYING))
618 alSourcePause(channels[channel].sid);
619 else if ((!paused) && (state == AL_PAUSED))
621 alSourcePlay(channels[channel].sid);
622 channels[channel].startpaused = false;
627 void F_API OPENAL_SetSFXMasterVolume(int volume)
629 if (!initialized) return;
630 ALfloat gain = ((ALfloat) volume) / 255.0f;
631 alListenerf(AL_GAIN, gain);
634 signed char F_API OPENAL_StopSound(int channel)
636 if (!initialized) return false;
638 if (channel == FSOUND_ALL)
640 for (int i = 0; i < num_channels; i++)
645 if ((channel < 0) || (channel >= num_channels)) return false;
646 alSourceStop(channels[channel].sid);
647 channels[channel].startpaused = false;
651 FSOUND_STREAM * F_API OPENAL_Stream_Open(const char *name_or_data, unsigned int mode, int offset, int length)
653 return (FSOUND_STREAM *) OPENAL_Sample_Load(FSOUND_FREE, name_or_data, mode, offset, length);
656 signed char F_API OPENAL_Stream_Close(FSOUND_STREAM *stream)
658 OPENAL_Sample_Free((FSOUND_SAMPLE *) stream);
661 FSOUND_SAMPLE * F_API OPENAL_Stream_GetSample(FSOUND_STREAM *stream)
663 if (!initialized) return NULL;
664 return (FSOUND_SAMPLE *) stream;
667 int F_API OPENAL_Stream_PlayEx(int channel, FSOUND_STREAM *stream, FSOUND_DSPUNIT *dsp, signed char startpaused)
669 return OPENAL_PlaySoundEx(channel, (FSOUND_SAMPLE *) stream, dsp, startpaused);
672 signed char F_API OPENAL_Stream_Stop(FSOUND_STREAM *stream)
674 if (!initialized) return false;
675 for (int i = 0; i < num_channels; i++)
677 if (channels[i].sample == (FSOUND_SAMPLE *) stream)
679 alSourceStop(channels[i].sid);
680 channels[i].startpaused = false;
686 signed char F_API OPENAL_Stream_SetMode(FSOUND_STREAM *stream, unsigned int mode)
688 return OPENAL_Sample_SetMode((FSOUND_SAMPLE *) stream, mode);
691 void F_API OPENAL_Update()
693 if (!initialized) return;
694 alcProcessContext(alcGetCurrentContext());
697 signed char F_API OPENAL_SetOutput(int outputtype)