]> git.jsancho.org Git - lugaru.git/blob - Source/openal_wrapper.cpp
Removed unused file and code
[lugaru.git] / Source / openal_wrapper.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "Quaternions.h"
25 #include "openal_wrapper.h"
26 #include "Sounds.h"
27
28 // NOTE:
29 // FMOD uses a Left Handed Coordinate system, OpenAL uses a Right Handed
30 //  one...so we just need to flip the sign on the Z axis when appropriate.
31
32 typedef struct {
33     ALuint sid;
34     OPENAL_SAMPLE *sample;
35     bool startpaused;
36     float position[3];
37 } OPENAL_Channels;
38
39 typedef struct OPENAL_SAMPLE {
40     char *name;
41     ALuint bid;  // buffer id.
42     int mode;
43     int is2d;
44 } OPENAL_SAMPLE;
45
46 static size_t num_channels = 0;
47 static OPENAL_Channels *impl_channels = NULL;
48 static bool initialized = false;
49 static float listener_position[3];
50
51 static void set_channel_position(const int channel, const float x,
52                                  const float y, const float z)
53 {
54     OPENAL_Channels *chan = &impl_channels[channel];
55
56     chan->position[0] = x;
57     chan->position[1] = y;
58     chan->position[2] = z;
59
60     OPENAL_SAMPLE *sptr = chan->sample;
61     if (sptr == NULL)
62         return;
63
64     const ALuint sid = chan->sid;
65     const bool no_attenuate = sptr->is2d;
66
67     if (no_attenuate) {
68         alSourcei(sid, AL_SOURCE_RELATIVE, AL_TRUE);
69         alSource3f(sid, AL_POSITION, 0.0f, 0.0f, 0.0f);
70     } else {
71         alSourcei(sid, AL_SOURCE_RELATIVE, AL_FALSE);
72         alSource3f(sid, AL_POSITION, x, y, z);
73     }
74 }
75
76
77 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)
78 {
79     if (!initialized)
80         return;
81     if (pos != NULL) {
82         alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]);
83         listener_position[0] = pos[0];
84         listener_position[1] = pos[1];
85         listener_position[2] = -pos[2];
86     }
87
88     ALfloat vec[6] = { fx, fy, -fz, tz, ty, -tz };
89     alListenerfv(AL_ORIENTATION, vec);
90
91     // we ignore velocity, since doppler's broken in the Linux AL at the moment...
92
93     // adjust existing positions...
94     for (int i = 0; i < num_channels; i++) {
95         const float *p = impl_channels[i].position;
96         set_channel_position(i, p[0], p[1], p[2]);
97     }
98 }
99
100 AL_API signed char OPENAL_3D_SetAttributes(int channel, const float *pos, const float *vel)
101 {
102     if (!initialized)
103         return false;
104     if ((channel < 0) || (channel >= num_channels))
105         return false;
106
107     if (pos != NULL)
108         set_channel_position(channel, pos[0], pos[1], -pos[2]);
109
110     // we ignore velocity, since doppler's broken in the Linux AL at the moment...
111
112     return true;
113 }
114
115 AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ &pos, const float *vel)
116 {
117     if (!initialized)
118         return false;
119     if ((channel < 0) || (channel >= num_channels))
120         return false;
121
122     set_channel_position(channel, pos.x, pos.y, -pos.z);
123
124     return true;
125 }
126
127 AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags)
128 {
129     if (initialized)
130         return false;
131     if (maxsoftwarechannels == 0)
132         return false;
133
134     if (flags != 0)  // unsupported.
135         return false;
136
137     ALCdevice *dev = alcOpenDevice(NULL);
138     if (!dev)
139         return false;
140
141     ALint caps[] = { ALC_FREQUENCY, mixrate, 0 };
142     ALCcontext *ctx = alcCreateContext(dev, caps);
143     if (!ctx) {
144         alcCloseDevice(dev);
145         return false;
146     }
147
148     alcMakeContextCurrent(ctx);
149     alcProcessContext(ctx);
150
151     bool cmdline(const char * cmd);
152     if (cmdline("openalinfo")) {
153         printf("AL_VENDOR: %s\n", (char *) alGetString(AL_VENDOR));
154         printf("AL_RENDERER: %s\n", (char *) alGetString(AL_RENDERER));
155         printf("AL_VERSION: %s\n", (char *) alGetString(AL_VERSION));
156         printf("AL_EXTENSIONS: %s\n", (char *) alGetString(AL_EXTENSIONS));
157     }
158
159     num_channels = maxsoftwarechannels;
160     impl_channels = new OPENAL_Channels[maxsoftwarechannels];
161     memset(impl_channels, '\0', sizeof (OPENAL_Channels) * num_channels);
162     for (int i = 0; i < num_channels; i++)
163         alGenSources(1, &impl_channels[i].sid);  // !!! FIXME: verify this didn't fail!
164
165     initialized = true;
166     return true;
167 }
168
169 AL_API void OPENAL_Close()
170 {
171     if (!initialized)
172         return;
173
174     ALCcontext *ctx = alcGetCurrentContext();
175     if (ctx) {
176         for (int i = 0; i < num_channels; i++) {
177             alSourceStop(impl_channels[i].sid);
178             alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
179             alDeleteSources(1, &impl_channels[i].sid);
180         }
181         ALCdevice *dev = alcGetContextsDevice(ctx);
182         alcMakeContextCurrent(NULL);
183         alcSuspendContext(ctx);
184         alcDestroyContext(ctx);
185         alcCloseDevice(dev);
186     }
187
188     num_channels = 0;
189     delete[] impl_channels;
190     impl_channels = NULL;
191
192     initialized = false;
193 }
194
195 static OPENAL_SAMPLE *OPENAL_GetCurrentSample(int channel)
196 {
197     if (!initialized)
198         return NULL;
199     if ((channel < 0) || (channel >= num_channels))
200         return NULL;
201     return impl_channels[channel].sample;
202 }
203
204 static signed char OPENAL_GetPaused(int channel)
205 {
206     if (!initialized)
207         return false;
208     if ((channel < 0) || (channel >= num_channels))
209         return false;
210     if (impl_channels[channel].startpaused)
211         return(true);
212
213     ALint state = 0;
214     alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
215     return((state == AL_PAUSED) ? true : false);
216 }
217
218 static unsigned int OPENAL_GetLoopMode(int channel)
219 {
220     if (!initialized)
221         return 0;
222     if ((channel < 0) || (channel >= num_channels))
223         return 0;
224     ALint loop = 0;
225     alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop);
226     if (loop)
227         return(OPENAL_LOOP_NORMAL);
228     return OPENAL_LOOP_OFF;
229 }
230
231 static signed char OPENAL_IsPlaying(int channel)
232 {
233     if (!initialized)
234         return false;
235     if ((channel < 0) || (channel >= num_channels))
236         return false;
237     ALint state = 0;
238     alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
239     return((state == AL_PLAYING) ? true : false);
240 }
241
242 static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
243 {
244     if (!initialized)
245         return -1;
246     if (sptr == NULL)
247         return -1;
248     if (dsp != NULL)
249         return -1;
250     if (channel == OPENAL_FREE) {
251         for (int i = 0; i < num_channels; i++) {
252             ALint state = 0;
253             alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
254             if ((state != AL_PLAYING) && (state != AL_PAUSED)) {
255                 channel = i;
256                 break;
257             }
258         }
259     }
260
261     if ((channel < 0) || (channel >= num_channels))
262         return -1;
263     alSourceStop(impl_channels[channel].sid);
264     impl_channels[channel].sample = sptr;
265     alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
266     alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
267     set_channel_position(channel, 0.0f, 0.0f, 0.0f);
268
269     impl_channels[channel].startpaused = ((startpaused) ? true : false);
270     if (!startpaused)
271         alSourcePlay(impl_channels[channel].sid);
272     return channel;
273 }
274
275
276 static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
277 {
278 #ifdef __POWERPC__
279     const int bigendian = 1;
280 #else
281     const int bigendian = 0;
282 #endif
283
284     // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy.  :/
285     char *fname = (char *) alloca(strlen(_fname) + 16);
286     strcpy(fname, _fname);
287     char *ptr = strchr(fname, '.');
288     if (ptr)
289         *ptr = '\0';
290     strcat(fname, ".ogg");
291
292     // just in case...
293 #undef fopen
294     FILE *io = fopen(fname, "rb");
295     if (io == NULL)
296         return NULL;
297
298     ALubyte *retval = NULL;
299
300 #if 0  // untested, so disable this!
301     // Can we just feed it to the AL compressed?
302     if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis")) {
303         format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
304         freq = 44100;
305         fseek(io, 0, SEEK_END);
306         size = ftell(io);
307         fseek(io, 0, SEEK_SET);
308         retval = (ALubyte *) malloc(size);
309         size_t rc = fread(retval, size, 1, io);
310         fclose(io);
311         if (rc != 1) {
312             free(retval);
313             return NULL;
314         }
315         return retval;
316     }
317 #endif
318
319     // Uncompress and feed to the AL.
320     OggVorbis_File vf;
321     memset(&vf, '\0', sizeof (vf));
322     if (ov_open(io, &vf, NULL, 0) == 0) {
323         int bitstream = 0;
324         vorbis_info *info = ov_info(&vf, -1);
325         size = 0;
326         format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
327         freq = info->rate;
328
329         if ((info->channels != 1) && (info->channels != 2)) {
330             ov_clear(&vf);
331             return NULL;
332         }
333
334         char buf[1024 * 16];
335         long rc = 0;
336         size_t allocated = 64 * 1024;
337         retval = (ALubyte *) malloc(allocated);
338         while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 ) {
339             if (rc > 0) {
340                 size += rc;
341                 if (size >= allocated) {
342                     allocated *= 2;
343                     ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
344                     if (tmp == NULL) {
345                         free(retval);
346                         retval = NULL;
347                         break;
348                     }
349                     retval = tmp;
350                 }
351                 memcpy(retval + (size - rc), buf, rc);
352             }
353         }
354         ov_clear(&vf);
355         return retval;
356     }
357
358     fclose(io);
359     return NULL;
360 }
361
362
363 AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
364 {
365     if (!initialized)
366         return NULL;
367     if (index != OPENAL_FREE)
368         return NULL;  // this is all the game does...
369     if (offset != 0)
370         return NULL;  // this is all the game does...
371     if (length != 0)
372         return NULL;  // this is all the game does...
373     if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D))
374         return NULL;  // this is all the game does...
375
376     OPENAL_SAMPLE *retval = NULL;
377     ALuint bufferName = 0;
378     ALenum format = AL_NONE;
379     ALsizei size = 0;
380     ALuint frequency = 0;
381     void *data = decode_to_pcm(name_or_data, format, size, frequency);
382     if (data == NULL)
383         return NULL;
384
385     ALuint bid = 0;
386     alGetError();
387     alGenBuffers(1, &bid);
388     if (alGetError() == AL_NO_ERROR) {
389         alBufferData(bid, format, data, size, frequency);
390         retval = new OPENAL_SAMPLE;
391         retval->bid = bid;
392         retval->mode = OPENAL_LOOP_OFF;
393         retval->is2d = (mode == OPENAL_2D);
394         retval->name = new char[strlen(name_or_data) + 1];
395         if (retval->name)
396             strcpy(retval->name, name_or_data);
397     }
398
399     free(data);
400     return(retval);
401 }
402
403 AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
404 {
405     if (!initialized)
406         return;
407     if (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;
413             }
414         }
415         alDeleteBuffers(1, &sptr->bid);
416         delete[] sptr->name;
417         delete sptr;
418     }
419 }
420
421 static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
422 {
423     if (!initialized)
424         return false;
425     if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF))
426         return false;
427     if (!sptr)
428         return false;
429     sptr->mode = mode;
430     return true;
431 }
432
433 AL_API signed char OPENAL_SetFrequency(int channel, int freq)
434 {
435     if (!initialized)
436         return false;
437     if (channel == OPENAL_ALL) {
438         for (int i = 0; i < num_channels; i++)
439             OPENAL_SetFrequency(i, freq);
440         return true;
441     }
442
443     if ((channel < 0) || (channel >= num_channels))
444         return false;
445     if (freq == 8012)
446         // hack
447         alSourcef(impl_channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
448     else
449         alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
450     return true;
451 }
452
453 AL_API signed char OPENAL_SetVolume(int channel, int vol)
454 {
455     if (!initialized)
456         return false;
457
458     if (channel == OPENAL_ALL) {
459         for (int i = 0; i < num_channels; i++)
460             OPENAL_SetVolume(i, vol);
461         return true;
462     }
463
464     if ((channel < 0) || (channel >= num_channels))
465         return false;
466
467     if (vol < 0)
468         vol = 0;
469     else if (vol > 255)
470         vol = 255;
471     ALfloat gain = ((ALfloat) vol) / 255.0f;
472     alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
473     return true;
474 }
475
476 AL_API signed char OPENAL_SetPaused(int channel, signed char paused)
477 {
478     if (!initialized)
479         return false;
480
481     if (channel == OPENAL_ALL) {
482         for (int i = 0; i < num_channels; i++)
483             OPENAL_SetPaused(i, paused);
484         return true;
485     }
486
487     if ((channel < 0) || (channel >= num_channels))
488         return false;
489
490     ALint state = 0;
491     if (impl_channels[channel].startpaused)
492         state = AL_PAUSED;
493     else
494         alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state);
495
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;
501     }
502     return true;
503 }
504
505 AL_API void OPENAL_SetSFXMasterVolume(int volume)
506 {
507     if (!initialized)
508         return;
509     ALfloat gain = ((ALfloat) volume) / 255.0f;
510     alListenerf(AL_GAIN, gain);
511 }
512
513 AL_API signed char OPENAL_StopSound(int channel)
514 {
515     if (!initialized)
516         return false;
517
518     if (channel == OPENAL_ALL) {
519         for (int i = 0; i < num_channels; i++)
520             OPENAL_StopSound(i);
521         return true;
522     }
523
524     if ((channel < 0) || (channel >= num_channels))
525         return false;
526     alSourceStop(impl_channels[channel].sid);
527     impl_channels[channel].startpaused = false;
528     return true;
529 }
530
531 AL_API void OPENAL_Stream_Close(OPENAL_STREAM *stream)
532 {
533     OPENAL_Sample_Free((OPENAL_SAMPLE *) stream);
534 }
535
536 static OPENAL_SAMPLE *OPENAL_Stream_GetSample(OPENAL_STREAM *stream)
537 {
538     if (!initialized)
539         return NULL;
540     return (OPENAL_SAMPLE *) stream;
541 }
542
543 static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM *stream, OPENAL_DSPUNIT *dsp, signed char startpaused)
544 {
545     return OPENAL_PlaySoundEx(channel, (OPENAL_SAMPLE *) stream, dsp, startpaused);
546 }
547
548 static signed char OPENAL_Stream_Stop(OPENAL_STREAM *stream)
549 {
550     if (!initialized)
551         return false;
552     for (int i = 0; i < num_channels; i++) {
553         if (impl_channels[i].sample == (OPENAL_SAMPLE *) stream) {
554             alSourceStop(impl_channels[i].sid);
555             impl_channels[i].startpaused = false;
556         }
557     }
558     return true;
559 }
560
561 AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM *stream, unsigned int mode)
562 {
563     return OPENAL_Sample_SetMode((OPENAL_SAMPLE *) stream, mode);
564 }
565
566 AL_API void OPENAL_Update()
567 {
568     if (!initialized)
569         return;
570     alcProcessContext(alcGetCurrentContext());
571 }
572
573 AL_API signed char OPENAL_SetOutput(int outputtype)
574 {
575     return true;
576 }
577
578 extern int channels[];
579
580 extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
581 {
582     const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
583     if (currSample && currSample == samp[chan]) {
584         if (OPENAL_GetPaused(channels[chan])) {
585             OPENAL_StopSound(channels[chan]);
586             channels[chan] = OPENAL_FREE;
587         } else if (OPENAL_IsPlaying(channels[chan])) {
588             int loop_mode = OPENAL_GetLoopMode(channels[chan]);
589             if (loop_mode & OPENAL_LOOP_OFF) {
590                 channels[chan] = OPENAL_FREE;
591             }
592         }
593     } else {
594         channels[chan] = OPENAL_FREE;
595     }
596
597     channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
598     if (channels[chan] < 0) {
599         channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
600     }
601 }
602
603 extern "C" void PlayStreamEx(int chan, OPENAL_STREAM *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
604 {
605     const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
606     if (currSample && currSample == OPENAL_Stream_GetSample(sptr)) {
607         OPENAL_StopSound(channels[chan]);
608         OPENAL_Stream_Stop(sptr);
609     } else {
610         OPENAL_Stream_Stop(sptr);
611         channels[chan] = OPENAL_FREE;
612     }
613
614     channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
615     if (channels[chan] < 0) {
616         channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);
617     }
618 }