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/XYZ.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.
40 OPENAL_SAMPLE* sample;
45 typedef struct OPENAL_SAMPLE
48 ALuint bid; // buffer id.
53 static size_t num_channels = 0;
54 static OPENAL_Channels* impl_channels = NULL;
55 static bool initialized = false;
56 static float listener_position[3];
58 static void set_channel_position(const int channel, const float x,
59 const float y, const float z)
61 OPENAL_Channels* chan = &impl_channels[channel];
63 chan->position[0] = x;
64 chan->position[1] = y;
65 chan->position[2] = z;
67 OPENAL_SAMPLE* sptr = chan->sample;
72 const ALuint sid = chan->sid;
73 const bool no_attenuate = sptr->is2d;
76 alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
77 alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
79 alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
80 alSource3f(sid, AL_POSITION, x, y, z);
84 AL_API void OPENAL_3D_Listener_SetAttributes(const float* pos, const float*, float fx, float fy, float fz, float tx, float ty, float tz)
90 alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
91 listener_position[0] = pos[0];
92 listener_position[1] = pos[1];
93 listener_position[2] = -pos[2];
96 ALfloat vec[6] = { fx, fy, -fz, tx, ty, -tz };
97 alListenerfv(AL_ORIENTATION, vec);
99 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
101 // adjust existing positions...
102 for (unsigned i = 0; i < num_channels; i++) {
103 const float* p = impl_channels[i].position;
104 set_channel_position(i, p[0], p[1], p[2]);
108 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float* pos)
113 if ((channel < 0) || (channel >= (int)num_channels)) {
118 set_channel_position(channel, pos[0], pos[1], -pos[2]);
121 // we ignore velocity, since doppler's broken in the Linux AL at the moment...
126 AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ& pos)
131 if ((channel < 0) || (channel >= (int)num_channels)) {
135 set_channel_position(channel, pos.x, pos.y, -pos.z);
140 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
145 if (maxsoftwarechannels == 0) {
149 if (flags != 0) { // unsupported.
153 ALCdevice* dev = alcOpenDevice(NULL);
158 ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
159 ALCcontext* ctx = alcCreateContext(dev, caps);
165 alcMakeContextCurrent(ctx);
166 alcProcessContext(ctx);
168 if (commandLineOptions[OPENALINFO]) {
169 printf("AL_VENDOR: %s\n", (char*)alGetString(AL_VENDOR));
170 printf("AL_RENDERER: %s\n", (char*)alGetString(AL_RENDERER));
171 printf("AL_VERSION: %s\n", (char*)alGetString(AL_VERSION));
172 printf("AL_EXTENSIONS: %s\n", (char*)alGetString(AL_EXTENSIONS));
175 num_channels = maxsoftwarechannels;
176 impl_channels = new OPENAL_Channels[maxsoftwarechannels];
177 memset(impl_channels, '\0', sizeof(OPENAL_Channels) * num_channels);
178 for (unsigned i = 0; i < num_channels; i++) {
179 alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail!
186 AL_API void OPENAL_Close()
192 ALCcontext* ctx = alcGetCurrentContext();
194 for (unsigned i = 0; i < num_channels; i++) {
195 alSourceStop(impl_channels[i].sid);
196 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
197 alDeleteSources(1, &impl_channels[i].sid);
199 ALCdevice* dev = alcGetContextsDevice(ctx);
200 alcMakeContextCurrent(NULL);
201 alcSuspendContext(ctx);
202 alcDestroyContext(ctx);
207 delete[] impl_channels;
208 impl_channels = NULL;
213 static OPENAL_SAMPLE* OPENAL_GetCurrentSample(int channel)
218 if ((channel < 0) || (channel >= (int)num_channels)) {
221 return impl_channels[channel].sample;
224 static signed char OPENAL_GetPaused(int channel)
229 if ((channel < 0) || (channel >= (int)num_channels)) {
232 if (impl_channels[channel].startpaused) {
237 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
238 return ((state == AL_PAUSED) ? true : false);
241 static unsigned int OPENAL_GetLoopMode(int channel)
246 if ((channel < 0) || (channel >= (int)num_channels)) {
250 alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
252 return (OPENAL_LOOP_NORMAL);
254 return OPENAL_LOOP_OFF;
257 static signed char OPENAL_IsPlaying(int channel)
262 if ((channel < 0) || (channel >= (int)num_channels)) {
266 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
267 return ((state == AL_PLAYING) ? true : false);
270 static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE* sptr, OPENAL_DSPUNIT* dsp, signed char startpaused)
281 if (channel == OPENAL_FREE) {
282 for (unsigned i = 0; i < num_channels; i++) {
284 alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
285 if ((state != AL_PLAYING) && (state != AL_PAUSED)) {
292 if ((channel < 0) || (channel >= (int)num_channels)) {
295 alSourceStop(impl_channels[channel].sid);
296 impl_channels[channel].sample = sptr;
297 alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
298 alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
299 set_channel_position(channel, 0.0f, 0.0f, 0.0f);
301 impl_channels[channel].startpaused = ((startpaused) ? true : false);
303 alSourcePlay(impl_channels[channel].sid);
308 static void* decode_to_pcm(const char* _fname, ALenum& format, ALsizei& size, ALuint& freq)
311 const int bigendian = 1;
313 const int bigendian = 0;
316 // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
317 char* fname = (char*)alloca(strlen(_fname) + 16);
318 strcpy(fname, _fname);
319 char* ptr = strchr(fname, '.');
323 strcat(fname, ".ogg");
326 FILE* io = fopen(fname, "rb");
331 ALubyte* retval = NULL;
333 #if 0 // untested, so disable this!
334 // Can we just feed it to the AL compressed?
335 if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis")) {
336 format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
338 fseek(io, 0, SEEK_END);
340 fseek(io, 0, SEEK_SET);
341 retval = (ALubyte *) malloc(size);
342 size_t rc = fread(retval, size, 1, io);
352 // Uncompress and feed to the AL.
354 memset(&vf, '\0', sizeof(vf));
355 if (ov_open(io, &vf, NULL, 0) == 0) {
357 vorbis_info* info = ov_info(&vf, -1);
359 format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
362 if ((info->channels != 1) && (info->channels != 2)) {
369 size_t allocated = 64 * 1024;
370 retval = (ALubyte*)malloc(allocated);
371 while ((rc = ov_read(&vf, buf, sizeof(buf), bigendian, 2, 1, &bitstream)) != 0) {
374 if (size >= (int)allocated) {
376 ALubyte* tmp = (ALubyte*)realloc(retval, allocated);
384 memcpy(retval + (size - rc), buf, rc);
395 AL_API OPENAL_SAMPLE* OPENAL_Sample_Load(int index, const char* name_or_data, unsigned int mode, int offset, int length)
400 if (index != OPENAL_FREE) {
401 return NULL; // this is all the game does...
404 return NULL; // this is all the game does...
407 return NULL; // this is all the game does...
409 if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) {
410 return NULL; // this is all the game does...
413 OPENAL_SAMPLE* retval = NULL;
414 ALenum format = AL_NONE;
416 ALuint frequency = 0;
417 void* data = decode_to_pcm(name_or_data, format, size, frequency);
424 alGenBuffers(1, &bid);
425 if (alGetError() == AL_NO_ERROR) {
426 alBufferData(bid, format, data, size, frequency);
427 retval = new OPENAL_SAMPLE;
429 retval->mode = OPENAL_LOOP_OFF;
430 retval->is2d = (mode == OPENAL_2D);
431 retval->name = new char[strlen(name_or_data) + 1];
433 strcpy(retval->name, name_or_data);
441 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE* sptr)
447 for (unsigned i = 0; i < num_channels; i++) {
448 if (impl_channels[i].sample == sptr) {
449 alSourceStop(impl_channels[i].sid);
450 alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
451 impl_channels[i].sample = NULL;
454 alDeleteBuffers(1, &sptr->bid);
460 static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE* sptr, unsigned int mode)
465 if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF)) {
475 AL_API signed char OPENAL_SetFrequency(int channel, bool slomo)
480 if (channel == OPENAL_ALL) {
481 for (unsigned i = 0; i < num_channels; i++) {
482 OPENAL_SetFrequency(i, slomo);
487 if ((channel < 0) || (channel >= (int)num_channels)) {
491 alSourcef(impl_channels[channel].sid, AL_PITCH, ((ALfloat)slomofreq) / 44100.0f);
493 alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
498 AL_API signed char OPENAL_SetVolume(int channel, int vol)
504 if (channel == OPENAL_ALL) {
505 for (unsigned i = 0; i < num_channels; i++) {
506 OPENAL_SetVolume(i, vol);
511 if ((channel < 0) || (channel >= (int)num_channels)) {
517 } else if (vol > 255) {
520 ALfloat gain = ((ALfloat)vol) / 255.0f;
521 alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
525 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
531 if (channel == OPENAL_ALL) {
532 for (unsigned i = 0; i < num_channels; i++) {
533 OPENAL_SetPaused(i, paused);
538 if ((channel < 0) || (channel >= (int)num_channels)) {
543 if (impl_channels[channel].startpaused) {
546 alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
549 if ((paused) && (state == AL_PLAYING)) {
550 alSourcePause(impl_channels[channel].sid);
551 } else if ((!paused) && (state == AL_PAUSED)) {
552 alSourcePlay(impl_channels[channel].sid);
553 impl_channels[channel].startpaused = false;
558 AL_API void OPENAL_SetSFXMasterVolume(int volume)
563 ALfloat gain = ((ALfloat)volume) / 255.0f;
564 alListenerf(AL_GAIN, gain);
567 AL_API signed char OPENAL_StopSound(int channel)
573 if (channel == OPENAL_ALL) {
574 for (unsigned i = 0; i < num_channels; i++) {
580 if ((channel < 0) || (channel >= (int)num_channels)) {
583 alSourceStop(impl_channels[channel].sid);
584 impl_channels[channel].startpaused = false;
588 static OPENAL_SAMPLE* OPENAL_Stream_GetSample(OPENAL_STREAM* stream)
593 return (OPENAL_SAMPLE*)stream;
596 static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM* stream, OPENAL_DSPUNIT* dsp, signed char startpaused)
598 return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE*)stream, dsp, startpaused);
601 static signed char OPENAL_Stream_Stop(OPENAL_STREAM* stream)
606 for (unsigned i = 0; i < num_channels; i++) {
607 if (impl_channels[i].sample == (OPENAL_SAMPLE*)stream) {
608 alSourceStop(impl_channels[i].sid);
609 impl_channels[i].startpaused = false;
615 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM* stream, unsigned int mode)
617 return OPENAL_Sample_SetMode((OPENAL_SAMPLE*)stream, mode);
620 AL_API void OPENAL_Update()
625 alcProcessContext(alcGetCurrentContext());
628 extern int channels[];
630 extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE* sptr, OPENAL_DSPUNIT* dsp, signed char startpaused)
632 const OPENAL_SAMPLE* currSample = OPENAL_GetCurrentSample(channels[chan]);
633 if (currSample && currSample == samp[chan]) {
634 if (OPENAL_GetPaused(channels[chan])) {
635 OPENAL_StopSound(channels[chan]);
636 channels[chan] = OPENAL_FREE;
637 } else if (OPENAL_IsPlaying(channels[chan])) {
638 int loop_mode = OPENAL_GetLoopMode(channels[chan]);
639 if (loop_mode & OPENAL_LOOP_OFF) {
640 channels[chan] = OPENAL_FREE;
644 channels[chan] = OPENAL_FREE;
647 channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
648 if (channels[chan] < 0) {
649 channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
653 extern "C" void PlayStreamEx(int chan, OPENAL_STREAM* sptr, OPENAL_DSPUNIT* dsp, signed char startpaused)
655 const OPENAL_SAMPLE* currSample = OPENAL_GetCurrentSample(channels[chan]);
656 if (currSample && currSample == OPENAL_Stream_GetSample(sptr)) {
657 OPENAL_StopSound(channels[chan]);
658 OPENAL_Stream_Stop(sptr);
660 OPENAL_Stream_Stop(sptr);
661 channels[chan] = OPENAL_FREE;
664 channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
665 if (channels[chan] < 0) {
666 channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);