OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4SoundInstance Class Reference

#include <C4SoundInstance.h>

Collaboration diagram for C4SoundInstance:
[legend]

Public Member Functions

 ~C4SoundInstance ()
 
C4ObjectgetObj () const
 
bool isStarted () const
 
void Clear ()
 
bool Create (C4SoundEffect *pEffect, bool fLoop=false, int32_t iVolume=100, C4Object *pObj=nullptr, int32_t iNearInstanceMax=0, int32_t iFalloffDistance=0, int32_t inPitch=0, C4SoundModifier *modifier=nullptr)
 
bool CheckStart ()
 
bool Start ()
 
bool Stop ()
 
bool Playing ()
 
void Execute ()
 
void SetVolume (int32_t inVolume)
 
void SetPan (int32_t inPan)
 
void SetPitch (int32_t inPitch)
 
void SetVolumeByPos (int32_t x, int32_t y)
 
void SetObj (C4Object *pnObj)
 
void ClearPointers (C4Object *pObj)
 
bool Inside (int32_t iX, int32_t iY, int32_t iRad)
 
C4SoundModifierGetModifier () const
 
void SetModifier (C4SoundModifier *new_modifier, bool is_global)
 
void SetPlayer (int32_t new_player)
 

Protected Member Functions

 C4SoundInstance ()
 

Protected Attributes

C4SoundEffectpEffect
 
int32_t iVolume
 
int32_t iPan
 
int32_t iPitch
 
int32_t iChannel
 
bool pitch_dirty
 
C4TimeMilliseconds tStarted
 
int32_t iNearInstanceMax
 
bool fLooping
 
C4ObjectpObj
 
int32_t iFalloffDistance
 
C4SoundModifiermodifier
 
bool has_local_modifier
 
C4SoundInstancepNext
 
int32_t player
 

Friends

class C4SoundEffect
 
class C4SoundSystem
 
class C4SoundModifier
 

Detailed Description

Definition at line 57 of file C4SoundInstance.h.

Constructor & Destructor Documentation

C4SoundInstance::C4SoundInstance ( )
protected

Definition at line 202 of file C4SoundInstance.cpp.

202  :
203  pEffect (nullptr),
204  iVolume(0), iPan(0), iPitch(0),
205  player(NO_OWNER),
206  pitch_dirty(false),
207  iChannel (-1),
208  modifier(nullptr),
209  has_local_modifier(false),
210  pNext (nullptr)
211 {
212 }
C4SoundModifier * modifier
C4SoundInstance * pNext
const int NO_OWNER
Definition: C4Constants.h:138
C4SoundEffect * pEffect
C4SoundInstance::~C4SoundInstance ( )

Definition at line 214 of file C4SoundInstance.cpp.

References Clear().

215 {
216  Clear();
217 }

Here is the call graph for this function:

Member Function Documentation

bool C4SoundInstance::CheckStart ( )

Definition at line 265 of file C4SoundInstance.cpp.

References C4NearSoundRadius, fLooping, C4SoundEffect::GetStartedInstanceCount(), C4Object::GetX(), C4Object::GetY(), iNearInstanceMax, isStarted(), C4SoundEffect::Length, C4TimeMilliseconds::Now(), pEffect, pObj, Start(), and tStarted.

Referenced by Execute().

266 {
267  // already started?
268  if (isStarted()) return true;
269  // don't bother if half the time is up and the sound is not looping
271  return false;
272  // do near-instances check
273  int32_t iNearInstances = pObj ? pEffect->GetStartedInstanceCount(pObj->GetX(), pObj->GetY(), C4NearSoundRadius)
275  // over maximum?
276  if (iNearInstances > iNearInstanceMax) return false;
277  // Start
278  return Start();
279 }
int32_t GetY() const
Definition: C4Object.h:287
const int32_t C4NearSoundRadius
Definition: C4SoundSystem.h:29
bool isStarted() const
int32_t GetStartedInstanceCount(int32_t iX, int32_t iY, int32_t iRad)
int32_t GetX() const
Definition: C4Object.h:286
C4SoundEffect * pEffect
int32_t iNearInstanceMax
static C4TimeMilliseconds Now()
C4TimeMilliseconds tStarted

Here is the call graph for this function:

Here is the caller graph for this function:

void C4SoundInstance::Clear ( )

Definition at line 219 of file C4SoundInstance.cpp.

References C4SoundModifier::DelRef(), has_local_modifier, iChannel, modifier, and Stop().

Referenced by ~C4SoundInstance().

