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.
104 typedef struct FSOUND_STREAM
106 ALuint bid; // buffer id.
111 static size_t num_channels = 0;
112 static OPENAL_Channels *channels = NULL;
113 static bool initialized = false;
116 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)
118 if (!initialized) return;
120 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
121 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
122 alListenerfv(AL_ORIENTATION, vec);
124 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
127 signed char F_API OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
129 if (!initialized) return FALSE;
130 if ((channel < 0) || (channel >= num_channels)) return FALSE;
132 if ((pos != NULL) && (!channels[channel].sample->is2d))
133 alSource3f(channels[channel].sid, AL_POSITION, pos[0], pos[1], -pos[2]);
135 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
139 void F_API OPENAL_3D_SetDopplerFactor(float scale)
141 if (!initialized) return;
142 // unimplemented...looks like init routines just call this with scale == 0.0f anyhow.
145 signed char F_API OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
147 if (initialized) return FALSE;
148 if (maxsoftwarechannels == 0) return FALSE;
150 if (flags != 0) // unsupported.
153 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
155 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
157 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
162 ALCdevice *dev = alcOpenDevice(NULL);
166 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
167 ALCcontext *ctx = alcCreateContext(dev, caps);
174 alcMakeContextCurrent(ctx);
175 alcProcessContext(ctx);
177 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
178 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
179 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
180 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
182 num_channels = maxsoftwarechannels;
183 channels = new OPENAL_Channels[maxsoftwarechannels];
184 memset(channels, '\0', sizeof (OPENAL_Channels) * num_channels);
185 for (int i = 0; i < num_channels; i++)
186 alGenSources(1, &channels[i].sid); // !!! FIXME: verify this didn't fail!
192 void F_API OPENAL_Close()
194 if (!initialized) return;
196 ALCcontext *ctx = alcGetCurrentContext();
199 for (int i = 0; i < num_channels; i++)
201 alSourceStop(channels[i].sid);
202 alSourcei(channels[i].sid, AL_BUFFER, 0);
203 alDeleteSources(1, &channels[i].sid);
205 ALCdevice *dev = alcGetContextsDevice(ctx);
206 alcMakeContextCurrent(NULL);
207 alcSuspendContext(ctx);
208 alcDestroyContext(ctx);
220 FSOUND_SAMPLE *F_API OPENAL_GetCurrentSample(int channel)
222 if (!initialized) return NULL;
223 if ((channel < 0) || (channel >= num_channels)) return NULL;
224 return channels[channel].sample;
227 signed char F_API OPENAL_GetPaused(int channel)
229 if (!initialized) return FALSE;
230 if ((channel < 0) || (channel >= num_channels)) return FALSE;
231 if (channels[channel].startpaused)
235 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
236 return((state == AL_PAUSED) ? TRUE : FALSE);
239 unsigned int F_API OPENAL_GetLoopMode(int channel)
241 if (!initialized) return 0;
242 if ((channel < 0) || (channel >= num_channels)) return 0;
244 alGetSourceiv(channels[channel].sid, AL_LOOPING, &loop);
246 return(FSOUND_LOOP_NORMAL);
247 return FSOUND_LOOP_OFF;
250 signed char F_API OPENAL_IsPlaying(int channel)
252 if (!initialized) return FALSE;
253 if ((channel < 0) || (channel >= num_channels)) return FALSE;
255 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
256 return((state == AL_PLAYING) ? TRUE : FALSE);
259 int F_API OPENAL_PlaySoundEx(int channel, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
261 if (!initialized) return -1;
262 if (sptr == NULL) return -1;
263 if (dsp != NULL) return -1;
264 if (channel == FSOUND_FREE)
266 for (int i = 0; i < num_channels; i++)
269 alGetSourceiv(channels[i].sid, AL_SOURCE_STATE, &state);
270 if ((state != AL_PLAYING) && (state != AL_PAUSED))
278 if ((channel < 0) || (channel >= num_channels)) return -1;
279 alSourceStop(channels[channel].sid);
280 channels[channel].sample = sptr;
281 alSourcei(channels[channel].sid, AL_BUFFER, sptr->bid);
282 alSourcei(channels[channel].sid, AL_LOOPING, (sptr->mode == FSOUND_LOOP_OFF) ? AL_FALSE : AL_TRUE);
283 alSourcei(channels[channel].sid, AL_SOURCE_RELATIVE, (sptr->is2d) ? AL_TRUE : AL_FALSE);
284 alSource3f(channels[channel].sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
286 channels[channel].startpaused = ((startpaused) ? true : false);
288 alSourcePlay(channels[channel].sid);
293 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
295 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
296 char *fname = (char *) alloca(strlen(_fname) + 16);
297 strcpy(fname, _fname);
298 char *ptr = strchr(fname, '.');
299 if (ptr) *ptr = NULL;
300 strcat(fname, ".ogg");
304 FILE *io = fopen(fname, "rb");
308 ALubyte *retval = NULL;
310 // Can we just feed it to the AL compressed?
311 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
313 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
315 fseek(io, 0, SEEK_END);
317 fseek(io, 0, SEEK_SET);
318 retval = (ALubyte *) malloc(size);
319 size_t rc = fread(retval, size, 1, io);
329 // Uncompress and feed to the AL.
331 memset(&vf, '\0', sizeof (vf));
332 if (ov_open(io, &vf, NULL, 0) == 0)
335 vorbis_info *info = ov_info(&vf, -1);
337 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
340 if ((info->channels != 1) && (info->channels != 2))
348 size_t allocated = 64 * 1024;
349 retval = (ALubyte *) malloc(allocated);
350 while ( (rc = ov_read(&vf, buf, sizeof (buf), 0, 2, 1, &bitstream)) != 0 )
355 if (size >= allocated)
358 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
367 memcpy(retval + (size - rc), buf, rc);
379 FSOUND_SAMPLE * F_API OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
381 if (!initialized) return NULL;
382 if (index != FSOUND_FREE) return NULL; // this is all the game does...
383 if (offset != 0) return NULL; // this is all the game does...
384 if (length != 0) return NULL; // this is all the game does...
385 if ((mode != FSOUND_HW3D) && (mode != FSOUND_2D)) return NULL; // this is all the game does...
387 FSOUND_SAMPLE *retval = NULL;
388 ALuint bufferName = 0;
389 ALenum format = AL_NONE;
391 ALuint frequency = 0;
392 void *data = decode_to_pcm(name_or_data, format, size, frequency);
398 alGenBuffers(1, &bid);
399 if (alGetError() == AL_NO_ERROR)
401 alBufferData(bid, format, data, size, frequency);
402 retval = new FSOUND_SAMPLE;
404 retval->mode = FSOUND_LOOP_OFF;
405 retval->is2d = (mode == FSOUND_2D);
412 void F_API OPENAL_Sample_Free(FSOUND_SAMPLE *sptr)
414 if (!initialized) return;
417 for (int i = 0; i < num_channels; i++)
419 if (channels[i].sample == sptr)
421 alSourceStop(channels[i].sid);
422 alSourcei(channels[i].sid, AL_BUFFER, 0);
423 channels[i].sample = NULL;
426 alDeleteBuffers(1, &sptr->bid);
431 signed char F_API OPENAL_Sample_SetMode(FSOUND_SAMPLE *sptr, unsigned int mode)
433 if (!initialized) return FALSE;
434 if ((mode != FSOUND_LOOP_NORMAL) && (mode != FSOUND_LOOP_OFF)) return FALSE;
435 if (!sptr) return FALSE;
440 signed char F_API OPENAL_Sample_SetMinMaxDistance(FSOUND_SAMPLE *sptr, float min, float max)
442 if (!initialized) return FALSE;
443 if (sptr == NULL) return FALSE;
444 // !!! FIXME: write me
448 signed char F_API OPENAL_SetFrequency(int channel, int freq)
450 if (!initialized) return FALSE;
451 if (channel == FSOUND_ALL)
453 for (int i = 0; i < num_channels; i++)
454 OPENAL_SetFrequency(i, freq);
458 if ((channel < 0) || (channel >= num_channels)) return FALSE;
459 if (freq == 8012) // hack
460 alSourcef(channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
462 alSourcef(channels[channel].sid, AL_PITCH, 1.0f);
463 return TRUE; // ignore this for now...
466 signed char F_API OPENAL_SetVolume(int channel, int vol)
468 if (!initialized) return FALSE;
469 if ((channel < 0) || (channel >= num_channels)) return FALSE;
470 if ((vol < 0) || (vol > 255)) return FALSE;
471 ALfloat gain = ((ALfloat) vol) / 255.0f;
472 alSourcef(channels[channel].sid, AL_GAIN, gain);
476 signed char F_API OPENAL_SetPaused(int channel, signed char paused)
478 if (!initialized) return FALSE;
479 if ((channel < 0) || (channel >= num_channels)) return FALSE;
482 if (channels[channel].startpaused)
485 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
487 if ((paused) && (state == AL_PLAYING))
488 alSourcePause(channels[channel].sid);
489 else if ((!paused) && (state == AL_PAUSED))
491 alSourcePlay(channels[channel].sid);
492 channels[channel].startpaused = false;
497 void F_API OPENAL_SetSFXMasterVolume(int volume)
499 if (!initialized) return;
500 ALfloat gain = ((ALfloat) volume) / 255.0f;
501 alListenerf(AL_GAIN, gain);
504 signed char F_API OPENAL_StopSound(int channel)
506 if (!initialized) return FALSE;
507 if ((channel < 0) || (channel >= num_channels)) return FALSE;
508 alSourceStop(channels[channel].sid);
512 FSOUND_STREAM * F_API OPENAL_Stream_Open(const char *name_or_data, unsigned int mode, int offset, int length)
514 return (FSOUND_STREAM *) OPENAL_Sample_Load(FSOUND_FREE, name_or_data, mode, offset, length);
517 signed char F_API OPENAL_Stream_Close(FSOUND_STREAM *stream)
519 OPENAL_Sample_Free((FSOUND_SAMPLE *) stream);
522 FSOUND_SAMPLE * F_API OPENAL_Stream_GetSample(FSOUND_STREAM *stream)
524 if (!initialized) return NULL;
525 return NULL; // this is wrong, but it works for Lugaru 1.
528 int F_API OPENAL_Stream_PlayEx(int channel, FSOUND_STREAM *stream, FSOUND_DSPUNIT *dsp, signed char startpaused)
530 return OPENAL_PlaySoundEx(channel, (FSOUND_SAMPLE *) stream, dsp, startpaused);
533 signed char F_API OPENAL_Stream_Stop(FSOUND_STREAM *stream)
535 if (!initialized) return FALSE;
536 for (int i = 0; i < num_channels; i++)
538 if (channels[i].sample == (FSOUND_SAMPLE *) stream)
540 alSourceStop(channels[i].sid);
541 channels[i].startpaused = false;
547 signed char F_API OPENAL_Stream_SetMode(FSOUND_STREAM *stream, unsigned int mode)
549 return OPENAL_Sample_SetMode((FSOUND_SAMPLE *) stream, mode);
552 void F_API OPENAL_Update()
554 if (!initialized) return;
555 alcProcessContext(alcGetCurrentContext());
558 signed char F_API OPENAL_SetOutput(int outputtype)