123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
-
-
-
- #ifdef HAVE_OPENAL
- #ifdef __APPLE__
- #include <OpenAL/al.h>
- #include <OpenAL/alc.h>
- #include <AL/alut.h>
- #else
- # include <AL/al.h>
- # include <AL/alc.h>
- # include <AL/alut.h>
- # include <AL/alext.h>
- #endif
- #endif
-
- #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 "Sound.h"
-
- #ifdef DEBUG_MEMEORY
- # include "memeory_test.h"
- #endif
-
-
-
- Sound::Sound()
- {
- mSource[0] = 0;
- mBuffer[0] = 0;
- mNextBuffer = 0;
- mNextSource = 0;
- mInit = false;
- }
-
-
- Sound::~Sound()
- {
- if (mInit)
- {
- #ifdef HAVE_OPENAL
- alutExit();
- #endif
- }
- }
-
-
- 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
-
- #ifdef HAVE_OPENAL
- alutInit(NULL, 0);
-
- mInit = true;
- printf("@Created OpenAL Context...\n");
- #else
- printf("*Couldn't create sound Context...\n");
- #endif
-
- return 0;
- }
-
-
- void Sound::listenAt(float pos[3], float angle[3])
- {
- if (!mInit)
- return;
-
- #ifdef HAVE_OPENAL
- alListenerfv(AL_POSITION, pos);
- alListenerfv(AL_ORIENTATION, angle);
- #endif
- }
-
-
- void Sound::sourceAt(int source, float pos[3])
- {
- if (!mInit || source < 0)
- return;
-
- #ifdef HAVE_OPENAL
- alSourcefv(mSource[source-1], AL_POSITION, pos);
- #endif
- }
-
-
-
- int Sound::add(char *filename, int *source, unsigned int flags)
- {
- #ifdef HAVE_OPENAL
- ALsizei size;
- ALfloat freq;
- ALenum format;
- ALvoid *data;
- #endif
-
-
- if (!mInit || !filename || !source)
- {
- printf("Sound::Add> ERROR pre condition assertion failed\n");
- return -1000;
- }
-
- *source = -1;
-
- #ifdef HAVE_OPENAL
- alGetError();
-
- alGenBuffers(1, &mBuffer[mNextBuffer]);
-
- if (alGetError() != AL_NO_ERROR)
- {
- fprintf(stderr, "Sound::Init> alGenBuffers call failed\n");
- return -1;
- }
-
- alGetError();
-
- alGenSources(1, &mSource[mNextSource]);
-
- if (alGetError() != AL_NO_ERROR)
- {
- fprintf(stderr, "Sound::Init> alGenSources call failed\n");
- return -2;
- }
-
-
-
- data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
-
- if (alGetError() != AL_NO_ERROR)
- {
- fprintf(stderr, "Could not load %s\n", filename);
- return -3;
- }
-
- alBufferData(mBuffer[mNextBuffer], format, data, size, freq);
-
- alSourcei(mSource[mNextSource], AL_BUFFER, mBuffer[mNextBuffer]);
-
- if (flags & SoundFlagsLoop)
- {
- alSourcei(mSource[mNextSource], AL_LOOPING, 1);
- }
-
- ++mNextBuffer;
- ++mNextSource;
-
- *source = mNextBuffer;
-
- return 0;
- #else
- return -1;
- #endif
- }
-
-
- int Sound::add(unsigned char *wav, int *source, unsigned int flags)
- {
- #ifdef HAVE_OPENAL
- ALsizei size = 0, freq = 0;
- ALvoid *data;
- #endif
-
- if (!mInit || !wav || !source)
- {
- printf("Sound::Add> ERROR pre condition assertion failed\n");
- return -1000;
- }
-
- *source = -1;
-
- #ifdef HAVE_OPENAL
- data = wav;
-
- alGetError();
-
- alGenBuffers(1, &mBuffer[mNextBuffer]);
-
- if (alGetError() != AL_NO_ERROR)
- {
- fprintf(stderr, "Sound::Init> alGenBuffers call failed\n");
- return -1;
- }
-
- alGetError();
-
- alGenSources(1, &mSource[mNextSource]);
-
- if (alGetError() != AL_NO_ERROR)
- {
- fprintf(stderr, "Sound::Init> alGenSources call failed\n");
- return -2;
- }
-
- #warning "AL_FORMAT_WAVE_EXT does not exist on Mac!"
-
- alBufferData(mBuffer[mNextBuffer], 0x10002, data, size, freq);
-
- alSourcei(mSource[mNextSource], AL_BUFFER, mBuffer[mNextBuffer]);
-
- if (flags & SoundFlagsLoop)
- {
- alSourcei(mSource[mNextSource], AL_LOOPING, 1);
- }
-
- ++mNextBuffer;
- ++mNextSource;
-
- *source = mNextBuffer;
-
- return 0;
- #else
- return -1;
- #endif
- }
-
-
- void Sound::play(int source)
- {
- if (!mInit)
- {
- printf("Sound::Play> ERROR: Sound not initialized\n");
- return;
- }
-
- if (source < 0)
- {
- printf("Sound::Play> ERROR: Source Id invalid\n");
- return;
- }
-
- #ifdef HAVE_OPENAL
- alSourcePlay(mSource[source-1]);
- #endif
- }
-
-
- void Sound::stop(int source)
- {
- if (!mInit || source < 0)
- {
- printf("Sound::Stop> ERROR pre condition assertion failed\n");
- return;
- }
-
- #ifdef HAVE_OPENAL
- alSourceStop(mSource[source-1]);
- #endif
- }
-
-
-
-
-
- #ifdef UNIT_TEST_SOUND
- int main(int argc, char* argv[])
- {
- Sound snd;
- FILE *f;
- unsigned int l;
- unsigned char *buf;
- int id, ret;
-
-
- if (argc > 1)
- {
- snd.init();
- printf("Loading %s\n", argv[1]);
- ret = snd.add(argv[1], &id, SoundFlagsNone);
- printf("Load returned %i\n", ret);
- printf("Playing %u::%s\n", id, argv[1]);
- snd.play(id);
-
- printf("Waiting...\n");
- sleep(5);
-
- f = fopen(argv[1], "rb");
-
- if (f)
- {
- fseek(f, 0, SEEK_END);
- l = ftell(f);
- fseek(f, 0, SEEK_SET);
-
- buf = new unsigned char[l];
- fread(buf, l, 1, f);
-
- fclose(f);
-
- printf("Loading buffer of %s\n", argv[1]);
- ret = snd.add(buf, &id, SoundFlagsNone);
- printf("Load returned %i\n", ret);
- printf("Playing buffer of %u::%s\n", id, argv[1]);
- snd.play(id);
-
- delete [] buf;
- printf("Waiting...\n");
- sleep(5);
- }
- }
- else
- {
- printf("%s filename.wav\n", argv[0]);
- }
-
- return 0;
- }
- #endif
|