220 {
221  Stop();
222  iChannel = -1;
223  if (modifier)
224  {
225  modifier->DelRef();
226  modifier = nullptr;
227  has_local_modifier = false;
228  }
229 }
C4SoundModifier * modifier

Here is the call graph for this function:

Here is the caller graph for this function:

void C4SoundInstance::ClearPointers ( C4Object pObj)

Definition at line 410 of file C4SoundInstance.cpp.

References fLooping, C4Object::GetX(), C4Object::GetY(), Playing(), pObj, SetVolumeByPos(), and Stop().

Referenced by Execute().

411 {
412  if (!Playing()) { Stop(); return; }
413  if (pObj == pDelete)
414  {
415  // stop if looping (would most likely loop forever)
416  if (fLooping)
417  Stop();
418  // otherwise: set volume by last position
419  else
420  SetVolumeByPos(pObj->GetX(), pObj->GetY());
421  pObj = nullptr;
422  }
423 }
int32_t GetY() const
Definition: C4Object.h:287
int32_t GetX() const
Definition: C4Object.h:286
void SetVolumeByPos(int32_t x, int32_t y)

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4SoundInstance::Create ( C4SoundEffect pEffect,
bool  fLoop = false,
int32_t  iVolume = 100,
C4Object pObj = nullptr,
int32_t  iNearInstanceMax = 0,
int32_t  iFalloffDistance = 0,
int32_t  inPitch = 0,
C4SoundModifier modifier = nullptr 
)

Definition at line 231 of file C4SoundInstance.cpp.

References C4SoundModifier::AddRef(), Config, Execute(), fLooping, has_local_modifier, iChannel, iFalloffDistance, iNearInstanceMax, iPan, iPitch, iVolume, NO_OWNER, C4TimeMilliseconds::Now(), pEffect, pitch_dirty, Playing(), pObj, C4ConfigSound::RXSound, SetPlayer(), C4Config::Sound, Stop(), and tStarted.

Referenced by C4SoundEffect::New().

232 {
233  // Sound check
234  if (!Config.Sound.RXSound || !pnEffect) return false;
235  // Already playing? Stop
236  if (Playing()) { Stop(); return false; }
237  // Set effect
238  pEffect = pnEffect;
239  // Set
241  iVolume = inVolume; iPan = 0; iChannel = -1;
242  iPitch = inPitch; pitch_dirty = (iPitch != 0);
243  iNearInstanceMax = inNearInstanceMax;
245  pObj = pnObj;
246  fLooping = fLoop;
247  if ((this->modifier = modifier))
248  {
249  modifier->AddRef();
250  has_local_modifier = true;
251  }
252  SetPlayer(NO_OWNER); // may be updated on first execution
253  // Start
254  Execute();
255  return true;
256 }
C4Config Config
Definition: C4Config.cpp:837
int32_t RXSound
Definition: C4Config.h:127
void SetPlayer(int32_t new_player)
int32_t iFalloffDistance
const int NO_OWNER
Definition: C4Constants.h:138
int32_t iNearInstanceMax
C4ConfigSound Sound
Definition: C4Config.h:255
static C4TimeMilliseconds Now()
C4TimeMilliseconds tStarted

Here is the call graph for this function:

Here is the caller graph for this function:

void C4SoundInstance::Execute ( )

Definition at line 347 of file C4SoundInstance.cpp.

References C4Object::Audible, C4Object::AudiblePan, C4Object::AudiblePlayer, C4AudibilityRadius, CheckStart(), Clamp(), ClearPointers(), Config, iChannel, iFalloffDistance, iPan, iPitch, isStarted(), iVolume, pitch_dirty, player, pObj, SetPlayer(), C4Config::Sound, C4ConfigSound::SoundVolume, and C4PropList::Status.

Referenced by Create(), SoundLevel(), SoundPan(), SoundPitch(), SoundUpdate(), and Start().

