Changeset 5961 in orxonox.OLD for branches/avi_play/src/lib/sound
- Timestamp:
- Dec 7, 2005, 2:53:43 PM (19 years ago)
- Location:
- branches/avi_play/src/lib/sound
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/avi_play/src/lib/sound/sound_buffer.cc
r5422 r5961 20 20 #include "sound_engine.h" 21 21 22 23 22 using namespace std; 24 25 23 26 24 ////////////////// … … 36 34 this->setName(fileName); 37 35 38 SoundEngine::getInstance()->addBuffer(this);39 40 36 ALenum format; 41 37 ALvoid* data; … … 47 43 alGenBuffers(1, &this->bufferID); 48 44 if ((result = alGetError()) != AL_NO_ERROR) 49 SoundEngine::PrintALErrorString(result);45 PRINTF(2)("%s\n", SoundEngine::getALErrorString(result)); 50 46 51 47 // read in the wav data … … 59 55 #endif 60 56 if ((result = alGetError()) != AL_NO_ERROR) 61 SoundEngine::PrintALErrorString(result);57 PRINTF(2)("%s\n", SoundEngine::getALErrorString(result)); 62 58 63 59 // send the loaded wav data to the buffer 64 60 alBufferData(this->bufferID, format, data, this->size, freq); 65 61 if ((result = alGetError()) != AL_NO_ERROR) 66 SoundEngine::PrintALErrorString(result);62 PRINTF(2)("%s\n", SoundEngine::getALErrorString(result)); 67 63 68 64 // remove the wav data (redundant) 69 65 alutUnloadWAV(format, data, this->size, freq); 70 66 if ((result = alGetError()) != AL_NO_ERROR) 71 SoundEngine::PrintALErrorString(result);67 PRINTF(2)("%s\n", SoundEngine::getALErrorString(result)); 72 68 } 73 69 -
branches/avi_play/src/lib/sound/sound_engine.cc
r5924 r5961 27 27 #include "resource_manager.h" 28 28 #include "debug.h" 29 #include " ini_parser.h"29 #include "parser/ini_parser/ini_parser.h" 30 30 #include "globals.h" 31 31 … … 47 47 this->bufferList = NULL; 48 48 this->sourceList = NULL; 49 50 this->device = NULL; 51 this->context = NULL; 52 53 this->maxSourceCount = 32; 49 54 } 50 55 … … 64 69 while (this->sourceList->size() > 0) 65 70 delete dynamic_cast<SoundSource*>(this->sourceList->front()); 71 } 72 73 while(!this->ALSources.empty()) 74 { 75 alDeleteSources(1, &this->ALSources.top()); 76 this->ALSources.pop(); 66 77 } 67 78 … … 86 97 void SoundEngine::loadSettings(IniParser* iniParser) 87 98 { 99 const char* channels = iniParser->getVar(CONFIG_NAME_AUDIO_CHANNELS, CONFIG_SECTION_AUDIO, "32"); 100 this->maxSourceCount = atoi(channels); 88 101 const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "80"); 89 102 this->musicVolume = atof(musicVolume)/100.0; … … 118 131 119 132 120 /** 121 * adds a SoundBuffer to the bufferList of the SoundEngine 122 * @param buffer The buffer to add to the bufferList 123 */ 124 void SoundEngine::addBuffer(SoundBuffer* buffer) 125 { 126 if (unlikely(this->bufferList == NULL)) 127 this->bufferList = ClassList::getList(CL_SOUND_BUFFER); 128 } 129 130 /** 131 * removes a SoundBuffer from the bufferList of the SoundEngine 132 * @param buffer The buffer to delete from the SoundEngine 133 */ 134 void SoundEngine::removeBuffer(SoundBuffer* buffer) 135 { 136 // look if there are any sources that have the buffer still loaded 137 if (this->sourceList != NULL) 138 { 139 list<BaseObject*>::const_iterator source; 140 for (source = this->sourceList->begin(); source != this->sourceList->end(); source++) 141 { 142 if (buffer == static_cast<SoundSource*>(*source)->getBuffer()) 143 delete (*source); 133 void SoundEngine::popALSource(ALuint& source) 134 { 135 if (source != 0) 136 return; 137 else 138 { 139 140 /// @TODO try to create more sources if needed 141 if (!this->ALSources.empty()) 142 { 143 144 source = this->ALSources.top(); 145 printf("test: : %d\n", source); 146 this->ALSources.pop(); 144 147 } 145 148 } 146 149 } 147 150 148 /**149 * adds a SoundSource to the sourceList of the SoundEngine150 * @param source The source to add to the sourceList151 */152 void SoundEngine::addSource(SoundSource* source)153 {154 this->sourceList = ClassList::getList(CL_SOUND_SOURCE);155 }156 151 157 152 /** … … 180 175 181 176 // updating all the Sources positions 182 if (likely(this->sourceList != NULL ))177 if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL)) 183 178 { 184 179 list<BaseObject*>::const_iterator sourceIT; … … 187 182 { 188 183 source = static_cast<SoundSource*>(*sourceIT); 189 if ( likely(source->getNode() != NULL))184 if (source->isPlaying()) 190 185 { 191 alSource3f(source->getID(), AL_POSITION, 192 source->getNode()->getAbsCoor().x, 193 source->getNode()->getAbsCoor().y, 194 source->getNode()->getAbsCoor().z); 195 alSource3f(source->getID(), AL_VELOCITY, 196 source->getNode()->getVelocity().x, 197 source->getNode()->getVelocity().y, 198 source->getNode()->getVelocity().z); 186 int play; 187 alGetSourcei(source->getID(), AL_SOURCE_STATE, &play); 188 if(play == AL_PLAYING) 189 { 190 if (likely(source->getNode() != NULL)) 191 { 192 alSource3f(source->getID(), AL_POSITION, 193 source->getNode()->getAbsCoor().x, 194 source->getNode()->getAbsCoor().y, 195 source->getNode()->getAbsCoor().z); 196 alSource3f(source->getID(), AL_VELOCITY, 197 source->getNode()->getVelocity().x, 198 source->getNode()->getVelocity().y, 199 source->getNode()->getVelocity().z); 200 } 201 202 } 203 else 204 { 205 source->stop(); 206 } 199 207 } 200 208 } … … 281 289 282 290 // INITIALIZING THE DEVICE: 283 #if ndef AL_VERSION_1_1291 #ifdef AL_VERSION_1_1 284 292 ALubyte deviceName[] = 285 293 #else … … 300 308 301 309 if ((result = alGetError()) != AL_NO_ERROR) 302 SoundEngine::PrintALErrorString(result);310 PRINTF(2)("%s\n", SoundEngine::getALErrorString(result)); 303 311 304 312 this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY); … … 313 321 bool SoundEngine::allocateSources(unsigned int count) 314 322 { 315 ALuint* sourceList = new ALuint[count];316 323 ALenum result; 317 318 alGenSources(count, sourceList);319 if ((result = alGetError()) != AL_NO_ERROR)320 {321 SoundEngine::PrintALErrorString(result);322 return false;323 }324 325 /// @TODO check syntax326 327 328 324 // Setting default values. 329 for (int i = 0; i < count; i++) 330 { 331 alSourcef (sourceList[i], AL_PITCH, 1.0 ); 332 alSourcef (sourceList[i], AL_GAIN, this->getEffectsVolume() ); 333 alSourcei (sourceList[i], AL_LOOPING, AL_FALSE ); 334 this->ALSources.push(sourceList[i]); 325 for (unsigned int i = 0; i < count; i++) 326 { 327 ALuint source; 328 329 alGenSources(1, &source); 330 if ((result = alGetError()) != AL_NO_ERROR) 331 PRINTF(1)("Error Generating Sources: '%s'\n", SoundEngine::getALErrorString(result)); 332 333 alSourcef (source, AL_PITCH, 1.0 ); 334 alSourcef (source, AL_GAIN, this->getEffectsVolume() ); 335 alSourcei (source, AL_LOOPING, AL_FALSE ); 336 this->ALSources.push(source); 335 337 } 336 338 return true; … … 341 343 * @param err The error found 342 344 */ 343 void SoundEngine::PrintALErrorString(ALenum err)345 const char* SoundEngine::getALErrorString(ALenum err) 344 346 { 345 347 switch(err) 346 348 { 347 349 case AL_NO_ERROR: 348 PRINTF(4)("AL_NO_ERROR\n"); 349 break; 350 350 return ("AL_NO_ERROR"); 351 351 case AL_INVALID_NAME: 352 PRINTF(2)("AL_INVALID_NAME\n"); 353 break; 354 352 return ("AL_INVALID_NAME"); 355 353 case AL_INVALID_ENUM: 356 PRINTF(2)("AL_INVALID_ENUM\n"); 357 break; 358 354 return ("AL_INVALID_ENUM"); 359 355 case AL_INVALID_VALUE: 360 PRINTF(2)("AL_INVALID_VALUE\n"); 361 break; 362 356 return ("AL_INVALID_VALUE"); 363 357 case AL_INVALID_OPERATION: 364 PRINTF(2)("AL_INVALID_OPERATION\n"); 365 break; 366 358 return ("AL_INVALID_OPERATION"); 367 359 case AL_OUT_OF_MEMORY: 368 PRINTF(2)("AL_OUT_OF_MEMORY\n"); 369 break; 360 return ("AL_OUT_OF_MEMORY"); 370 361 }; 371 362 } … … 373 364 void SoundEngine::listDevices() 374 365 { 375 376 366 printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER)); 377 367 } -
branches/avi_play/src/lib/sound/sound_engine.h
r5924 r5961 26 26 //! A class that handles audio via the openAudioLibrary 27 27 class SoundEngine : public BaseObject { 28 29 28 public: 30 29 virtual ~SoundEngine(); … … 49 48 50 49 // administrative 51 void addBuffer(SoundBuffer* buffer); 52 void removeBuffer(SoundBuffer* buffer); 53 void addSource(SoundSource* source); 50 void popALSource(ALuint& source); 51 void pushALSource(ALuint& source) { if (source != 0) this->ALSources.push(source); }; 54 52 55 53 void flushUnusedBuffers(); … … 57 55 void flushAllSources(); 58 56 57 bool initAudio(); 59 58 bool allocateSources(unsigned int count); 60 bool initAudio();61 59 62 60 // error handling: 63 static void PrintALErrorString(ALenum err); 64 // static void PrintALCErrorString(ALenum err); 65 61 static const char* getALErrorString(ALenum err); 66 62 67 63 private: 68 64 SoundEngine(); 65 69 66 void listDevices(); 70 67 -
branches/avi_play/src/lib/sound/sound_source.cc
r5924 r5961 34 34 35 35 // adding the Source to the SourcesList of the SoundEngine 36 SoundEngine::getInstance()->addSource(this);37 38 36 this->buffer = buffer; 39 37 this->sourceNode = sourceNode; 40 38 41 alGenSources(1, &this->sourceID); 42 if ((result = alGetError()) != AL_NO_ERROR) 43 SoundEngine::PrintALErrorString(result); 44 if (this->buffer != NULL) 45 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 46 alSourcef (this->sourceID, AL_PITCH, 1.0 ); 47 alSourcef (this->sourceID, AL_GAIN, SoundEngine::getInstance()->getEffectsVolume() ); 48 alSourcei (sourceID, AL_LOOPING, AL_FALSE ); 39 this->sourceID = 0; 40 this->bPlay = false; 49 41 } 50 42 … … 54 46 SoundSource::~SoundSource() 55 47 { 56 //SoundEngine::getInstance()->removeSource(this); 57 alDeleteSources(1, &this->sourceID); 48 SoundEngine::getInstance()->pushALSource(this->sourceID); 58 49 } 59 50 … … 63 54 void SoundSource::play() 64 55 { 56 if (this->sourceID == 0) 57 SoundEngine::getInstance()->popALSource(this->sourceID); 65 58 alSourcePlay(this->sourceID); 59 this->bPlay = true; 66 60 } 67 61 … … 72 66 void SoundSource::play(const SoundBuffer* buffer) 73 67 { 68 if (unlikely(this->sourceID == 0)) 69 SoundEngine::getInstance()->popALSource(this->sourceID); 70 71 printf("%d\n",sourceID); 74 72 alSourceStop(this->sourceID); 75 73 alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); … … 78 76 if (unlikely(this->buffer != NULL)) 79 77 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 78 this->bPlay = true; 80 79 } 81 80 … … 85 84 void SoundSource::stop() 86 85 { 86 this->bPlay = false; 87 87 alSourceStop(this->sourceID); 88 SoundEngine::getInstance()->pushALSource(this->sourceID); 88 89 } 89 90 -
branches/avi_play/src/lib/sound/sound_source.h
r5924 r5961 31 31 /** @returns The ID of this Source */ 32 32 inline ALuint getID() const { return this->sourceID; } 33 /** @returns true, if the Source is Playing */ 34 inline bool isPlaying() const { return this->bPlay; }; 33 35 /** @returns the SoundBuffer of this Source */ 34 36 inline const SoundBuffer* getBuffer() const { return this->buffer; }
Note: See TracChangeset
for help on using the changeset viewer.