Ticket #1604: archiveStream.diff
File archiveStream.diff, 45.6 KB (added by , 12 years ago) |
---|
-
source/ps/GameSetup/Config.cpp
84 84 CFG_GET_USER_VAL("silhouettes", Bool, g_Silhouettes); 85 85 CFG_GET_USER_VAL("showsky", Bool, g_ShowSky); 86 86 87 #if CONFIG2_AUDIO 87 88 float gain = 0.5f; 88 89 float musicGain = 0.5f; 89 90 float ambientGain = 0.5f; … … 99 100 CFG_GET_USER_VAL("sound.bufferCount", Int, bufferCount); 100 101 CFG_GET_USER_VAL("sound.bufferSize", UnsignedLong, bufferSize); 101 102 102 g_SoundManager->SetMasterGain(gain); 103 g_SoundManager->SetMusicGain(musicGain); 104 g_SoundManager->SetAmbientGain(ambientGain); 105 g_SoundManager->SetActionGain(actionGain); 103 if ( g_SoundManager ) { 104 g_SoundManager->SetMasterGain(gain); 105 g_SoundManager->SetMusicGain(musicGain); 106 g_SoundManager->SetAmbientGain(ambientGain); 107 g_SoundManager->SetActionGain(actionGain); 106 108 107 g_SoundManager->SetMemoryUsage(bufferSize, bufferCount); 109 g_SoundManager->SetMemoryUsage(bufferSize, bufferCount); 110 } 111 #endif 108 112 } 109 113 110 114 -
source/ps/GameSetup/GameSetup.cpp
190 190 { 191 191 PROFILE3("render"); 192 192 193 g_SoundManager->IdleTask(); 194 193 #if CONFIG2_AUDIO 194 if ( g_SoundManager ) 195 g_SoundManager->IdleTask(); 196 #endif 195 197 ogl_WarnIfError(); 196 198 197 199 g_Profiler2.RecordGPUFrameStart(); … … 685 687 // resource 686 688 // first shut down all resource owners, and then the handle manager. 687 689 TIMER_BEGIN(L"resource modules"); 688 delete g_SoundManager; 690 #if CONFIG2_AUDIO 691 if ( g_SoundManager ) 692 delete g_SoundManager; 693 #endif 689 694 690 695 g_VFS.reset(); 691 696 … … 862 867 g_ScriptStatsTable = new CScriptStatsTable; 863 868 g_ProfileViewer.AddRootTable(g_ScriptStatsTable); 864 869 865 g_SoundManager = new CSoundManager();870 CSoundManager::CreateSoundManager(); 866 871 867 872 InitScripting(); // before GUI 868 873 … … 926 931 // speed up startup by disabling all sound 927 932 // (OpenAL init will be skipped). 928 933 // must be called before first snd_open. 929 g_SoundManager->SetEnabled(false);934 CSoundManager::SetEnabled(false); 930 935 } 931 936 932 937 g_GUI = new CGUIManager(g_ScriptingHost.GetScriptInterface()); -
source/ps/Game.cpp
294 294 if (doInterpolate) 295 295 { 296 296 m_TurnManager->Interpolate(deltaSimTime, deltaRealTime); 297 g_SoundManager->IdleTask(); 297 #if CONFIG2_AUDIO 298 if ( g_SoundManager ) 299 g_SoundManager->IdleTask(); 300 #endif 298 301 } 299 302 300 303 // TODO: maybe we should add a CCmpParticleInterface that passes the interpolation commands -
source/soundmanager/items/CSoundItem.h
21 21 #include "CSoundBase.h" 22 22 #include "soundmanager/data/SoundData.h" 23 23 24 #if CONFIG2_AUDIO 24 25 25 26 class CSoundItem : public CSoundBase 26 27 { … … 34 35 35 36 }; 36 37 38 #endif 37 39 #endif // INCLUDED_CSOUNDITEM_H -
source/soundmanager/items/CBufferItem.h
18 18 #ifndef INCLUDED_CBUFFERITEM_H 19 19 #define INCLUDED_CBUFFERITEM_H 20 20 21 #include "lib/config2.h" 22 23 #if CONFIG2_AUDIO 24 21 25 #include "CSoundBase.h" 22 26 23 27 class CBufferItem : public CSoundBase … … 35 39 36 40 }; 37 41 38 42 #endif 39 43 #endif // INCLUDED_CBUFFERITEM_H -
source/soundmanager/items/CStreamItem.h
21 21 #include "soundmanager/data/SoundData.h" 22 22 #include "CSoundBase.h" 23 23 24 #if CONFIG2_AUDIO 25 24 26 class CStreamItem : public CSoundBase 25 27 { 26 28 public: … … 35 37 36 38 }; 37 39 40 #endif 38 41 #endif // INCLUDED_CSTREAMITEM_H -
source/soundmanager/items/ISoundItem.h
19 19 #define INCLUDED_ISOUNDITEM_H 20 20 21 21 #include "lib/external_libraries/openal.h" 22 #include "lib/config2.h" 22 23 #include "maths/Vector3D.h" 23 24 24 25 #include <string> 26 #if CONFIG2_AUDIO 25 27 26 28 class ISoundItem 27 29 { … … 56 58 virtual void SetRollOff(ALfloat gain) = 0; 57 59 }; 58 60 59 61 #endif 60 62 #endif // INCLUDED_ISOUNDITEM_H 63 No newline at end of file -
source/soundmanager/items/CSoundBase.cpp
22 22 #include "lib/timer.h" 23 23 #include "soundmanager/SoundManager.h" 24 24 #include "soundmanager/data/SoundData.h" 25 #include "ps/CLogger.h" 25 26 26 27 #include <iostream> 28 #if CONFIG2_AUDIO 27 29 28 30 CSoundBase::CSoundBase() 29 31 { … … 73 75 74 76 void CSoundBase::SetGain(ALfloat gain) 75 77 { 78 AL_CHECK 79 76 80 alSourcef(m_ALSource, AL_GAIN, gain); 81 82 AL_CHECK 77 83 } 78 84 79 85 void CSoundBase::SetRollOff(ALfloat rolls) 80 86 { 81 87 alSourcef(m_ALSource, AL_ROLLOFF_FACTOR, rolls); 88 AL_CHECK 82 89 } 83 90 84 91 void CSoundBase::EnsurePlay() … … 89 96 90 97 void CSoundBase::SetCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain) 91 98 { 92 alSourcef(m_ALSource, innerCone, AL_CONE_INNER_ANGLE); 93 alSourcef(m_ALSource, outerCone, AL_CONE_OUTER_ANGLE); 94 alSourcef(m_ALSource, coneGain, AL_CONE_OUTER_GAIN); 99 AL_CHECK 100 alSourcef(m_ALSource, AL_CONE_INNER_ANGLE, innerCone); 101 AL_CHECK 102 alSourcef(m_ALSource, AL_CONE_OUTER_ANGLE, outerCone); 103 AL_CHECK 104 alSourcef(m_ALSource, AL_CONE_OUTER_GAIN, coneGain); 105 AL_CHECK 95 106 } 96 107 97 108 void CSoundBase::SetPitch(ALfloat pitch) 98 109 { 99 110 alSourcef(m_ALSource, AL_PITCH, pitch); 111 AL_CHECK 100 112 } 101 113 102 114 void CSoundBase::SetDirection(const CVector3D& direction) 103 115 { 104 116 alSourcefv(m_ALSource, AL_DIRECTION, direction.GetFloatArray()); 117 AL_CHECK 105 118 } 106 119 107 120 bool CSoundBase::InitOpenAL() … … 109 122 alGetError(); /* clear error */ 110 123 alGenSources(1, &m_ALSource); 111 124 long anErr = alGetError(); 112 if (anErr != AL_NO_ERROR) 125 126 AL_CHECK 127 128 if (anErr == AL_NO_ERROR) 113 129 { 114 printf("- Error creating sources %ld !!\n", anErr);115 }116 else117 {118 130 ALfloat source0Pos[]={ -2.0, 0.0, 0.0}; 119 131 ALfloat source0Vel[]={ 0.0, 0.0, 0.0}; 120 132 … … 123 135 alSourcefv(m_ALSource,AL_POSITION,source0Pos); 124 136 alSourcefv(m_ALSource,AL_VELOCITY,source0Vel); 125 137 alSourcei(m_ALSource,AL_LOOPING,AL_FALSE); 138 AL_CHECK 139 126 140 return true; 127 141 } 128 142 return false; … … 132 146 { 133 147 int proc_state; 134 148 alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state); 149 AL_CHECK 135 150 136 151 return (proc_state == AL_PLAYING); 137 152 } … … 149 164 void CSoundBase::SetLocation (const CVector3D& position) 150 165 { 151 166 alSourcefv(m_ALSource,AL_POSITION, position.GetFloatArray()); 167 AL_CHECK 152 168 } 153 169 154 170 bool CSoundBase::HandleFade() 155 171 { 172 AL_CHECK 156 173 if (m_StartFadeTime != 0) 157 174 { 158 175 double currTime = timer_Time(); … … 169 186 } 170 187 else 171 188 alSourcef(m_ALSource, AL_GAIN, curGain); 189 190 AL_CHECK 172 191 } 173 192 return true; 174 193 } … … 182 201 { 183 202 m_Looping = loops; 184 203 alSourcei(m_ALSource, AL_LOOPING, loops ? AL_TRUE : AL_FALSE); 204 AL_CHECK 185 205 } 186 206 187 207 void CSoundBase::Play() … … 189 209 m_ShouldBePlaying = true; 190 210 if (m_ALSource != 0) 191 211 alSourcePlay(m_ALSource); 212 AL_CHECK 192 213 } 193 214 194 215 void CSoundBase::PlayAndDelete() … … 215 236 { 216 237 SetLooping(true); 217 238 Play(); 239 AL_CHECK 218 240 } 219 241 } 220 242 … … 229 251 alGetSourcef(m_ALSource, AL_GAIN, &m_StartVolume); 230 252 m_EndVolume = newVolume; 231 253 } 254 AL_CHECK 232 255 } 233 256 234 257 void CSoundBase::PlayAsMusic() … … 251 274 alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state); 252 275 if (proc_state == AL_PLAYING) 253 276 alSourceStop(m_ALSource); 277 278 AL_CHECK 254 279 } 255 280 } 256 281 … … 274 299 m_Name->assign(anst.begin(), anst.end()); 275 300 } 276 301 302 #endif 303 No newline at end of file -
source/soundmanager/items/CSoundItem.cpp
20 20 #include "CSoundItem.h" 21 21 22 22 #include "soundmanager/data/SoundData.h" 23 #include "soundmanager/SoundManager.h" 23 24 24 25 #include <iostream> 25 26 27 #if CONFIG2_AUDIO 26 28 27 29 CSoundItem::CSoundItem() 28 30 { … … 38 40 39 41 CSoundItem::~CSoundItem() 40 42 { 43 AL_CHECK 41 44 ALuint al_buf; 42 45 43 46 Stop(); 44 47 alSourceUnqueueBuffers(m_ALSource, 1, &al_buf); 48 AL_CHECK 45 49 } 46 50 47 51 bool CSoundItem::IdleTask() … … 52 56 { 53 57 int proc_state; 54 58 alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state); 59 AL_CHECK 55 60 return (proc_state != AL_STOPPED); 56 61 } 57 62 return true; … … 63 68 { 64 69 m_SoundData = itemData->IncrementCount(); 65 70 alSourcei(m_ALSource, AL_BUFFER, m_SoundData->GetBuffer()); 71 72 AL_CHECK 66 73 } 67 74 } 75 76 #endif 77 No newline at end of file -
source/soundmanager/items/CBufferItem.cpp
20 20 #include "CBufferItem.h" 21 21 22 22 #include "soundmanager/data/SoundData.h" 23 #include "soundmanager/SoundManager.h" 23 24 24 25 #include <iostream> 25 26 27 #if CONFIG2_AUDIO 28 29 26 30 CBufferItem::CBufferItem(CSoundData* sndData) 27 31 { 28 32 ResetVars(); … … 33 37 34 38 CBufferItem::~CBufferItem() 35 39 { 40 AL_CHECK 41 36 42 Stop(); 37 43 int num_processed; 38 44 alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed); … … 41 47 { 42 48 ALuint* al_buf = new ALuint[num_processed]; 43 49 alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf); 44 50 51 AL_CHECK 45 52 delete[] al_buf; 46 53 } 47 54 } … … 67 74 { 68 75 ALuint al_buf; 69 76 alSourceUnqueueBuffers(m_ALSource, 1, &al_buf); 77 AL_CHECK 70 78 alSourceQueueBuffers(m_ALSource, 1, &al_buf); 79 AL_CHECK 71 80 } 72 81 } 73 82 … … 76 85 77 86 void CBufferItem::Attach(CSoundData* itemData) 78 87 { 88 AL_CHECK 79 89 if (itemData != NULL) 80 90 { 81 91 m_SoundData = itemData->IncrementCount(); 82 92 alSourceQueueBuffers(m_ALSource, m_SoundData->GetBufferCount(),(const ALuint *) m_SoundData->GetBufferPtr()); 93 AL_CHECK 83 94 } 84 95 } 85 96 … … 88 99 m_Looping = loops; 89 100 } 90 101 102 #endif 103 No newline at end of file -
source/soundmanager/items/CSoundBase.h
24 24 25 25 #include <string> 26 26 27 #if CONFIG2_AUDIO 28 27 29 class CSoundBase : public ISoundItem 28 30 { 29 31 protected: … … 86 88 87 89 }; 88 90 91 #endif 89 92 #endif // INCLUDED_CSOUNDBASE_H -
source/soundmanager/items/CStreamItem.cpp
20 20 #include "CStreamItem.h" 21 21 22 22 #include "soundmanager/data/OggData.h" 23 #include "soundmanager/SoundManager.h" 23 24 24 25 #include <iostream> 25 26 27 #if CONFIG2_AUDIO 28 26 29 CStreamItem::CStreamItem(CSoundData* sndData) 27 30 { 28 31 ResetVars(); … … 41 44 { 42 45 ALuint* al_buf = new ALuint[num_processed]; 43 46 alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf); 47 AL_CHECK 44 48 delete[] al_buf; 45 49 } 46 50 } 47 51 48 52 bool CStreamItem::IdleTask() 49 53 { 54 AL_CHECK 50 55 HandleFade(); 56 AL_CHECK 51 57 52 58 int proc_state; 53 59 alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state); 60 AL_CHECK 54 61 55 62 if (proc_state == AL_STOPPED) 56 63 { … … 65 72 { 66 73 int num_processed; 67 74 alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed); 75 AL_CHECK 68 76 69 77 if (num_processed > 0) 70 78 { 71 79 ALuint* al_buf = new ALuint[num_processed]; 72 80 alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf); 81 AL_CHECK 73 82 int didWrite = tmp->FetchDataIntoBuffer(num_processed, al_buf); 74 83 alSourceQueueBuffers(m_ALSource, didWrite, al_buf); 84 AL_CHECK 75 85 delete[] al_buf; 76 86 } 77 87 } … … 89 99 { 90 100 m_SoundData = itemData->IncrementCount(); 91 101 alSourceQueueBuffers(m_ALSource, m_SoundData->GetBufferCount(), (const ALuint *)m_SoundData->GetBufferPtr()); 102 AL_CHECK 92 103 } 93 104 } 94 105 … … 97 108 m_Looping = loops; 98 109 } 99 110 111 #endif 112 No newline at end of file -
source/soundmanager/SoundManager.cpp
27 27 #include "soundmanager/js/AmbientSound.h" 28 28 #include "soundmanager/js/MusicSound.h" 29 29 #include "soundmanager/js/Sound.h" 30 #include "ps/CLogger.h" 30 31 31 CSoundManager* g_SoundManager ;32 CSoundManager* g_SoundManager = NULL; 32 33 34 35 36 33 37 void CSoundManager::ScriptingInit() 34 38 { 35 39 JAmbientSound::ScriptingInit(); … … 38 42 JSoundPlayer::ScriptingInit(); 39 43 } 40 44 45 void CSoundManager::CreateSoundManager() 46 { 47 #if CONFIG2_AUDIO 48 g_SoundManager = new CSoundManager(); 49 #endif 50 } 51 52 void CSoundManager::SetEnabled(bool doEnable) 53 { 54 #if CONFIG2_AUDIO 55 56 if ( g_SoundManager && !doEnable ) 57 { 58 delete g_SoundManager; 59 60 g_SoundManager = NULL; 61 } 62 else if ( ! g_SoundManager && doEnable ) 63 { 64 CSoundManager::CreateSoundManager(); 65 } 66 #endif 67 } 68 69 #if CONFIG2_AUDIO 70 71 void CSoundManager::al_ReportError(ALenum err, const char* caller, int line) 72 { 73 LOGERROR(L"OpenAL error: %hs; called from %hs (line %d)\n", alGetString(err), caller, line); 74 } 75 76 void CSoundManager::al_check(const char* caller, int line) 77 { 78 ALenum err = alGetError(); 79 if (err != AL_NO_ERROR) 80 al_ReportError(err, caller, line); 81 } 82 41 83 CSoundManager::CSoundManager() 42 84 { 43 85 m_Items = new ItemsList; … … 47 89 m_MusicGain = 1; 48 90 m_AmbientGain = 1; 49 91 m_ActionGain = 1; 50 m_Enabled = true;51 92 m_BufferCount = 50; 52 93 m_BufferSize = 65536; 53 94 m_MusicEnabled = true; 54 AlcInit();95 m_Enabled = AlcInit() == INFO::OK; 55 96 } 56 97 57 98 CSoundManager::~CSoundManager() … … 78 119 { 79 120 Status ret = INFO::OK; 80 121 122 AL_CHECK 123 81 124 m_Device = alcOpenDevice(NULL); 82 125 if(m_Device) 83 126 { … … 93 136 const char* dev_name = (const char*)alcGetString(m_Device, ALC_DEVICE_SPECIFIER); 94 137 95 138 if(err == ALC_NO_ERROR && m_Device && m_Context) 96 debug_printf(L"Sound: AlcInit success, using %hs\n", dev_name);139 LOGERROR(L"Sound: AlcInit success, using %hs\n", dev_name); 97 140 else 98 141 { 99 debug_printf(L"Sound: AlcInit failed, m_Device=%p m_Context=%p dev_name=%hs err=%d\n", m_Device, m_Context, dev_name, err);142 LOGERROR(L"Sound: AlcInit failed, m_Device=%p m_Context=%p dev_name=%hs err=%d\n", m_Device, m_Context, dev_name, err); 100 143 // FIXME Hack to get around exclusive access to the sound device 101 144 #if OS_UNIX 102 145 ret = INFO::OK; … … 142 185 143 186 ISoundItem* CSoundManager::LoadItem(const VfsPath& itemPath) 144 187 { 188 AL_CHECK 189 145 190 CSoundData* itemData = CSoundData::SoundDataFromFile(itemPath); 146 191 ISoundItem* answer = NULL; 192 193 AL_CHECK 147 194 148 195 if (itemData != NULL) 149 196 { … … 198 245 deadItems++; 199 246 } 200 247 } 248 AL_CHECK 201 249 if (m_CurrentTune) 202 250 m_CurrentTune->EnsurePlay(); 203 251 if (m_CurrentEnvirons) 204 252 m_CurrentEnvirons->EnsurePlay(); 253 AL_CHECK 205 254 } 206 255 207 256 void CSoundManager::DeleteItem(long itemNum) … … 232 281 alDistanceModel(AL_EXPONENT_DISTANCE); 233 282 } 234 283 235 void CSoundManager::SetEnabled(bool doEnable)236 {237 m_Enabled = doEnable;238 }239 240 284 void CSoundManager::PlayActionItem(ISoundItem* anItem) 241 285 { 242 286 if (anItem) … … 245 289 { 246 290 anItem->SetGain(m_Gain * m_ActionGain); 247 291 anItem->Play(); 292 AL_CHECK 248 293 } 249 294 } 250 295 } … … 255 300 if (m_Enabled && (m_ActionGain > 0)) { 256 301 anItem->SetGain(m_Gain * groupGain); 257 302 anItem->Play(); 303 AL_CHECK 258 304 } 259 305 } 260 306 } … … 271 317 272 318 void CSoundManager::SetMusicItem(ISoundItem* anItem) 273 319 { 320 AL_CHECK 274 321 if (m_CurrentTune) 275 322 { 276 323 m_CurrentTune->FadeAndDelete(3.00); … … 291 338 anItem->StopAndDelete(); 292 339 } 293 340 } 341 AL_CHECK 294 342 } 295 343 296 344 void CSoundManager::SetAmbientItem(ISoundItem* anItem) … … 312 360 m_CurrentEnvirons->FadeToIn(m_Gain * m_AmbientGain, 2.00); 313 361 } 314 362 } 363 AL_CHECK 315 364 } 316 365 366 #endif -
source/soundmanager/data/ogg.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 #ifndef INCLUDED_OGG 18 #define INCLUDED_OGG 19 20 #include "lib/config2.h" 21 22 #if CONFIG2_AUDIO 23 24 #include "lib/external_libraries/openal.h" 25 #include "lib/file/vfs/vfs.h" 26 27 class OggStream 28 { 29 public: 30 virtual ~OggStream() { } 31 virtual ALenum Format() = 0; 32 virtual ALsizei SamplingRate() = 0; 33 virtual bool atFileEOF() = 0; 34 virtual Status ResetFile() = 0; 35 36 /** 37 * @return bytes read (<= size) or a (negative) Status 38 **/ 39 virtual Status GetNextChunk(u8* buffer, size_t size) = 0; 40 }; 41 42 typedef shared_ptr<OggStream> OggStreamPtr; 43 44 extern Status OpenOggStream(const OsPath& pathname, OggStreamPtr& stream); 45 46 /** 47 * A non-streaming OggStream (reading the whole file in advance) 48 * that can cope with archived/compressed files. 49 */ 50 extern Status OpenOggNonstream(const PIVFS& vfs, const VfsPath& pathname, OggStreamPtr& stream); 51 52 #endif // CONFIG2_AUDIO 53 54 #endif // INCLUDED_OGG -
source/soundmanager/data/SoundData.cpp
20 20 #include "SoundData.h" 21 21 22 22 #include "OggData.h" 23 #include "lib/file/vfs/vfs_util.h"24 #include "ps/Filesystem.h"25 23 26 24 #include <iostream> 27 25 26 #if CONFIG2_AUDIO 27 28 28 DataMap* CSoundData::sSoundData = NULL; 29 29 30 30 CSoundData::CSoundData() … … 96 96 CSoundData* answer = NULL; 97 97 COggData* oggAnswer = new COggData(); 98 98 99 OsPath realPath; 100 Status ret = g_VFS->GetRealPath(itemPath, realPath); 101 if (ret == INFO::OK) 102 { 103 if (oggAnswer->InitOggFile(realPath.string().c_str())) 104 { 105 answer = oggAnswer; 106 } 107 } 99 if (oggAnswer->InitOggFile(itemPath)) 100 answer = oggAnswer; 101 108 102 return answer; 109 103 } 110 104 … … 140 134 return &m_ALBuffer; 141 135 } 142 136 137 #endif 138 No newline at end of file -
source/soundmanager/data/OggData.cpp
20 20 #include "OggData.h" 21 21 22 22 #include "soundmanager/SoundManager.h" 23 #include "ps/Filesystem.h" 23 24 24 #include <wchar.h> 25 #include <iostream> 25 #if CONFIG2_AUDIO 26 26 27 27 COggData::COggData() 28 28 { … … 32 32 COggData::~COggData() 33 33 { 34 34 alDeleteBuffers(m_BuffersUsed, m_Buffer); 35 ov_clear(&m_vf);36 35 } 37 36 38 37 void COggData::SetFormatAndFreq(int form, ALsizei freq) … … 41 40 m_Frequency = freq; 42 41 } 43 42 44 bool COggData::InitOggFile(const wchar_t* fileLoc)43 bool COggData::InitOggFile(const VfsPath& itemPath) 45 44 { 46 int buffersToStart = g_SoundManager->GetBufferCount(); 47 char nameH[300]; 48 sprintf(nameH, "%ls", fileLoc); 49 50 FILE* f = fopen(nameH, "rb"); 51 m_current_section = 0; 52 int err = ov_open_callbacks(f, &m_vf, NULL, 0, OV_CALLBACKS_DEFAULT); 53 if (err < 0) 54 { 55 fprintf(stderr,"Input does not appear to be an Ogg bitstream :%d :%d.\n", err, ferror(f)); 56 return false; 57 } 45 int buffersToStart = g_SoundManager->GetBufferCount(); 46 OpenOggNonstream( g_VFS, itemPath, ogg); 58 47 59 m_FileName = CStrW(fileLoc);60 61 48 m_FileFinished = false; 62 SetFormatAndFreq((m_vf.vi->channels == 1)? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16 , (ALsizei)m_vf.vi->rate);63 49 50 SetFormatAndFreq(ogg->Format(), ogg->SamplingRate() ); 51 64 52 alGetError(); /* clear error */ 65 53 alGenBuffers(buffersToStart, m_Buffer); 66 54 … … 96 84 97 85 void COggData::ResetFile() 98 86 { 99 ov_time_seek(&m_vf, 0); 100 m_current_section = 0; 87 ogg->ResetFile(); 101 88 m_FileFinished = false; 102 89 } 103 90 … … 110 97 { 111 98 long bufferSize = g_SoundManager->GetBufferSize(); 112 99 113 char* pcmout = new char[bufferSize + 5000];100 u8* pcmout = new u8[bufferSize + 5000]; 114 101 int buffersWritten = 0; 115 102 116 103 for (int i = 0; (i < count) && !m_FileFinished; i++) 117 104 { 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 { 125 m_FileFinished=true; 126 break; 127 } 128 else if (ret < 0) 129 { 130 /* error in the stream. Not a problem, just reporting it in 131 case we (the app) cares. In this case, we don't. */ 132 } 133 else 134 { 135 totalRet += ret; 136 readDest += ret; 137 } 138 } 105 Status totalRet = ogg->GetNextChunk( pcmout, bufferSize); 106 m_FileFinished = ogg->atFileEOF(); 139 107 if (totalRet > 0) 140 108 { 141 109 buffersWritten++; … … 157 125 return m_Buffer; 158 126 } 159 127 128 #endif 160 129 161 130 162 131 163 -
source/soundmanager/data/SoundData.h
20 20 21 21 #include "lib/external_libraries/openal.h" 22 22 #include "lib/file/vfs/vfs_path.h" 23 #include "lib/ os_path.h"23 #include "lib/config2.h" 24 24 25 25 #include <string> 26 26 #include <map> 27 27 28 #if CONFIG2_AUDIO 29 28 30 class CSoundData; 29 31 typedef std::map<std::wstring, CSoundData*> DataMap; 30 32 … … 61 63 62 64 }; 63 65 66 #endif 64 67 #endif // INCLUDED_SOUNDDATA_H -
source/soundmanager/data/OggData.h
20 20 21 21 #include "SoundData.h" 22 22 #include "lib/external_libraries/openal.h" 23 #include "vorbis/vorbisfile.h" 23 #include "ogg.h" 24 #if CONFIG2_AUDIO 24 25 25 26 class COggData : public CSoundData 26 27 { … … 31 32 COggData(); 32 33 virtual ~COggData(); 33 34 34 virtual bool InitOggFile(const wchar_t* fileLoc);35 virtual bool InitOggFile(const VfsPath& itemPath); 35 36 virtual bool IsFileFinished(); 36 37 virtual bool IsOneShot(); 37 38 … … 39 40 virtual void ResetFile(); 40 41 41 42 protected: 42 Ogg Vorbis_File m_vf;43 int m_current_section;43 OggStreamPtr ogg; 44 // int m_current_section; 44 45 bool m_FileFinished; 45 46 bool m_OneShot; 46 47 ALuint m_Buffer[100]; … … 53 54 ALuint* GetBufferPtr(); 54 55 }; 55 56 56 57 #endif 57 58 #endif // INCLUDED_OGGDATA_H -
source/soundmanager/data/ogg.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 "ogg.h" 19 20 #if CONFIG2_AUDIO 21 22 #include "lib/external_libraries/openal.h" 23 #include "lib/external_libraries/vorbis.h" 24 25 #include "lib/byte_order.h" 26 #include "lib/file/io/io.h" 27 #include "lib/file/file_system.h" 28 29 #include "lib/file/vfs/vfs_util.h" 30 #include "ps/Filesystem.h" 31 32 33 static Status LibErrorFromVorbis(int err) 34 { 35 switch(err) 36 { 37 case 0: 38 return INFO::OK; 39 case OV_HOLE: 40 return ERR::AGAIN; 41 case OV_EREAD: 42 return ERR::IO; 43 case OV_EFAULT: 44 return ERR::LOGIC; 45 case OV_EIMPL: 46 return ERR::NOT_SUPPORTED; 47 case OV_EINVAL: 48 return ERR::INVALID_PARAM; 49 case OV_ENOTVORBIS: 50 return ERR::NOT_SUPPORTED; 51 case OV_EBADHEADER: 52 return ERR::CORRUPTED; 53 case OV_EVERSION: 54 return ERR::INVALID_VERSION; 55 case OV_ENOTAUDIO: 56 return ERR::_1; 57 case OV_EBADPACKET: 58 return ERR::_2; 59 case OV_EBADLINK: 60 return ERR::_3; 61 case OV_ENOSEEK: 62 return ERR::_4; 63 default: 64 return ERR::FAIL; 65 } 66 } 67 68 69 //----------------------------------------------------------------------------- 70 71 class VorbisFileAdapter 72 { 73 public: 74 VorbisFileAdapter(const PFile& openedFile) 75 : file(openedFile) 76 , size(FileSize(openedFile->Pathname())) 77 , offset(0) 78 { 79 } 80 81 static size_t Read(void* bufferToFill, size_t itemSize, size_t numItems, void* context) 82 { 83 VorbisFileAdapter* adapter = (VorbisFileAdapter*)context; 84 const off_t sizeRequested = numItems*itemSize; 85 const off_t sizeRemaining = adapter->size - adapter->offset; 86 const size_t sizeToRead = (size_t)std::min(sizeRequested, sizeRemaining); 87 88 io::Operation op(*adapter->file.get(), bufferToFill, sizeToRead, adapter->offset); 89 if(io::Run(op) == INFO::OK) 90 { 91 adapter->offset += sizeToRead; 92 return sizeToRead; 93 } 94 95 errno = EIO; 96 return 0; 97 } 98 99 static int Seek(void* context, ogg_int64_t offset, int whence) 100 { 101 VorbisFileAdapter* adapter = (VorbisFileAdapter*)context; 102 103 off_t origin = 0; 104 switch(whence) 105 { 106 case SEEK_SET: 107 origin = 0; 108 break; 109 case SEEK_CUR: 110 origin = adapter->offset; 111 break; 112 case SEEK_END: 113 origin = adapter->size+1; 114 break; 115 NODEFAULT; 116 } 117 118 adapter->offset = Clamp(off_t(origin+offset), off_t(0), adapter->size); 119 return 0; 120 } 121 122 static int Close(void* context) 123 { 124 VorbisFileAdapter* adapter = (VorbisFileAdapter*)context; 125 adapter->file.reset(); 126 return 0; // return value is ignored 127 } 128 129 static long Tell(void* context) 130 { 131 VorbisFileAdapter* adapter = (VorbisFileAdapter*)context; 132 return adapter->offset; 133 } 134 135 private: 136 PFile file; 137 off_t size; 138 off_t offset; 139 }; 140 141 //----------------------------------------------------------------------------- 142 143 class VorbisBufferAdapter 144 { 145 public: 146 VorbisBufferAdapter(const shared_ptr<u8>& buffer, size_t size) 147 : buffer(buffer) 148 , size(size) 149 , offset(0) 150 { 151 } 152 153 static size_t Read(void* bufferToFill, size_t itemSize, size_t numItems, void* context) 154 { 155 VorbisBufferAdapter* adapter = (VorbisBufferAdapter*)context; 156 const off_t sizeRequested = numItems*itemSize; 157 const off_t sizeRemaining = adapter->size - adapter->offset; 158 const size_t sizeToRead = (size_t)std::min(sizeRequested, sizeRemaining); 159 160 memcpy(bufferToFill, adapter->buffer.get() + adapter->offset, sizeToRead); 161 162 adapter->offset += sizeToRead; 163 return sizeToRead; 164 } 165 166 static int Seek(void* context, ogg_int64_t offset, int whence) 167 { 168 VorbisBufferAdapter* adapter = (VorbisBufferAdapter*)context; 169 170 off_t origin = 0; 171 switch(whence) 172 { 173 case SEEK_SET: 174 origin = 0; 175 break; 176 case SEEK_CUR: 177 origin = adapter->offset; 178 break; 179 case SEEK_END: 180 origin = adapter->size+1; 181 break; 182 NODEFAULT; 183 } 184 185 adapter->offset = Clamp(off_t(origin+offset), off_t(0), adapter->size); 186 return 0; 187 } 188 189 static int Close(void* context) 190 { 191 VorbisBufferAdapter* adapter = (VorbisBufferAdapter*)context; 192 adapter->buffer.reset(); 193 return 0; // return value is ignored 194 } 195 196 static long Tell(void* context) 197 { 198 VorbisBufferAdapter* adapter = (VorbisBufferAdapter*)context; 199 return adapter->offset; 200 } 201 202 private: 203 shared_ptr<u8> buffer; 204 off_t size; 205 off_t offset; 206 }; 207 208 209 //----------------------------------------------------------------------------- 210 211 template <typename Adapter> 212 class OggStreamImpl : public OggStream 213 { 214 public: 215 OggStreamImpl(const Adapter& adapter) 216 : adapter(adapter) 217 { 218 m_fileEOF = false; 219 } 220 221 Status Open() 222 { 223 ov_callbacks callbacks; 224 callbacks.read_func = Adapter::Read; 225 callbacks.close_func = Adapter::Close; 226 callbacks.seek_func = Adapter::Seek; 227 callbacks.tell_func = Adapter::Tell; 228 const int ret = ov_open_callbacks(&adapter, &vf, 0, 0, callbacks); 229 if(ret != 0) 230 WARN_RETURN(LibErrorFromVorbis(ret)); 231 232 const int link = -1; // retrieve info for current bitstream 233 info = ov_info(&vf, link); 234 if(!info) 235 WARN_RETURN(ERR::INVALID_HANDLE); 236 237 return INFO::OK; 238 } 239 240 virtual ALenum Format() 241 { 242 return (info->channels == 1)? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; 243 } 244 245 virtual ALsizei SamplingRate() 246 { 247 return info->rate; 248 } 249 virtual bool atFileEOF() 250 { 251 return m_fileEOF; 252 } 253 254 virtual Status ResetFile() 255 { 256 ov_time_seek( &vf, 0 ); 257 m_fileEOF = false; 258 } 259 260 virtual Status GetNextChunk(u8* buffer, size_t size) 261 { 262 // we may have to call ov_read multiple times because it 263 // treats the buffer size "as a limit and not a request" 264 size_t bytesRead = 0; 265 for(;;) 266 { 267 const int isBigEndian = (BYTE_ORDER == BIG_ENDIAN); 268 const int wordSize = sizeof(i16); 269 const int isSigned = 1; 270 int bitstream; // unused 271 const int ret = ov_read(&vf, (char*)buffer+bytesRead, int(size-bytesRead), isBigEndian, wordSize, isSigned, &bitstream); 272 if(ret == 0) { // EOF 273 m_fileEOF = true; 274 return (Status)bytesRead; 275 } 276 else if(ret < 0) 277 WARN_RETURN(LibErrorFromVorbis(ret)); 278 else // success 279 { 280 bytesRead += ret; 281 if(bytesRead == size) 282 return (Status)bytesRead; 283 } 284 } 285 } 286 287 private: 288 Adapter adapter; 289 OggVorbis_File vf; 290 vorbis_info* info; 291 bool m_fileEOF; 292 }; 293 294 295 //----------------------------------------------------------------------------- 296 297 Status OpenOggStream(const OsPath& pathname, OggStreamPtr& stream) 298 { 299 PFile file(new File); 300 RETURN_STATUS_IF_ERR(file->Open(pathname, L'r')); 301 302 shared_ptr<OggStreamImpl<VorbisFileAdapter> > tmp(new OggStreamImpl<VorbisFileAdapter>(VorbisFileAdapter(file))); 303 RETURN_STATUS_IF_ERR(tmp->Open()); 304 stream = tmp; 305 return INFO::OK; 306 } 307 308 Status OpenOggNonstream(const PIVFS& vfs, const VfsPath& pathname, OggStreamPtr& stream) 309 { 310 shared_ptr<u8> contents; 311 size_t size; 312 RETURN_STATUS_IF_ERR(vfs->LoadFile(pathname, contents, size)); 313 314 shared_ptr<OggStreamImpl<VorbisBufferAdapter> > tmp(new OggStreamImpl<VorbisBufferAdapter>(VorbisBufferAdapter(contents, size))); 315 RETURN_STATUS_IF_ERR(tmp->Open()); 316 stream = tmp; 317 return INFO::OK; 318 } 319 320 #endif // CONFIG2_AUDIO -
source/soundmanager/SoundManager.h
23 23 24 24 #include <vector> 25 25 #include <map> 26 #if CONFIG2_AUDIO 26 27 28 29 #define AL_CHECK CSoundManager::al_check(__func__, __LINE__); 30 27 31 typedef std::vector<ISoundItem*> ItemsList; 28 32 29 33 … … 45 49 long m_BufferSize; 46 50 int m_BufferCount; 47 51 bool m_MusicEnabled; 52 bool m_SoundEnabled; 48 53 49 54 public: 50 55 CSoundManager(); … … 53 58 ISoundItem* LoadItem(const VfsPath& itemPath); 54 59 55 60 static void ScriptingInit(); 61 static void CreateSoundManager(); 62 static void SetEnabled(bool doEnable); 63 64 static void al_ReportError(ALenum err, const char* caller, int line); 65 static void al_check(const char* caller, int line); 56 66 57 67 void SetMusicEnabled (bool isEnabled); 68 void setSoundEnabled( bool enabled ); 58 69 59 70 ISoundItem* ItemFromWAV(VfsPath& fname); 60 71 ISoundItem* ItemFromOgg(VfsPath& fname); … … 78 89 void SetAmbientGain(float gain); 79 90 void SetActionGain(float gain); 80 91 81 void SetEnabled(bool doEnable);82 92 protected: 83 93 void InitListener(); 84 94 virtual Status AlcInit(); 85 95 86 96 }; 87 97 98 #else 99 class CSoundManager 100 { 101 public: 102 static void ScriptingInit(); 103 static void CreateSoundManager(); 104 static void SetEnabled(bool doEnable); 105 }; 106 #endif 107 108 88 109 extern CSoundManager* g_SoundManager; 89 110 90 91 111 #endif // INCLUDED_SOUNDMANAGER_H -
source/soundmanager/js/SoundGroup.cpp
41 41 42 42 #include <algorithm> 43 43 44 44 45 extern CGame *g_Game; 45 46 46 47 #define PI 3.14126f … … 155 156 156 157 void CSoundGroup::UploadPropertiesAndPlay(int theIndex, const CVector3D& position) 157 158 { 158 bool isOnscreen; 159 ALfloat initialRolllOff = 0.02f; 160 ALfloat itemRollOff = initialRolllOff; 159 #if CONFIG2_AUDIO 160 if ( g_SoundManager ) { 161 bool isOnscreen; 162 ALfloat initialRolllOff = 0.02f; 163 ALfloat itemRollOff = initialRolllOff; 161 164 162 float offSet = RadiansOffCenter(position, isOnscreen, itemRollOff);165 float offSet = RadiansOffCenter(position, isOnscreen, itemRollOff); 163 166 164 if (isOnscreen || TestFlag(eDistanceless) || TestFlag(eOmnipresent))165 {166 if (snd_group.size() == 0)167 Reload();167 if (isOnscreen || TestFlag(eDistanceless) || TestFlag(eOmnipresent)) 168 { 169 if (snd_group.size() == 0) 170 Reload(); 168 171 169 ISoundItem* hSound = snd_group[theIndex];170 CVector3D origin = g_Game->GetView()->GetCamera()->GetOrientation().GetTranslation();171 float sndDist = origin.Y;172 ISoundItem* hSound = snd_group[theIndex]; 173 CVector3D origin = g_Game->GetView()->GetCamera()->GetOrientation().GetTranslation(); 174 float sndDist = origin.Y; 172 175 173 if (!TestFlag(eOmnipresent)) 174 { 175 hSound->SetLocation(CVector3D((sndDist * sin(offSet)), 0, sndDist * cos(offSet))); 176 if (TestFlag(eDistanceless)) 177 hSound->SetRollOff(initialRolllOff); 176 if (!TestFlag(eOmnipresent)) 177 { 178 hSound->SetLocation(CVector3D((sndDist * sin(offSet)), 0, sndDist * cos(offSet))); 179 if (TestFlag(eDistanceless)) 180 hSound->SetRollOff(initialRolllOff); 181 else 182 hSound->SetRollOff(itemRollOff); 183 } 184 185 if (TestFlag(eRandPitch)) 186 hSound->SetPitch(RandFloat(m_PitchLower, m_PitchUpper)); 178 187 else 179 hSound->SetRollOff(itemRollOff); 180 } 188 hSound->SetPitch(m_Pitch); 181 189 182 if (TestFlag(eRandPitch)) 183 hSound->SetPitch(RandFloat(m_PitchLower, m_PitchUpper)); 184 else 185 hSound->SetPitch(m_Pitch); 190 ALfloat theGain = m_Gain; 191 if (TestFlag(eRandGain)) 192 theGain = RandFloat(m_GainLower, m_GainUpper); 186 193 187 ALfloat theGain = m_Gain; 188 if (TestFlag(eRandGain)) 189 theGain = RandFloat(m_GainLower, m_GainUpper); 194 hSound->SetCone(m_ConeInnerAngle, m_ConeOuterAngle, m_ConeOuterGain); 190 195 191 hSound->SetCone(m_ConeInnerAngle, m_ConeOuterAngle, m_ConeOuterGain); 192 193 g_SoundManager->PlayGroupItem(hSound, theGain); 196 g_SoundManager->PlayGroupItem(hSound, theGain); 197 } 194 198 } 199 #endif 195 200 } 196 201 197 202 … … 216 221 { 217 222 m_index = 0; // reset our index 218 223 224 #if CONFIG2_AUDIO 219 225 snd_group.clear(); 220 226 221 for (size_t i = 0; i < filenames.size(); i++) 222 { 223 VfsPath thePath = m_filepath/filenames[i]; 224 ISoundItem* temp = g_SoundManager->LoadItem(thePath); 227 if ( g_SoundManager ) { 228 for (size_t i = 0; i < filenames.size(); i++) 229 { 230 VfsPath thePath = m_filepath/filenames[i]; 231 ISoundItem* temp = g_SoundManager->LoadItem(thePath); 225 232 226 if (temp == NULL) 227 HandleError(L"error loading sound", thePath, ERR::FAIL); 228 else 229 snd_group.push_back(temp); 233 if (temp == NULL) 234 HandleError(L"error loading sound", thePath, ERR::FAIL); 235 else 236 snd_group.push_back(temp); 237 } 238 239 if (TestFlag(eRandOrder)) 240 random_shuffle(snd_group.begin(), snd_group.end()); 230 241 } 231 232 if (TestFlag(eRandOrder)) 233 random_shuffle(snd_group.begin(), snd_group.end()); 242 #endif 234 243 } 235 244 236 245 void CSoundGroup::ReleaseGroup() 237 246 { 247 #if CONFIG2_AUDIO 238 248 for (size_t i = 0; i < snd_group.size(); i++) 239 249 { 240 250 snd_group[i]->FadeAndDelete(0.2); 241 251 } 242 252 snd_group.clear(); 253 #endif 243 254 } 244 255 245 256 void CSoundGroup::Update(float UNUSED(TimeSinceLastFrame)) -
source/soundmanager/js/SoundPlayer.cpp
34 34 35 35 bool JSoundPlayer::StartMusic(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 36 36 { 37 g_SoundManager->SetMusicEnabled(true); 38 37 #if CONFIG2_AUDIO 38 if ( g_SoundManager ) 39 g_SoundManager->SetMusicEnabled(true); 40 #endif 39 41 return true; 40 42 } 41 43 42 44 // request the sound be played until free() is called. returns immediately. 43 45 bool JSoundPlayer::StopMusic(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 44 46 { 45 g_SoundManager->SetMusicEnabled(false); 47 #if CONFIG2_AUDIO 48 if ( g_SoundManager ) 49 g_SoundManager->SetMusicEnabled(false); 50 #endif 46 51 47 52 return true; 48 53 } … … 61 66 JSoundPlayer* newObject = new JSoundPlayer(); 62 67 newObject->m_EngineOwned = false; 63 68 JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); 64 69 65 70 return JS_TRUE; 66 71 } 67 72 -
source/soundmanager/js/AmbientSound.cpp
32 32 // start playing the sound, all ambient sounds loop 33 33 bool JAmbientSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 34 34 { 35 ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); 35 #if CONFIG2_AUDIO 36 if ( g_SoundManager ) { 37 ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); 36 38 37 if (aSnd) 38 aSnd->PlayAsAmbient(); 39 else 40 debug_printf(L"sound item could not be loaded to play: %ls\n", m_FileName.string().c_str()); 41 39 if (aSnd) 40 aSnd->PlayAsAmbient(); 41 else 42 debug_printf(L"sound item could not be loaded to play: %ls\n", m_FileName.string().c_str()); 43 } 44 #endif 42 45 return true; 43 46 } 44 47 45 48 // start playing the sound, all ambient sounds loop 46 49 bool JAmbientSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 47 50 { 48 ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); 51 #if CONFIG2_AUDIO 52 if ( g_SoundManager ) { 53 ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); 49 54 50 if (aSnd) 51 aSnd->PlayAsAmbient(); 52 else 53 debug_printf(L"sound item could not be loaded to loop: %ls\n", m_FileName.string().c_str()); 54 55 if (aSnd) 56 aSnd->PlayAsAmbient(); 57 else 58 debug_printf(L"sound item could not be loaded to loop: %ls\n", m_FileName.string().c_str()); 59 } 60 #endif 55 61 return true; 56 62 } 57 63 bool JAmbientSound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 58 64 { 59 g_SoundManager->SetAmbientItem(0L); 65 #if CONFIG2_AUDIO 66 if ( g_SoundManager ) 67 g_SoundManager->SetAmbientItem(0L); 68 #endif 60 69 61 70 return true; 62 71 } … … 90 99 CStrW filename; 91 100 if (! ToPrimitive<CStrW>(cx, JS_ARGV(cx, vp)[0], filename)) 92 101 return JS_FALSE; 93 102 94 103 JAmbientSound* newObject = new JAmbientSound(filename); 95 104 newObject->m_EngineOwned = false; 96 105 97 106 JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); 98 107 99 108 return JS_TRUE; 100 109 } 110 -
source/soundmanager/js/Sound.cpp
27 27 28 28 JSound::JSound(const VfsPath& pathname) 29 29 { 30 m_SndItem = g_SoundManager->LoadItem(pathname); 30 #if CONFIG2_AUDIO 31 if ( g_SoundManager ) 32 m_SndItem = g_SoundManager->LoadItem(pathname); 33 #endif 31 34 } 32 35 33 36 JSound::~JSound() 34 37 { 38 #if CONFIG2_AUDIO 35 39 if (m_SndItem) 36 40 { 37 41 m_SndItem->FadeAndDelete(0.2); 38 42 m_SndItem = 0; 39 43 } 44 #endif 40 45 } 41 46 42 47 bool JSound::ClearSoundItem() 43 48 { 49 #if CONFIG2_AUDIO 44 50 m_SndItem = 0L; 51 #endif 45 52 return true; 46 53 } 47 54 48 55 bool JSound::SetGain(JSContext* cx, uintN UNUSED(argc), jsval* argv) 49 56 { 57 #if CONFIG2_AUDIO 50 58 if (! m_SndItem) 51 59 return false; 52 60 … … 54 62 if (! ToPrimitive<float>(cx, argv[0], gain)) 55 63 return false; 56 64 57 m_SndItem->SetGain(gain); 65 m_SndItem->SetGain(gain); 66 #endif 58 67 return true; 59 68 } 60 69 61 70 bool JSound::SetPitch(JSContext* cx, uintN UNUSED(argc), jsval* argv) 62 71 { 72 #if CONFIG2_AUDIO 63 73 if (! m_SndItem) 64 74 return false; 65 75 … … 68 78 return false; 69 79 70 80 m_SndItem->SetPitch(pitch); 81 #endif 71 82 return true; 72 83 } 73 84 74 85 bool JSound::SetPosition(JSContext* cx, uintN argc, jsval* argv) 75 86 { 87 #if CONFIG2_AUDIO 76 88 if (! m_SndItem) 77 89 return false; 78 90 … … 84 96 return false; 85 97 86 98 m_SndItem->SetLocation(pos); 87 99 #endif 88 100 return true; 89 101 } 90 102 91 103 92 104 bool JSound::Fade(JSContext* cx, uintN UNUSED(argc), jsval* argv) 93 105 { 106 #if CONFIG2_AUDIO 94 107 if (! m_SndItem) 95 108 return false; 96 109 … … 104 117 105 118 m_SndItem->SetGain(initial_gain); 106 119 m_SndItem->FadeToIn(final_gain, length); 107 120 #endif 108 121 return true; 109 122 } 110 123 111 124 bool JSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 112 125 { 126 #if CONFIG2_AUDIO 113 127 if (! m_SndItem) 114 128 return false; 115 129 116 130 m_SndItem->Play(); 117 131 #endif 118 132 return true; 119 133 } 120 134 121 135 bool JSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 122 136 { 137 #if CONFIG2_AUDIO 123 138 if (! m_SndItem) 124 139 return false; 125 140 126 141 m_SndItem->PlayLoop(); 127 142 #endif 128 143 return true; 129 144 } 130 145 131 146 bool JSound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 132 147 { 148 #if CONFIG2_AUDIO 133 149 if (m_SndItem) 134 150 { 135 151 m_SndItem->FadeAndDelete(0.2); 136 152 m_SndItem = 0; 137 153 } 138 154 #endif 139 155 return true; 140 156 } 141 157 … … 155 171 156 172 CStr JSound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 157 173 { 174 #if CONFIG2_AUDIO 158 175 return "[object Sound: " + (m_SndItem ? m_SndItem->GetName() : "(null)") + "]"; 176 #else 177 return "[object Sound: audio disabled]"; 178 #endif 159 179 } 160 180 161 181 JSBool JSound::Construct(JSContext* cx, uintN UNUSED(argc), jsval* vp) … … 169 189 JSound* newObject = new JSound(filename); 170 190 newObject->m_EngineOwned = false; 171 191 JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); 172 192 173 193 return JS_TRUE; 174 194 } -
source/soundmanager/js/MusicSound.cpp
31 31 32 32 bool JMusicSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 33 33 { 34 ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); 35 if (aSnd != NULL) 36 aSnd->PlayAsMusic(); 37 34 #if CONFIG2_AUDIO 35 if ( g_SoundManager ) { 36 ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); 37 if (aSnd != NULL) 38 aSnd->PlayAsMusic(); 39 } 40 #endif 38 41 return true; 39 42 } 40 43 41 44 // request the sound be played until free() is called. returns immediately. 42 45 bool JMusicSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) 43 46 { 44 ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); 45 if (aSnd != NULL) 46 aSnd->PlayAsMusic(); 47 47 #if CONFIG2_AUDIO 48 if ( g_SoundManager ) { 49 ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); 50 if (aSnd != NULL) 51 aSnd->PlayAsMusic(); 52 } 53 #endif 48 54 return true; 49 55 } 50 56 … … 75 81 JMusicSound* newObject = new JMusicSound(filename); 76 82 newObject->m_EngineOwned = false; 77 83 JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); 78 84 79 85 return JS_TRUE; 80 86 } 87 88 -
source/soundmanager/js/SoundGroup.h
53 53 #define INCLUDED_SOUNDGROUP_H 54 54 55 55 #include "lib/file/vfs/vfs_path.h" 56 #include "lib/config2.h" 56 57 57 58 #include <vector> 58 59 59 60 class CVector3D; 61 62 60 63 class ISoundItem; 61 64 62 65 enum eSndGrpFlags … … 109 112 110 113 size_t m_index; // index of the next sound to play 111 114 115 #if CONFIG2_AUDIO 112 116 std::vector<ISoundItem*> snd_group; // we store the handles so we can load now and play later 117 #endif 113 118 std::vector<std::wstring> filenames; // we need the filenames so we can reload when necessary. 114 119 115 120 VfsPath m_filepath; // the file path for the list of sound file resources -
source/soundmanager/js/Sound.h
35 35 #include "scripting/ScriptableObject.h" 36 36 #include "soundmanager/items/ISoundItem.h" 37 37 38 38 39 class JSound : public CJSObject<JSound> 39 40 { 40 41 public: … … 61 62 static void ScriptingInit(); 62 63 63 64 protected: 65 #if CONFIG2_AUDIO 64 66 ISoundItem* m_SndItem; 67 #endif 65 68 }; 66 69 67 70 #endif // #ifndef INCLUDED_JSOUND -
source/simulation2/components/CCmpSoundManager.cpp
36 36 37 37 DEFAULT_COMPONENT_ALLOCATOR(SoundManager) 38 38 39 #if CONFIG2_AUDIO 39 40 std::map<std::wstring, CSoundGroup*> m_SoundGroups; 41 #endif 40 42 41 43 static std::string GetSchema() 42 44 { … … 49 51 50 52 virtual void Deinit() 51 53 { 54 #if CONFIG2_AUDIO 52 55 for (std::map<std::wstring, CSoundGroup*>::iterator it = m_SoundGroups.begin(); it != m_SoundGroups.end(); ++it) 53 56 delete it->second; 54 57 m_SoundGroups.clear(); 58 #endif 55 59 } 56 60 57 61 virtual void Serialize(ISerializer& UNUSED(serialize)) … … 76 80 // or on some other timer? 77 81 const CMessageUpdate& msgData = static_cast<const CMessageUpdate&> (msg); 78 82 float t = msgData.turnLength.ToFloat(); 83 #if CONFIG2_AUDIO 79 84 for (std::map<std::wstring, CSoundGroup*>::iterator it = m_SoundGroups.begin(); it != m_SoundGroups.end(); ++it) 80 85 if (it->second) 81 86 it->second->Update(t); 87 #endif 82 88 break; 83 89 } 84 90 } … … 86 92 87 93 virtual void PlaySoundGroup(std::wstring name, entity_id_t source) 88 94 { 95 #if CONFIG2_AUDIO 89 96 // Make sure the sound group is loaded 90 97 CSoundGroup* group; 91 98 if (m_SoundGroups.find(name) == m_SoundGroups.end()) … … 127 134 128 135 group->PlayNext(sourcePos); 129 136 } 137 #endif 130 138 } 131 139 }; 132 140 133 141 REGISTER_COMPONENT_TYPE(SoundManager) 142 143