348 {
349  // Object deleted?
350  if (pObj && !pObj->Status) ClearPointers(pObj);
351  // initial values
352  int32_t iVol = iVolume * 256 * Config.Sound.SoundVolume / 100, iPan = C4SoundInstance::iPan;
353  // bound to an object?
354  if (pObj)
355  {
356  int iAudibility = pObj->Audible;
358  // apply custom falloff distance
359  if (iFalloffDistance)
360  {
361  iAudibility = Clamp<int32_t>(100 + (iAudibility - 100) * C4AudibilityRadius / iFalloffDistance, 0,100);
362  }
363  iVol = iVol * iAudibility / 100;
364  iPan += pObj->AudiblePan;
365  }
366  // sound off?
367  if (!iVol)
368  {
369  // stop, if started
370  if (isStarted())
371  {
372 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
373  Mix_HaltChannel(iChannel);
374 #elif AUDIO_TK == AUDIO_TK_OPENAL
375  alDeleteSources(1, (ALuint*)&iChannel);
376 #endif
377  iChannel = -1;
378  }
379  }
380  else
381  {
382  // start
383  if (!isStarted())
384  if (!CheckStart())
385  return;
386  // set volume & panning
387 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
388  Mix_Volume(iChannel, (iVol * MIX_MAX_VOLUME) / (100 * 256));
389  Mix_SetPanning(iChannel, Clamp((100 - iPan) * 256 / 100, 0, 255), Clamp((100 + iPan) * 256 / 100, 0, 255));
390 #elif AUDIO_TK == AUDIO_TK_OPENAL
391  alSource3f(iChannel, AL_POSITION, Clamp<float>(float(iPan)/100.0f, -1.0f, +1.0f), 0, -0.7f);
392  alSourcef(iChannel, AL_GAIN, float(iVol) / (100.0f*256.0f));
393  if (pitch_dirty)
394  {
395  // set pitch; map -90..+100 range to 0.1f..2.0f
396  alSourcef(iChannel, AL_PITCH, std::max(float(iPitch + 100) / 100.0f, 0.1f));
397  pitch_dirty = false;
398  }
399 #endif
400  }
401 }
C4Config Config
Definition: C4Config.cpp:837
void ClearPointers(C4Object *pObj)
void SetPlayer(int32_t new_player)
int32_t iFalloffDistance
int32_t SoundVolume
Definition: C4Config.h:133
bool isStarted() const
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:46
int32_t AudiblePlayer
Definition: C4Object.h:121
int32_t AudiblePan
Definition: C4Object.h:121
int32_t Status
Definition: C4PropList.h:170
const int32_t C4AudibilityRadius
Definition: C4SoundSystem.h:30
int32_t Audible
Definition: C4Object.h:121
C4ConfigSound Sound
Definition: C4Config.h:255

Here is the call graph for this function:

Here is the caller graph for this function:

C4SoundModifier* C4SoundInstance::GetModifier ( ) const
inline

Definition at line 101 of file C4SoundInstance.h.

References modifier.

101 { return modifier; }
C4SoundModifier * modifier
C4Object* C4SoundInstance::getObj ( ) const
inline

Definition at line 85 of file C4SoundInstance.h.

References pObj.

85 { return pObj; }
bool C4SoundInstance::Inside ( int32_t  iX,
int32_t  iY,
int32_t  iRad 
)

Definition at line 425 of file C4SoundInstance.cpp.

References C4Object::GetX(), C4Object::GetY(), and pObj.

426 {
427  return pObj &&
428  (pObj->GetX() - iX) * (pObj->GetX() - iX) + (pObj->GetY() - iY) * (pObj->GetY() - iY) <= iRad * iRad;
429 }
int32_t GetY() const
Definition: C4Object.h:287
int32_t GetX() const
Definition: C4Object.h:286

Here is the call graph for this function:

bool C4SoundInstance::isStarted ( ) const
inline

Definition at line 86 of file C4SoundInstance.h.

Referenced by CheckStart(), Execute(), SetModifier(), and Start().

86 { return iChannel != -1; }

Here is the caller graph for this function:

bool C4SoundInstance::Playing ( )

Definition at line 328 of file C4SoundInstance.cpp.

References Application, fLooping, iChannel, C4MusicSystem::MODInitialized, C4Application::MusicSystem, and pEffect.

Referenced by ClearPointers(), Create(), and Stop().

329 {
330  if (!pEffect) return false;
331  if (fLooping) return true;
332 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
333  return Application.MusicSystem.MODInitialized && (iChannel != -1) && Mix_Playing(iChannel);
334 #elif AUDIO_TK == AUDIO_TK_OPENAL
335  if (iChannel == -1)
336  return false;
337  else
338  {
339  ALint state;
340  alGetSourcei(iChannel, AL_SOURCE_STATE, &state);
341  return state == AL_PLAYING;
342  }
343 #endif
344  return false;
345 }
C4SoundEffect * pEffect
C4Application Application
Definition: C4Globals.cpp:44
C4MusicSystem MusicSystem
Definition: C4Application.h:41

Here is the caller graph for this function:

void C4SoundInstance::SetModifier ( C4SoundModifier new_modifier,
bool  is_global 
)

Definition at line 431 of file C4SoundInstance.cpp.

References C4SoundModifier::AddRef(), C4SoundModifier::ApplyTo(), C4SoundModifier::DelRef(), has_local_modifier, iChannel, isStarted(), and modifier.

