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
99 ALuint bid; // buffer id.
103 static size_t num_channels = 0;
104 static OPENAL_Channels *channels = NULL;
105 static bool initialized = false;
108 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)
110 if (!initialized) return;
112 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
113 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
114 alListenerfv(AL_ORIENTATION, vec);
116 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
119 signed char F_API OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
121 if (!initialized) return FALSE;
122 if ((channel < 0) || (channel >= num_channels)) return FALSE;
125 alSource3f(channels[channel].sid, AL_POSITION, pos[0], pos[1], -pos[2]);
127 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
131 void F_API OPENAL_3D_SetDopplerFactor(float scale)
133 if (!initialized) return;
134 // unimplemented...looks like init routines just call this with scale == 0.0f anyhow.
137 signed char F_API OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
139 if (initialized) return FALSE;
140 if (maxsoftwarechannels == 0) return FALSE;
142 if (flags != 0) // unsupported.
145 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
147 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
149 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
154 ALCdevice *dev = alcOpenDevice(NULL);
158 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
159 ALCcontext *ctx = alcCreateContext(dev, caps);
166 alcMakeContextCurrent(ctx);
167 alcProcessContext(ctx);
169 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
170 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
171 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
172 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
174 num_channels = maxsoftwarechannels;
175 channels = new OPENAL_Channels[maxsoftwarechannels];
176 memset(channels, '\0', sizeof (OPENAL_Channels) * num_channels);
177 for (int i = 0; i < num_channels; i++)
178 alGenSources(1, &channels[i].sid); // !!! FIXME: verify this didn't fail!
184 void F_API OPENAL_Close()
186 if (!initialized) return;
188 ALCcontext *ctx = alcGetCurrentContext();
191 for (int i = 0; i < num_channels; i++)
193 alSourceStop(channels[i].sid);
194 alSourcei(channels[i].sid, AL_BUFFER, 0);
195 alDeleteSources(1, &channels[i].sid);
197 ALCdevice *dev = alcGetContextsDevice(ctx);
198 alcMakeContextCurrent(NULL);
199 alcSuspendContext(ctx);
200 alcDestroyContext(ctx);
212 FSOUND_SAMPLE *F_API OPENAL_GetCurrentSample(int channel)
214 if (!initialized) return NULL;
215 if ((channel < 0) || (channel >= num_channels)) return NULL;
216 return channels[channel].sample;
219 signed char F_API OPENAL_GetPaused(int channel)
221 if (!initialized) return FALSE;
222 if ((channel < 0) || (channel >= num_channels)) return FALSE;
223 if (channels[channel].startpaused)
227 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
228 return((state == AL_PAUSED) ? TRUE : FALSE);
231 unsigned int F_API OPENAL_GetLoopMode(int channel)
233 if (!initialized) return 0;
234 if ((channel < 0) || (channel >= num_channels)) return 0;
236 alGetSourceiv(channels[channel].sid, AL_LOOPING, &loop);
238 return(FSOUND_LOOP_NORMAL);
239 return FSOUND_LOOP_OFF;
242 signed char F_API OPENAL_IsPlaying(int channel)
244 if (!initialized) return FALSE;
245 if ((channel < 0) || (channel >= num_channels)) return FALSE;
247 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
248 return((state == AL_PLAYING) ? TRUE : FALSE);
251 int F_API OPENAL_PlaySoundEx(int channel, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
253 if (!initialized) return -1;
254 if (sptr == NULL) return -1;
255 if (dsp != NULL) return -1;
256 if (channel == FSOUND_FREE)
258 for (int i = 0; i < num_channels; i++)
261 alGetSourceiv(channels[i].sid, AL_SOURCE_STATE, &state);
262 if ((state != AL_PLAYING) && (state != AL_PAUSED))
270 if ((channel < 0) || (channel >= num_channels)) return -1;
272 alSourceStop(channels[channel].sid);
273 channels[channel].sample = sptr;
274 alSourcei(channels[channel].sid, AL_BUFFER, sptr->bid);
275 alSourcei(channels[channel].sid, AL_LOOPING, (sptr->mode == FSOUND_LOOP_OFF) ? AL_FALSE : AL_TRUE);
276 channels[channel].startpaused = ((startpaused) ? true : false);
278 alSourcePlay(channels[channel].sid);
283 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
285 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
286 char *fname = (char *) alloca(strlen(_fname) + 16);
287 strcpy(fname, _fname);
288 char *ptr = strchr(fname, '.');
289 if (ptr) *ptr = NULL;
290 strcat(fname, ".ogg");
294 FILE *io = fopen(fname, "rb");
298 ALubyte *retval = NULL;
300 // Can we just feed it to the AL compressed?
301 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
303 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
305 fseek(io, 0, SEEK_END);
307 fseek(io, 0, SEEK_SET);
308 retval = (ALubyte *) malloc(size);
309 size_t rc = fread(retval, size, 1, io);
319 // Uncompress and feed to the AL.
321 memset(&vf, '\0', sizeof (vf));
322 if (ov_open(io, &vf, NULL, 0) == 0)
325 vorbis_info *info = ov_info(&vf, -1);
327 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
330 if ((info->channels != 1) && (info->channels != 2))
338 size_t allocated = 64 * 1024;
339 retval = (ALubyte *) malloc(allocated);
340 while ( (rc = ov_read(&vf, buf, sizeof (buf), 0, 2, 1, &bitstream)) != 0 )
345 if (size >= allocated)
348 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
357 memcpy(retval + (size - rc), buf, rc);
369 FSOUND_SAMPLE * F_API OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
371 if (!initialized) return NULL;
372 if (index != FSOUND_FREE) return NULL; // this is all the game does...
373 if (offset != 0) return NULL; // this is all the game does...
374 if (length != 0) return NULL; // this is all the game does...
375 if ((mode != FSOUND_HW3D) && (mode != FSOUND_2D)) return NULL; // this is all the game does...
377 FSOUND_SAMPLE *retval = NULL;
378 ALuint bufferName = 0;
379 ALenum format = AL_NONE;
381 ALuint frequency = 0;
382 void *data = decode_to_pcm(name_or_data, format, size, frequency);
388 alGenBuffers(1, &bid);
389 if (alGetError() == AL_NO_ERROR)
391 alBufferData(bid, format, data, size, frequency);
392 retval = new FSOUND_SAMPLE;
394 retval->mode = FSOUND_LOOP_OFF;
401 void F_API OPENAL_Sample_Free(FSOUND_SAMPLE *sptr)
403 if (!initialized) return;
406 for (int i = 0; i < num_channels; i++)
408 if (channels[i].sample == sptr)
410 alSourceStop(channels[i].sid);
411 alSourcei(channels[i].sid, AL_BUFFER, 0);
412 channels[i].sample = NULL;
415 alDeleteBuffers(1, &sptr->bid);
420 signed char F_API OPENAL_Sample_SetMode(FSOUND_SAMPLE *sptr, unsigned int mode)
422 if (!initialized) return FALSE;
423 if (mode != FSOUND_LOOP_NORMAL) return FALSE;
424 if (!sptr) return FALSE;
429 signed char F_API OPENAL_Sample_SetMinMaxDistance(FSOUND_SAMPLE *sptr, float min, float max)
431 if (!initialized) return FALSE;
432 if (sptr == NULL) return FALSE;
433 // !!! FIXME: write me
437 signed char F_API OPENAL_SetFrequency(int channel, int freq)
439 if (!initialized) return FALSE;
440 return TRUE; // ignore this for now...
443 signed char F_API OPENAL_SetVolume(int channel, int vol)
445 if (!initialized) return FALSE;
446 if ((channel < 0) || (channel >= num_channels)) return FALSE;
447 if ((vol < 0) || (vol > 255)) return FALSE;
448 ALfloat gain = ((ALfloat) vol) / 255.0f;
449 alSourcef(channels[channel].sid, AL_GAIN, gain);
453 signed char F_API OPENAL_SetPaused(int channel, signed char paused)
455 if (!initialized) return FALSE;
456 if ((channel < 0) || (channel >= num_channels)) return FALSE;
459 if (channels[channel].startpaused)
462 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
464 if ((paused) && (state == AL_PLAYING))
465 alSourcePause(channels[channel].sid);
466 else if ((!paused) && (state == AL_PAUSED))
468 alSourcePlay(channels[channel].sid);
469 channels[channel].startpaused = false;
474 void F_API OPENAL_SetSFXMasterVolume(int volume)
476 if (!initialized) return;
477 ALfloat gain = ((ALfloat) volume) / 255.0f;
478 alListenerf(AL_GAIN, gain);
481 signed char F_API OPENAL_StopSound(int channel)
483 if (!initialized) return FALSE;
484 if ((channel < 0) || (channel >= num_channels)) return FALSE;
485 alSourceStop(channels[channel].sid);
489 FSOUND_STREAM * F_API OPENAL_Stream_Open(const char *name_or_data, unsigned int mode, int offset, int length)
491 if (!initialized) return NULL;
495 signed char F_API OPENAL_Stream_Close(FSOUND_STREAM *stream)
497 if (!initialized) return FALSE;
501 FSOUND_SAMPLE * F_API OPENAL_Stream_GetSample(FSOUND_STREAM *stream)
503 if (!initialized) return NULL;
507 int F_API OPENAL_Stream_PlayEx(int channel, FSOUND_STREAM *stream, FSOUND_DSPUNIT *dsp, signed char startpaused)
509 if (!initialized) return -1;
513 signed char F_API OPENAL_Stream_Stop(FSOUND_STREAM *stream)
515 if (!initialized) return FALSE;
519 signed char F_API OPENAL_Stream_SetMode(FSOUND_STREAM *stream, unsigned int mode)
521 if (!initialized) return FALSE;
525 void F_API OPENAL_Update()
527 if (!initialized) return;
528 alcProcessContext(alcGetCurrentContext());