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/>.
21 #include "Audio/openal_wrapper.hpp"
23 #include "Audio/Sounds.hpp"
25 #include "Math/Quaternions.hpp"
31 extern float slomofreq;
34 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
35 // one...so we just need to flip the sign on the Z axis when appropriate.
39 OPENAL_SAMPLE *sample;
44 typedef struct OPENAL_SAMPLE {
46 ALuint bid; // buffer id.
51 static size_t num_channels = 0;
52 static OPENAL_Channels *impl_channels = NULL;
53 static bool initialized = false;
54 static float listener_position[3];
56 static void set_channel_position(const int channel, const float x,
57 const float y, const float z)
59 OPENAL_Channels *chan = &impl_channels[channel];
61 chan->position[0] = x;
62 chan->position[1] = y;
63 chan->position[2] = z;
65 OPENAL_SAMPLE *sptr = chan->sample;
69 const ALuint sid = chan->sid;
70 const bool no_attenuate = sptr->is2d;
73 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
74 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
76 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
77 alSource3f(sid, AL_POSITION, x, y, z);
82 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)
87 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
88 listener_position[0] = pos[0];
89 listener_position[1] = pos[1];
90 listener_position[2] = -pos[2];
93 ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
94 alListenerfv(AL_ORIENTATION, vec);
96 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
98 // adjust existing positions...
99 for (int i = 0; i < num_channels; i++) {
100 const float *p = impl_channels[i].position;
101 set_channel_position(i, p[0], p[1], p[2]);
105 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
109 if ((channel < 0) || (channel >= num_channels))
113 set_channel_position(channel, pos[0], pos[1], -pos[2]);
115 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
120 AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ &pos, const float *vel)
124 if ((channel < 0) || (channel >= num_channels))
127 set_channel_position(channel, pos.x, pos.y, -pos.z);
132 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
136 if (maxsoftwarechannels == 0)
139 if (flags != 0) // unsupported.
142 ALCdevice *dev = alcOpenDevice(NULL);
146 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
147 ALCcontext *ctx = alcCreateContext(dev, caps);
153 alcMakeContextCurrent(ctx);
154 alcProcessContext(ctx);
156 if (commandLineOptions[OPENALINFO]) {
157 printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
158 printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
159 printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
160 printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
163 num_channels = maxsoftwarechannels;
164 impl_channels = new OPENAL_Channels[maxsoftwarechannels];
165 memset(impl_channels, '\0', sizeof (OPENAL_Channels) * num_channels);
166 for (int i = 0; i < num_channels; i++)
167 alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail!
173 AL_API void OPENAL_Close()
178 ALCcontext *ctx = alcGetCurrentContext();
180 for (int i = 0; i < num_channels; i++) {
181 alSourceStop(impl_channels[i].sid);
182 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
183 alDeleteSources(1, &impl_channels[i].sid);
185 ALCdevice *dev = alcGetContextsDevice(ctx);
186 alcMakeContextCurrent(NULL);
187 alcSuspendContext(ctx);
188 alcDestroyContext(ctx);
193 delete[] impl_channels;
194 impl_channels = NULL;
199 static OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
203 if ((channel < 0) || (channel >= num_channels))
205 return impl_channels[channel].sample;
208 static signed char OPENAL_GetPaused(int channel)
212 if ((channel < 0) || (channel >= num_channels))
214 if (impl_channels[channel].startpaused)
218 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
219 return((state == AL_PAUSED) ? true : false);
222 static unsigned int OPENAL_GetLoopMode(int channel)
226 if ((channel < 0) || (channel >= num_channels))
229 alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
231 return(OPENAL_LOOP_NORMAL);
232 return OPENAL_LOOP_OFF;
235 static signed char OPENAL_IsPlaying(int channel)
239 if ((channel < 0) || (channel >= num_channels))
242 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
243 return((state == AL_PLAYING) ? true : false);
246 static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
254 if (channel == OPENAL_FREE) {
255 for (int i = 0; i < num_channels; i++) {
257 alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
258 if ((state != AL_PLAYING) && (state != AL_PAUSED)) {
265 if ((channel < 0) || (channel >= num_channels))
267 alSourceStop(impl_channels[channel].sid);
268 impl_channels[channel].sample = sptr;
269 alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
270 alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
271 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
273 impl_channels[channel].startpaused = ((startpaused) ? true : false);
275 alSourcePlay(impl_channels[channel].sid);
280 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
283 const int bigendian = 1;
285 const int bigendian = 0;
288 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
289 char *fname = (char *) alloca(strlen(_fname) + 16);
290 strcpy(fname, _fname);
291 char *ptr = strchr(fname, '.');
294 strcat(fname, ".ogg");
297 FILE *io = fopen(fname, "rb");
301 ALubyte *retval = NULL;
303 #if 0 // untested, so disable this!
304 // Can we just feed it to the AL compressed?
305 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis")) {
306 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
308 fseek(io, 0, SEEK_END);
310 fseek(io, 0, SEEK_SET);
311 retval = (ALubyte *) malloc(size);
312 size_t rc = fread(retval, size, 1, io);
322 // Uncompress and feed to the AL.
324 memset(&vf, '\0', sizeof (vf));
325 if (ov_open(io, &vf, NULL, 0) == 0) {
327 vorbis_info *info = ov_info(&vf, -1);
329 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
332 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), bigendian, 2, 1, &bitstream)) != 0 ) {
344 if (size >= allocated) {
346 ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
354 memcpy(retval + (size - rc), buf, rc);
366 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
370 if (index != OPENAL_FREE)
371 return NULL; // this is all the game does...
373 return NULL; // this is all the game does...
375 return NULL; // this is all the game does...
376 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D))
377 return NULL; // this is all the game does...
379 OPENAL_SAMPLE *retval = NULL;
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) {
391 alBufferData(bid, format, data, size, frequency);
392 retval = new OPENAL_SAMPLE;
394 retval->mode = OPENAL_LOOP_OFF;
395 retval->is2d = (mode == OPENAL_2D);
396 retval->name = new char[strlen(name_or_data) + 1];
398 strcpy(retval->name, name_or_data);
405 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
410 for (int i = 0; i < num_channels; i++) {
411 if (impl_channels[i].sample == sptr) {
412 alSourceStop(impl_channels[i].sid);
413 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
414 impl_channels[i].sample = NULL;
417 alDeleteBuffers(1, &sptr->bid);
423 static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
427 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF))
435 AL_API signed char OPENAL_SetFrequency(int channel, bool slomo)
439 if (channel == OPENAL_ALL) {
440 for (int i = 0; i < num_channels; i++)
441 OPENAL_SetFrequency(i, slomo);
445 if ((channel < 0) || (channel >= num_channels))
448 alSourcef(impl_channels[channel].sid, AL_PITCH, ((ALfloat) slomofreq) / 44100.0f);
450 alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
454 AL_API signed char OPENAL_SetVolume(int channel, int vol)
459 if (channel == OPENAL_ALL) {
460 for (int i = 0; i < num_channels; i++)
461 OPENAL_SetVolume(i, vol);
465 if ((channel < 0) || (channel >= num_channels))
472 ALfloat gain = ((ALfloat) vol) / 255.0f;
473 alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
477 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
482 if (channel == OPENAL_ALL) {
483 for (int i = 0; i < num_channels; i++)
484 OPENAL_SetPaused(i, paused);
488 if ((channel < 0) || (channel >= num_channels))
492 if (impl_channels[channel].startpaused)
495 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
497 if ((paused) && (state == AL_PLAYING))
498 alSourcePause(impl_channels[channel].sid);
499 else if ((!paused) && (state == AL_PAUSED)) {
500 alSourcePlay(impl_channels[channel].sid);
501 impl_channels[channel].startpaused = false;
506 AL_API void OPENAL_SetSFXMasterVolume(int volume)
510 ALfloat gain = ((ALfloat) volume) / 255.0f;
511 alListenerf(AL_GAIN, gain);
514 AL_API signed char OPENAL_StopSound(int channel)
519 if (channel == OPENAL_ALL) {
520 for (int i = 0; i < num_channels; i++)
525 if ((channel < 0) || (channel >= num_channels))
527 alSourceStop(impl_channels[channel].sid);
528 impl_channels[channel].startpaused = false;
532 static OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
536 return (OPENAL_SAMPLE *) stream;
539 static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
541 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
544 static signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
548 for (int i = 0; i < num_channels; i++) {
549 if (impl_channels[i].sample == (OPENAL_SAMPLE *) stream) {
550 alSourceStop(impl_channels[i].sid);
551 impl_channels[i].startpaused = false;
557 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
559 return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
562 AL_API void OPENAL_Update()
566 alcProcessContext(alcGetCurrentContext());
569 AL_API signed char OPENAL_SetOutput(int outputtype)
574 extern int channels[];
576 extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
578 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
579 if (currSample && currSample == samp[chan]) {
580 if (OPENAL_GetPaused(channels[chan])) {
581 OPENAL_StopSound(channels[chan]);
582 channels[chan] = OPENAL_FREE;
583 } else if (OPENAL_IsPlaying(channels[chan])) {
584 int loop_mode = OPENAL_GetLoopMode(channels[chan]);
585 if (loop_mode & OPENAL_LOOP_OFF) {
586 channels[chan] = OPENAL_FREE;
590 channels[chan] = OPENAL_FREE;
593 channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
594 if (channels[chan] < 0) {
595 channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
599 extern "C" void PlayStreamEx(int chan, OPENAL_STREAM *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
601 const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
602 if (currSample && currSample == OPENAL_Stream_GetSample(sptr)) {
603 OPENAL_StopSound(channels[chan]);
604 OPENAL_Stream_Stop(sptr);
606 OPENAL_Stream_Stop(sptr);
607 channels[chan] = OPENAL_FREE;
610 channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
611 if (channels[chan] < 0) {
612 channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);