Referenced by SetPlayer().

432 {
433  // do not overwrite local modifier with global one
434  if (is_global)
435  {
436  if (has_local_modifier) return;
437  }
438  else
439  {
440  // this sound has its own modifier now and doesn't care for global ones
441  has_local_modifier = (new_modifier != nullptr);
442  }
443  if (new_modifier != modifier)
444  {
445  // update modifier and ref-count
446  C4SoundModifier *old_modifier = modifier;
447  modifier = new_modifier;
448  if (modifier) modifier->AddRef();
449  if (old_modifier) old_modifier->DelRef();
450  // Apply new modifier
451  if (isStarted())
452  {
453  if (modifier)
454  {
455 #if AUDIO_TK == AUDIO_TK_OPENAL
457 #endif
458  }
459  else
460  {
461 #if (AUDIO_TK == AUDIO_TK_OPENAL) && defined(HAVE_ALEXT)
462  alSource3i(iChannel, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, 0, AL_FILTER_NULL);
463 #endif
464  }
465  }
466  }
467 }
C4SoundModifier * modifier
bool isStarted() const
void ApplyTo(ALuint source)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4SoundInstance::SetObj ( C4Object pnObj)
inline

Definition at line 98 of file C4SoundInstance.h.

98 { pObj = pnObj; }
void C4SoundInstance::SetPan ( int32_t  inPan)
inline

Definition at line 95 of file C4SoundInstance.h.

Referenced by SoundPan().

95 { iPan = inPan; }

Here is the caller graph for this function:

void C4SoundInstance::SetPitch ( int32_t  inPitch)

Definition at line 258 of file C4SoundInstance.cpp.

References iPitch, and pitch_dirty.

Referenced by SoundPitch(), and SoundUpdate().

259 {
260  // set pitch and update on next call to Execute
261  iPitch = inPitch;
262  pitch_dirty = true;
263 }

Here is the caller graph for this function:

void C4SoundInstance::SetPlayer ( int32_t  new_player)

Definition at line 469 of file C4SoundInstance.cpp.

References Application, C4SoundModifierList::GetGlobalModifier(), C4SoundSystem::Modifiers, player, SetModifier(), and C4Application::SoundSystem.

Referenced by Create(), Execute(), and SetVolumeByPos().

470 {
471  // update player and associated sound modifier
472  player = new_player;
474 }
C4SoundSystem SoundSystem
Definition: C4Application.h:42
C4SoundModifierList Modifiers
Definition: C4SoundSystem.h:54
C4SoundModifier * GetGlobalModifier(int32_t player_index) const
C4Application Application
Definition: C4Globals.cpp:44
void SetModifier(C4SoundModifier *new_modifier, bool is_global)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4SoundInstance::SetVolume ( int32_t  inVolume)
inline

Definition at line 94 of file C4SoundInstance.h.

Referenced by SoundLevel(), and SoundUpdate().

94 { iVolume = inVolume; }

Here is the caller graph for this function:

void C4SoundInstance::SetVolumeByPos ( int32_t  x,
int32_t  y 
)

Definition at line 403 of file C4SoundInstance.cpp.

References C4ViewportList::GetAudibility(), iPan, iVolume, NO_OWNER, player, SetPlayer(), and Viewports.

Referenced by ClearPointers(), and StartSoundEffectAt().

404 {
405  int32_t vol_player = NO_OWNER;
406  iVolume = ::Viewports.GetAudibility(x, y, &iPan, 0, &vol_player);
407  if (vol_player != player) SetPlayer(vol_player);
408 }
void SetPlayer(int32_t new_player)
int32_t GetAudibility(int32_t iX, int32_t iY, int32_t *iPan, int32_t iAudibilityRadius=0, int32_t *outPlayer=nullptr)
const int NO_OWNER
Definition: C4Constants.h:138
C4ViewportList Viewports
Definition: C4Viewport.cpp:841

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4SoundInstance::Start ( )

Definition at line 281 of file C4SoundInstance.cpp.

References Application, C4SoundModifier::ApplyTo(), Execute(), fLooping, iChannel, isStarted(), modifier, C4MusicSystem::MODInitialized, C4Application::MusicSystem, pEffect, C4SoundEffect::pSample, and C4MusicSystem::SelectContext().

Referenced by CheckStart().

