123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- /*!
- * \file src/Sound.cpp
- * \brief This is the audio manager Implementation
- *
- * \author Mongoose
- * \author xythobuz
- */
-
- #ifdef __APPLE__
- #include <OpenAL/al.h>
- #else
- #include <AL/al.h>
- #endif
-
- #include <AL/alut.h>
-
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <assert.h>
-
- #include "Sound.h"
-
- Sound::Sound()
- {
- mSource[0] = 0;
- mBuffer[0] = 0;
- mNextBuffer = 0;
- mNextSource = 0;
- mInit = false;
- }
-
-
- Sound::~Sound()
- {
- if (mInit)
- {
- alutExit();
- }
- }
-
-
- int Sound::init()
- {
- #ifndef __APPLE__
- int fd;
-
- fd = open("/dev/dsp", O_RDWR);
-
- if (fd < 0)
- {
- perror("Sound::Init> Could not open /dev/dsp : ");
- return -1;
- }
-
- close(fd);
- #endif
-
- ALCdevice *Device = alcOpenDevice("OSS");
- ALCcontext *Context = alcCreateContext(Device, NULL);
- alcMakeContextCurrent(Context);
-
- if (alutInitWithoutContext(NULL, NULL) == AL_FALSE) {
- printf("Sound::Init> Could not initialize alut (%s)\n", alutGetErrorString(alutGetError()));
- return -2;
- }
-
- mInit = true;
- printf("Created OpenAL Context\n");
-
- return 0;
- }
-
-
- void Sound::listenAt(float pos[3], float angle[3])
- {
- assert(mInit == true);
- assert(pos != NULL);
- assert(angle != NULL);
-
- alListenerfv(AL_POSITION, pos);
- alListenerfv(AL_ORIENTATION, angle);
- }
-
-
- void Sound::sourceAt(int source, float pos[3])
- {
- assert(mInit == true);
- assert(source >= 0);
- assert(pos != NULL);
-
- alSourcefv(mSource[source-1], AL_POSITION, pos);
- }
-
-
- //! \fixme Seperate sourcing and buffering, Mongoose 2002.01.04
- int Sound::addFile(const char *filename, int *source, unsigned int flags)
- {
- ALsizei size;
- ALfloat freq;
- ALenum format;
- ALvoid *data;
-
- assert(mInit == true);
- assert(filename != NULL);
- assert(filename[0] != '\0');
- assert(source != NULL);
-
- *source = -1;
-
- alGetError();
-
- alGenBuffers(1, &mBuffer[mNextBuffer]);
-
- if (alGetError() != AL_NO_ERROR)
- {
- fprintf(stderr, "Sound::AddFile> alGenBuffers call failed\n");
- return -1;
- }
-
- alGetError();
-
- alGenSources(1, &mSource[mNextSource]);
-
- if (alGetError() != AL_NO_ERROR)
- {
- fprintf(stderr, "Sound::AddFile> alGenSources call failed\n");
- return -2;
- }
-
- // err = alutLoadWAV(filename, &data, &format, &size, &bits, &freq);
- // is deprecated!
- data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
-
- if (alutGetError() != ALUT_ERROR_NO_ERROR)
- {
- fprintf(stderr, "Could not load %s\n", filename);
- return -3;
- }
-
- alBufferData(mBuffer[mNextBuffer], format, data, size, static_cast<ALsizei>(freq));
-
- alSourcei(mSource[mNextSource], AL_BUFFER, mBuffer[mNextBuffer]);
-
- if (flags & SoundFlagsLoop)
- {
- alSourcei(mSource[mNextSource], AL_LOOPING, 1);
- }
-
- ++mNextBuffer;
- ++mNextSource;
-
- *source = mNextBuffer;
-
- return 0;
- }
-
- int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags)
- {
- ALsizei size;
- ALfloat freq;
- ALenum format;
- ALvoid *data;
- int error = 0;
-
- assert(mInit == true);
- assert(wav != NULL);
- assert(source != NULL);
-
- *source = -1;
-
- data = wav;
-
- alGetError();
-
- alGenBuffers(1, &mBuffer[mNextBuffer]);
-
- if (alGetError() != AL_NO_ERROR)
- {
- fprintf(stderr, "Sound::AddWave> alGenBuffers call failed\n");
- return -1;
- }
-
- alGetError();
-
- alGenSources(1, &mSource[mNextSource]);
-
- if (alGetError() != AL_NO_ERROR)
- {
- fprintf(stderr, "Sound::AddWave> alGenSources call failed\n");
- return -2;
- }
-
- //AL_FORMAT_WAVE_EXT does not exist on Mac!"
- // alBufferData(mBuffer[mNextBuffer], AL_FORMAT_WAVE_EXT, data, size, freq);
- // Idea: Fill Buffer with
- // alutLoadMemoryFromFileImage
- // (const ALvoid *data, ALsizei length, ALenum *format, ALsizei *size, ALfloat *frequency)
-
- data = alutLoadMemoryFromFileImage(wav, length, &format, &size, &freq);
-
- if (((error = alutGetError()) != ALUT_ERROR_NO_ERROR) || (data == NULL)) {
- fprintf(stderr, "Could not load wav buffer (%s)\n", alutGetErrorString(error));
- return -3;
- }
-
-
- alBufferData(mBuffer[mNextBuffer], format, data, size, static_cast<ALsizei>(freq));
-
- alSourcei(mSource[mNextSource], AL_BUFFER, mBuffer[mNextBuffer]);
-
- if (flags & SoundFlagsLoop)
- {
- alSourcei(mSource[mNextSource], AL_LOOPING, 1);
- }
-
- ++mNextBuffer;
- ++mNextSource;
-
- *source = mNextBuffer;
-
- //! \fixme Should free alut buffer?
-
- return 0;
- }
-
-
- void Sound::play(int source)
- {
- assert(mInit == true);
- assert(source >= 0);
-
- alSourcePlay(mSource[source-1]);
- }
-
-
- void Sound::stop(int source)
- {
- assert(mInit == true);
- assert(source >= 0);
-
- alSourceStop(mSource[source-1]);
- }
|