Ticket #1223: patch12.diff
File patch12.diff, 85.7 KB (added by , 12 years ago) |
---|
-
build/premake/premake4.lua
519 519 "ps/Network", 520 520 "ps/GameSetup", 521 521 "ps/XML", 522 "sound", 522 "soundmanager", 523 "soundmanager/data", 524 "soundmanager/items", 525 "soundmanager/js", 523 526 "scripting", 524 527 "maths", 525 528 "maths/scripting", … … 533 536 "boost", 534 537 "enet", 535 538 "libcurl", 539 "vorbis", 540 "openal" 536 541 } 537 542 setup_static_lib_project("engine", source_dirs, extern_libs, {}) 538 543 … … 553 558 end 554 559 setup_static_lib_project("graphics", source_dirs, extern_libs, {}) 555 560 556 557 561 source_dirs = { 558 562 "tools/atlas/GameInterface", 559 563 "tools/atlas/GameInterface/Handlers" -
source/ps/GameSetup/Config.cpp
23 23 #include "lib/timer.h" 24 24 #include "lib/res/sound/snd_mgr.h" 25 25 #include "Config.h" 26 #include "soundmanager/CSoundManager.h" 26 27 27 28 28 // (these variables are documented in the header.) 29 29 30 30 CStrW g_CursorName = L"test"; … … 81 81 CFG_GET_USER_VAL("particles", Bool, g_Particles); 82 82 83 83 float gain = -1.0f; 84 float musicGain = -1.0f; 85 float ambientGain = -1.0f; 86 float actionGain = -1.0f; 87 int bufferCount = 50; 88 unsigned long bufferSize = 65536; 89 84 90 CFG_GET_USER_VAL("sound.mastergain", Float, gain); 85 if(gain >= 0.0f) 86 WARN_IF_ERR(snd_set_master_gain(gain)); 91 CFG_GET_USER_VAL("sound.musicgain", Float, musicGain); 92 CFG_GET_USER_VAL("sound.ambientgain", Float, ambientGain); 93 CFG_GET_USER_VAL("sound.actiongain", Float, actionGain); 94 95 CFG_GET_USER_VAL("sound.bufferCount", Int, bufferCount); 96 CFG_GET_USER_VAL("sound.bufferSize", UnsignedLong, bufferSize); 97 98 g_SoundManager->setMasterGain( gain ); 99 g_SoundManager->setMusicGain( musicGain ); 100 g_SoundManager->setAmbientGain( ambientGain ); 101 g_SoundManager->setActionGain( actionGain ); 102 103 g_SoundManager->setMemoryUsage( bufferSize, bufferCount); 87 104 } 88 105 89 106 -
source/ps/GameSetup/GameSetup.cpp
102 102 #include "tools/atlas/GameInterface/GameLoop.h" 103 103 #include "tools/atlas/GameInterface/View.h" 104 104 105 #include "soundmanager/CSoundManager.h" 105 106 106 107 #if !(OS_WIN || OS_MACOSX || OS_ANDROID) // assume all other platforms use X11 for wxWidgets 107 108 #define MUST_INIT_X11 1 … … 203 204 { 204 205 PROFILE3("render"); 205 206 207 g_SoundManager->idleTask(); 208 206 209 ogl_WarnIfError(); 207 210 208 211 g_Profiler2.RecordGPUFrameStart(); … … 330 333 { 331 334 // maths 332 335 JSI_Vector3D::init(); 333 336 337 CSoundManager::ScriptingInit(); 334 338 // graphics 335 339 CGameView::ScriptingInit(); 336 340 … … 338 342 CRenderer::ScriptingInit(); 339 343 340 344 // sound 341 JSI_Sound::ScriptingInit();345 // JSI_Sound::ScriptingInit(); 342 346 343 347 // ps 344 348 JSI_Console::init(); … … 476 480 g_VFS->Mount(L"", modLoosePath / modName/"", flags, priority); 477 481 g_VFS->Mount(L"", modArchivePath / modName/"", flags, priority); 478 482 } 483 484 g_SoundManager = new CSoundManager(); 479 485 480 486 // note: don't bother with g_VFS->TextRepresentation - directories 481 487 // haven't yet been populated and are empty. … … 691 697 // resource 692 698 // first shut down all resource owners, and then the handle manager. 693 699 TIMER_BEGIN(L"resource modules"); 694 snd_shutdown();700 delete g_SoundManager; 695 701 696 702 g_VFS.reset(); 697 703 … … 930 936 // speed up startup by disabling all sound 931 937 // (OpenAL init will be skipped). 932 938 // must be called before first snd_open. 933 snd_disable(true);939 g_SoundManager->setEnabled( false ); 934 940 } 935 941 936 942 g_GUI = new CGUIManager(g_ScriptingHost.GetScriptInterface()); -
source/ps/Game.cpp
45 45 #include "simulation2/components/ICmpPlayerManager.h" 46 46 47 47 #include "gui/GUIManager.h" 48 #include "soundmanager/CSoundManager.h" 48 49 49 50 extern bool g_GameRestarted; 50 51 … … 299 300 if (doInterpolate) 300 301 { 301 302 m_TurnManager->Interpolate(deltaTime); 303 g_SoundManager->idleTask(); 302 304 } 303 305 304 306 // TODO: maybe we should add a CCmpParticleInterface that passes the interpolation commands -
source/soundmanager/items/CSoundItem.h
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 19 #ifndef SoundTester_CSoundItem_h 20 #define SoundTester_CSoundItem_h 21 22 #include "CSoundBase.h" 23 #include "soundmanager/data/CSoundData.h" 24 25 26 class CSoundItem :public CSoundBase 27 { 28 protected: 29 30 public: 31 CSoundItem (); 32 CSoundItem (CSoundData* sndData); 33 34 virtual ~CSoundItem (); 35 void attach ( CSoundData* itemData ); 36 bool idleTask (); 37 38 protected: 39 40 41 }; 42 43 44 45 46 47 48 #endif -
source/soundmanager/items/CBufferItem.h
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 19 #ifndef SoundTester_CBufferItem_h 20 #define SoundTester_CBufferItem_h 21 22 #include "CSoundBase.h" 23 24 class CBufferItem : public CSoundBase 25 { 26 public: 27 CBufferItem (CSoundData* sndData); 28 virtual ~CBufferItem (); 29 30 virtual void setLooping ( bool loops ); 31 virtual bool idleTask (); 32 33 protected: 34 virtual void attach ( CSoundData* itemData ); 35 36 37 }; 38 39 40 #endif -
source/soundmanager/items/CStreamItem.h
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef SoundTester_CStreamItem_h 19 #define SoundTester_CStreamItem_h 20 21 #include "soundmanager/data/CSoundData.h" 22 #include "CSoundBase.h" 23 24 class CStreamItem : public CSoundBase 25 { 26 public: 27 CStreamItem (CSoundData* sndData); 28 virtual ~CStreamItem (); 29 30 virtual void setLooping ( bool loops ); 31 virtual bool idleTask (); 32 33 protected: 34 virtual void attach ( CSoundData* itemData ); 35 36 }; 37 38 #endif -
source/soundmanager/items/ISoundItem.h
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef SoundTester_ISoundItem_h 19 #define SoundTester_ISoundItem_h 20 21 #include <string> 22 #include "lib/external_libraries/openal.h" 23 #include "maths/Vector3D.h" 24 25 26 class ISoundItem 27 { 28 29 public: 30 virtual ~ISoundItem(){}; 31 virtual bool getLooping () = 0; 32 virtual void setLooping (bool loop) = 0; 33 virtual bool isPlaying () = 0; 34 35 36 virtual std::string getName () = 0; 37 virtual bool idleTask () = 0; 38 39 virtual void play () = 0; 40 virtual void stop () = 0; 41 42 virtual void ensurePlay () = 0; 43 virtual void playAsMusic () = 0; 44 virtual void playAsAmbient () = 0; 45 46 virtual void playAndDelete () = 0; 47 virtual void stopAndDelete () = 0; 48 virtual void fadeToIn ( ALfloat newVolume, double fadeDuration) = 0; 49 virtual void fadeAndDelete ( double fadeTime ) = 0; 50 virtual void playLoop () = 0; 51 52 virtual void setCone (ALfloat innerCone, ALfloat outerCone, ALfloat coneGain) = 0; 53 virtual void setPitch (ALfloat pitch) = 0; 54 virtual void setGain (ALfloat gain) = 0; 55 virtual void setLocation (const CVector3D& position) = 0; 56 virtual void setRollOff (ALfloat gain) = 0; 57 }; 58 59 60 #endif //SoundTester_ISoundItem_h 61 No newline at end of file -
source/soundmanager/items/CSoundBase.cpp
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 #include "precompiled.h" 18 19 #include "CSoundBase.h" 20 #include "soundmanager/CSoundManager.h" 21 #include "soundmanager/data/CSoundData.h" 22 23 #include <iostream> 24 25 #include "lib/timer.h" 26 27 28 CSoundBase::CSoundBase() 29 { 30 resetVars(); 31 } 32 33 CSoundBase::~CSoundBase() 34 { 35 stop(); 36 if ( mALSource != 0 ) { 37 alDeleteSources( 1, &mALSource); 38 mALSource = 0; 39 } 40 if ( mSoundData != 0 ) { 41 CSoundData::releaseSoundData( mSoundData ); 42 mSoundData = 0; 43 } 44 if ( mName ) 45 delete mName; 46 } 47 48 void CSoundBase::resetVars() 49 { 50 mALSource = 0; 51 mSoundData = 0; 52 mLastPlay = false; 53 mLooping = false; 54 mStartFadeTime = 0; 55 mEndFadeTime = 0; 56 mStartVolume = 0; 57 mEndVolume = 0; 58 59 resetFade(); 60 mName = new std::string( "sound name" ); 61 } 62 63 void CSoundBase::resetFade() 64 { 65 mStartFadeTime = 0; 66 mEndFadeTime = 0; 67 mStartVolume = 0; 68 mEndVolume = 0; 69 mShouldBePlaying = false; 70 } 71 72 void CSoundBase::setGain(ALfloat gain) 73 { 74 alSourcef(mALSource, AL_GAIN, gain); 75 } 76 void CSoundBase::setRollOff(ALfloat rolls) 77 { 78 alSourcef(mALSource, AL_ROLLOFF_FACTOR, rolls); 79 } 80 void CSoundBase::ensurePlay() 81 { 82 if ( mShouldBePlaying && !isPlaying() ) 83 play(); 84 } 85 86 void CSoundBase::setCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain) 87 { 88 alSourcef( mALSource, innerCone, AL_CONE_INNER_ANGLE); 89 alSourcef( mALSource, outerCone, AL_CONE_OUTER_ANGLE); 90 alSourcef( mALSource, coneGain, AL_CONE_OUTER_GAIN); 91 } 92 93 void CSoundBase::setPitch(ALfloat pitch) 94 { 95 alSourcef( mALSource, AL_PITCH, pitch); 96 } 97 98 void CSoundBase::setDirection(const CVector3D& direction) 99 { 100 alSourcefv( mALSource, AL_DIRECTION, direction.GetFloatArray() ); 101 } 102 103 bool CSoundBase::initOpenAL() 104 { 105 alGetError(); /* clear error */ 106 alGenSources( 1, &mALSource); 107 long anErr = alGetError(); 108 if( anErr != AL_NO_ERROR) 109 { 110 printf("- Error creating sources %ld !!\n", anErr ); 111 } 112 else 113 { 114 ALfloat source0Pos[]={ -2.0, 0.0, 0.0}; 115 ALfloat source0Vel[]={ 0.0, 0.0, 0.0}; 116 117 alSourcef( mALSource,AL_PITCH,1.0f); 118 alSourcef( mALSource,AL_GAIN,1.0f); 119 alSourcefv( mALSource,AL_POSITION,source0Pos); 120 alSourcefv( mALSource,AL_VELOCITY,source0Vel); 121 alSourcei( mALSource,AL_LOOPING,AL_FALSE); 122 return true; 123 } 124 return false; 125 } 126 127 bool CSoundBase::isPlaying() 128 { 129 int proc_state; 130 alGetSourceiv( mALSource, AL_SOURCE_STATE, &proc_state); 131 132 return ( proc_state == AL_PLAYING ); 133 } 134 135 void CSoundBase::setLastPlay( bool last ) 136 { 137 mLastPlay = last; 138 } 139 140 bool CSoundBase::idleTask() 141 { 142 return true; 143 } 144 145 void CSoundBase::setLocation (const CVector3D& position) 146 { 147 alSourcefv( mALSource,AL_POSITION, position.GetFloatArray() ); 148 } 149 150 bool CSoundBase::handleFade() 151 { 152 if ( mStartFadeTime != 0 ) { 153 double currTime = timer_Time(); 154 double pctDone = std::min( 1.0, (currTime - mStartFadeTime) / (mEndFadeTime - mStartFadeTime) ); 155 pctDone = std::max( 0.0, pctDone ); 156 ALfloat curGain = ((mEndVolume - mStartVolume ) * pctDone) + mStartVolume; 157 158 if (curGain == 0 ) 159 stop(); 160 else if ( curGain == mEndVolume ) { 161 alSourcef( mALSource, AL_GAIN, curGain); 162 resetFade(); 163 } 164 else 165 alSourcef( mALSource, AL_GAIN, curGain); 166 } 167 return true; 168 } 169 170 bool CSoundBase::getLooping() 171 { 172 return mLooping; 173 } 174 void CSoundBase::setLooping( bool loops ) 175 { 176 mLooping = loops; 177 alSourcei( mALSource, AL_LOOPING, loops ? AL_TRUE : AL_FALSE ); 178 } 179 180 void CSoundBase::play() 181 { 182 mShouldBePlaying = true; 183 if ( mALSource != 0 ) 184 alSourcePlay( mALSource ); 185 } 186 void CSoundBase::playAndDelete() 187 { 188 setLastPlay( true ); 189 play(); 190 } 191 192 void CSoundBase::fadeAndDelete( double fadeTime ) 193 { 194 setLastPlay( true ); 195 fadeToIn( 0, fadeTime ); 196 } 197 198 void CSoundBase::stopAndDelete() 199 { 200 setLastPlay( true ); 201 stop(); 202 } 203 204 void CSoundBase::playLoop() 205 { 206 if ( mALSource != 0 ) { 207 setLooping( true ); 208 play(); 209 } 210 } 211 212 void CSoundBase::fadeToIn( ALfloat newVolume, double fadeDuration) 213 { 214 int proc_state; 215 alGetSourceiv( mALSource, AL_SOURCE_STATE, &proc_state); 216 if ( proc_state == AL_PLAYING ) { 217 mStartFadeTime = timer_Time(); 218 mEndFadeTime = mStartFadeTime + fadeDuration; 219 alGetSourcef( mALSource, AL_GAIN, &mStartVolume); 220 mEndVolume = newVolume; 221 } 222 223 } 224 225 void CSoundBase::playAsMusic() 226 { 227 g_SoundManager->setMusicItem( this ); 228 } 229 230 void CSoundBase::playAsAmbient() 231 { 232 g_SoundManager->setAmbientItem( this ); 233 } 234 235 void CSoundBase::stop() 236 { 237 mShouldBePlaying = false; 238 if ( mALSource != 0 ) { 239 int proc_state; 240 alSourcei( mALSource, AL_LOOPING, AL_FALSE ); 241 alGetSourceiv( mALSource, AL_SOURCE_STATE, &proc_state); 242 if ( proc_state == AL_PLAYING ) 243 alSourceStop( mALSource ); 244 } 245 } 246 247 const char* CSoundBase::Name() 248 { 249 return mName->c_str(); 250 } 251 252 std::string CSoundBase::getName() 253 { 254 return std::string( mName->c_str() ); 255 } 256 257 void CSoundBase::setNameFromPath( char* fileLoc ) 258 { 259 std::string anst( fileLoc ); 260 size_t pos = anst.find_last_of("/"); 261 if(pos != std::wstring::npos) 262 mName->assign(anst.begin() + pos + 1, anst.end()); 263 else 264 mName->assign(anst.begin(), anst.end()); 265 } 266 -
source/soundmanager/items/CSoundItem.cpp
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 #include "precompiled.h" 18 19 #include "CSoundItem.h" 20 #include "soundmanager/data/CSoundData.h" 21 22 #include <iostream> 23 24 25 CSoundItem::CSoundItem() 26 { 27 resetVars(); 28 } 29 30 CSoundItem::CSoundItem(CSoundData* sndData) 31 { 32 resetVars(); 33 if ( initOpenAL() ) 34 attach( sndData ); 35 } 36 37 CSoundItem::~CSoundItem() 38 { 39 ALuint al_buf; 40 41 stop(); 42 alSourceUnqueueBuffers(mALSource, 1, &al_buf); 43 } 44 45 bool CSoundItem::idleTask() 46 { 47 handleFade(); 48 49 if ( mLastPlay ) 50 { 51 int proc_state; 52 alGetSourceiv( mALSource, AL_SOURCE_STATE, &proc_state); 53 return ( proc_state != AL_STOPPED ); 54 } 55 return true; 56 } 57 58 void CSoundItem::attach( CSoundData* itemData ) 59 { 60 if ( itemData != NULL ) { 61 mSoundData = itemData->incrementCount(); 62 alSourcei( mALSource, AL_BUFFER, mSoundData->getBuffer() ); 63 } 64 } -
source/soundmanager/items/CBufferItem.cpp
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 #include "precompiled.h" 18 #include "CBufferItem.h" 19 #include "soundmanager/data/CSoundData.h" 20 21 #include <iostream> 22 23 CBufferItem::CBufferItem(CSoundData* sndData) 24 { 25 resetVars(); 26 if ( initOpenAL() ) 27 attach( sndData ); 28 } 29 30 31 CBufferItem::~CBufferItem() 32 { 33 stop(); 34 int num_processed; 35 alGetSourcei( mALSource, AL_BUFFERS_PROCESSED, &num_processed); 36 37 if (num_processed > 0) 38 { 39 ALuint* al_buf = new ALuint[num_processed]; 40 alSourceUnqueueBuffers(mALSource, num_processed, al_buf); 41 42 delete[] al_buf; 43 } 44 } 45 46 47 bool CBufferItem::idleTask() 48 { 49 handleFade(); 50 51 if ( mLastPlay ) 52 { 53 int proc_state; 54 alGetSourceiv( mALSource, AL_SOURCE_STATE, &proc_state); 55 return ( proc_state != AL_STOPPED ); 56 } 57 58 if ( getLooping() ) { 59 int num_processed; 60 alGetSourcei( mALSource, AL_BUFFERS_PROCESSED, &num_processed); 61 62 for ( int i = 0; i < num_processed; i++ ) 63 { 64 ALuint al_buf; 65 alSourceUnqueueBuffers(mALSource, 1, &al_buf); 66 alSourceQueueBuffers(mALSource, 1, &al_buf); 67 } 68 } 69 70 return true; 71 } 72 73 void CBufferItem::attach( CSoundData* itemData ) 74 { 75 if ( itemData != NULL ) { 76 mSoundData = itemData->incrementCount(); 77 alSourceQueueBuffers(mALSource, mSoundData->getBufferCount(),(const ALuint *) mSoundData->getBufferPtr()); 78 } 79 } 80 81 void CBufferItem::setLooping( bool loops ) 82 { 83 mLooping = loops; 84 } 85 -
source/soundmanager/items/CSoundBase.h
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 19 #ifndef SoundTester_CSoundBase_h 20 #define SoundTester_CSoundBase_h 21 22 #include <string> 23 #include "lib/external_libraries/openal.h" 24 #include "soundmanager/items/ISoundItem.h" 25 #include "soundmanager/data/CSoundData.h" 26 27 28 class CSoundBase :public ISoundItem 29 { 30 protected: 31 32 ALuint mALSource; 33 CSoundData* mSoundData; 34 35 std::string* mName; 36 bool mLastPlay; 37 bool mLooping; 38 bool mShouldBePlaying; 39 40 double mStartFadeTime; 41 double mEndFadeTime; 42 ALfloat mStartVolume; 43 ALfloat mEndVolume; 44 45 public: 46 CSoundBase (); 47 48 virtual ~CSoundBase (); 49 50 virtual bool initOpenAL(); 51 virtual void resetVars(); 52 virtual void ensurePlay(); 53 54 virtual void setGain (ALfloat gain); 55 virtual void setRollOff (ALfloat gain); 56 virtual void setPitch(ALfloat pitch); 57 virtual void setDirection(const CVector3D& direction); 58 virtual void setCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain); 59 virtual void setLastPlay( bool last ); 60 61 void play (); 62 void playAndDelete (); 63 bool idleTask (); 64 void playLoop (); 65 void stop (); 66 void stopAndDelete (); 67 void fadeToIn ( ALfloat newVolume, double fadeDuration); 68 69 void playAsMusic (); 70 void playAsAmbient (); 71 72 const char* Name(); 73 std::string getName(); 74 75 virtual bool getLooping (); 76 virtual void setLooping ( bool loops ); 77 virtual bool isPlaying(); 78 virtual void setLocation (const CVector3D& position); 79 virtual void fadeAndDelete ( double fadeTime ); 80 81 protected: 82 83 void setNameFromPath( char* fileLoc ); 84 void resetFade(); 85 bool handleFade(); 86 87 88 }; 89 90 91 92 93 94 95 #endif -
source/soundmanager/items/CStreamItem.cpp
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 #include "precompiled.h" 18 19 #include "CStreamItem.h" 20 #include "soundmanager/data/COggData.h" 21 22 #include <iostream> 23 24 CStreamItem::CStreamItem(CSoundData* sndData) 25 { 26 resetVars(); 27 if ( initOpenAL() ) 28 attach( sndData ); 29 } 30 31 CStreamItem::~CStreamItem() 32 { 33 stop(); 34 35 int num_processed; 36 alGetSourcei( mALSource, AL_BUFFERS_PROCESSED, &num_processed); 37 38 if (num_processed > 0) 39 { 40 ALuint* al_buf = new ALuint[num_processed]; 41 alSourceUnqueueBuffers(mALSource, num_processed, al_buf); 42 delete[] al_buf; 43 } 44 } 45 46 bool CStreamItem::idleTask() 47 { 48 handleFade(); 49 50 int proc_state; 51 alGetSourceiv( mALSource, AL_SOURCE_STATE, &proc_state); 52 53 if ( proc_state == AL_STOPPED ) { 54 if ( mLastPlay ) 55 return ( proc_state != AL_STOPPED ); 56 } 57 else { 58 COggData* tmp = (COggData*)mSoundData; 59 60 if ( ! tmp->isFileFinished() ) { 61 int num_processed; 62 alGetSourcei( mALSource, AL_BUFFERS_PROCESSED, &num_processed); 63 64 if (num_processed > 0) 65 { 66 ALuint* al_buf = new ALuint[num_processed]; 67 alSourceUnqueueBuffers(mALSource, num_processed, al_buf); 68 int didWrite = tmp->fetchDataIntoBuffer( num_processed, al_buf); 69 alSourceQueueBuffers( mALSource, didWrite, al_buf); 70 delete[] al_buf; 71 } 72 } 73 else if ( getLooping() ) 74 { 75 tmp->resetFile(); 76 } 77 } 78 return true; 79 } 80 81 void CStreamItem::attach( CSoundData* itemData ) 82 { 83 if ( itemData != NULL ) { 84 mSoundData = itemData->incrementCount(); 85 alSourceQueueBuffers(mALSource, mSoundData->getBufferCount(), (const ALuint *)mSoundData->getBufferPtr()); 86 } 87 } 88 89 void CStreamItem::setLooping( bool loops ) 90 { 91 mLooping = loops; 92 } 93 -
source/soundmanager/CSoundManager.cpp
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "precompiled.h" 19 20 #include "CSoundManager.h" 21 #include "soundmanager/items/CSoundItem.h" 22 #include "soundmanager/items/CBufferItem.h" 23 #include "soundmanager/items/CStreamItem.h" 24 #include "soundmanager/js/JAmbientSound.h" 25 #include "soundmanager/js/JMusicSound.h" 26 #include "soundmanager/js/JSound.h" 27 #include "soundmanager/data/CSoundData.h" 28 29 30 CSoundManager* g_SoundManager; 31 32 void CSoundManager::ScriptingInit() 33 { 34 JAmbientSound::ScriptingInit(); 35 JMusicSound::ScriptingInit(); 36 JSound::ScriptingInit(); 37 } 38 39 CSoundManager::CSoundManager() 40 { 41 mItems = new ItemsList; 42 mCurrentEnvirons = 0; 43 mCurrentTune = 0; 44 mGain = 1; 45 mMusicGain = 1; 46 mAmbientGain = 1; 47 mActionGain = 1; 48 mEnabled = true; 49 mBufferCount = 50; 50 mBufferSize = 65536; 51 52 alc_init(); 53 } 54 55 CSoundManager::~CSoundManager() 56 { 57 ItemsList::iterator lstr = mItems->begin(); 58 while ( lstr != mItems->end() ) { 59 (*lstr)->stop(); 60 delete *lstr; 61 lstr++; 62 } 63 64 alcDestroyContext( mContext ); 65 alcCloseDevice( mDevice ); 66 67 delete mItems; 68 mItems = 0L; 69 mCurrentEnvirons = 0; 70 mCurrentTune = 0; 71 } 72 73 74 Status CSoundManager::alc_init() 75 { 76 Status ret = INFO::OK; 77 78 mDevice = alcOpenDevice(NULL); 79 if(mDevice) 80 { 81 mContext = alcCreateContext(mDevice, 0); // no attrlist needed 82 if(mContext) 83 alcMakeContextCurrent(mContext); 84 } 85 86 // check if init succeeded. 87 // some OpenAL implementations don't indicate failure here correctly; 88 // we need to check if the device and context pointers are actually valid. 89 ALCenum err = alcGetError(mDevice); 90 if(err != ALC_NO_ERROR || !mDevice || !mContext) 91 { 92 #if OS_UNIX 93 ret = INFO::OK; 94 #else 95 ret = ERR::FAIL; 96 #endif 97 } 98 99 const char* dev_name = (const char*)alcGetString(mDevice, ALC_DEVICE_SPECIFIER); 100 wchar_t buf[200]; 101 swprintf(buf, ARRAY_SIZE(buf), L"SND| alc_init: success, using %hs\n", dev_name); 102 103 return ret; 104 } 105 void CSoundManager::setMemoryUsage( long bufferSize, int bufferCount ) 106 { 107 mBufferCount = bufferCount; 108 mBufferSize = bufferSize; 109 } 110 long CSoundManager::getBufferCount() 111 { 112 return mBufferCount; 113 } 114 long CSoundManager::getBufferSize() 115 { 116 return mBufferSize; 117 } 118 119 120 void CSoundManager::setMasterGain( float gain) 121 { 122 mGain = gain; 123 } 124 void CSoundManager::setMusicGain( float gain) 125 { 126 mMusicGain = gain; 127 } 128 void CSoundManager::setAmbientGain( float gain) 129 { 130 mAmbientGain = gain; 131 } 132 void CSoundManager::setActionGain( float gain) 133 { 134 mActionGain = gain; 135 } 136 137 138 ISoundItem* CSoundManager::loadItem( const VfsPath* itemPath ) 139 { 140 CSoundData* itemData = CSoundData::soundDataFromFile( itemPath ); 141 ISoundItem* answer = NULL; 142 143 if ( itemData != NULL ) { 144 if ( itemData->isOneShot() ) { 145 if ( itemData->getBufferCount() == 1 ) 146 answer = new CSoundItem( itemData ); 147 else 148 answer = new CBufferItem( itemData ); 149 } 150 else { 151 answer = new CStreamItem( itemData ); 152 } 153 154 if ( answer != NULL ) 155 mItems->push_back( answer ); 156 } 157 158 159 return answer; 160 } 161 162 unsigned long CSoundManager::count() 163 { 164 return mItems->size(); 165 } 166 167 void CSoundManager::idleTask() 168 { 169 if ( mItems ) 170 { 171 ItemsList::iterator lstr = mItems->begin(); 172 ItemsList deadItemList; 173 ItemsList* nextItemList = new ItemsList; 174 175 176 while ( lstr != mItems->end() ) { 177 if ( (*lstr)->idleTask() ) 178 nextItemList->push_back( *lstr ); 179 else 180 deadItemList.push_back( *lstr ); 181 lstr++; 182 } 183 delete mItems; 184 mItems = nextItemList; 185 186 ItemsList::iterator deadItems = deadItemList.begin(); 187 while ( deadItems != deadItemList.end() ) 188 { 189 delete *deadItems; 190 deadItems++; 191 } 192 } 193 if ( mCurrentTune ) 194 mCurrentTune->ensurePlay(); 195 if ( mCurrentEnvirons ) 196 mCurrentEnvirons->ensurePlay(); 197 } 198 199 void CSoundManager::deleteItem( long itemNum ) 200 { 201 ItemsList::iterator lstr = mItems->begin(); 202 lstr += itemNum; 203 204 delete *lstr; 205 206 mItems->erase( lstr ); 207 } 208 209 ISoundItem* CSoundManager::getSoundItem( unsigned long itemRow ) 210 { 211 return (*mItems)[itemRow]; 212 } 213 214 void CSoundManager::InitListener() 215 { 216 ALfloat listenerPos[]={0.0,0.0,0.0}; 217 ALfloat listenerVel[]={0.0,0.0,0.0}; 218 ALfloat listenerOri[]={0.0,0.0,-1.0, 0.0,1.0,0.0}; 219 220 alListenerfv(AL_POSITION,listenerPos); 221 alListenerfv(AL_VELOCITY,listenerVel); 222 alListenerfv(AL_ORIENTATION,listenerOri); 223 224 alDistanceModel(AL_EXPONENT_DISTANCE); 225 } 226 227 void CSoundManager::setEnabled( bool doEnable ) 228 { 229 mEnabled = doEnable; 230 } 231 232 void CSoundManager::playActionItem( ISoundItem* anItem ) 233 { 234 if ( anItem ) 235 { 236 if ( mEnabled && ( mActionGain > 0 ) ) { 237 anItem->setGain( mGain * mActionGain ); 238 anItem->play(); 239 } 240 } 241 } 242 void CSoundManager::playGroupItem( ISoundItem* anItem, ALfloat groupGain) 243 { 244 if ( anItem ) 245 { 246 if ( mEnabled && ( mActionGain > 0 ) ) { 247 anItem->setGain( mGain * groupGain ); 248 anItem->play(); 249 } 250 } 251 } 252 void CSoundManager::setMusicItem( ISoundItem* anItem ) 253 { 254 if ( mCurrentTune ) { 255 mCurrentTune->fadeAndDelete(3.00); 256 mCurrentTune = 0L; 257 } 258 idleTask(); 259 if ( anItem ) 260 { 261 if ( mEnabled && ( mMusicGain > 0 ) ) { 262 mCurrentTune = anItem; 263 mCurrentTune->setGain( 0 ); 264 mCurrentTune->playLoop(); 265 mCurrentTune->fadeToIn( mGain * mMusicGain, 3.00 ); 266 } 267 } 268 } 269 270 void CSoundManager::setAmbientItem( ISoundItem* anItem ) 271 { 272 if ( mCurrentEnvirons ) { 273 mCurrentEnvirons->fadeAndDelete(3.00); 274 mCurrentEnvirons = 0L; 275 } 276 idleTask(); 277 278 if ( anItem ) 279 { 280 if ( mEnabled && ( mAmbientGain > 0 ) ) { 281 mCurrentEnvirons = anItem; 282 mCurrentEnvirons->setGain( 0 ); 283 mCurrentEnvirons->playLoop(); 284 mCurrentEnvirons->fadeToIn( mGain * mAmbientGain, 3.00 ); 285 } 286 } 287 } 288 -
source/soundmanager/data/COggData.h
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 19 #ifndef SoundTester_COggData_h 20 #define SoundTester_COggData_h 21 22 #include "CSoundData.h" 23 #include "vorbis/vorbisfile.h" 24 #include "lib/external_libraries/openal.h" 25 26 class COggData : public CSoundData 27 { 28 ALuint mFormat; 29 long mFrequency; 30 31 public: 32 COggData (); 33 virtual ~COggData (); 34 35 virtual bool InitOggFile( const wchar_t* fileLoc ); 36 virtual bool isFileFinished(); 37 virtual bool isOneShot(); 38 39 virtual int fetchDataIntoBuffer( int count, ALuint* buffers); 40 virtual void resetFile(); 41 42 protected: 43 OggVorbis_File m_vf; 44 int m_current_section; 45 bool mFileFinished; 46 bool mOneShot; 47 ALuint mBuffer[100]; 48 int mBuffersUsed; 49 50 bool addDataBuffer( char* data, long length); 51 void setFormatAndFreq( int form, ALsizei freq); 52 ALsizei getBufferCount(); 53 ALuint getBuffer(); 54 ALuint* getBufferPtr(); 55 }; 56 57 58 59 #endif -
source/soundmanager/data/CSoundData.cpp
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "precompiled.h" 19 #include "CSoundData.h" 20 21 22 #include <iostream> 23 #include "COggData.h" 24 #include "ps/Filesystem.h" 25 #include "lib/file/vfs/vfs_util.h" 26 27 DataMap* CSoundData::sSoundData = NULL; 28 29 CSoundData::CSoundData() 30 { 31 initProperties(); 32 } 33 34 CSoundData::~CSoundData() 35 { 36 if ( mALBuffer != 0 ) 37 alDeleteBuffers( 1, &mALBuffer ); 38 } 39 40 void CSoundData::initProperties() 41 { 42 mALBuffer = 0; 43 mRetentionCount = 0; 44 } 45 46 void CSoundData::releaseSoundData( CSoundData* theData ) 47 { 48 DataMap::iterator itemFind; 49 50 if ( theData->decrementCount() ) { 51 if ( ( itemFind = CSoundData::sSoundData->find( theData->getFileName() ) ) != sSoundData->end() ) 52 { 53 CSoundData* dier = itemFind->second; 54 CSoundData::sSoundData->erase( itemFind ); 55 delete dier; 56 } 57 } 58 } 59 60 CSoundData* CSoundData::soundDataFromFile( const VfsPath* itemPath ) 61 { 62 if ( CSoundData::sSoundData == NULL ) 63 CSoundData::sSoundData = new DataMap; 64 65 Path fExt = itemPath->Extension(); 66 DataMap::iterator itemFind; 67 CSoundData* answer = NULL; 68 69 70 if ( ( itemFind = CSoundData::sSoundData->find( itemPath->string() ) ) != sSoundData->end() ) 71 { 72 // debug_printf(L"data found in cache at: %ls\n\n", itemPath.string().c_str()); 73 answer = itemFind->second; 74 } 75 else 76 { 77 if ( fExt == ".ogg" ) 78 answer = soundDataFromOgg( itemPath ); 79 80 if ( answer && answer->isOneShot() ) 81 (*CSoundData::sSoundData)[itemPath->string()] = answer; 82 83 } 84 return answer; 85 } 86 87 bool CSoundData::isOneShot() 88 { 89 return true; 90 } 91 92 93 CSoundData* CSoundData::soundDataFromOgg( const VfsPath* itemPath ) 94 { 95 CSoundData* answer = NULL; 96 COggData* oggAnswer = new COggData(); 97 98 OsPath realPath; 99 Status ret = g_VFS->GetRealPath( *itemPath, realPath); 100 if ( ret == INFO::OK ) { 101 if ( oggAnswer->InitOggFile( realPath.string().c_str() ) ) { 102 answer = oggAnswer; 103 } 104 } 105 return answer; 106 } 107 108 109 ALsizei CSoundData::getBufferCount() 110 { 111 return 1; 112 } 113 114 std::wstring CSoundData::getFileName() 115 { 116 return mFileName; 117 } 118 119 120 121 122 123 124 125 126 127 CSoundData* CSoundData::incrementCount() 128 { 129 mRetentionCount++; 130 return this; 131 } 132 133 bool CSoundData::decrementCount() 134 { 135 mRetentionCount--; 136 137 return ( mRetentionCount <= 0 ); 138 } 139 140 ALuint CSoundData::getBuffer() 141 { 142 return mALBuffer; 143 } 144 ALuint* CSoundData::getBufferPtr() 145 { 146 return &mALBuffer; 147 } 148 -
source/soundmanager/data/COggData.cpp
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 #include "precompiled.h" 18 19 20 #include "COggData.h" 21 22 23 #include <wchar.h> 24 #include <iostream> 25 #include "soundmanager/CSoundManager.h" 26 #include "lib/external_libraries/openal.h" 27 28 COggData::COggData() 29 { 30 mOneShot = false; 31 } 32 33 COggData::~COggData() 34 { 35 alDeleteBuffers( mBuffersUsed, mBuffer ); 36 ov_clear(&m_vf); 37 } 38 39 void COggData::setFormatAndFreq( int form, ALsizei freq) 40 { 41 mFormat = form; 42 mFrequency = freq; 43 } 44 45 bool COggData::InitOggFile( const wchar_t* fileLoc ) 46 { 47 int buffersToStart = g_SoundManager->getBufferCount(); 48 49 // fprintf(stderr, "ready to open ogg file at:%ls \r\r", fileLoc); 50 51 char nameH[300]; 52 sprintf( nameH, "%ls", fileLoc ); 53 54 FILE* f = fopen( nameH, "rb"); 55 m_current_section = 0; 56 int err = ov_open_callbacks(f, &m_vf, NULL, 0, OV_CALLBACKS_DEFAULT); 57 if ( err < 0) { 58 fprintf(stderr,"Input does not appear to be an Ogg bitstream :%d :%d.\n", err, ferror(f) ); 59 return false; 60 } 61 62 mFileName = std::wstring(fileLoc); 63 64 mFileFinished = false; 65 setFormatAndFreq( (m_vf.vi->channels == 1)? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16 , (ALsizei)m_vf.vi->rate ); 66 67 alGetError(); /* clear error */ 68 alGenBuffers( buffersToStart, mBuffer); 69 70 if(alGetError() != AL_NO_ERROR) 71 { 72 printf("- Error creating initial buffer !!\n"); 73 return false; 74 } 75 else 76 { 77 mBuffersUsed = fetchDataIntoBuffer( buffersToStart, mBuffer); 78 if ( mFileFinished ) { 79 mOneShot = true; 80 if ( mBuffersUsed < buffersToStart ) { 81 alDeleteBuffers( buffersToStart - mBuffersUsed, &mBuffer[mBuffersUsed] ); 82 } 83 } 84 } 85 return true; 86 } 87 88 ALsizei COggData::getBufferCount() 89 { 90 return mBuffersUsed; 91 } 92 93 bool COggData::isFileFinished() 94 { 95 return mFileFinished; 96 } 97 98 void COggData::resetFile() 99 { 100 ov_time_seek( &m_vf, 0 ); 101 m_current_section = 0; 102 mFileFinished = false; 103 } 104 105 bool COggData::isOneShot() 106 { 107 return mOneShot; 108 } 109 110 int COggData::fetchDataIntoBuffer( int count, ALuint* buffers) 111 { 112 long bufferSize = g_SoundManager->getBufferSize(); 113 114 char* pcmout = new char[bufferSize + 5000]; 115 int buffersWritten = 0; 116 117 for(int i = 0; ( i < count ) && !mFileFinished; i++) { 118 char* readDest = pcmout; 119 long totalRet = 0; 120 while (totalRet < bufferSize ) 121 { 122 long ret=ov_read(&m_vf,readDest, 4096,0,2,1, &m_current_section); 123 if (ret == 0) { 124 mFileFinished=true; 125 break; 126 } else if (ret < 0) { 127 /* error in the stream. Not a problem, just reporting it in 128 case we (the app) cares. In this case, we don't. */ 129 } else { 130 totalRet += ret; 131 readDest += ret; 132 } 133 } 134 if ( totalRet > 0 ) 135 { 136 buffersWritten++; 137 alBufferData( buffers[i], mFormat, pcmout, (ALsizei)totalRet, (int)mFrequency); 138 } 139 } 140 delete[] pcmout; 141 return buffersWritten; 142 } 143 144 145 ALuint COggData::getBuffer() 146 { 147 return mBuffer[0]; 148 } 149 ALuint* COggData::getBufferPtr() 150 { 151 return mBuffer; 152 } 153 154 155 156 157 -
source/soundmanager/data/CSoundData.h
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 19 #ifndef SoundTester_CSoundData_h 20 #define SoundTester_CSoundData_h 21 #include "lib/os_path.h" 22 23 #include "string" 24 #include "map" 25 #include "lib/external_libraries/openal.h" 26 #include "lib/file/vfs/vfs_path.h" 27 28 class CSoundData; 29 typedef std::map<std::wstring, CSoundData*> DataMap; 30 31 32 33 class CSoundData 34 { 35 public: 36 static CSoundData* soundDataFromFile( const VfsPath* itemPath ); 37 static CSoundData* soundDataFromOgg( const VfsPath* itemPath ); 38 39 static void releaseSoundData( CSoundData* theData ); 40 41 CSoundData (); 42 CSoundData (ALuint dataSource); 43 virtual ~CSoundData (); 44 45 CSoundData* incrementCount(); 46 bool decrementCount(); 47 void initProperties(); 48 virtual bool isOneShot(); 49 50 51 virtual ALuint getBuffer(); 52 virtual ALsizei getBufferCount(); 53 std::wstring getFileName(); 54 virtual ALuint* getBufferPtr(); 55 56 protected: 57 static DataMap* sSoundData; 58 59 ALuint mALBuffer; 60 int mRetentionCount; 61 std::wstring mFileName; 62 63 64 65 }; 66 67 68 69 70 71 72 #endif -
source/soundmanager/CSoundManager.h
1 /* Copyright (C) 2012 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 19 #ifndef SoundTester_CSoundManager_h 20 #define SoundTester_CSoundManager_h 21 22 #include "vector" 23 #include "map" 24 25 #include "soundmanager/items/ISoundItem.h" 26 #include "lib/file/vfs/vfs_path.h" 27 28 typedef std::vector<ISoundItem*> ItemsList; 29 30 31 class CSoundManager 32 { 33 protected: 34 35 ALuint mALEnvironment; 36 ALCcontext* mContext; 37 ALCdevice* mDevice; 38 ISoundItem* mCurrentTune; 39 ISoundItem* mCurrentEnvirons; 40 ItemsList* mItems; 41 float mGain; 42 float mMusicGain; 43 float mAmbientGain; 44 float mActionGain; 45 bool mEnabled; 46 long mBufferSize; 47 int mBufferCount; 48 49 public: 50 CSoundManager (); 51 virtual ~CSoundManager (); 52 53 ISoundItem* loadItem( const VfsPath* itemPath ); 54 55 static void ScriptingInit(); 56 57 float radiansOffCenter( const CVector3D& position, bool& onScreen ); 58 59 60 ISoundItem* itemFromWAV ( VfsPath& fname); 61 ISoundItem* itemFromOgg ( VfsPath& fname); 62 63 ISoundItem* getSoundItem ( unsigned long itemRow ); 64 unsigned long count (); 65 void idleTask (); 66 void deleteItem ( long itemNum ); 67 68 void setMemoryUsage( long bufferSize, int bufferCount ); 69 long getBufferCount(); 70 long getBufferSize(); 71 72 void setMusicItem( ISoundItem* anItem ); 73 void setAmbientItem( ISoundItem* anItem ); 74 void playActionItem( ISoundItem* anItem ); 75 void playGroupItem( ISoundItem* anItem, ALfloat groupGain); 76 77 void setMasterGain( float gain); 78 void setMusicGain( float gain); 79 void setAmbientGain( float gain); 80 void setActionGain( float gain); 81 82 void setEnabled( bool doEnable ); 83 protected: 84 void InitListener(); 85 virtual Status alc_init(); 86 87 }; 88 89 90 91 extern CSoundManager* g_SoundManager; 92 93 94 95 96 97 98 99 #endif -
source/soundmanager/js/JAmbientSound.cpp
1 /* Copyright (C) 2009 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 #include "precompiled.h" 18 19 #include "JAmbientSound.h" 20 #include "maths/Vector3D.h" 21 22 #include "lib/utf8.h" 23 #include "ps/Filesystem.h" 24 25 #include "soundmanager/CSoundManager.h" 26 27 JAmbientSound::JAmbientSound(const VfsPath& pathname) 28 { 29 mFileName = new VfsPath( pathname.string().c_str() ); 30 } 31 32 JAmbientSound::~JAmbientSound() 33 { 34 } 35 36 37 // start playing the sound, all ambient sounds loop 38 bool JAmbientSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 39 { 40 ISoundItem* aSnd = g_SoundManager->loadItem( mFileName ); 41 42 aSnd->playAsAmbient(); 43 44 return true; 45 } 46 47 // start playing the sound, all ambient sounds loop 48 bool JAmbientSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 49 { 50 ISoundItem* aSnd = g_SoundManager->loadItem( mFileName ); 51 52 aSnd->playAsAmbient(); 53 return true; 54 } 55 bool JAmbientSound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 56 { 57 g_SoundManager->setAmbientItem( 0L ); 58 59 return true; 60 } 61 62 // Script-bound functions 63 64 65 void JAmbientSound::ScriptingInit() 66 { 67 AddMethod<CStr, &JAmbientSound::ToString>("toString", 0); 68 AddMethod<bool, &JAmbientSound::Play>("play", 0); 69 AddMethod<bool, &JAmbientSound::Loop>("loop", 0); 70 AddMethod<bool, &JAmbientSound::Free>("free", 0); 71 72 CJSObject<JAmbientSound>::ScriptingInit("AmbientSound", &JAmbientSound::Construct, 1); 73 } 74 75 CStr JAmbientSound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 76 { 77 std::ostringstream stringStream; 78 stringStream << "[object AmbientSound: "; 79 stringStream << mFileName->string().c_str(); 80 81 return stringStream.str(); 82 } 83 84 JSBool JAmbientSound::Construct(JSContext* cx, uintN UNUSED(argc), jsval* vp) 85 { 86 // JSU_REQUIRE_MIN_PARAMS(1); 87 88 CStrW filename; 89 if (! ToPrimitive<CStrW>(cx, JS_ARGV(cx, vp)[0], filename)) 90 return JS_FALSE; 91 92 JAmbientSound* newObject = new JAmbientSound(filename); 93 newObject->m_EngineOwned = false; 94 95 JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); 96 97 return JS_TRUE; 98 } -
source/soundmanager/js/JSound.cpp
1 /* Copyright (C) 2009 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 #include "precompiled.h" 18 #include "JSound.h" 19 #include "maths/Vector3D.h" 20 21 #include "lib/utf8.h" 22 #include "ps/Filesystem.h" 23 24 #include "soundmanager/CSoundManager.h" 25 26 27 JSound::JSound(const VfsPath& pathname) 28 { 29 mSndItem = g_SoundManager->loadItem( &pathname ); 30 } 31 32 JSound::~JSound() 33 { 34 if ( mSndItem ) { 35 mSndItem->fadeAndDelete(0.2); 36 mSndItem = 0; 37 } 38 } 39 40 bool JSound::clearSoundItem() 41 { 42 mSndItem = 0L; 43 return true; 44 } 45 46 bool JSound::SetGain(JSContext* cx, uintN UNUSED(argc), jsval* argv) 47 { 48 if (! mSndItem ) 49 return false; 50 51 float gain; 52 if (! ToPrimitive<float>(cx, argv[0], gain)) 53 return false; 54 55 mSndItem->setGain( gain ); 56 return true; 57 } 58 59 bool JSound::SetPitch(JSContext* cx, uintN UNUSED(argc), jsval* argv) 60 { 61 if (! mSndItem ) 62 return false; 63 64 float pitch; 65 if (! ToPrimitive<float>(cx, argv[0], pitch)) 66 return false; 67 68 mSndItem->setPitch( pitch ); 69 return true; 70 } 71 72 bool JSound::SetPosition(JSContext* cx, uintN argc, jsval* argv) 73 { 74 if (! mSndItem ) 75 return false; 76 77 ENSURE(argc >= 1); // FIXME 78 79 CVector3D pos; 80 // absolute world coords 81 if (!ToPrimitive<CVector3D>(cx, argv[0], pos)) 82 return false; 83 84 mSndItem->setLocation( pos ); 85 86 return true; 87 } 88 89 90 bool JSound::Fade(JSContext* cx, uintN UNUSED(argc), jsval* argv) 91 { 92 if (! mSndItem ) 93 return false; 94 95 // ENSURE(argc >= 3); // FIXME 96 float initial_gain, final_gain; 97 float length; 98 if (! (ToPrimitive<float>(cx, argv[0], initial_gain) 99 && ToPrimitive<float>(cx, argv[1], final_gain) 100 && ToPrimitive<float>(cx, argv[2], length))) 101 return false; 102 103 mSndItem->setGain( initial_gain ); 104 mSndItem->fadeToIn( final_gain, length ); 105 106 return true; 107 } 108 109 bool JSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 110 { 111 if (! mSndItem ) 112 return false; 113 114 mSndItem->play(); 115 116 return true; 117 } 118 119 bool JSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 120 { 121 if (! mSndItem ) 122 return false; 123 124 mSndItem->playLoop(); 125 126 return true; 127 } 128 129 bool JSound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 130 { 131 if ( mSndItem ) { 132 mSndItem->fadeAndDelete(0.2); 133 mSndItem = 0; 134 } 135 136 return true; 137 } 138 139 void JSound::ScriptingInit() 140 { 141 AddMethod<CStr, &JSound::ToString>("toString", 0); 142 AddMethod<bool, &JSound::Play>("play", 0); 143 AddMethod<bool, &JSound::Loop>("loop", 0); 144 AddMethod<bool, &JSound::Free>("free", 0); 145 AddMethod<bool, &JSound::SetGain>("setGain", 0); 146 AddMethod<bool, &JSound::SetPitch>("setPitch", 0); 147 AddMethod<bool, &JSound::SetPosition>("setPosition", 0); 148 AddMethod<bool, &JSound::Fade>("fade", 0); 149 150 CJSObject<JSound>::ScriptingInit("Sound", &JSound::Construct, 1); 151 } 152 153 CStr JSound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 154 { 155 return "[object Sound: " + ( mSndItem ? mSndItem->getName() : "(null)" ) + "]"; 156 } 157 158 JSBool JSound::Construct(JSContext* cx, uintN UNUSED(argc), jsval* vp) 159 { 160 // JSU_REQUIRE_MIN_PARAMS(1); 161 162 CStrW filename; 163 if (! ToPrimitive<CStrW>(cx, JS_ARGV(cx, vp)[0], filename)) 164 return JS_FALSE; 165 166 JSound* newObject = new JSound(filename); 167 newObject->m_EngineOwned = false; 168 JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); 169 170 return JS_TRUE; 171 } -
source/soundmanager/js/JMusicSound.cpp
1 /* Copyright (C) 2009 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 #include "precompiled.h" 18 19 #include "JMusicSound.h" 20 #include "maths/Vector3D.h" 21 22 #include "lib/utf8.h" 23 #include "ps/Filesystem.h" 24 25 #include "soundmanager/CSoundManager.h" 26 27 28 JMusicSound::JMusicSound(const VfsPath& pathname) 29 { 30 mFileName = new VfsPath( pathname.string().c_str() ); 31 } 32 33 JMusicSound::~JMusicSound() 34 { 35 delete mFileName; 36 } 37 38 bool JMusicSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 39 { 40 ISoundItem* aSnd = g_SoundManager->loadItem( mFileName ); 41 aSnd->playAsMusic(); 42 43 return true; 44 } 45 46 // request the sound be played until free() is called. returns immediately. 47 bool JMusicSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 48 { 49 ISoundItem* aSnd = g_SoundManager->loadItem( mFileName ); 50 aSnd->playAsMusic(); 51 52 return true; 53 } 54 55 void JMusicSound::ScriptingInit() 56 { 57 AddMethod<CStr, &JMusicSound::ToString>("toString", 0); 58 AddMethod<bool, &JMusicSound::Play>("play", 0); 59 AddMethod<bool, &JMusicSound::Loop>("loop", 0); 60 61 CJSObject<JMusicSound>::ScriptingInit("MusicSound", &JMusicSound::Construct, 1); 62 } 63 64 CStr JMusicSound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 65 { 66 std::ostringstream stringStream; 67 stringStream << "[object MusicSound: "; 68 stringStream << mFileName->string().c_str(); 69 70 return stringStream.str(); 71 } 72 73 JSBool JMusicSound::Construct(JSContext* cx, uintN UNUSED(argc), jsval* vp) 74 { 75 // JSU_REQUIRE_MIN_PARAMS(1); 76 77 CStrW filename; 78 if (! ToPrimitive<CStrW>(cx, JS_ARGV(cx, vp)[0], filename)) 79 return JS_FALSE; 80 81 JMusicSound* newObject = new JMusicSound(filename); 82 newObject->m_EngineOwned = false; 83 JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); 84 85 return JS_TRUE; 86 } -
source/soundmanager/js/SMSoundGroup.cpp
1 /* Copyright (C) 2010 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 /** 19 * ========================================================================= 20 * File : SoundGroup.cpp 21 * Project : 0 A.D. 22 * Description : Loads up a group of sound files with shared properties, 23 * and provides a simple interface for playing them. 24 * ========================================================================= 25 */ 26 27 #include "precompiled.h" 28 #include "soundmanager/CSoundManager.h" 29 #include "SMSoundGroup.h" 30 31 #include <algorithm> 32 33 #include "lib/rand.h" 34 35 #include "ps/XML/Xeromyces.h" 36 #include "ps/CLogger.h" 37 #include "ps/Filesystem.h" 38 #include "ps/Util.h" 39 #include "ps/Game.h" 40 #include "ps/CLogger.h" 41 #include "graphics/GameView.h" 42 #include "graphics/Camera.h" 43 #include "soundmanager/items/ISoundItem.h" 44 45 extern CGame *g_Game; 46 47 #define PI 3.14126f 48 49 50 static const bool DISABLE_INTENSITY = true; // disable for now since it's broken 51 52 void CSMSoundGroup::SetGain(float gain) 53 { 54 gain = std::min(gain, 1.0f); 55 m_Gain = gain; 56 } 57 58 void CSMSoundGroup::SetDefaultValues() 59 { 60 m_index = 0; 61 m_Flags = 0; 62 m_Intensity = 0; 63 m_CurTime = 0.0f; 64 65 // sane defaults; will probably be replaced by the values read during LoadSoundGroup. 66 SetGain(0.7f); 67 m_Pitch = 1.0f; 68 m_Priority = 60; 69 m_PitchUpper = 1.1f; 70 m_PitchLower = 0.9f; 71 m_GainUpper = 1.0f; 72 m_GainLower = 0.8f; 73 m_ConeOuterGain = 0.0f; 74 m_ConeInnerAngle = 360.0f; 75 m_ConeOuterAngle = 360.0f; 76 m_Decay = 3.0f; 77 m_IntensityThreshold = 3; 78 // WARNING: m_TimeWindow is currently unused and uninitialized 79 } 80 81 CSMSoundGroup::CSMSoundGroup() 82 { 83 SetDefaultValues(); 84 } 85 86 CSMSoundGroup::CSMSoundGroup(const VfsPath& pathnameXML) 87 { 88 SetDefaultValues(); 89 LoadSoundGroup(pathnameXML); 90 } 91 92 CSMSoundGroup::~CSMSoundGroup() 93 { 94 // clean up all the handles from this group. 95 ReleaseGroup(); 96 } 97 98 static float RandFloat(float min, float max) 99 { 100 return float(rand(min*100.0f, max*100.0f) / 100.0f); 101 } 102 103 float CSMSoundGroup::radiansOffCenter( const CVector3D& position, bool& onScreen, float& itemRollOff ) 104 { 105 float x, y; 106 float answer = 0.0; 107 const size_t screenWidth = g_Game->GetView()->GetCamera()->GetViewPort().m_Width; 108 const size_t screenHeight = g_Game->GetView()->GetCamera()->GetViewPort().m_Height; 109 float bufferSize = screenWidth * 0.10; 110 const size_t audioWidth = screenWidth; 111 float radianCap = PI / 3; 112 113 g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, x, y); 114 115 onScreen = true; 116 117 if ( x < -bufferSize ){ 118 onScreen = false; 119 answer = -radianCap; 120 } 121 else if ( x > screenWidth + bufferSize ) { 122 onScreen = false; 123 answer = radianCap; 124 } 125 else { 126 if ( ( x < 0 ) || ( x > screenWidth ) ) { 127 itemRollOff = 0.5; 128 } 129 float pixPerRadian = audioWidth / ( radianCap * 2 ); 130 answer = ( x - (screenWidth/2) ) / pixPerRadian; 131 } 132 133 if ( y < -bufferSize ){ 134 onScreen = false; 135 } 136 else if ( y > screenHeight + bufferSize ) { 137 onScreen = false; 138 } 139 else { 140 if ( ( y < 0 ) || ( y > screenHeight ) ) { 141 itemRollOff = 0.5; 142 } 143 } 144 145 146 // debug_printf(L"do play at x portion:%f pts x:%f, y=%f at radians=%f\n\n", answer, x, y, answer ); 147 148 return answer; 149 } 150 151 void CSMSoundGroup::UploadPropertiesAndPlay(int theIndex, const CVector3D& position) 152 { 153 bool isOnscreen; 154 ALfloat itemRollOff = 0.02f; 155 156 float offSet = radiansOffCenter( position, isOnscreen, itemRollOff); 157 158 if ( isOnscreen || TestFlag(eDistanceless) || TestFlag(eOmnipresent) ) { 159 if ( snd_group.size() == 0 ) 160 Reload(); 161 162 ISoundItem* hSound = snd_group[theIndex]; 163 CVector3D origin = g_Game->GetView()->GetCamera()->GetOrientation().GetTranslation(); 164 float sndDist = origin.Y; 165 166 if(!TestFlag(eOmnipresent)) { 167 hSound->setLocation( CVector3D( (sndDist * sin(offSet)), 0, sndDist * cos(offSet) )); 168 hSound->setRollOff( itemRollOff ); 169 } 170 171 if( TestFlag(eRandPitch) ) 172 hSound->setPitch( RandFloat( m_PitchLower, m_PitchUpper ) ); 173 else 174 hSound->setPitch( m_Pitch ); 175 176 ALfloat theGain = m_Gain; 177 if( TestFlag(eRandGain) ) 178 theGain = RandFloat( m_GainLower, m_GainUpper); 179 180 hSound->setCone( m_ConeInnerAngle, m_ConeOuterAngle, m_ConeOuterGain); 181 182 g_SoundManager->playGroupItem( hSound, theGain ); 183 } 184 } 185 186 187 static void HandleError(const std::wstring& message, const VfsPath& pathname, Status err) 188 { 189 if(err == ERR::AGAIN) 190 return; // open failed because sound is disabled (don't log this) 191 LOGERROR(L"%ls: pathname=%ls, error=%ls", message.c_str(), pathname.string().c_str(), ErrorString(err)); 192 } 193 194 void CSMSoundGroup::PlayNext(const CVector3D& position) 195 { 196 // if no sounds, return 197 if (filenames.size() == 0) 198 return; 199 200 m_index = (size_t)rand(0, (size_t)filenames.size()); 201 UploadPropertiesAndPlay( m_index, position); 202 } 203 204 void CSMSoundGroup::Reload() 205 { 206 m_index = 0; // reset our index 207 208 snd_group.clear(); 209 210 for(size_t i = 0; i < filenames.size(); i++) 211 { 212 VfsPath thePath = m_filepath/filenames[i]; 213 ISoundItem* temp = g_SoundManager->loadItem( &thePath ); 214 215 if ( temp == NULL ) 216 HandleError( L"error loading sound", thePath, NULL); 217 else 218 snd_group.push_back(temp); 219 } 220 221 if(TestFlag(eRandOrder)) 222 random_shuffle(snd_group.begin(), snd_group.end()); 223 } 224 225 void CSMSoundGroup::ReleaseGroup() 226 { 227 for(size_t i = 0; i < snd_group.size(); i++) 228 { 229 snd_group[i]->fadeAndDelete(0.2); 230 } 231 snd_group.clear(); 232 } 233 234 void CSMSoundGroup::Update(float UNUSED(TimeSinceLastFrame)) 235 { 236 } 237 238 bool CSMSoundGroup::LoadSoundGroup(const VfsPath& pathnameXML) 239 { 240 // LOGERROR(L"loading new sound group '%ls'", pathnameXML.string().c_str()); 241 242 CXeromyces XeroFile; 243 if (XeroFile.Load(g_VFS, pathnameXML) != PSRETURN_OK) { 244 HandleError( L"error loading file", pathnameXML, NULL); 245 return false; 246 } 247 // Define elements used in XML file 248 #define EL(x) int el_##x = XeroFile.GetElementID(#x) 249 #define AT(x) int at_##x = XeroFile.GetAttributeID(#x) 250 EL(soundgroup); 251 EL(gain); 252 EL(looping); 253 EL(omnipresent); 254 EL(pitch); 255 EL(priority); 256 EL(randorder); 257 EL(randgain); 258 EL(randpitch); 259 EL(conegain); 260 EL(coneinner); 261 EL(coneouter); 262 EL(sound); 263 EL(gainupper); 264 EL(gainlower); 265 EL(pitchupper); 266 EL(pitchlower); 267 EL(path); 268 EL(threshold); 269 EL(decay); 270 #undef AT 271 #undef EL 272 273 XMBElement root = XeroFile.GetRoot(); 274 275 if (root.GetNodeName() != el_soundgroup) 276 { 277 LOGERROR(L"Invalid SoundGroup format (unrecognised root element '%hs')", XeroFile.GetElementString(root.GetNodeName()).c_str()); 278 return false; 279 } 280 281 XERO_ITER_EL(root, child) 282 { 283 284 int child_name = child.GetNodeName(); 285 286 if(child_name == el_gain) 287 { 288 SetGain(child.GetText().ToFloat()); 289 } 290 else if(child_name == el_looping) 291 { 292 if(child.GetText().ToInt() == 1) 293 SetFlag(eLoop); 294 } 295 else if(child_name == el_omnipresent) 296 { 297 if(child.GetText().ToInt() == 1) 298 SetFlag(eOmnipresent); 299 } 300 else if(child_name == el_pitch) 301 { 302 this->m_Pitch = child.GetText().ToFloat(); 303 } 304 else if(child_name == el_priority) 305 { 306 this->m_Priority = child.GetText().ToFloat(); 307 } 308 else if(child_name == el_randorder) 309 { 310 if(child.GetText().ToInt() == 1) 311 SetFlag(eRandOrder); 312 } 313 else if(child_name == el_randgain) 314 { 315 if(child.GetText().ToInt() == 1) 316 SetFlag(eRandGain); 317 } 318 else if(child_name == el_gainupper) 319 { 320 this->m_GainUpper = child.GetText().ToFloat(); 321 } 322 else if(child_name == el_gainlower) 323 { 324 this->m_GainLower = child.GetText().ToFloat(); 325 } 326 else if(child_name == el_randpitch) 327 { 328 if(child.GetText().ToInt() == 1) 329 SetFlag(eRandPitch); 330 } 331 else if(child_name == el_pitchupper) 332 { 333 this->m_PitchUpper = child.GetText().ToFloat(); 334 } 335 else if(child_name == el_pitchlower) 336 { 337 this->m_PitchLower = child.GetText().ToFloat(); 338 } 339 else if(child_name == el_conegain) 340 { 341 this->m_ConeOuterGain = child.GetText().ToFloat(); 342 } 343 else if(child_name == el_coneinner) 344 { 345 this->m_ConeInnerAngle = child.GetText().ToFloat(); 346 } 347 else if(child_name == el_coneouter) 348 { 349 this->m_ConeOuterAngle = child.GetText().ToFloat(); 350 } 351 else if(child_name == el_sound) 352 { 353 this->filenames.push_back(child.GetText().FromUTF8()); 354 } 355 else if(child_name == el_path) 356 { 357 m_filepath = child.GetText().FromUTF8(); 358 } 359 else if(child_name == el_threshold) 360 { 361 m_IntensityThreshold = child.GetText().ToFloat(); 362 } 363 else if(child_name == el_decay) 364 { 365 m_Decay = child.GetText().ToFloat(); 366 } 367 } 368 369 return true; 370 } -
source/soundmanager/js/JAmbientSound.h
1 /* Copyright (C) 2009 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef INCLUDED_JAMBIENTSOUND 19 #define INCLUDED_JAMBIENTSOUND 20 21 #include "scripting/ScriptableObject.h" 22 #include "soundmanager/items/ISoundItem.h" 23 24 class JAmbientSound : public CJSObject<JAmbientSound> 25 { 26 public: 27 JAmbientSound (const VfsPath& pathname); 28 virtual ~JAmbientSound (); 29 30 31 CStr ToString(JSContext* cx, uintN argc, jsval* argv); 32 33 bool Play(JSContext* cx, uintN argc, jsval* argv); 34 35 bool Loop(JSContext* cx, uintN argc, jsval* argv); 36 bool Free(JSContext* cx, uintN argc, jsval* argv); 37 38 bool SetGain(JSContext* cx, uintN argc, jsval* argv); 39 bool SetPitch(JSContext* cx, uintN argc, jsval* argv); 40 bool Fade(JSContext* cx, uintN argc, jsval* argv); 41 42 static JSBool Construct(JSContext* cx, uintN argc, jsval* vp); 43 void clearSoundItem(); 44 static void ScriptingInit(); 45 protected: 46 47 VfsPath* mFileName; 48 49 }; 50 51 #endif // #ifndef INCLUDED_JAMBIENTSOUND -
source/soundmanager/js/JSound.h
1 /* Copyright (C) 2009 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 // JS sound binding 19 20 // interface rationale: 21 // - can't just expose fire and forget playSound to script code: 22 // we sometimes need to loop until a certain condition is met 23 // (e.g. building is complete) => need means of access (Handle) to sound. 24 // 25 // - the current 64-bit Handle can't be stored as-is by JS code; 26 // we could make it 32 bit, but that limits its usefulness 27 // (barely enough tag bits). 28 // 29 // - instead, we provide a thin class wrapper (using scriptableobject.h) 30 // on top of the snd API that encapsulates the Handle. 31 32 #ifndef INCLUDED_JSOUND 33 #define INCLUDED_JSOUND 34 35 #include "scripting/ScriptableObject.h" 36 #include "soundmanager/items/ISoundItem.h" 37 38 class JSound : public CJSObject<JSound> 39 { 40 public: 41 42 // note: filename is stored by handle manager; no need to keep a copy here. 43 44 JSound(const VfsPath& pathname); 45 virtual ~JSound(); 46 47 CStr ToString(JSContext* cx, uintN argc, jsval* argv); 48 49 bool Play(JSContext* cx, uintN argc, jsval* argv); 50 bool Loop(JSContext* cx, uintN argc, jsval* argv); 51 52 bool Free(JSContext* cx, uintN argc, jsval* argv); 53 bool SetGain(JSContext* cx, uintN argc, jsval* argv); 54 bool SetPitch(JSContext* cx, uintN argc, jsval* argv); 55 bool SetPosition(JSContext* cx, uintN argc, jsval* argv); 56 bool clearSoundItem(); 57 58 bool Fade(JSContext* cx, uintN argc, jsval* argv); 59 60 static JSBool Construct(JSContext* cx, uintN argc, jsval* vp); 61 static void ScriptingInit(); 62 63 protected: 64 ISoundItem* mSndItem; 65 }; 66 67 #endif // #ifndef INCLUDED_JSOUND -
source/soundmanager/js/JMusicSound.h
1 /* Copyright (C) 2009 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 // JS sound binding 19 20 // interface rationale: 21 // - can't just expose fire and forget playSound to script code: 22 // we sometimes need to loop until a certain condition is met 23 // (e.g. building is complete) => need means of access (Handle) to sound. 24 // 25 // - the current 64-bit Handle can't be stored as-is by JS code; 26 // we could make it 32 bit, but that limits its usefulness 27 // (barely enough tag bits). 28 // 29 // - instead, we provide a thin class wrapper (using scriptableobject.h) 30 // on top of the snd API that encapsulates the Handle. 31 32 #ifndef INCLUDED_JMUSICSOUND 33 #define INCLUDED_JMUSICSOUND 34 35 #include "scripting/ScriptableObject.h" 36 #include "soundmanager/items/ISoundItem.h" 37 38 class JMusicSound : public CJSObject<JMusicSound> 39 { 40 public: 41 JMusicSound(const VfsPath& pathname); 42 virtual ~JMusicSound(); 43 44 // Script-bound functions 45 46 CStr ToString(JSContext* cx, uintN argc, jsval* argv); 47 48 bool Play(JSContext* cx, uintN argc, jsval* argv); 49 bool Loop(JSContext* cx, uintN argc, jsval* argv); 50 51 static JSBool Construct(JSContext* cx, uintN argc, jsval* vp); 52 53 static void ScriptingInit(); 54 55 protected: 56 VfsPath* mFileName; 57 }; 58 59 #endif // #ifndef INCLUDED_JMUSICSOUND -
source/soundmanager/js/SMSoundGroup.h
1 /* Copyright (C) 2009 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 /** 19 * ========================================================================= 20 * File : SoundGroup.h 21 * Project : 0 A.D. 22 * Description : Loads up a group of sound files with shared properties, 23 * and provides a simple interface for playing them. 24 * ========================================================================= 25 */ 26 27 /* 28 Example usage: 29 30 31 Example SoundGroup.xml 32 <?xml version="1.0" encoding="utf-8"?> 33 <SoundGroup> 34 <Gain>1.0</Gain> 35 <Looping>0</Looping> 36 <Pitch>1.0</Pitch> 37 <Priority>100</Priority> 38 <RandOrder>0</RandOrder> 39 <RandGain>0</RandGain> 40 <RandPitch>0</RandPitch> 41 <ConeGain>1.0</ConeGain> 42 <ConeInner>360</ConeInner> 43 <ConeOuter>360</ConeOuter> 44 <Sound>audio/voice/hellenes/soldier/Attack_Attackx.ogg</Sound> 45 <Sound>audio/voice/hellenes/soldier/Attack_Chargex.ogg</Sound> 46 <Sound>audio/voice/hellenes/soldier/Attack_Engagex.ogg</Sound> 47 <Sound>audio/voice/hellenes/soldier/Attack_ForMyFamily.ogg</Sound> 48 </SoundGroup> 49 50 */ 51 52 #ifndef INCLUDED_SMSOUNDGROUP 53 #define INCLUDED_SMSOUNDGROUP 54 55 #include "lib/file/vfs/vfs_path.h" 56 57 #include <vector> 58 59 class ISoundItem; 60 61 enum eSndGrpFlags 62 { 63 eRandOrder = 0x01, 64 eRandGain = 0x02, 65 eRandPitch = 0x04, 66 eLoop = 0x08, 67 eOmnipresent = 0x10, 68 eDistanceless = 0x20 69 }; 70 71 72 class CSMSoundGroup 73 { 74 NONCOPYABLE(CSMSoundGroup); 75 public: 76 CSMSoundGroup(const VfsPath& pathnameXML); 77 CSMSoundGroup(void); 78 ~CSMSoundGroup(void); 79 80 // Play next sound in group 81 // @param position world position of the entity generating the sound 82 // (ignored if the eOmnipresent flag is set) 83 void PlayNext(const CVector3D& position); 84 85 float radiansOffCenter( const CVector3D& position, bool& onScreen, float& itemRollOff ); 86 87 // Load a group 88 bool LoadSoundGroup(const VfsPath& pathnameXML); 89 90 void Reload(); 91 92 // Release all remaining loaded handles 93 void ReleaseGroup(); 94 95 // Update SoundGroup, remove dead sounds from intensity count 96 void Update(float TimeSinceLastFrame); 97 98 // Set a flag using a value from eSndGrpFlags 99 inline void SetFlag(int flag) { m_Flags = (unsigned char)(m_Flags | flag); } 100 101 // Test flag, returns true if flag is set. 102 inline bool TestFlag(int flag) { return (m_Flags & flag) != 0; } 103 104 private: 105 void SetGain(float gain); 106 void UploadPropertiesAndPlay(int theIndex, const CVector3D& position); 107 void SetDefaultValues(); 108 109 size_t m_index; // index of the next sound to play 110 111 std::vector<ISoundItem*> snd_group; // we store the handles so we can load now and play later 112 std::vector<std::wstring> filenames; // we need the filenames so we can reload when necessary. 113 114 VfsPath m_filepath; // the file path for the list of sound file resources 115 116 float m_CurTime; // Time elapsed since soundgroup was created 117 float m_TimeWindow; // The Intensity Threshold Window 118 size_t m_IntensityThreshold; // the allowable intensity before a sound switch 119 size_t m_Intensity; // our current intensity(number of sounds played since m_CurTime - m_TimeWindow) 120 float m_Decay; // 121 unsigned char m_Flags; // up to eight individual parameters, use with eSndGrpFlags. 122 123 float m_Gain; 124 float m_Pitch; 125 float m_Priority; 126 float m_ConeOuterGain; 127 float m_PitchUpper; 128 float m_PitchLower; 129 float m_GainUpper; 130 float m_GainLower; 131 float m_ConeInnerAngle; 132 float m_ConeOuterAngle; 133 }; 134 135 #endif //#ifndef INCLUDED_SOUNDGROUP -
source/main.cpp
383 383 // coincide in position and orientation. 384 384 float down[3] = { -up[0], -up[1], -up[2] }; 385 385 386 {387 PROFILE3("sound update");388 if (snd_update(pos, dir, down) < 0)389 debug_printf(L"snd_update failed\n");390 }386 // { 387 // PROFILE3("sound update"); 388 // if (snd_update(pos, dir, down) < 0) 389 // debug_printf(L"snd_update failed\n"); 390 // } 391 391 } 392 392 else 393 393 { 394 PROFILE3("sound update (0)");395 if (snd_update(0, 0, 0) < 0)396 debug_printf(L"snd_update (pos=0 version) failed\n");394 // PROFILE3("sound update (0)"); 395 // if (snd_update(0, 0, 0) < 0) 396 // debug_printf(L"snd_update (pos=0 version) failed\n"); 397 397 } 398 398 399 399 // Immediately flush any messages produced by simulation code … … 477 477 // run non-visual simulation replay if requested 478 478 if (args.Has("replay")) 479 479 { 480 snd_disable(true);481 482 480 Paths paths(args); 483 481 g_VFS = CreateVfs(20 * MiB); 484 482 g_VFS->Mount(L"cache/", paths.Cache(), VFS_MOUNT_ARCHIVABLE); -
source/simulation2/components/CCmpSoundManager.cpp
24 24 #include "simulation2/MessageTypes.h" 25 25 #include "simulation2/components/ICmpPosition.h" 26 26 #include "simulation2/components/ICmpRangeManager.h" 27 #include "sound /SoundGroup.h"27 #include "soundmanager/js/SMSoundGroup.h" 28 28 29 29 class CCmpSoundManager : public ICmpSoundManager 30 30 { … … 36 36 37 37 DEFAULT_COMPONENT_ALLOCATOR(SoundManager) 38 38 39 std::map<std::wstring, CS oundGroup*> m_SoundGroups;39 std::map<std::wstring, CSMSoundGroup*> m_SoundGroups; 40 40 41 41 static std::string GetSchema() 42 42 { … … 49 49 50 50 virtual void Deinit() 51 51 { 52 for (std::map<std::wstring, CS oundGroup*>::iterator it = m_SoundGroups.begin(); it != m_SoundGroups.end(); ++it)52 for (std::map<std::wstring, CSMSoundGroup*>::iterator it = m_SoundGroups.begin(); it != m_SoundGroups.end(); ++it) 53 53 delete it->second; 54 54 m_SoundGroups.clear(); 55 55 } … … 76 76 // or on some other timer? 77 77 const CMessageUpdate& msgData = static_cast<const CMessageUpdate&> (msg); 78 78 float t = msgData.turnLength.ToFloat(); 79 for (std::map<std::wstring, CS oundGroup*>::iterator it = m_SoundGroups.begin(); it != m_SoundGroups.end(); ++it)79 for (std::map<std::wstring, CSMSoundGroup*>::iterator it = m_SoundGroups.begin(); it != m_SoundGroups.end(); ++it) 80 80 if (it->second) 81 81 it->second->Update(t); 82 82 break; … … 87 87 virtual void PlaySoundGroup(std::wstring name, entity_id_t source) 88 88 { 89 89 // Make sure the sound group is loaded 90 CS oundGroup* group;90 CSMSoundGroup* group; 91 91 if (m_SoundGroups.find(name) == m_SoundGroups.end()) 92 92 { 93 group = new CS oundGroup();93 group = new CSMSoundGroup(); 94 94 if (!group->LoadSoundGroup(L"audio/" + name)) 95 95 { 96 96 LOGERROR(L"Failed to load sound group '%ls'", name.c_str()); -
binaries/data/mods/public/gui/session/session.js
408 408 // currentAmbient = newRandomSound("ambient", "temperate_", "dayscape"); 409 409 410 410 const AMBIENT = "audio/ambient/dayscape/day_temperate_gen_03.ogg"; 411 currentAmbient = new Sound(AMBIENT);411 currentAmbient = new AmbientSound(AMBIENT); 412 412 413 413 if (currentAmbient) 414 414 { 415 415 currentAmbient.loop(); 416 currentAmbient.setGain(0.8);417 416 } 418 417 break; 419 418 … … 428 427 { 429 428 if (currentAmbient) 430 429 { 431 currentAmbient.f ade(-1, 0.0, 5.0);430 currentAmbient.free(); 432 431 currentAmbient = null; 433 432 } 434 433 } -
binaries/data/mods/public/gui/common/music.js
72 72 switch (this.currentState) 73 73 { 74 74 case this.states.OFF: 75 if (this.isPlaying())76 {77 this.currentMusic.fade(-1, 0.0, 3.0);78 this.currentMusic = null;79 }80 75 break; 81 76 82 77 case this.states.MENU: … … 146 141 147 142 Music.prototype.switchMusic = function(track, fadeInPeriod, isLooping) 148 143 { 149 if (this.currentMusic) 150 { 151 this.currentMusic.fade(-1, 0.0, 5.0); 152 this.currentMusic = null; 153 } 144 this.currentMusic = new MusicSound(this.RELATIVE_MUSIC_PATH + track); 154 145 155 this.currentMusic = new Sound(this.RELATIVE_MUSIC_PATH + track);156 157 146 if (this.currentMusic) 158 147 { 159 148 if (isLooping) 160 149 this.currentMusic.loop(); 161 150 else 162 151 this.currentMusic.play(); 163 164 if (fadeInPeriod)165 this.currentMusic.fade(0.0, this.musicGain, fadeInPeriod);166 152 } 167 153 }; 168 154 -
binaries/data/mods/public/gui/common/functions_utility_music.js
66 66 67 67 //console.write("Playing " + randomSoundPath + " ..."); 68 68 69 switch (soundType) 70 { 71 case "music": 72 return new MusicSound(randomSoundPath); 73 break; 74 case "ambient": 75 return new AmbientSound(randomSoundPath); 76 break; 77 case "effect": 78 console.write ("am loading effect '*"+randomSoundPath+"*'"); 79 break; 80 default: 81 break; 82 } 69 83 return new Sound(randomSoundPath); 70 84 } 71 85 -
binaries/data/mods/public/hwdetect/hwdetect.js
213 213 } 214 214 215 215 // http://trac.wildfiregames.com/ticket/685 216 if (os_macosx)217 {218 warnings.push("Audio has been disabled, due to problems with OpenAL on OS X.");219 disable_audio = true;220 }216 // if (os_macosx) 217 // { 218 // warnings.push("Audio has been disabled, due to problems with OpenAL on OS X."); 219 // disable_audio = true; 220 // } 221 221 222 222 // http://trac.wildfiregames.com/ticket/684 223 223 // https://bugs.freedesktop.org/show_bug.cgi?id=24047 -
binaries/data/config/default.cfg
73 73 74 74 ; GENERAL PREFERENCES: 75 75 76 sound.mastergain = 0.5 76 sound.mastergain = 0.9 77 sound.musicgain = 0.2 78 sound.ambientgain = 0.6 79 sound.actiongain = 0.7 80 sound.bufferCount = 50 81 sound.bufferSize = 65536 77 82 78 83 ; Camera control settings 79 84 view.scroll.speed = 120.0