8 #include "MacCompatibility.h"
15 #include "vorbis/vorbisfile.h"
18 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
19 // one...so we just need to flip the sign on the Z axis when appropriate.
22 #define DYNAMIC_LOAD_OPENAL 0
24 #if DYNAMIC_LOAD_OPENAL
28 #define AL_FUNC(t,ret,fn,params,call,rt) \
30 static ret ALAPIENTRY (*p##fn) params = NULL; \
31 ret ALAPIENTRY fn params { rt p##fn call; } \
36 static void *aldlhandle = NULL;
38 static bool lookup_alsym(const char *funcname, void **func, const char *libname)
43 *func = dlsym(aldlhandle, funcname);
46 fprintf(stderr, "Failed to find OpenAL symbol \"%s\" in \"%s\"\n",
53 static void unload_alsyms(void)
55 #define AL_FUNC(t,ret,fn,params,call,rt) p##fn = NULL;
65 static bool lookup_all_alsyms(const char *libname)
69 if ( (aldlhandle = dlopen(libname, RTLD_GLOBAL | RTLD_NOW)) == NULL )
74 #define AL_FUNC(t,ret,fn,params,call,rt) \
75 if (!lookup_alsym(#fn, (void **) &p##fn, libname)) retval = false;
85 #define lookup_all_alsyms(x) (true)
86 #define unload_alsyms()
93 FSOUND_SAMPLE *sample;
98 typedef struct FSOUND_SAMPLE
101 ALuint bid; // buffer id.
107 typedef struct FSOUND_STREAM
110 ALuint bid; // buffer id.
115 static size_t num_channels = 0;
116 static OPENAL_Channels *channels = NULL;
117 static bool initialized = false;
118 static float listener_position[3];
121 static inline bool source_too_close(const int channel)
123 const OPENAL_Channels *chan = &channels[channel];
124 const float *pos = chan->position;
125 const float distance = sqrtf(powf((pos[0] - listener_position[0]), 2.0f) +
126 powf((pos[1] - listener_position[1]), 2.0f) +
127 powf((pos[2] - listener_position[2]), 2.0f));
128 return (distance <= chan->sample->min_distance);
132 static void set_channel_position(const int channel, const float x,
133 const float y, const float z)
135 OPENAL_Channels *chan = &channels[channel];
137 chan->position[0] = x;
138 chan->position[1] = y;
139 chan->position[2] = z;
141 FSOUND_SAMPLE *sptr = chan->sample;
145 const ALuint sid = chan->sid;
146 const bool no_attenuate = ((sptr->is2d) || (source_too_close(channel)));
150 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
151 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
155 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
156 alSource3f(sid, AL_POSITION, x, y, z);
161 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)
163 if (!initialized) return;
166 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
167 listener_position[0] = pos[0];
168 listener_position[1] = pos[1];
169 listener_position[2] = -pos[2];
172 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
173 alListenerfv(AL_ORIENTATION, vec);
175 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
177 // adjust existing positions...
178 for (int i = 0; i < num_channels; i++)
180 const float *p = channels[i].position;
181 set_channel_position(i, p[0], p[1], p[2]);
185 signed char F_API OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
187 if (!initialized) return FALSE;
188 if ((channel < 0) || (channel >= num_channels)) return FALSE;
191 set_channel_position(channel, pos[0], pos[1], -pos[2]);
193 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
198 void F_API OPENAL_3D_SetDopplerFactor(float scale)
200 if (!initialized) return;
201 // unimplemented...looks like init routines just call this with scale == 0.0f anyhow.
204 signed char F_API OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
206 if (initialized) return FALSE;
207 if (maxsoftwarechannels == 0) return FALSE;
209 if (flags != 0) // unsupported.
212 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
214 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
216 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
221 ALCdevice *dev = alcOpenDevice(NULL);
225 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
226 ALCcontext *ctx = alcCreateContext(dev, caps);
233 alcMakeContextCurrent(ctx);
234 alcProcessContext(ctx);
236 bool cmdline(const char *cmd);
237 if (cmdline("openalinfo"))
239 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
240 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
241 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
242 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
245 num_channels = maxsoftwarechannels;
246 channels = new OPENAL_Channels[maxsoftwarechannels];
247 memset(channels, '\0', sizeof (OPENAL_Channels) * num_channels);
248 for (int i = 0; i < num_channels; i++)
249 alGenSources(1, &channels[i].sid); // !!! FIXME: verify this didn't fail!
255 void F_API OPENAL_Close()
257 if (!initialized) return;
259 ALCcontext *ctx = alcGetCurrentContext();
262 for (int i = 0; i < num_channels; i++)
264 alSourceStop(channels[i].sid);
265 alSourcei(channels[i].sid, AL_BUFFER, 0);
266 alDeleteSources(1, &channels[i].sid);
268 ALCdevice *dev = alcGetContextsDevice(ctx);
269 alcMakeContextCurrent(NULL);
270 alcSuspendContext(ctx);
271 alcDestroyContext(ctx);
283 FSOUND_SAMPLE *F_API OPENAL_GetCurrentSample(int channel)
285 if (!initialized) return NULL;
286 if ((channel < 0) || (channel >= num_channels)) return NULL;
287 return channels[channel].sample;
290 signed char F_API OPENAL_GetPaused(int channel)
292 if (!initialized) return FALSE;
293 if ((channel < 0) || (channel >= num_channels)) return FALSE;
294 if (channels[channel].startpaused)
298 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
299 return((state == AL_PAUSED) ? TRUE : FALSE);
302 unsigned int F_API OPENAL_GetLoopMode(int channel)
304 if (!initialized) return 0;
305 if ((channel < 0) || (channel >= num_channels)) return 0;
307 alGetSourceiv(channels[channel].sid, AL_LOOPING, &loop);
309 return(FSOUND_LOOP_NORMAL);
310 return FSOUND_LOOP_OFF;
313 signed char F_API OPENAL_IsPlaying(int channel)
315 if (!initialized) return FALSE;
316 if ((channel < 0) || (channel >= num_channels)) return FALSE;
318 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
319 return((state == AL_PLAYING) ? TRUE : FALSE);
322 int F_API OPENAL_PlaySoundEx(int channel, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
324 if (!initialized) return -1;
325 if (sptr == NULL) return -1;
326 if (dsp != NULL) return -1;
327 if (channel == FSOUND_FREE)
329 for (int i = 0; i < num_channels; i++)
332 alGetSourceiv(channels[i].sid, AL_SOURCE_STATE, &state);
333 if ((state != AL_PLAYING) && (state != AL_PAUSED))
341 if ((channel < 0) || (channel >= num_channels)) return -1;
342 alSourceStop(channels[channel].sid);
343 channels[channel].sample = sptr;
344 alSourcei(channels[channel].sid, AL_BUFFER, sptr->bid);
345 alSourcei(channels[channel].sid, AL_LOOPING, (sptr->mode == FSOUND_LOOP_OFF) ? AL_FALSE : AL_TRUE);
346 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
348 channels[channel].startpaused = ((startpaused) ? true : false);
350 alSourcePlay(channels[channel].sid);
355 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
358 const int bigendian = 1;
360 const int bigendian = 0;
363 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
364 char *fname = (char *) alloca(strlen(_fname) + 16);
365 strcpy(fname, _fname);
366 char *ptr = strchr(fname, '.');
367 if (ptr) *ptr = NULL;
368 strcat(fname, ".ogg");
372 FILE *io = fopen(fname, "rb");
376 ALubyte *retval = NULL;
378 #if 0 // untested, so disable this!
379 // Can we just feed it to the AL compressed?
380 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
382 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
384 fseek(io, 0, SEEK_END);
386 fseek(io, 0, SEEK_SET);
387 retval = (ALubyte *) malloc(size);
388 size_t rc = fread(retval, size, 1, io);
399 // Uncompress and feed to the AL.
401 memset(&vf, '\0', sizeof (vf));
402 if (ov_open(io, &vf, NULL, 0) == 0)
405 vorbis_info *info = ov_info(&vf, -1);
407 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
410 if ((info->channels != 1) && (info->channels != 2))
418 size_t allocated = 64 * 1024;
419 retval = (ALubyte *) malloc(allocated);
420 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
425 if (size >= allocated)
428 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
437 memcpy(retval + (size - rc), buf, rc);
449 FSOUND_SAMPLE * F_API OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
451 if (!initialized) return NULL;
452 if (index != FSOUND_FREE) return NULL; // this is all the game does...
453 if (offset != 0) return NULL; // this is all the game does...
454 if (length != 0) return NULL; // this is all the game does...
455 if ((mode != FSOUND_HW3D) && (mode != FSOUND_2D)) return NULL; // this is all the game does...
457 FSOUND_SAMPLE *retval = NULL;
458 ALuint bufferName = 0;
459 ALenum format = AL_NONE;
461 ALuint frequency = 0;
462 void *data = decode_to_pcm(name_or_data, format, size, frequency);
468 alGenBuffers(1, &bid);
469 if (alGetError() == AL_NO_ERROR)
471 alBufferData(bid, format, data, size, frequency);
472 retval = new FSOUND_SAMPLE;
474 retval->mode = FSOUND_LOOP_OFF;
475 retval->is2d = (mode == FSOUND_2D);
476 retval->name = new char[strlen(name_or_data) + 1];
478 strcpy(retval->name, name_or_data);
485 void F_API OPENAL_Sample_Free(FSOUND_SAMPLE *sptr)
487 if (!initialized) return;
490 for (int i = 0; i < num_channels; i++)
492 if (channels[i].sample == sptr)
494 alSourceStop(channels[i].sid);
495 alSourcei(channels[i].sid, AL_BUFFER, 0);
496 channels[i].sample = NULL;
499 alDeleteBuffers(1, &sptr->bid);
505 signed char F_API OPENAL_Sample_SetMode(FSOUND_SAMPLE *sptr, unsigned int mode)
507 if (!initialized) return FALSE;
508 if ((mode != FSOUND_LOOP_NORMAL) && (mode != FSOUND_LOOP_OFF)) return FALSE;
509 if (!sptr) return FALSE;
514 signed char F_API OPENAL_Sample_SetMinMaxDistance(FSOUND_SAMPLE *sptr, float mindist, float maxdist)
516 if (!initialized) return FALSE;
517 if (sptr == NULL) return FALSE;
518 sptr->min_distance = mindist;
519 // we ignore maxdist. It's not really important to this game, and the
520 // FMOD docs suggest that it's worthless anyhow.
522 // recalc sources to see if we need to adjust attenuation.
523 for (int i = 0; i < num_channels; i++)
525 if (channels[i].sample == sptr)
527 const float *p = channels[i].position;
528 set_channel_position(i, p[0], p[1], p[2]);
535 signed char F_API OPENAL_SetFrequency(int channel, int freq)
537 if (!initialized) return FALSE;
538 if (channel == FSOUND_ALL)
540 for (int i = 0; i < num_channels; i++)
541 OPENAL_SetFrequency(i, freq);
545 if ((channel < 0) || (channel >= num_channels)) return FALSE;
546 if (freq == 8012) // hack
547 alSourcef(channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
549 alSourcef(channels[channel].sid, AL_PITCH, 1.0f);
553 signed char F_API OPENAL_SetVolume(int channel, int vol)
555 if (!initialized) return FALSE;
557 if (channel == FSOUND_ALL)
559 for (int i = 0; i < num_channels; i++)
560 OPENAL_SetVolume(i, vol);
564 if ((channel < 0) || (channel >= num_channels)) return FALSE;
566 if (vol < 0) vol = 0;
567 else if (vol > 255) vol = 255;
568 ALfloat gain = ((ALfloat) vol) / 255.0f;
569 alSourcef(channels[channel].sid, AL_GAIN, gain);
573 signed char F_API OPENAL_SetPaused(int channel, signed char paused)
575 if (!initialized) return FALSE;
577 if (channel == FSOUND_ALL)
579 for (int i = 0; i < num_channels; i++)
580 OPENAL_SetPaused(i, paused);
584 if ((channel < 0) || (channel >= num_channels)) return FALSE;
587 if (channels[channel].startpaused)
590 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
592 if ((paused) && (state == AL_PLAYING))
593 alSourcePause(channels[channel].sid);
594 else if ((!paused) && (state == AL_PAUSED))
596 alSourcePlay(channels[channel].sid);
597 channels[channel].startpaused = false;
602 void F_API OPENAL_SetSFXMasterVolume(int volume)
604 if (!initialized) return;
605 ALfloat gain = ((ALfloat) volume) / 255.0f;
606 alListenerf(AL_GAIN, gain);
609 signed char F_API OPENAL_StopSound(int channel)
611 if (!initialized) return FALSE;
613 if (channel == FSOUND_ALL)
615 for (int i = 0; i < num_channels; i++)
620 if ((channel < 0) || (channel >= num_channels)) return FALSE;
621 alSourceStop(channels[channel].sid);
622 channels[channel].startpaused = false;
626 FSOUND_STREAM * F_API OPENAL_Stream_Open(const char *name_or_data, unsigned int mode, int offset, int length)
628 return (FSOUND_STREAM *) OPENAL_Sample_Load(FSOUND_FREE, name_or_data, mode, offset, length);
631 signed char F_API OPENAL_Stream_Close(FSOUND_STREAM *stream)
633 OPENAL_Sample_Free((FSOUND_SAMPLE *) stream);
636 FSOUND_SAMPLE * F_API OPENAL_Stream_GetSample(FSOUND_STREAM *stream)
638 if (!initialized) return NULL;
639 return (FSOUND_SAMPLE *) stream;
642 int F_API OPENAL_Stream_PlayEx(int channel, FSOUND_STREAM *stream, FSOUND_DSPUNIT *dsp, signed char startpaused)
644 return OPENAL_PlaySoundEx(channel, (FSOUND_SAMPLE *) stream, dsp, startpaused);
647 signed char F_API OPENAL_Stream_Stop(FSOUND_STREAM *stream)
649 if (!initialized) return FALSE;
650 for (int i = 0; i < num_channels; i++)
652 if (channels[i].sample == (FSOUND_SAMPLE *) stream)
654 alSourceStop(channels[i].sid);
655 channels[i].startpaused = false;
661 signed char F_API OPENAL_Stream_SetMode(FSOUND_STREAM *stream, unsigned int mode)
663 return OPENAL_Sample_SetMode((FSOUND_SAMPLE *) stream, mode);
666 void F_API OPENAL_Update()
668 if (!initialized) return;
669 alcProcessContext(alcGetCurrentContext());
672 signed char F_API OPENAL_SetOutput(int outputtype)