|
From: Seb <whi...@us...> - 2004-10-13 18:38:41
|
Update of /cvsroot/epfl/tgengine-0.1 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21015 Modified Files: soundmanager.cc soundmanager.h Log Message: Nouvelle version du Sound Manager. Detection automatique de l'endian. Nouvelles methodes pour gerer le sound manager. Index: soundmanager.cc =================================================================== RCS file: /cvsroot/epfl/tgengine-0.1/soundmanager.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** soundmanager.cc 10 Oct 2004 21:16:13 -0000 1.1 --- soundmanager.cc 13 Oct 2004 18:38:19 -0000 1.2 *************** *** 1,25 **** // Sebastien "Whistle Master or Seb" Samyn: sebastien.samyn *at* epfl.ch - #include <map> - #include <vector> - #include <string> - #include <iostream> - - #ifdef TGOSX - #include <OpenAL/al.h> - #include <OpenAL/alut.h> - #include <OpenAL/alc.h> - #include <OpenAL/altypes.h> - #else - #include <AL/al.h> - #include <AL/alut.h> - #include <AL/alc.h> - #include <AL/altypes.h> - #endif - - #include <vorbis/vorbisfile.h> - #include "soundmanager.h" - #include "engine.h" --- 1,5 ---- *************** *** 30,33 **** --- 10,19 ---- using namespace std; + namespace tg + { + + SoundManager* SoundManager::pSoundManager = NULL; + + // Constructor SoundManager::SoundManager() *************** *** 40,68 **** } - // Constructor - SoundManager::SoundManager(bool little) - { - if(little == true) - { - endian = 0; - - #ifdef DEBUG - cout << "OpenAL:: Starting SoundManager" << endl; - cout << "OpenAL:: Little Endian detected..." << endl; - #endif - } - else - { - endian = 1; - - #ifdef DEBUG - cout << "OpenAL:: Starting SoundManager" << endl; - cout << "OpenAL:: Big Endian detected..." << endl; - #endif - } - - init(); - } - // Destructor SoundManager::~SoundManager() --- 26,29 ---- *************** *** 83,86 **** --- 44,64 ---- #endif + if (Engine::pEndian->bLittle == true) + { + #ifdef DEBUG + cout << "OpenAL:: Little Endian detected..." << endl; + #endif + + endian = 0; + } + else + { + #ifdef DEBUG + cout << "OpenAL:: Big Endian detected..." << endl; + #endif + + endian = 1; + } + alutInit(NULL, 0); *************** *** 94,97 **** --- 72,95 ---- cout << "OpenAL:: Loading Lib: Vers = " << alversion << " | Renderer = " << alrenderer << " | Vendor = " << alvendor << endl; #endif + + Engine::pVarManager->AddVar("sound", 1); + enable = &Engine::pVarManager->GetVar("sound")->intValue; + + } + + // Disable the Sound Manager and stop all sounds in list + void SoundManager::DisableSoundManager() + { + map<string, ALuint>::iterator iter = SoundList.begin (); + for (; iter!=SoundList.end(); iter++) + alSourceStop(iter->second); + + Engine::pVarManager->AddVar("sound", 0); + } + + // Enable the Sound Manager + void SoundManager::EnableSoundManager() + { + Engine::pVarManager->AddVar("sound", 1); } *************** *** 100,104 **** { #ifdef DEBUG ! cout << "OpenAL:: SetListenerPosition (" << x << "," << y << "," << z << ")" << endl; #endif --- 98,102 ---- { #ifdef DEBUG ! //cout << "OpenAL:: SetListenerPosition (" << x << "," << y << "," << z << ")" << endl; #endif *************** *** 111,115 **** { #ifdef DEBUG ! cout << "OpenAL:: SetListenerOrientation" << endl; #endif --- 109,113 ---- { #ifdef DEBUG ! //cout << "OpenAL:: SetListenerOrientation" << endl; #endif *************** *** 146,152 **** alGenBuffers(1, &buffer); ! if ((result = alGetError()) != AL_NO_ERROR) ! throw GetALErrorString(result); ! // Read in the wav data from file. Check that it loaded correctly. --- 144,150 ---- alGenBuffers(1, &buffer); ! if ((result = alGetError()) != AL_NO_ERROR) ! throw GetALErrorString(result); ! // Read in the wav data from file. Check that it loaded correctly. *************** *** 236,240 **** if ((result = alGetError()) != AL_NO_ERROR) ! throw GetALErrorString(result); // Setup the source properties. --- 234,238 ---- if ((result = alGetError()) != AL_NO_ERROR) ! throw GetALErrorString(result); // Setup the source properties. *************** *** 256,259 **** --- 254,261 ---- void SoundManager::AddWAV(string name, bool relative,bool looping) { + FILE *f; + string tmp_name; + char *path; + try { *************** *** 262,269 **** #endif ! SoundList[name] = LoadWavALSample("data/sounds/"+name+".wav", looping); ! if(relative) ! alSourcei(SoundList[name],AL_SOURCE_RELATIVE,AL_TRUE); } --- 264,284 ---- #endif ! tmp_name = "data/sounds/"+name+".wav"; ! path = (char*) tmp_name.c_str(); ! ! f = fopen(path, "rb"); ! ! if (f == NULL) ! { ! cerr << "OpenAL:: Cannot open " << tmp_name << " for reading..." << endl; ! } ! else ! { ! SoundList[name] = LoadWavALSample("data/sounds/"+name+".wav", looping); ! ! if(relative) ! alSourcei(SoundList[name],AL_SOURCE_RELATIVE,AL_TRUE); ! } } *************** *** 276,364 **** } - // Sound properties (position and velocity) - void SoundManager::SetSoundProperties(string name, float x, float y, float z, float vx, float vy, float vz) - { - #ifdef DEBUG - cout << "OpenAL:: SetSoundProperties: " << name << "(" << x << "," << y << "," << z << ")" << endl; - #endif - - //set the sounds position and velocity - alSource3f(SoundList[name],AL_POSITION,x/100,y/100,z/100); - alSource3f(SoundList[name],AL_VELOCITY,vx,vy,vz); - } - - // Sound volume - void SoundManager::SetSoundVolume(string name, float x) - { - #ifdef DEBUG - cout << "OpenAL:: SetSoundVolume: " << name << "(" << x << ")" << endl; - #endif - - //set the sounds volume - alSourcef (SoundList[name], AL_GAIN,x); - } - - // Play a sound - void SoundManager::PlaySound(string name) - { - #ifdef DEBUG - cout << "OpenAL:: PlaySound: " << name << endl; - #endif - - alSourcePlay(SoundList[name]); - } - - // Stop a sound - void SoundManager::StopSound(string name) - { - #ifdef DEBUG - cout << "OpenAL:: StopSound: " << name << endl; - #endif - - alSourceStop(SoundList[name]); - } - - // Get Status - int SoundManager::SoundStatus(string name) - { - ALint state; - - #ifdef DEBUG - cout << "OpenAL:: SoundStatus: " << name << endl; - #endif - - alGetSourcei(SoundList[name], AL_SOURCE_STATE, &state); - - if (state == AL_PLAYING) - return 1; - else if (state == AL_PAUSED) - return 0; - else if (state == AL_STOPPED) - return -1; - - return -2; - } - - // Kill all buffers and sources - void SoundManager::KillALData() - { - // Release all buffer data. - for (vector<ALuint>::iterator iter = Buffers.begin(); iter != Buffers.end(); ++iter) - alDeleteBuffers(1, (ALuint*)(*iter)); - - // Release all source data. - for (vector<ALuint>::iterator iter = Sources.begin(); iter != Sources.end(); ++iter) - alDeleteBuffers(1, (ALuint*)(*iter)); - - #ifdef DEBUG - cout << "OpenAL:: KillALData" << endl; - #endif - - // Destroy the lists. - Buffers.clear(); - Sources.clear(); - SoundList.clear(); - } - // Creat a new buffer for a sound ALuint SoundManager::LoadOggALBuffer(char path[40]) --- 291,294 ---- *************** *** 474,477 **** --- 404,411 ---- void SoundManager::AddOGG(string name, bool relative,bool looping) { + FILE *f; + string tmp_name; + char *path; + try { *************** *** 480,487 **** #endif ! SoundList[name] = LoadOggALSample("data/sounds/"+name+".ogg", looping); ! if(relative) ! alSourcei(SoundList[name],AL_SOURCE_RELATIVE,AL_TRUE); } --- 414,434 ---- #endif ! tmp_name = "data/sounds/"+name+".ogg"; ! path = (char*) tmp_name.c_str(); ! ! f = fopen(path, "rb"); ! ! if (f == NULL) ! { ! cerr << "OpenAL:: Cannot open " << tmp_name << " for reading..." << endl; ! } ! else ! { ! SoundList[name] = LoadOggALSample("data/sounds/"+name+".ogg", looping); ! ! if(relative) ! alSourcei(SoundList[name],AL_SOURCE_RELATIVE,AL_TRUE); ! } } *************** *** 505,515 **** f = fopen(fileName, "rb"); - if (f == NULL) - { - cerr << "OpenAL:: Cannot open " << fileName << " for reading..." << endl; - exit(-1); - } - // end if - vorbis_info *pInfo; OggVorbis_File oggFile; --- 452,455 ---- *************** *** 517,524 **** // Try opening the given file if (ov_open(f, &oggFile, NULL, 0) != 0) ! { cerr << "OpenAL:: Error opening " << fileName << " for decoding..." << endl; exit(-1); ! } // end if --- 457,464 ---- // Try opening the given file if (ov_open(f, &oggFile, NULL, 0) != 0) ! { cerr << "OpenAL:: Error opening " << fileName << " for decoding..." << endl; exit(-1); ! } // end if *************** *** 535,556 **** // The frequency of the sampling rate freq = pInfo->rate; ! // Keep reading until all is read do ! { // Read up to a buffer's worth of decoded sound data bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream); if (bytes < 0) ! { ov_clear(&oggFile); cerr << "OpenAL:: Error decoding " << fileName << "..." << endl; exit(-1); ! } // end if // Append to end of buffer buffer.insert(buffer.end(), array, array + bytes); ! } while (bytes > 0); --- 475,497 ---- // The frequency of the sampling rate freq = pInfo->rate; ! // Keep reading until all is read do ! { // Read up to a buffer's worth of decoded sound data bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream); if (bytes < 0) ! { ov_clear(&oggFile); cerr << "OpenAL:: Error decoding " << fileName << "..." << endl; exit(-1); ! } // end if // Append to end of buffer buffer.insert(buffer.end(), array, array + bytes); ! ! } while (bytes > 0); *************** *** 559,562 **** --- 500,620 ---- } + // Sound properties (position and velocity) + void SoundManager::SetSoundProperties(string name, float x, float y, float z, float vx, float vy, float vz) + { + #ifdef DEBUG + cout << "OpenAL:: SetSoundProperties: " << name << "(" << x << "," << y << "," << z << ")" << endl; + #endif + + //set the sounds position and velocity + if(CheckSound(name)) + { + alSource3f(SoundList[name],AL_POSITION,x/100,y/100,z/100); + alSource3f(SoundList[name],AL_VELOCITY,vx,vy,vz); + } + } + + // Sound volume + void SoundManager::SetSoundVolume(string name, float x) + { + #ifdef DEBUG + cout << "OpenAL:: SetSoundVolume: " << name << "(" << x << ")" << endl; + #endif + + //set the sounds volume + if(CheckSound(name)) + alSourcef (SoundList[name], AL_GAIN,x); + } + + // Play a sound + void SoundManager::PlaySound(string name) + { + #ifdef DEBUG + cout << "OpenAL:: PlaySound: " << name << endl; + #endif + + if(CheckSound(name)) + alSourcePlay(SoundList[name]); + } + + // Stop a sound + void SoundManager::StopSound(string name) + { + #ifdef DEBUG + cout << "OpenAL:: StopSound: " << name << endl; + #endif + + if(CheckSound(name)) + alSourceStop(SoundList[name]); + } + + // Get Status + int SoundManager::SoundStatus(string name) + { + ALint state; + + #ifdef DEBUG + cout << "OpenAL:: SoundStatus: " << name << endl; + #endif + + if(CheckSound(name)) + { + alGetSourcei(SoundList[name], AL_SOURCE_STATE, &state); + + if (state == AL_PLAYING) + return 1; + else if (state == AL_PAUSED) + return 0; + else if (state == AL_STOPPED) + return -1; + } + + return -2; + } + + // Check if sound exists and if sound is enable + bool SoundManager::CheckSound(string name) + { + enable = &Engine::pVarManager->GetVar("sound")->intValue; + + if(SoundList[name]) + { + if(*enable) + { + return AL_TRUE; + } + else + { + return AL_FALSE; + } + } + else + { + cerr << "OpenAL:: Sound " << name << " not loaded..." << endl; + return AL_FALSE; + } + } + + // Kill all buffers and sources + void SoundManager::KillALData() + { + // Release all buffer data. + for (vector<ALuint>::iterator iter = Buffers.begin(); iter != Buffers.end(); ++iter) + alDeleteBuffers(1, (ALuint*)(*iter)); + + // Release all source data. + for (vector<ALuint>::iterator iter = Sources.begin(); iter != Sources.end(); ++iter) + alDeleteBuffers(1, (ALuint*)(*iter)); + + #ifdef DEBUG + cout << "OpenAL:: KillALData" << endl; + #endif + + // Destroy the lists. + Buffers.clear(); + Sources.clear(); + SoundList.clear(); + } + // Handling Errors string SoundManager::GetALErrorString(ALenum err) *************** *** 587,593 **** return string("AL_OUT_OF_MEMORY"); break; }; - - return 0; } --- 645,653 ---- return string("AL_OUT_OF_MEMORY"); break; + + default: + return string("UNKNOW_AL_ERROR"); + break; }; } *************** *** 598,602 **** { case ALC_NO_ERROR: ! return string("AL_NO_ERROR"); break; --- 658,662 ---- { case ALC_NO_ERROR: ! return string("ALC_NO_ERROR"); break; *************** *** 620,625 **** return string("ALC_OUT_OF_MEMORY"); break; }; - - return 0; } --- 680,689 ---- return string("ALC_OUT_OF_MEMORY"); break; + + default: + return string("UNKNOW_ALC_ERROR"); + break; }; } + + } \ No newline at end of file Index: soundmanager.h =================================================================== RCS file: /cvsroot/epfl/tgengine-0.1/soundmanager.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** soundmanager.h 10 Oct 2004 21:16:13 -0000 1.1 --- soundmanager.h 13 Oct 2004 18:38:19 -0000 1.2 *************** *** 1,7 **** --- 1,11 ---- // Sebastien "Whistle Master or Seb" Samyn: sebastien.samyn *chez* epfl.ch + #ifndef _TGSOUNDMANAGER_H + #define _TGSOUNDMANAGER_H + #include <map> #include <vector> #include <string> + #include <iostream> #ifdef TGOSX *************** *** 25,28 **** --- 29,35 ---- using std::vector; + namespace tg + { + //SoundManager class SoundManager *************** *** 30,41 **** public: ~SoundManager(); SoundManager(); ! ! SoundManager(bool little); ! void AddWAV(string name, bool relative,bool looping); void AddOGG(string name, bool relative,bool looping); void PlaySound(string name); void StopSound(string name); --- 37,52 ---- public: + static SoundManager* pSoundManager; + + int* enable; // Check if soundmanager is enable or not + ~SoundManager(); SoundManager(); ! void AddWAV(string name, bool relative,bool looping); void AddOGG(string name, bool relative,bool looping); + bool CheckSound(string name); + void PlaySound(string name); void StopSound(string name); *************** *** 51,57 **** void KillALData(); ! protected: ! int endian; // 0 for Little-Endian, 1 for Big-Endian void init(); --- 62,71 ---- void KillALData(); ! void EnableSoundManager(); ! void DisableSoundManager(); ! ! private: ! int endian; // 0 for Little-Endian, 1 for Big-Endian void init(); *************** *** 78,79 **** --- 92,96 ---- }; + } + + #endif |