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;
97 typedef struct FSOUND_SAMPLE
100 ALuint bid; // buffer id.
105 typedef struct FSOUND_STREAM
108 ALuint bid; // buffer id.
113 static size_t num_channels = 0;
114 static OPENAL_Channels *channels = NULL;
115 static bool initialized = false;
118 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)
120 if (!initialized) return;
122 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
123 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
124 alListenerfv(AL_ORIENTATION, vec);
126 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
129 signed char F_API OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
131 if (!initialized) return FALSE;
132 if ((channel < 0) || (channel >= num_channels)) return FALSE;
134 if ((pos != NULL) && (!channels[channel].sample->is2d))
135 alSource3f(channels[channel].sid, AL_POSITION, pos[0], pos[1], -pos[2]);
137 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
141 void F_API OPENAL_3D_SetDopplerFactor(float scale)
143 if (!initialized) return;
144 // unimplemented...looks like init routines just call this with scale == 0.0f anyhow.
147 signed char F_API OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
149 if (initialized) return FALSE;
150 if (maxsoftwarechannels == 0) return FALSE;
152 if (flags != 0) // unsupported.
155 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
157 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
159 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
164 ALCdevice *dev = alcOpenDevice(NULL);
168 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
169 ALCcontext *ctx = alcCreateContext(dev, caps);
176 alcMakeContextCurrent(ctx);
177 alcProcessContext(ctx);
179 bool cmdline(const char *cmd);
180 if (cmdline("openalinfo"))
182 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
183 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
184 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
185 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
188 num_channels = maxsoftwarechannels;
189 channels = new OPENAL_Channels[maxsoftwarechannels];
190 memset(channels, '\0', sizeof (OPENAL_Channels) * num_channels);
191 for (int i = 0; i < num_channels; i++)
192 alGenSources(1, &channels[i].sid); // !!! FIXME: verify this didn't fail!
198 void F_API OPENAL_Close()
200 if (!initialized) return;
202 ALCcontext *ctx = alcGetCurrentContext();
205 for (int i = 0; i < num_channels; i++)
207 alSourceStop(channels[i].sid);
208 alSourcei(channels[i].sid, AL_BUFFER, 0);
209 alDeleteSources(1, &channels[i].sid);
211 ALCdevice *dev = alcGetContextsDevice(ctx);
212 alcMakeContextCurrent(NULL);
213 alcSuspendContext(ctx);
214 alcDestroyContext(ctx);
226 FSOUND_SAMPLE *F_API OPENAL_GetCurrentSample(int channel)
228 if (!initialized) return NULL;
229 if ((channel < 0) || (channel >= num_channels)) return NULL;
230 return channels[channel].sample;
233 signed char F_API OPENAL_GetPaused(int channel)
235 if (!initialized) return FALSE;
236 if ((channel < 0) || (channel >= num_channels)) return FALSE;
237 if (channels[channel].startpaused)
241 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
242 return((state == AL_PAUSED) ? TRUE : FALSE);
245 unsigned int F_API OPENAL_GetLoopMode(int channel)
247 if (!initialized) return 0;
248 if ((channel < 0) || (channel >= num_channels)) return 0;
250 alGetSourceiv(channels[channel].sid, AL_LOOPING, &loop);
252 return(FSOUND_LOOP_NORMAL);
253 return FSOUND_LOOP_OFF;
256 signed char F_API OPENAL_IsPlaying(int channel)
258 if (!initialized) return FALSE;
259 if ((channel < 0) || (channel >= num_channels)) return FALSE;
261 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
262 return((state == AL_PLAYING) ? TRUE : FALSE);
265 int F_API OPENAL_PlaySoundEx(int channel, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
267 if (!initialized) return -1;
268 if (sptr == NULL) return -1;
269 if (dsp != NULL) return -1;
270 if (channel == FSOUND_FREE)
272 for (int i = 0; i < num_channels; i++)
275 alGetSourceiv(channels[i].sid, AL_SOURCE_STATE, &state);
276 if ((state != AL_PLAYING) && (state != AL_PAUSED))
284 if ((channel < 0) || (channel >= num_channels)) return -1;
285 alSourceStop(channels[channel].sid);
286 channels[channel].sample = sptr;
287 alSourcei(channels[channel].sid, AL_BUFFER, sptr->bid);
288 alSourcei(channels[channel].sid, AL_LOOPING, (sptr->mode == FSOUND_LOOP_OFF) ? AL_FALSE : AL_TRUE);
289 alSourcei(channels[channel].sid, AL_SOURCE_RELATIVE, (sptr->is2d) ? AL_TRUE : AL_FALSE);
290 alSource3f(channels[channel].sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
292 channels[channel].startpaused = ((startpaused) ? true : false);
294 alSourcePlay(channels[channel].sid);
299 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
302 const int bigendian = 1;
304 const int bigendian = 0;
307 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
308 char *fname = (char *) alloca(strlen(_fname) + 16);
309 strcpy(fname, _fname);
310 char *ptr = strchr(fname, '.');
311 if (ptr) *ptr = NULL;
312 strcat(fname, ".ogg");
316 FILE *io = fopen(fname, "rb");
320 ALubyte *retval = NULL;
322 #if 0 // untested, so disable this!
323 // Can we just feed it to the AL compressed?
324 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
326 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
328 fseek(io, 0, SEEK_END);
330 fseek(io, 0, SEEK_SET);
331 retval = (ALubyte *) malloc(size);
332 size_t rc = fread(retval, size, 1, io);
343 // Uncompress and feed to the AL.
345 memset(&vf, '\0', sizeof (vf));
346 if (ov_open(io, &vf, NULL, 0) == 0)
349 vorbis_info *info = ov_info(&vf, -1);
351 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
354 if ((info->channels != 1) && (info->channels != 2))
362 size_t allocated = 64 * 1024;
363 retval = (ALubyte *) malloc(allocated);
364 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
369 if (size >= allocated)
372 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
381 memcpy(retval + (size - rc), buf, rc);
393 FSOUND_SAMPLE * F_API OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
395 if (!initialized) return NULL;
396 if (index != FSOUND_FREE) return NULL; // this is all the game does...
397 if (offset != 0) return NULL; // this is all the game does...
398 if (length != 0) return NULL; // this is all the game does...
399 if ((mode != FSOUND_HW3D) && (mode != FSOUND_2D)) return NULL; // this is all the game does...
401 FSOUND_SAMPLE *retval = NULL;
402 ALuint bufferName = 0;
403 ALenum format = AL_NONE;
405 ALuint frequency = 0;
406 void *data = decode_to_pcm(name_or_data, format, size, frequency);
412 alGenBuffers(1, &bid);
413 if (alGetError() == AL_NO_ERROR)
415 alBufferData(bid, format, data, size, frequency);
416 retval = new FSOUND_SAMPLE;
418 retval->mode = FSOUND_LOOP_OFF;
419 retval->is2d = (mode == FSOUND_2D);
420 retval->name = new char[strlen(name_or_data) + 1];
422 strcpy(retval->name, name_or_data);
429 void F_API OPENAL_Sample_Free(FSOUND_SAMPLE *sptr)
431 if (!initialized) return;
434 for (int i = 0; i < num_channels; i++)
436 if (channels[i].sample == sptr)
438 alSourceStop(channels[i].sid);
439 alSourcei(channels[i].sid, AL_BUFFER, 0);
440 channels[i].sample = NULL;
443 alDeleteBuffers(1, &sptr->bid);
449 signed char F_API OPENAL_Sample_SetMode(FSOUND_SAMPLE *sptr, unsigned int mode)
451 if (!initialized) return FALSE;
452 if ((mode != FSOUND_LOOP_NORMAL) && (mode != FSOUND_LOOP_OFF)) return FALSE;
453 if (!sptr) return FALSE;
458 signed char F_API OPENAL_Sample_SetMinMaxDistance(FSOUND_SAMPLE *sptr, float min, float max)
460 if (!initialized) return FALSE;
461 if (sptr == NULL) return FALSE;
462 // !!! FIXME: write me
466 signed char F_API OPENAL_SetFrequency(int channel, int freq)
468 if (!initialized) return FALSE;
469 if (channel == FSOUND_ALL)
471 for (int i = 0; i < num_channels; i++)
472 OPENAL_SetFrequency(i, freq);
476 if ((channel < 0) || (channel >= num_channels)) return FALSE;
477 if (freq == 8012) // hack
478 alSourcef(channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
480 alSourcef(channels[channel].sid, AL_PITCH, 1.0f);
484 signed char F_API OPENAL_SetVolume(int channel, int vol)
486 if (!initialized) return FALSE;
488 if (channel == FSOUND_ALL)
490 for (int i = 0; i < num_channels; i++)
491 OPENAL_SetVolume(i, vol);
495 if ((channel < 0) || (channel >= num_channels)) return FALSE;
497 if (vol < 0) vol = 0;
498 else if (vol > 255) vol = 255;
499 ALfloat gain = ((ALfloat) vol) / 255.0f;
500 alSourcef(channels[channel].sid, AL_GAIN, gain);
504 signed char F_API OPENAL_SetPaused(int channel, signed char paused)
506 if (!initialized) return FALSE;
508 if (channel == FSOUND_ALL)
510 for (int i = 0; i < num_channels; i++)
511 OPENAL_SetPaused(i, paused);
515 if ((channel < 0) || (channel >= num_channels)) return FALSE;
518 if (channels[channel].startpaused)
521 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
523 if ((paused) && (state == AL_PLAYING))
524 alSourcePause(channels[channel].sid);
525 else if ((!paused) && (state == AL_PAUSED))
527 alSourcePlay(channels[channel].sid);
528 channels[channel].startpaused = false;
533 void F_API OPENAL_SetSFXMasterVolume(int volume)
535 if (!initialized) return;
536 ALfloat gain = ((ALfloat) volume) / 255.0f;
537 alListenerf(AL_GAIN, gain);
540 signed char F_API OPENAL_StopSound(int channel)
542 if (!initialized) return FALSE;
544 if (channel == FSOUND_ALL)
546 for (int i = 0; i < num_channels; i++)
551 if ((channel < 0) || (channel >= num_channels)) return FALSE;
552 alSourceStop(channels[channel].sid);
553 channels[channel].startpaused = false;
557 FSOUND_STREAM * F_API OPENAL_Stream_Open(const char *name_or_data, unsigned int mode, int offset, int length)
559 return (FSOUND_STREAM *) OPENAL_Sample_Load(FSOUND_FREE, name_or_data, mode, offset, length);
562 signed char F_API OPENAL_Stream_Close(FSOUND_STREAM *stream)
564 OPENAL_Sample_Free((FSOUND_SAMPLE *) stream);
567 FSOUND_SAMPLE * F_API OPENAL_Stream_GetSample(FSOUND_STREAM *stream)
569 if (!initialized) return NULL;
570 return (FSOUND_SAMPLE *) stream;
573 int F_API OPENAL_Stream_PlayEx(int channel, FSOUND_STREAM *stream, FSOUND_DSPUNIT *dsp, signed char startpaused)
575 return OPENAL_PlaySoundEx(channel, (FSOUND_SAMPLE *) stream, dsp, startpaused);
578 signed char F_API OPENAL_Stream_Stop(FSOUND_STREAM *stream)
580 if (!initialized) return FALSE;
581 for (int i = 0; i < num_channels; i++)
583 if (channels[i].sample == (FSOUND_SAMPLE *) stream)
585 alSourceStop(channels[i].sid);
586 channels[i].startpaused = false;
592 signed char F_API OPENAL_Stream_SetMode(FSOUND_STREAM *stream, unsigned int mode)
594 return OPENAL_Sample_SetMode((FSOUND_SAMPLE *) stream, mode);
597 void F_API OPENAL_Update()
599 if (!initialized) return;
600 alcProcessContext(alcGetCurrentContext());
603 signed char F_API OPENAL_SetOutput(int outputtype)