OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4SoundSystem.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Handles the sound bank and plays effects using DSoundX */
19 
20 #include "C4Include.h"
21 #include "platform/C4SoundSystem.h"
22 
24 #include "lib/C4Random.h"
25 #include "object/C4Object.h"
26 #include "game/C4Game.h"
27 #include "config/C4Config.h"
28 #include "game/C4Application.h"
29 #include "game/C4Viewport.h"
32 
34  FirstSound (nullptr)
35 {
36 }
37 
39 {
40 
41 }
42 
44 {
47  return false;
48 
49  // Might be reinitialisation
50  ClearEffects();
51  // (re)init EFX
52  Modifiers.Init();
53  // Open sound file
54  if (!SoundFile.IsOpen())
55  if (!Reloc.Open(SoundFile, C4CFN_Sound)) return false;
56  // Load static sound from Sound.ocg
57  LoadEffects(SoundFile, nullptr, false);
58 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
59  Mix_AllocateChannels(C4MaxSoundInstances);
60 #endif
61  return true;
62 }
63 
65 {
66  ClearEffects();
67  Modifiers.Clear();
68  // Close sound file
69  SoundFile.Close();
70 }
71 
73 {
74  // Clear sound bank
75  C4SoundEffect *csfx,*next;
76  for (csfx=FirstSound; csfx; csfx=next)
77  {
78  next=csfx->Next;
79  delete csfx;
80  }
81  FirstSound=nullptr;
82 }
83 
85 {
86 #if AUDIO_TK == AUDIO_TK_OPENAL
88 #endif
89  C4SoundEffect *csfx;
90  for (csfx=FirstSound; csfx; csfx=csfx->Next)
91  {
92  // Instance removal check
93  csfx->Execute();
94  }
95 }
96 
97 C4SoundEffect* C4SoundSystem::GetEffect(const char *szSndName)
98 {
99  // Remember wildcards before adding .* extension - if there are 2 versions with different file extensions, play the last added
100  bool bRandomSound = SCharCount('?',szSndName) || SCharCount('*',szSndName);
101  // Evaluate sound name
102  char szName[C4MaxSoundName+2+1];
103  SCopy(szSndName,szName,C4MaxSoundName);
104  // Any extension accepted
105  DefaultExtension(szName,"*");
106  // Play nth Sound. Standard: 1
107  int32_t iNumber = 1;
108  // Sound with a wildcard: determine number of available matches
109  if (bRandomSound)
110  {
111  iNumber = 0;
112  // Count matching sounds
113  for (C4SoundEffect *pSfx=FirstSound; pSfx; pSfx=pSfx->Next)
114  if (WildcardMatch(szName,pSfx->Name))
115  ++iNumber;
116  // Nothing found? Abort
117  if(iNumber == 0)
118  return nullptr;
119  iNumber=UnsyncedRandom(iNumber)+1;
120  }
121  // Find requested sound effect in bank
122  C4SoundEffect *pSfx;
123  for (pSfx=FirstSound; pSfx; pSfx=pSfx->Next)
124  if (WildcardMatch(szName,pSfx->Name))
125  if(!--iNumber)
126  break;
127  return pSfx; // Is still nullptr if nothing is found
128 }
129 
130 C4SoundInstance *C4SoundSystem::NewEffect(const char *szSndName, bool fLoop, int32_t iVolume, C4Object *pObj, int32_t iCustomFalloffDistance, int32_t iPitch, C4SoundModifier *modifier)
131 {
132  // Sound not active
133  if (!Config.Sound.RXSound) return nullptr;
134  // Get sound
135  C4SoundEffect *csfx;
136  if (!(csfx = GetEffect(szSndName)))
137  {
138  // Warn about missing or incorrectly spelled sound to allow finding mistakes earlier.
139  DebugLogF("Warning: could not find sound matching '%s'", szSndName);
140  return nullptr;
141  }
142  // Play
143  return csfx->New(fLoop, iVolume, pObj, iCustomFalloffDistance, iPitch, modifier);
144 }
145 
147 {
148  char szName[C4MaxSoundName+2+1];
149  // Evaluate sound name (see GetEffect)
150  SCopy(szSndName,szName,C4MaxSoundName);
151  DefaultExtension(szName,"*");
152  // Find an effect with a matching instance
153  for (C4SoundEffect *csfx = FirstSound; csfx; csfx = csfx->Next)
154  if (WildcardMatch(szName, csfx->Name))
155  {
156  C4SoundInstance *pInst = csfx->GetInstance(pObj);
157  if (pInst) return pInst;
158  }
159  return nullptr;
160 }
161 
162 // LoadEffects will load all sound effects of all known sound types (i.e. *.wav and *.ogg) as defined in C4CFN_SoundFiles
163 
164 int32_t C4SoundSystem::LoadEffects(C4Group &hGroup, const char *namespace_prefix, bool group_is_root)
165 {
166  // Local definition sounds: If there is a Sound.ocg in the group, load the sound from there
167  if(group_is_root && hGroup.FindEntry(C4CFN_Sound))
168  {
169  C4Group g;
170  g.OpenAsChild(&hGroup, C4CFN_Sound, false, false);
171  return LoadEffects(g, namespace_prefix, false);
172  }
173  int32_t iNum=0;
174  char szFilename[_MAX_FNAME+1];
175  char szFileType[_MAX_FNAME+1];
176  C4SoundEffect *nsfx;
177  // Process segmented list of file types
178  for (int32_t i = 0; SCopySegment(C4CFN_SoundFiles, i, szFileType, '|', _MAX_FNAME); i++)
179  {
180  // Search all sound files in group
181  hGroup.ResetSearch();
182  while (hGroup.FindNextEntry(szFileType, szFilename))
183  // Create and load effect
184  if ((nsfx = new C4SoundEffect))
185  {
186  if (nsfx->Load(szFilename, hGroup, namespace_prefix))
187  {
188  // Overload same name effects
189  RemoveEffect(nsfx->Name);
190  // Add effect
191  nsfx->Next=FirstSound;
192  FirstSound=nsfx;
193  iNum++;
194  }
195  else
196  delete nsfx;
197  }
198  }
199  // Load subgroups from Sound.ocg and other subgroups
200  if (!group_is_root)
201  {
202  hGroup.ResetSearch();
203  while (hGroup.FindNextEntry(C4CFN_SoundSubgroups, szFilename))
204  {
205  // Load from subgroup as a sub-namespace
206  // get namespace name
207  StdStrBuf sub_namespace;
208  if (namespace_prefix)
209  {
210  sub_namespace.Copy(namespace_prefix);
211  sub_namespace.Append("::");
212  }
213  sub_namespace.Append(szFilename, strlen(szFilename) - strlen(C4CFN_SoundSubgroups) + 1);
214  // load from child group
215  C4Group subgroup;
216  if (subgroup.OpenAsChild(&hGroup, szFilename, false, false))
217  {
218  iNum += LoadEffects(subgroup, sub_namespace.getData(), false);
219  }
220  }
221  }
222  return iNum;
223 }
224 
225 int32_t C4SoundSystem::RemoveEffect(const char *szFilename)
226 {
227  int32_t iResult=0;
228  C4SoundEffect *pNext,*pPrev=nullptr;
229  for (C4SoundEffect *pSfx=FirstSound; pSfx; pSfx=pNext)
230  {
231  pNext=pSfx->Next;
232  if (WildcardMatch(szFilename,pSfx->Name))
233  {
234  delete pSfx;
235  if (pPrev) pPrev->Next=pNext;
236  else FirstSound=pNext;
237  iResult++;
238  }
239  else
240  pPrev=pSfx;
241  }
242  return iResult;
243 }
244 
246 {
247  for (C4SoundEffect *pEff=FirstSound; pEff; pEff=pEff->Next)
248  pEff->ClearPointers(pObj);
249 }
250 
252 {
253  // Return by searching through effect linked list.
254  for (C4SoundEffect *pSfx = FirstSound; pSfx; pSfx = pSfx->Next)
255  if (pSfx->FirstInst) return pSfx->FirstInst;
256  return nullptr;
257 }
258 
260 {
261  // Return by searching through instance linked list and parent linked list of effects
262  assert(prev && prev->pEffect);
263  if (prev->pNext) return prev->pNext;
264  for (C4SoundEffect *pSfx = prev->pEffect->Next; pSfx; pSfx = pSfx->Next)
265  if (pSfx->FirstInst) return pSfx->FirstInst;
266  return nullptr;
267 }
268 
269 C4SoundInstance *StartSoundEffect(const char *szSndName, bool fLoop, int32_t iVolume, C4Object *pObj, int32_t iCustomFalloffDistance, int32_t iPitch, C4SoundModifier *modifier)
270 {
271  // Sound check
272  if (!Config.Sound.RXSound) return nullptr;
273  // Start new
274  return Application.SoundSystem.NewEffect(szSndName, fLoop, iVolume, pObj, iCustomFalloffDistance, iPitch, modifier);
275 }
276 
277 C4SoundInstance *StartSoundEffectAt(const char *szSndName, int32_t iX, int32_t iY, int32_t iVolume, int32_t iCustomFallofDistance, int32_t iPitch, C4SoundModifier *modifier)
278 {
279  // Sound check
280  if (!Config.Sound.RXSound) return nullptr;
281  // Create
282  C4SoundInstance *pInst = StartSoundEffect(szSndName, false, iVolume, nullptr, iCustomFallofDistance, iPitch, modifier);
283  // Set volume by position
284  if (pInst) pInst->SetVolumeByPos(iX, iY);
285  // Return
286  return pInst;
287 }
288 
289 C4SoundInstance *GetSoundInstance(const char *szSndName, C4Object *pObj)
290 {
291  return Application.SoundSystem.FindInstance(szSndName, pObj);
292 }
293 
294 void StopSoundEffect(const char *szSndName, C4Object *pObj)
295 {
296  // Find instance
297  C4SoundInstance *pInst = Application.SoundSystem.FindInstance(szSndName, pObj);
298  if (!pInst) return;
299  // Stop
300  pInst->Stop();
301 }
302 void SoundLevel(const char *szSndName, C4Object *pObj, int32_t iLevel)
303 {
304  // Sound level zero? Stop
305  if (!iLevel) { StopSoundEffect(szSndName, pObj); return; }
306  // Find or create instance
307  C4SoundInstance *pInst = Application.SoundSystem.FindInstance(szSndName, pObj);
308  if (!pInst) pInst = StartSoundEffect(szSndName, true, iLevel, pObj);
309  if (!pInst) return;
310  // Set volume
311  pInst->SetVolume(iLevel);
312  pInst->Execute();
313 }
314 
315 void SoundPan(const char *szSndName, C4Object *pObj, int32_t iPan)
316 {
317  // Find instance
318  C4SoundInstance *pInst = Application.SoundSystem.FindInstance(szSndName, pObj);
319  if (!pInst) return;
320  // Set pan
321  pInst->SetPan(iPan);
322  pInst->Execute();
323 }
324 
325 void SoundPitch(const char *szSndName, C4Object *pObj, int32_t iPitch)
326 {
327  // Find instance
328  C4SoundInstance *pInst = Application.SoundSystem.FindInstance(szSndName, pObj);
329  if (!pInst) return;
330  // Set pitch
331  pInst->SetPitch(iPitch);
332  pInst->Execute();
333 }
334 
335 void SoundUpdate(C4SoundInstance *pInst, int32_t iLevel, int32_t iPitch)
336 {
337  // Set sound data
338  pInst->SetVolume(iLevel);
339  pInst->SetPitch(iPitch);
340  // Ensure it's reflected in audio engine
341  pInst->Execute();
342 }
const char * getData() const
Definition: StdBuf.h:450
C4SoundSystem SoundSystem
Definition: C4Application.h:42
bool FindEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr)
Definition: C4Group.cpp:1774
C4Config Config
Definition: C4Config.cpp:837
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:243
int32_t LoadEffects(C4Group &hGroup, const char *namespace_prefix, bool group_is_root)
C4SoundInstance * StartSoundEffectAt(const char *szSndName, int32_t iX, int32_t iY, int32_t iVolume, int32_t iCustomFallofDistance, int32_t iPitch, C4SoundModifier *modifier)
int32_t RXSound
Definition: C4Config.h:127
void SelectContext()
C4SoundEffect * GetEffect(const char *szSound)
char Name[C4MaxSoundName+1]
void SetPitch(int32_t inPitch)
void StopSoundEffect(const char *szSndName, C4Object *pObj)
const int32_t C4MaxSoundName
Definition: C4SoundSystem.h:27
C4SoundInstance * pNext
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil)
Definition: Standard.cpp:290
#define C4CFN_SoundSubgroups
Definition: C4Components.h:27
C4SoundEffect * Next
C4SoundInstance * StartSoundEffect(const char *szSndName, bool fLoop, int32_t iVolume, C4Object *pObj, int32_t iCustomFalloffDistance, int32_t iPitch, C4SoundModifier *modifier)
void SoundUpdate(C4SoundInstance *pInst, int32_t iLevel, int32_t iPitch)
int iResult
Definition: C4GroupMain.cpp:39
bool DebugLogF(const char *strMessage...)
Definition: C4Log.cpp:281
C4SoundModifierList Modifiers
Definition: C4SoundSystem.h:54
#define C4CFN_SoundFiles
Definition: C4Components.h:171
bool IsOpen() const
Definition: C4Group.cpp:1891
C4SoundInstance * GetNextInstance(C4SoundInstance *prev) const
bool Load(const char *szFileName, C4Group &hGroup, const char *namespace_prefix)
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:527
#define C4CFN_Sound
Definition: C4Components.h:26
bool Close()
Definition: C4Group.cpp:755
bool Open(C4Group &hGroup, const char *filename) const
Definition: C4Reloc.cpp:69
const int32_t C4MaxSoundInstances
Definition: C4SoundSystem.h:28
C4SoundEffect * FirstSound
Definition: C4SoundSystem.h:57
#define _MAX_FNAME
void DefaultExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:284
void SoundPitch(const char *szSndName, C4Object *pObj, int32_t iPitch)
C4SoundInstance * New(bool fLoop=false, int32_t iVolume=100, C4Object *pObj=nullptr, int32_t iCustomFalloffDistance=0, int32_t iPitch=0, C4SoundModifier *modifier=nullptr)
C4Reloc Reloc
Definition: C4Reloc.cpp:22
void SetPan(int32_t inPan)
void SetVolume(int32_t inVolume)
C4SoundInstance * GetFirstInstance() const
bool FindNextEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr, bool fStartAtFilename=false)
Definition: C4Group.cpp:1780
void SoundPan(const char *szSndName, C4Object *pObj, int32_t iPan)
C4SoundEffect * pEffect
C4Group SoundFile
Definition: C4SoundSystem.h:56
C4SoundInstance * GetSoundInstance(const char *szSndName, C4Object *pObj)
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:384
C4SoundInstance * NewEffect(const char *szSound, bool fLoop=false, int32_t iVolume=100, C4Object *pObj=nullptr, int32_t iCustomFalloffDistance=0, int32_t iPitch=0, C4SoundModifier *modifier=nullptr)
void ClearPointers(C4Object *pObj)
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1013
void SoundLevel(const char *szSndName, C4Object *pObj, int32_t iLevel)
C4ConfigSound Sound
Definition: C4Config.h:255
C4SoundInstance * FindInstance(const char *szSound, C4Object *pObj)
void Copy()
Definition: StdBuf.h:475
void SetVolumeByPos(int32_t x, int32_t y)
int32_t RemoveEffect(const char *szFilename)
C4Application Application
Definition: C4Globals.cpp:44
bool InitializeMOD()
bool OpenAsChild(C4Group *pMother, const char *szEntryName, bool fExclusive=false, bool fCreate=false)
Definition: C4Group.cpp:1585
C4MusicSystem MusicSystem
Definition: C4Application.h:41