2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
25 #include "Quaternions.h"
26 #include "openal_wrapper.h"
30 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
31 // one...so we just need to flip the sign on the Z axis when appropriate.
35 OPENAL_SAMPLE *sample;
40 typedef struct OPENAL_SAMPLE {
42 ALuint bid; // buffer id.
47 static size_t num_channels = 0;
48 static OPENAL_Channels *impl_channels = NULL;
49 static bool initialized = false;
50 static float listener_position[3];
52 static void set_channel_position(const int channel, const float x,
53 const float y, const float z)
55 OPENAL_Channels *chan = &impl_channels[channel];
57 chan->position[0] = x;
58 chan->position[1] = y;
59 chan->position[2] = z;
61 OPENAL_SAMPLE *sptr = chan->sample;
65 const ALuint sid = chan->sid;
66 const bool no_attenuate = sptr->is2d;
69 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
70 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
72 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
73 alSource3f(sid, AL_POSITION, x, y, z);
78 AL_API void OPENAL_3D_Listener_SetAttributes(const float *pos, const float *vel, float fx, float fy, float fz, float tx, float ty, float tz)
83 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
84 listener_position[0] = pos[0];
85 listener_position[1] = pos[1];
86 listener_position[2] = -pos[2];
89 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
90 alListenerfv(AL_ORIENTATION, vec);
92 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
94 // adjust existing positions...
95 for (int i = 0; i < num_channels; i++) {
96 const float *p = impl_channels[i].position;
97 set_channel_position(i, p[0], p[1], p[2]);
101 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
105 if ((channel < 0) || (channel >= num_channels))
109 set_channel_position(channel, pos[0], pos[1], -pos[2]);
111 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
116 AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ &pos, const float *vel)
120 if ((channel < 0) || (channel >= num_channels))
123 set_channel_position(channel, pos.x, pos.y, -pos.z);
128 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
132 if (maxsoftwarechannels == 0)
135 if (flags != 0) // unsupported.
138 ALCdevice *dev = alcOpenDevice(NULL);
142 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
143 ALCcontext *ctx = alcCreateContext(dev, caps);
149 alcMakeContextCurrent(ctx);
150 alcProcessContext(ctx);
152 bool cmdline(const char * cmd);
153 if (cmdline("openalinfo")) {
154 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
155 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
156 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
157 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
160 num_channels = maxsoftwarechannels;
161 impl_channels = new OPENAL_Channels[maxsoftwarechannels];
162 memset(impl_channels, '\0', sizeof (OPENAL_Channels) * num_channels);
163 for (int i = 0; i < num_channels; i++)
164 alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail!
170 AL_API void OPENAL_Close()
175 ALCcontext *ctx = alcGetCurrentContext();
177 for (int i = 0; i < num_channels; i++) {
178 alSourceStop(impl_channels[i].sid);
179 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
180 alDeleteSources(1, &impl_channels[i].sid);
182 ALCdevice *dev = alcGetContextsDevice(ctx);
183 alcMakeContextCurrent(NULL);
184 alcSuspendContext(ctx);
185 alcDestroyContext(ctx);
190 delete[] impl_channels;
191 impl_channels = NULL;
196 static OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
200 if ((channel < 0) || (channel >= num_channels))
202 return impl_channels[channel].sample;
205 static signed char OPENAL_GetPaused(int channel)
209 if ((channel < 0) || (channel >= num_channels))
211 if (impl_channels[channel].startpaused)
215 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
216 return((state == AL_PAUSED) ? true : false);
219 static unsigned int OPENAL_GetLoopMode(int channel)
223 if ((channel < 0) || (channel >= num_channels))
226 alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
228 return(OPENAL_LOOP_NORMAL);
229 return OPENAL_LOOP_OFF;
232 static signed char OPENAL_IsPlaying(int channel)
236 if ((channel < 0) || (channel >= num_channels))
239 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
240 return((state == AL_PLAYING) ? true : false);
243 static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
251 if (channel == OPENAL_FREE) {
252 for (int i = 0; i < num_channels; i++) {
254 alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
255 if ((state != AL_PLAYING) && (state != AL_PAUSED)) {
262 if ((channel < 0) || (channel >= num_channels))
264 alSourceStop(impl_channels[channel].sid);
265 impl_channels[channel].sample = sptr;
266 alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
267 alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
268 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
270 impl_channels[channel].startpaused = ((startpaused) ? true : false);
272 alSourcePlay(impl_channels[channel].sid);
277 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
280 const int bigendian = 1;
282 const int bigendian = 0;
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, '.');
291 strcat(fname, ".ogg");
295 FILE *io = fopen(fname, "rb");
299 ALubyte *retval = NULL;
301 #if 0 // untested, so disable this!
302 // Can we just feed it to the AL compressed?
303 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) {
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)) {
337 size_t allocated = 64 * 1024;
338 retval = (ALubyte *) malloc(allocated);
339 while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 ) {
342 if (size >= allocated) {
344 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
352 memcpy(retval + (size - rc), buf, rc);
364 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
368 if (index != OPENAL_FREE)
369 return NULL; // this is all the game does...
371 return NULL; // this is all the game does...
373 return NULL; // this is all the game does...
374 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D))
375 return NULL; // this is all the game does...
377 OPENAL_SAMPLE *retval = NULL;
378 ALenum format = AL_NONE;
380 ALuint frequency = 0;
381 void *data = decode_to_pcm(name_or_data, format, size, frequency);
387 alGenBuffers(1, &bid);
388 if (alGetError() == AL_NO_ERROR) {
389 alBufferData(bid, format, data, size, frequency);
390 retval = new OPENAL_SAMPLE;
392 retval->mode = OPENAL_LOOP_OFF;
393 retval->is2d = (mode == OPENAL_2D);
394 retval->name = new char[strlen(name_or_data) + 1];
396 strcpy(retval->name, name_or_data);
403 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
408 for (int i = 0; i < num_channels; i++) {
409 if (impl_channels[i].sample == sptr) {
410 alSourceStop(impl_channels[i].sid);
411 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
412 impl_channels[i].sample = NULL;
415 alDeleteBuffers(1, &sptr->bid);
421 static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
425 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF))
433 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
437 if (channel == OPENAL_ALL) {
438 for (int i = 0; i < num_channels; i++)
439 OPENAL_SetFrequency(i, freq);
443 if ((channel < 0) || (channel >= num_channels))
447 alSourcef(impl_channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
449 alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
453 AL_API signed char OPENAL_SetVolume(int channel, int vol)
458 if (channel == OPENAL_ALL) {
459 for (int i = 0; i < num_channels; i++)
460 OPENAL_SetVolume(i, vol);
464 if ((channel < 0) || (channel >= num_channels))
471 ALfloat gain = ((ALfloat) vol) / 255.0f;
472 alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
476 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
481 if (channel == OPENAL_ALL) {
482 for (int i = 0; i < num_channels; i++)
483 OPENAL_SetPaused(i, paused);
487 if ((channel < 0) || (channel >= num_channels))
491 if (impl_channels[channel].startpaused)
494 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
496 if ((paused) && (state == AL_PLAYING))
497 alSourcePause(impl_channels[channel].sid);
498 else if ((!paused) && (state == AL_PAUSED)) {
499 alSourcePlay(impl_channels[channel].sid);
500 impl_channels[channel].startpaused = false;
505 AL_API void OPENAL_SetSFXMasterVolume(int volume)
509 ALfloat gain = ((ALfloat) volume) / 255.0f;
510 alListenerf(AL_GAIN, gain);
513 AL_API signed char OPENAL_StopSound(int channel)
518 if (channel == OPENAL_ALL) {
519 for (int i = 0; i < num_channels; i++)
524 if ((channel < 0) || (channel >= num_channels))
526 alSourceStop(impl_channels[channel].sid);
527 impl_channels[channel].startpaused = false;
531 static OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
535 return (OPENAL_SAMPLE *) stream;
538 static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
540 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
543 static signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
547 for (int i = 0; i < num_channels; i++) {
548 if (impl_channels[i].sample == (OPENAL_SAMPLE *) stream) {
549 alSourceStop(impl_channels[i].sid);
550 impl_channels[i].startpaused = false;
556 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
558 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
561 AL_API void OPENAL_Update()
565 alcProcessContext(alcGetCurrentContext());
568 AL_API signed char OPENAL_SetOutput(int outputtype)
573 extern int channels[];
575 extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
577 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
578 if (currSample && currSample == samp[chan]) {
579 if (OPENAL_GetPaused(channels[chan])) {
580 OPENAL_StopSound(channels[chan]);
581 channels[chan] = OPENAL_FREE;
582 } else if (OPENAL_IsPlaying(channels[chan])) {
583 int loop_mode = OPENAL_GetLoopMode(channels[chan]);
584 if (loop_mode & OPENAL_LOOP_OFF) {
585 channels[chan] = OPENAL_FREE;
589 channels[chan] = OPENAL_FREE;
592 channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
593 if (channels[chan] < 0) {
594 channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
598 extern "C" void PlayStreamEx(int chan, OPENAL_STREAM *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
600 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
601 if (currSample && currSample == OPENAL_Stream_GetSample(sptr)) {
602 OPENAL_StopSound(channels[chan]);
603 OPENAL_Stream_Stop(sptr);
605 OPENAL_Stream_Stop(sptr);
606 channels[chan] = OPENAL_FREE;
609 channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
610 if (channels[chan] < 0) {
611 channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);