Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
effectsound.c
Go to the documentation of this file.
1 
5 {
10  ref ScriptInvoker Event_OnSoundWaveStarted = new ScriptInvoker();
11  ref ScriptInvoker Event_OnSoundWaveEnded = new ScriptInvoker();
12  ref ScriptInvoker Event_OnSoundFadeInStopped = new ScriptInvoker();
13  ref ScriptInvoker Event_OnSoundFadeOutStarted = new ScriptInvoker();
15 
20  protected ref SoundParams m_SoundParams;
21  protected ref SoundObjectBuilder m_SoundObjectBuilder;
22  protected ref SoundObject m_SoundObject;
23  protected AbstractWave m_SoundWaveObject;
25 
30  protected WaveKind m_SoundWaveKind;
31  protected string m_SoundSetName;
32  protected bool m_SoundLoop;
33  protected bool m_SetEnvVariables;
34  protected bool m_SoundAutodestroy;
35  protected bool m_SoundWaveIsPlaying;
36  protected float m_SoundWaveLenght;
37  protected float m_SoundWaveVolume;
38  protected float m_SoundWaveVolumeMax;
39  protected float m_SoundWaveTime;
40  protected int m_SoundDoppler;
42 
47  protected bool m_SoundWaveStarting;
48  protected bool m_SoundWaveStopping;
49  protected bool m_SoundFadedOut;
50 
51  protected float m_SoundFadeInDuration;
52 
53  protected float m_SoundFadeOutStartTime;
54  protected float m_SoundFadeOutDuration;
55  protected float m_SoundFadeOutInitVolume;
57 
58 
59 
63  void EffectSound()
64  {
65  m_SoundWaveKind = WaveKind.WAVEEFFECTEX;
66  m_SoundWaveVolume = 0;
67  m_SoundWaveVolumeMax = 1;
68  m_SoundAutodestroy = false;
69  m_SoundWaveStopping = false;
70  m_SoundFadedOut = false;
71  m_SoundDoppler = -1;
72  }
73 
77  void ~EffectSound()
78  {
79 
80  }
81 
85  override void InitEffect()
86  {
87  super.InitEffect();
88 
89  // These will be called by the sound events
92  }
93 
97  override string GetDebugName()
98  {
99  string identifier;
100  if (m_SoundSetName != "")
101  {
102  identifier = m_SoundSetName;
103  }
104  else
105  {
106  identifier = "NO_SOUNDSET";
107  }
108 
109  return string.Format("%1:%2", super.GetDebugName(), identifier);
110  }
111 
112 
113 
118 
123  override EffectType GetEffectType()
124  {
125  return EffectType.SOUND;
126  }
127 
132  override bool IsSound()
133  {
134  return true;
135  }
136 
138 
139 
140 
146 
152  bool SoundPlayEx(out SoundParams params)
153  {
154  super.Start();
155 
156  if (m_SoundSetName != "")
157  {
158  vector position = GetCurrentLocalPosition();
159 
160  if ( SoundLoadEx(params) )
161  {
162  if (m_SetEnvVariables && m_SoundParams)
163  {
164  m_SoundObjectBuilder.AddEnvSoundVariables(GetPosition());
165  m_SoundObject = m_SoundObjectBuilder.BuildSoundObject();
166  m_SoundObject.SetKind( m_SoundWaveKind );
167  m_SoundObject.SetParent( m_ParentObject );
168  }
169 
170  if ( m_SoundObject )
171  {
172  SetCurrentLocalPosition(position, false);
173  m_SoundWaveObject = GetGame().GetSoundScene().Play3D( m_SoundObject, m_SoundObjectBuilder );
174  if ( !m_SoundWaveObject )
175  return false;
176 
177  // Wait for header to be loaded before asking for its length, else we block the main thread
178  if (m_SoundWaveObject.IsHeaderLoaded())
179  ValidateSoundWave();
180  else
181  m_SoundWaveObject.GetEvents().Event_OnSoundWaveHeaderLoaded.Insert(ValidateSoundWave);
182 
183  return true;
184  }
185  else
186  {
187  SoundError("m_SoundObject is null.");
188  }
189  }
190  }
191 
192  return false;
193  }
194 
199  bool SoundPlay()
200  {
201  SoundParams params;
202  return SoundPlayEx(params);
203  }
204 
208  override void Start()
209  {
210  SoundPlay();
211  }
212 
217  void SoundStop()
218  {
219  super.Stop();
220 
221  if ( IsSoundPlaying() )
222  {
223  if ( m_SoundFadeOutDuration > 0 && !m_SoundWaveStopping )
224  {
225  m_SoundWaveStopping = true;
226  m_SoundFadedOut = false;
227  m_SoundWaveStarting = false;
228  m_SoundFadeOutStartTime = m_SoundWaveTime;
229  }
230  else
231  {
232  m_SoundWaveObject.Stop();
233  }
234  }
235  else
236  {
237  SoundReset();
238  }
239  }
240 
244  override void Stop()
245  {
246  SoundStop();
247  }
248 
252  protected void SoundReset()
253  {
254  m_IsPlaying = false;
255  m_SoundWaveIsPlaying = false;
256  m_SoundWaveStopping = false;
257  m_SoundFadedOut = false;
258  m_SoundWaveVolume = m_SoundWaveVolumeMax;
259  m_SoundWaveTime = 0;
260  m_SoundFadeOutInitVolume = 0;
261  m_SoundFadeOutStartTime = 0;
262 
263  if ( m_SoundWaveObject )
264  {
265  m_SoundWaveObject.Stop();
266  m_SoundWaveObject.SetVolumeRelative( m_SoundWaveVolumeMax );
267  }
268  }
269 
274  bool IsSoundPlaying()
275  {
276  return m_SoundWaveIsPlaying;
277  }
278 
282  override bool IsPlaying()
283  {
284  return IsSoundPlaying(); // Just in case, as that's what used to be the actual way to check
285  }
286 
288 
289 
290 
295 
300  bool SoundLoadEx(out SoundParams params)
301  {
302  if ( !m_SoundParams || !m_SoundParams.IsValid() )
303  {
304  if (!params)
305  {
306  params = new SoundParams( m_SoundSetName );
307  }
308 
309  //Print("SoundLoad is loading..");
310  m_SoundParams = params;
311  if ( !m_SoundParams.IsValid() )
312  {
313  SoundError("Invalid sound set.");
314  return false;
315  }
316 
317  m_SoundObjectBuilder = new SoundObjectBuilder( m_SoundParams );
318  if (m_SetEnvVariables)
319  {
320  m_SoundObjectBuilder.AddEnvSoundVariables(GetPosition());
321  }
322 
323  m_SoundObject = m_SoundObjectBuilder.BuildSoundObject();
324 
325  if ( m_SoundObject )
326  {
327  m_SoundObject.SetKind( m_SoundWaveKind );
328  m_SoundObject.SetParent( m_ParentObject );
329  }
330  else
331  {
332  SoundError("m_SoundObject is null.");
333  }
334  }
335  else
336  {
337  //Print("SoundLoad is loaded.");
338  }
339 
340  return true;
341  }
342 
347  bool SoundLoad()
348  {
349  SoundParams params;
350  return SoundLoadEx(params);
351  }
352 
356  bool IsSoundValid()
357  {
358  return m_SoundParams.IsValid();
359  }
360 
365  protected void ValidateSoundWave()
366  {
367  m_SoundWaveLenght = m_SoundWaveObject.GetLength();
368 
369  if ( SoundWaveValidation() )
370  {
371  if ( m_SoundFadeInDuration > 0 )
372  {
373  m_SoundWaveObject.SetVolumeRelative( 0 );
374  m_SoundFadeOutStartTime = m_SoundWaveLenght - m_SoundFadeInDuration;
375  }
376 
377  SetSoundLoop( m_SoundLoop );
378 
379  m_SoundWaveStarting = true;
380 
381  AbstractWaveEvents events = m_SoundWaveObject.GetEvents();
382  events.Event_OnSoundWaveStarted.Insert( Event_OnSoundWaveStarted );
383  events.Event_OnSoundWaveEnded.Insert( Event_OnSoundWaveEnded );
384 
385  UpdateEvents();
386  }
387  else
388  {
389  m_SoundWaveObject.Stop();
390  }
391  }
392 
397  protected bool SoundWaveValidation()
398  {
399  bool valid = true;
400 
401  if ( m_SoundFadeInDuration > GetSoundWaveLength() )
402  {
403  SoundError("FadeIn is longer than sound wave length.");
404  valid = false;
405  }
406 
407  if ( m_SoundFadeOutDuration > GetSoundWaveLength() )
408  {
409  SoundError("FadeOut is longer than sound wave length.");
410  valid = false;
411  }
412 
413  if ( m_SoundFadeOutDuration + m_SoundFadeInDuration > GetSoundWaveLength() )
414  {
415  SoundError("FadeIn & FadeOut are longer than sound wave length.");
416  valid = false;
417  }
418 
419  return valid;
420  }
421 
427  protected void UpdateEvents()
428  {
429  if ( m_SoundWaveObject )
430  {
431  SetEnableEventFrame(true);
432  }
433  else
434  {
435  SetEnableEventFrame(false);
436  }
437  }
438 
440 
441 
442 
447 
454  override void Event_OnFrameUpdate(float time_delta)
455  {
456  if ( IsSoundPlaying() )
457  {
458  if (m_SoundDoppler != -1)
459  {
460  m_SoundWaveObject.SetDoppler(m_SoundDoppler);
461  }
462  // FadeIn
463  if ( m_SoundWaveStarting )
464  {
465  if ( m_SoundFadeInDuration > 0 )
466  {
467  SetSoundVolume( GetSoundVolume() + (time_delta / m_SoundFadeInDuration) );
468 
469  if ( GetSoundVolume() >= m_SoundWaveVolumeMax )
470  {
471  Event_OnSoundFadeInStopped();
472  SetSoundVolume( m_SoundWaveVolumeMax );
473  m_SoundWaveStarting = false;
474  }
475  }
476  else
477  {
478  SetSoundVolume( m_SoundWaveVolumeMax );
479  m_SoundWaveStarting = false;
480  }
481  }
482 
483  // FadeOut
484  if ( m_SoundWaveStopping )
485  {
486  if ( m_SoundFadeOutDuration > 0 )
487  {
488  if ( m_SoundFadeOutInitVolume == 0 )
489  {
490  m_SoundFadeOutInitVolume = GetSoundVolume();
491  Event_OnSoundFadeOutStarted();
492  }
493  SetSoundVolume( GetSoundVolume() - (m_SoundFadeOutInitVolume / m_SoundFadeOutDuration * time_delta) );
494  }
495  else
496  {
497  SetSoundVolume( 0 );
498  }
499 
500  if ( GetSoundVolume() <= 0 )
501  {
502  if ( m_SoundWaveObject )
503  {
504  m_SoundWaveObject.Stop();
505  m_SoundWaveStopping = false;
506  m_SoundFadedOut = true;
507  }
508  }
509  }
510 
511  // Counting timer here because loop play
512  m_SoundWaveTime += time_delta;
513  }
514  }
515 
521  override void Event_OnRegistered(int id)
522  {
523  super.Event_OnRegistered(id);
524 
525  Event_OnSoundWaveEnded.Insert( SEffectManager.Event_OnSoundWaveEnded );
526  }
527 
532  override void Event_OnUnregistered()
533  {
534  super.Event_OnUnregistered();
535 
536  Event_OnSoundWaveEnded.Remove( SEffectManager.Event_OnSoundWaveEnded );
537  }
538 
544  {
545  m_SoundWaveIsPlaying = true;
546 
547  Event_OnSoundWaveStarted.Invoke(this);
548 
550  }
551 
557  {
558  m_SoundWaveIsPlaying = false;
559 
560  Event_OnSoundWaveEnded.Invoke(this);
561 
563  }
564 
569  void Event_OnSoundFadeInStopped()
570  {
571  Event_OnSoundFadeInStopped.Invoke(this);
572  }
573 
578  void Event_OnSoundFadeOutStarted()
579  {
580  Event_OnSoundFadeOutStarted.Invoke(this);
581  }
582 
584 
585 
586 
591 
597  override void SetAutodestroy(bool auto_destroy)
598  {
599  super.SetAutodestroy(auto_destroy);
600  m_SoundAutodestroy = auto_destroy;
601  }
602 
607  override bool IsAutodestroy()
608  {
609  return IsSoundAutodestroy();
610  }
611 
616  void SetSoundAutodestroy(bool auto_destroy)
617  {
618  SetAutodestroy(auto_destroy);
619  }
620 
625  bool IsSoundAutodestroy()
626  {
627  return m_SoundAutodestroy;
628  }
629 
630  override bool CanDestroy()
631  {
632  return m_SoundFadeOutDuration <= 0 || m_SoundFadedOut;
633  }
634 
636 
637 
638 
643 
648  override void SetParent(Object parent_obj)
649  {
650  super.SetParent(parent_obj); // ...
651 
652  if (m_SoundObject)
653  {
654  m_SoundObject.SetParent(parent_obj);
655  }
656  }
657 
662  override Object GetParent()
663  {
664  if (m_SoundObject)
665  return Object.Cast(m_SoundObject.GetParent());
666  else
667  return super.GetParent();
668  }
669 
675  override Object GetCurrentParent()
676  {
677  if (m_SoundObject)
678  return Object.Cast(m_SoundObject.GetParent());
679  else
680  return super.GetParent(); // Yes, intentionally this one
681  }
682 
688  override void SetCurrentPosition( vector pos, bool updateCached = true )
689  {
690  super.SetCurrentPosition(pos, updateCached);
691 
692  if (m_SoundObject)
693  {
694  Object parent = GetParent();
695 
696  if (parent)
697  pos = parent.WorldToModel(pos);
698 
699  m_SoundObject.SetPosition(pos);
700  }
701  }
702 
707  override vector GetCurrentPosition()
708  {
709  if (m_SoundObject)
710  return m_SoundObject.GetPosition();
711 
712  if (m_ParentObject)
713  return m_ParentObject.ModelToWorld(GetPosition());
714 
715  return GetPosition();
716  }
717 
723  override void SetCurrentLocalPosition( vector pos, bool updateCached = true )
724  {
725  super.SetCurrentLocalPosition(pos, updateCached);
726 
727  if (m_SoundObject)
728  {
729  m_SoundObject.SetPosition(pos);
730  }
731  }
732 
737  override vector GetCurrentLocalPosition()
738  {
739  Object parent = GetParent();
740 
741  if (m_SoundObject)
742  {
743  //TODO(kumarjac): Create and expose 'SoundObject.GetLocalPosition'
744  if (parent)
745  return parent.WorldToModel(m_SoundObject.GetPosition());
746  else
747  return m_SoundObject.GetPosition();
748  }
749  else
750  {
751  if (parent)
752  return GetLocalPosition();
753  else
754  return GetPosition();
755  }
756 
757  return vector.Zero;
758  }
759 
765  void SetSoundWaveKind(WaveKind wave_kind)
766  {
767  m_SoundWaveKind = wave_kind;
768  }
769 
775  void SetSoundSet(string snd)
776  {
777  m_SoundSetName = snd;
778  }
779 
784  string GetSoundSet()
785  {
786  return m_SoundSetName;
787  }
788 
793  void SetSoundLoop(bool loop)
794  {
795  m_SoundLoop = loop;
796 
797  if ( m_SoundWaveObject )
798  m_SoundWaveObject.Loop( loop );
799  }
800 
805  void SetEnviromentVariables(bool setEnvVariables)
806  {
807  m_SetEnvVariables = setEnvVariables;
808  }
809 
815  float GetSoundWaveLenght()
816  {
817  return GetSoundWaveLength();
818  }
819 
824  float GetSoundWaveLength()
825  {
826  return m_SoundWaveLenght;
827  }
828 
833  void SetSoundVolume(float volume)
834  {
835  m_SoundWaveVolume = volume;
836  if ( m_SoundWaveObject )
837  m_SoundWaveObject.SetVolumeRelative( volume );
838  }
839 
844  float GetSoundVolume()
845  {
846  return m_SoundWaveVolume;
847  }
848 
855  void SetSoundMaxVolume(float volume)
856  {
857  m_SoundWaveVolumeMax = volume;
858  if ( m_SoundWaveObject )
859  m_SoundWaveObject.SetVolumeRelative( m_SoundWaveVolume );
860  }
861 
867  float GetSoundWaveTime()
868  {
869  return m_SoundWaveTime;
870  }
871 
876  void SetSoundFadeIn(float fade_in)
877  {
878  m_SoundFadeInDuration = fade_in;
879  }
880 
885  void SetSoundFadeOut(float fade_out)
886  {
887  m_SoundFadeOutDuration = fade_out;
888  }
889 
894  void SetDoppler(bool setDoppler)
895  {
897  m_SoundDoppler = 0;
898  if (setDoppler)
899  {
900  m_SoundDoppler = 1;
901  }
902  }
903 
905 
906 
907 
911  protected void SoundError(string err_msg)
912  {
913  ErrorEx(string.Format("%1: SoundSetName: '%2' :: %3", this, m_SoundSetName, err_msg));
914  }
915 }
GetGame
proto native CGame GetGame()
EffectType
EffectType
Enum to determine what type of effect the Effect is.
Definition: effect.c:2
m_IsPlaying
protected bool m_IsPlaying
Whether the Effect is currently playing.
Definition: effect.c:37
Event_OnSoundWaveEnded
ref ScriptInvoker Event_OnSoundWaveEnded
Definition: sound.c:115
SoundObject
class SoundObjectBuilder SoundObject(SoundParams soundParams)
m_ParentObject
protected Object m_ParentObject
Cached parent.
Definition: effect.c:39
SoundObjectBuilder
void SoundObjectBuilder(SoundParams soundParams)
SetEnableEventFrame
void SetEnableEventFrame(bool enable)
Enable Event_OnFrameUpdate for the effect.
Definition: effect.c:278
ErrorEx
enum ShapeType ErrorEx
SoundParams
Definition: sound.c:100
GetPosition
class JsonUndergroundAreaTriggerData GetPosition
Definition: undergroundarealoader.c:9
EffectSound
Wrapper class for managing sound through SEffectManager.
Definition: effectsound.c:4
Event_OnSoundWaveStarted
class SoundParams Event_OnSoundWaveStarted
vector
Definition: enconvert.c:105
Effect
void Effect()
ctor
Definition: effect.c:70
Object
Definition: objecttyped.c:1
WaveKind
WaveKind
Definition: sound.c:1
GetLocalPosition
vector GetLocalPosition()
Get the local position of the Effect.
Definition: effect.c:486
Event_OnStopped
ref ScriptInvoker Event_OnStopped
Event used when Stop was called.
Definition: effect.c:23
Event_OnEffectEnded
ref ScriptInvoker Event_OnEffectEnded
Event used when the actual effect stopped playing.
Definition: effect.c:25
m_SoundObjectBuilder
private ref SoundObjectBuilder m_SoundObjectBuilder
Definition: dayzplayer.c:165
SoundObjectBuilder
Definition: sound.c:45
Event_OnEffectStarted
ref ScriptInvoker Event_OnEffectStarted
Event used when the actual effect started playing.
Definition: effect.c:24
Event_OnStarted
Event_OnStarted
Event used when Start was called.
Definition: effect.c:300
AbstractWave
Definition: sound.c:118
SEffectManager
Manager class for managing Effect (EffectParticle, EffectSound)
Definition: effectmanager.c:5
ScriptInvoker
ScriptInvoker Class provide list of callbacks usage:
Definition: tools.c:115