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;
95 FSOUND_STREAM *stream;
98 typedef struct FSOUND_SAMPLE
100 ALuint bid; // buffer id.
102 } FSOUND_SAMPLE, FSOUND_STREAM;
105 static size_t num_channels = 0;
106 static OPENAL_Channels *channels = NULL;
107 static bool initialized = false;
110 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)
112 if (!initialized) return;
114 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
115 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
116 alListenerfv(AL_ORIENTATION, vec);
118 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
121 signed char F_API OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
123 if (!initialized) return FALSE;
124 if ((channel < 0) || (channel >= num_channels)) return FALSE;
127 alSource3f(channels[channel].sid, AL_POSITION, pos[0], pos[1], -pos[2]);
129 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
133 void F_API OPENAL_3D_SetDopplerFactor(float scale)
135 if (!initialized) return;
136 // unimplemented...looks like init routines just call this with scale == 0.0f anyhow.
139 signed char F_API OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
141 if (initialized) return FALSE;
142 if (maxsoftwarechannels == 0) return FALSE;
144 if (flags != 0) // unsupported.
147 if (!lookup_all_alsyms("./openal.so")) // !!! FIXME: linux specific lib name
149 if (!lookup_all_alsyms("openal.so.1")) // !!! FIXME: linux specific lib name
151 if (!lookup_all_alsyms("openal.so")) // !!! FIXME: linux specific lib name
156 ALCdevice *dev = alcOpenDevice(NULL);
160 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
161 ALCcontext *ctx = alcCreateContext(dev, caps);
168 alcMakeContextCurrent(ctx);
169 alcProcessContext(ctx);
171 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
172 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
173 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
174 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
176 num_channels = maxsoftwarechannels;
177 channels = new OPENAL_Channels[maxsoftwarechannels];
178 memset(channels, '\0', sizeof (OPENAL_Channels) * num_channels);
179 for (int i = 0; i < num_channels; i++)
180 alGenSources(1, &channels[i].sid); // !!! FIXME: verify this didn't fail!
186 void F_API OPENAL_Close()
188 if (!initialized) return;
190 ALCcontext *ctx = alcGetCurrentContext();
193 for (int i = 0; i < num_channels; i++)
195 alSourceStop(channels[i].sid);
196 alSourcei(channels[i].sid, AL_BUFFER, 0);
197 alDeleteSources(1, &channels[i].sid);
199 ALCdevice *dev = alcGetContextsDevice(ctx);
200 alcMakeContextCurrent(NULL);
201 alcSuspendContext(ctx);
202 alcDestroyContext(ctx);
214 FSOUND_SAMPLE *F_API OPENAL_GetCurrentSample(int channel)
216 if (!initialized) return NULL;
217 if ((channel < 0) || (channel >= num_channels)) return NULL;
218 return channels[channel].sample;
221 signed char F_API OPENAL_GetPaused(int channel)
223 if (!initialized) return FALSE;
224 if ((channel < 0) || (channel >= num_channels)) return FALSE;
225 if (channels[channel].startpaused)
229 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
230 return((state == AL_PAUSED) ? TRUE : FALSE);
233 unsigned int F_API OPENAL_GetLoopMode(int channel)
235 if (!initialized) return 0;
236 if ((channel < 0) || (channel >= num_channels)) return 0;
238 alGetSourceiv(channels[channel].sid, AL_LOOPING, &loop);
240 return(FSOUND_LOOP_NORMAL);
241 return FSOUND_LOOP_OFF;
244 signed char F_API OPENAL_IsPlaying(int channel)
246 if (!initialized) return FALSE;
247 if ((channel < 0) || (channel >= num_channels)) return FALSE;
249 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
250 return((state == AL_PLAYING) ? TRUE : FALSE);
253 int F_API OPENAL_PlaySoundEx(int channel, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
255 if (!initialized) return -1;
256 if (sptr == NULL) return -1;
257 if (dsp != NULL) return -1;
258 if (channel == FSOUND_FREE)
260 for (int i = 0; i < num_channels; i++)
263 alGetSourceiv(channels[i].sid, AL_SOURCE_STATE, &state);
264 if ((state != AL_PLAYING) && (state != AL_PAUSED))
272 if ((channel < 0) || (channel >= num_channels)) return -1;
273 alSourceStop(channels[channel].sid);
274 channels[channel].sample = sptr;
275 alSourcei(channels[channel].sid, AL_BUFFER, sptr->bid);
276 alSourcei(channels[channel].sid, AL_LOOPING, (sptr->mode == FSOUND_LOOP_OFF) ? AL_FALSE : AL_TRUE);
277 channels[channel].startpaused = ((startpaused) ? true : false);
279 alSourcePlay(channels[channel].sid);
284 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
286 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
287 char *fname = (char *) alloca(strlen(_fname) + 16);
288 strcpy(fname, _fname);
289 char *ptr = strchr(fname, '.');
290 if (ptr) *ptr = NULL;
291 strcat(fname, ".ogg");
295 FILE *io = fopen(fname, "rb");
299 ALubyte *retval = NULL;
301 // Can we just feed it to the AL compressed?
302 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
304 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
306 fseek(io, 0, SEEK_END);
308 fseek(io, 0, SEEK_SET);
309 retval = (ALubyte *) malloc(size);
310 size_t rc = fread(retval, size, 1, io);
320 // Uncompress and feed to the AL.
322 memset(&vf, '\0', sizeof (vf));
323 if (ov_open(io, &vf, NULL, 0) == 0)
326 vorbis_info *info = ov_info(&vf, -1);
328 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
331 if ((info->channels != 1) && (info->channels != 2))
339 size_t allocated = 64 * 1024;
340 retval = (ALubyte *) malloc(allocated);
341 while ( (rc = ov_read(&vf, buf, sizeof (buf), 0, 2, 1, &bitstream)) != 0 )
346 if (size >= allocated)
349 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
358 memcpy(retval + (size - rc), buf, rc);
370 FSOUND_SAMPLE * F_API OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
372 if (!initialized) return NULL;
373 if (index != FSOUND_FREE) return NULL; // this is all the game does...
374 if (offset != 0) return NULL; // this is all the game does...
375 if (length != 0) return NULL; // this is all the game does...
376 if ((mode != FSOUND_HW3D) && (mode != FSOUND_2D)) return NULL; // this is all the game does...
378 FSOUND_SAMPLE *retval = NULL;
379 ALuint bufferName = 0;
380 ALenum format = AL_NONE;
382 ALuint frequency = 0;
383 void *data = decode_to_pcm(name_or_data, format, size, frequency);
389 alGenBuffers(1, &bid);
390 if (alGetError() == AL_NO_ERROR)
392 alBufferData(bid, format, data, size, frequency);
393 retval = new FSOUND_SAMPLE;
395 retval->mode = FSOUND_LOOP_OFF;
402 void F_API OPENAL_Sample_Free(FSOUND_SAMPLE *sptr)
404 if (!initialized) return;
407 for (int i = 0; i < num_channels; i++)
409 if (channels[i].sample == sptr)
411 alSourceStop(channels[i].sid);
412 alSourcei(channels[i].sid, AL_BUFFER, 0);
413 channels[i].sample = NULL;
416 alDeleteBuffers(1, &sptr->bid);
421 signed char F_API OPENAL_Sample_SetMode(FSOUND_SAMPLE *sptr, unsigned int mode)
423 if (!initialized) return FALSE;
424 if ((mode != FSOUND_LOOP_NORMAL) && (mode != FSOUND_LOOP_OFF)) return FALSE;
425 if (!sptr) return FALSE;
430 signed char F_API OPENAL_Sample_SetMinMaxDistance(FSOUND_SAMPLE *sptr, float min, float max)
432 if (!initialized) return FALSE;
433 if (sptr == NULL) return FALSE;
434 // !!! FIXME: write me
438 signed char F_API OPENAL_SetFrequency(int channel, int freq)
440 if (!initialized) return FALSE;
441 if (channel == FSOUND_ALL)
443 for (int i = 0; i < num_channels; i++)
444 OPENAL_SetFrequency(i, freq);
448 if ((channel < 0) || (channel >= num_channels)) return FALSE;
449 if (freq == 8012) // hack
450 alSourcef(channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
452 alSourcef(channels[channel].sid, AL_PITCH, 1.0f);
453 return TRUE; // ignore this for now...
456 signed char F_API OPENAL_SetVolume(int channel, int vol)
458 if (!initialized) return FALSE;
459 if ((channel < 0) || (channel >= num_channels)) return FALSE;
460 if ((vol < 0) || (vol > 255)) return FALSE;
461 ALfloat gain = ((ALfloat) vol) / 255.0f;
462 alSourcef(channels[channel].sid, AL_GAIN, gain);
466 signed char F_API OPENAL_SetPaused(int channel, signed char paused)
468 if (!initialized) return FALSE;
469 if ((channel < 0) || (channel >= num_channels)) return FALSE;
472 if (channels[channel].startpaused)
475 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
477 if ((paused) && (state == AL_PLAYING))
478 alSourcePause(channels[channel].sid);
479 else if ((!paused) && (state == AL_PAUSED))
481 alSourcePlay(channels[channel].sid);
482 channels[channel].startpaused = false;
487 void F_API OPENAL_SetSFXMasterVolume(int volume)
489 if (!initialized) return;
490 ALfloat gain = ((ALfloat) volume) / 255.0f;
491 alListenerf(AL_GAIN, gain);
494 signed char F_API OPENAL_StopSound(int channel)
496 if (!initialized) return FALSE;
497 if ((channel < 0) || (channel >= num_channels)) return FALSE;
498 alSourceStop(channels[channel].sid);
502 FSOUND_STREAM * F_API OPENAL_Stream_Open(const char *name_or_data, unsigned int mode, int offset, int length)
504 return OPENAL_Sample_Load(FSOUND_FREE, name_or_data, mode, offset, length)
507 signed char F_API OPENAL_Stream_Close(FSOUND_STREAM *stream)
509 OPENAL_Sample_Free(stream);
512 FSOUND_SAMPLE * F_API OPENAL_Stream_GetSample(FSOUND_STREAM *stream)
514 if (!initialized) return NULL;
515 return NULL; // this is wrong, but it works for Lugaru 1.
518 int F_API OPENAL_Stream_PlayEx(int channel, FSOUND_STREAM *stream, FSOUND_DSPUNIT *dsp, signed char startpaused)
520 return OPENAL_PlaySoundEx(channel, stream, dsp, startpaused);
523 signed char F_API OPENAL_Stream_Stop(FSOUND_STREAM *stream)
525 if (!initialized) return FALSE;
526 for (int i = 0; i < num_channels; i++)
528 if (channels[i].sample == stream)
530 alSourceStop(channels[i].sid);
531 channels[i].startpaused = false;
537 signed char F_API OPENAL_Stream_SetMode(FSOUND_STREAM *stream, unsigned int mode)
539 return OPENAL_Sample_SetMode(stream, mode);
542 void F_API OPENAL_Update()
544 if (!initialized) return;
545 alcProcessContext(alcGetCurrentContext());
548 signed char F_API OPENAL_SetOutput(int outputtype)