282 {
283 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
284  // Be paranoid about SDL_Mixer initialisation
285  if (!Application.MusicSystem.MODInitialized) return false;
286  if ((iChannel = Mix_PlayChannel(-1, pEffect->pSample, fLooping? -1 : 0)) == -1)
287  return false;
288 #elif AUDIO_TK == AUDIO_TK_OPENAL
290  alGenSources(1, (ALuint*)&iChannel);
291  alSourcei(iChannel, AL_BUFFER, pEffect->pSample);
292  alSourcei(iChannel, AL_LOOPING, fLooping ? AL_TRUE : AL_FALSE);
294  alSourcePlay(iChannel);
295 #else
296  return false;
297 #endif
298  // Safety: Don't execute if start failed, or Execute() would try to start again
299  if (!isStarted()) return false;
300  // Update volume
301  Execute();
302  return true;
303 }
C4SoundModifier * modifier
C4SoundHandle pSample
void SelectContext()
bool isStarted() const
void ApplyTo(ALuint source)
C4SoundEffect * pEffect
C4Application Application
Definition: C4Globals.cpp:44
C4MusicSystem MusicSystem
Definition: C4Application.h:41

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4SoundInstance::Stop ( )

Definition at line 305 of file C4SoundInstance.cpp.

References fLooping, iChannel, pEffect, Playing(), and tStarted.

Referenced by Clear(), ClearPointers(), Create(), and StopSoundEffect().

306 {
307  if (!pEffect) return false;
308  // Stop sound
309  bool fRet = true;
310 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
311  // iChannel == -1 will halt all channels. Is that right?
312  if (Playing())
313  Mix_HaltChannel(iChannel);
314 #elif AUDIO_TK == AUDIO_TK_OPENAL
315  if (iChannel != -1)
316  {
317  if (Playing()) alSourceStop(iChannel);
318  ALuint c = iChannel;
319  alDeleteSources(1, &c);
320  }
321 #endif
322  iChannel = -1;
323  tStarted = 0;
324  fLooping = false;
325  return fRet;
326 }
C4SoundEffect * pEffect
C4TimeMilliseconds tStarted

Here is the call graph for this function:

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class C4SoundEffect
friend

Definition at line 59 of file C4SoundInstance.h.

friend class C4SoundModifier
friend

Definition at line 61 of file C4SoundInstance.h.

friend class C4SoundSystem
friend

Definition at line 60 of file C4SoundInstance.h.

Member Data Documentation

bool C4SoundInstance::fLooping
protected

Definition at line 72 of file C4SoundInstance.h.

Referenced by CheckStart(), ClearPointers(), Create(), Playing(), Start(), and Stop().

bool C4SoundInstance::has_local_modifier
protected

Definition at line 76 of file C4SoundInstance.h.

Referenced by Clear(), Create(), and SetModifier().

int32_t C4SoundInstance::iChannel
protected

Definition at line 68 of file C4SoundInstance.h.

Referenced by Clear(), Create(), Execute(), Playing(), SetModifier(), Start(), and Stop().

int32_t C4SoundInstance::iFalloffDistance
protected

Definition at line 74 of file C4SoundInstance.h.

Referenced by Create(), and Execute().

int32_t C4SoundInstance::iNearInstanceMax
protected

Definition at line 71 of file C4SoundInstance.h.

Referenced by CheckStart(), and Create().

int32_t C4SoundInstance::iPan
protected

Definition at line 68 of file C4SoundInstance.h.

Referenced by Create(), Execute(), and SetVolumeByPos().

int32_t C4SoundInstance::iPitch
protected

Definition at line 68 of file C4SoundInstance.h.

Referenced by Create(), Execute(), and SetPitch().

int32_t C4SoundInstance::iVolume
protected

Definition at line 68 of file C4SoundInstance.h.

Referenced by Create(), Execute(), and SetVolumeByPos().

C4SoundModifier* C4SoundInstance::modifier
protected

Definition at line 75 of file C4SoundInstance.h.

Referenced by Clear(), GetModifier(), SetModifier(), and Start().

C4SoundEffect* C4SoundInstance::pEffect
protected
bool C4SoundInstance::pitch_dirty
protected

Definition at line 69 of file C4SoundInstance.h.

Referenced by Create(), Execute(), and SetPitch().

int32_t C4SoundInstance::player
protected

Definition at line 83 of file C4SoundInstance.h.

Referenced by Execute(), SetPlayer(), and SetVolumeByPos().

C4Object* C4SoundInstance::pObj
protected

Definition at line 73 of file C4SoundInstance.h.

Referenced by CheckStart(), ClearPointers(), Create(), Execute(), getObj(), and Inside().

C4TimeMilliseconds C4SoundInstance::tStarted
protected

Definition at line 70 of file C4SoundInstance.h.

Referenced by CheckStart(), Create(), and Stop().


The documentation for this class was generated from the following files: