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)
301 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
302 char *fname = (char *) alloca(strlen(_fname) + 16);
303 strcpy(fname, _fname);
304 char *ptr = strchr(fname, '.');
305 if (ptr) *ptr = NULL;
306 strcat(fname, ".ogg");
310 FILE *io = fopen(fname, "rb");
314 ALubyte *retval = NULL;
316 #if 0 // untested, so disable this!
317 // Can we just feed it to the AL compressed?
318 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
320 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
322 fseek(io, 0, SEEK_END);
324 fseek(io, 0, SEEK_SET);
325 retval = (ALubyte *) malloc(size);
326 size_t rc = fread(retval, size, 1, io);
337 // Uncompress and feed to the AL.
339 memset(&vf, '\0', sizeof (vf));
340 if (ov_open(io, &vf, NULL, 0) == 0)
343 vorbis_info *info = ov_info(&vf, -1);
345 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
348 if ((info->channels != 1) && (info->channels != 2))
356 size_t allocated = 64 * 1024;
357 retval = (ALubyte *) malloc(allocated);
358 while ( (rc = ov_read(&vf, buf, sizeof (buf), 0, 2, 1, &bitstream)) != 0 )
363 if (size >= allocated)
366 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
375 memcpy(retval + (size - rc), buf, rc);
387 FSOUND_SAMPLE * F_API OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
389 if (!initialized) return NULL;
390 if (index != FSOUND_FREE) return NULL; // this is all the game does...
391 if (offset != 0) return NULL; // this is all the game does...
392 if (length != 0) return NULL; // this is all the game does...
393 if ((mode != FSOUND_HW3D) && (mode != FSOUND_2D)) return NULL; // this is all the game does...
395 FSOUND_SAMPLE *retval = NULL;
396 ALuint bufferName = 0;
397 ALenum format = AL_NONE;
399 ALuint frequency = 0;
400 void *data = decode_to_pcm(name_or_data, format, size, frequency);
406 alGenBuffers(1, &bid);
407 if (alGetError() == AL_NO_ERROR)
409 alBufferData(bid, format, data, size, frequency);
410 retval = new FSOUND_SAMPLE;
412 retval->mode = FSOUND_LOOP_OFF;
413 retval->is2d = (mode == FSOUND_2D);
414 retval->name = new char[strlen(name_or_data) + 1];
416 strcpy(retval->name, name_or_data);
423 void F_API OPENAL_Sample_Free(FSOUND_SAMPLE *sptr)
425 if (!initialized) return;
428 for (int i = 0; i < num_channels; i++)
430 if (channels[i].sample == sptr)
432 alSourceStop(channels[i].sid);
433 alSourcei(channels[i].sid, AL_BUFFER, 0);
434 channels[i].sample = NULL;
437 alDeleteBuffers(1, &sptr->bid);
443 signed char F_API OPENAL_Sample_SetMode(FSOUND_SAMPLE *sptr, unsigned int mode)
445 if (!initialized) return FALSE;
446 if ((mode != FSOUND_LOOP_NORMAL) && (mode != FSOUND_LOOP_OFF)) return FALSE;
447 if (!sptr) return FALSE;
452 signed char F_API OPENAL_Sample_SetMinMaxDistance(FSOUND_SAMPLE *sptr, float min, float max)
454 if (!initialized) return FALSE;
455 if (sptr == NULL) return FALSE;
456 // !!! FIXME: write me
460 signed char F_API OPENAL_SetFrequency(int channel, int freq)
462 if (!initialized) return FALSE;
463 if (channel == FSOUND_ALL)
465 for (int i = 0; i < num_channels; i++)
466 OPENAL_SetFrequency(i, freq);
470 if ((channel < 0) || (channel >= num_channels)) return FALSE;
471 if (freq == 8012) // hack
472 alSourcef(channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
474 alSourcef(channels[channel].sid, AL_PITCH, 1.0f);
478 signed char F_API OPENAL_SetVolume(int channel, int vol)
480 if (!initialized) return FALSE;
482 if (channel == FSOUND_ALL)
484 for (int i = 0; i < num_channels; i++)
485 OPENAL_SetVolume(i, vol);
489 if ((channel < 0) || (channel >= num_channels)) return FALSE;
491 if (vol < 0) vol = 0;
492 else if (vol > 255) vol = 255;
493 ALfloat gain = ((ALfloat) vol) / 255.0f;
494 alSourcef(channels[channel].sid, AL_GAIN, gain);
498 signed char F_API OPENAL_SetPaused(int channel, signed char paused)
500 if (!initialized) return FALSE;
502 if (channel == FSOUND_ALL)
504 for (int i = 0; i < num_channels; i++)
505 OPENAL_SetPaused(i, paused);
509 if ((channel < 0) || (channel >= num_channels)) return FALSE;
512 if (channels[channel].startpaused)
515 alGetSourceiv(channels[channel].sid, AL_SOURCE_STATE, &state);
517 if ((paused) && (state == AL_PLAYING))
518 alSourcePause(channels[channel].sid);
519 else if ((!paused) && (state == AL_PAUSED))
521 alSourcePlay(channels[channel].sid);
522 channels[channel].startpaused = false;
527 void F_API OPENAL_SetSFXMasterVolume(int volume)
529 if (!initialized) return;
530 ALfloat gain = ((ALfloat) volume) / 255.0f;
531 alListenerf(AL_GAIN, gain);
534 signed char F_API OPENAL_StopSound(int channel)
536 if (!initialized) return FALSE;
538 if (channel == FSOUND_ALL)
540 for (int i = 0; i < num_channels; i++)
545 if ((channel < 0) || (channel >= num_channels)) return FALSE;
546 alSourceStop(channels[channel].sid);
547 channels[channel].startpaused = false;
551 FSOUND_STREAM * F_API OPENAL_Stream_Open(const char *name_or_data, unsigned int mode, int offset, int length)
553 return (FSOUND_STREAM *) OPENAL_Sample_Load(FSOUND_FREE, name_or_data, mode, offset, length);
556 signed char F_API OPENAL_Stream_Close(FSOUND_STREAM *stream)
558 OPENAL_Sample_Free((FSOUND_SAMPLE *) stream);
561 FSOUND_SAMPLE * F_API OPENAL_Stream_GetSample(FSOUND_STREAM *stream)
563 if (!initialized) return NULL;
564 return (FSOUND_SAMPLE *) stream;
567 int F_API OPENAL_Stream_PlayEx(int channel, FSOUND_STREAM *stream, FSOUND_DSPUNIT *dsp, signed char startpaused)
569 return OPENAL_PlaySoundEx(channel, (FSOUND_SAMPLE *) stream, dsp, startpaused);
572 signed char F_API OPENAL_Stream_Stop(FSOUND_STREAM *stream)
574 if (!initialized) return FALSE;
575 for (int i = 0; i < num_channels; i++)
577 if (channels[i].sample == (FSOUND_SAMPLE *) stream)
579 alSourceStop(channels[i].sid);
580 channels[i].startpaused = false;
586 signed char F_API OPENAL_Stream_SetMode(FSOUND_STREAM *stream, unsigned int mode)
588 return OPENAL_Sample_SetMode((FSOUND_SAMPLE *) stream, mode);
591 void F_API OPENAL_Update()
593 if (!initialized) return;
594 alcProcessContext(alcGetCurrentContext());
597 signed char F_API OPENAL_SetOutput(int outputtype)