SoundSource.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Audio/AudioDefs.h"
  5. #include "../Scene/Component.h"
  6. namespace Urho3D
  7. {
  8. class Audio;
  9. class Sound;
  10. class SoundStream;
  11. /// Compressed audio decode buffer length in milliseconds.
  12. static const int STREAM_BUFFER_LENGTH = 100;
  13. /// %Sound source component with stereo position. A sound source needs to be created to a node to be considered "enabled" and be able to play, however that node does not need to belong to a scene.
  14. class URHO3D_API SoundSource : public Component
  15. {
  16. URHO3D_OBJECT(SoundSource, Component);
  17. public:
  18. /// Construct.
  19. explicit SoundSource(Context* context);
  20. /// Destruct. Remove self from the audio subsystem.
  21. ~SoundSource() override;
  22. /// Register object factory.
  23. /// @nobind
  24. static void RegisterObject(Context* context);
  25. /// Seek to time.
  26. void Seek(float seekTime);
  27. /// Play a sound.
  28. void Play(Sound* sound);
  29. /// Play a sound with specified frequency.
  30. void Play(Sound* sound, float frequency);
  31. /// Play a sound with specified frequency and gain.
  32. void Play(Sound* sound, float frequency, float gain);
  33. /// Play a sound with specified frequency, gain and panning.
  34. void Play(Sound* sound, float frequency, float gain, float panning);
  35. /// Start playing a sound stream.
  36. void Play(SoundStream* stream);
  37. /// Stop playback.
  38. void Stop();
  39. /// Set sound type, determines the master gain group.
  40. /// @property
  41. void SetSoundType(const String& type);
  42. /// Set frequency.
  43. /// @property
  44. void SetFrequency(float frequency);
  45. /// Set gain. 0.0 is silence, 1.0 is full volume.
  46. /// @property
  47. void SetGain(float gain);
  48. /// Set attenuation. 1.0 is unaltered. Used for distance attenuated playback.
  49. void SetAttenuation(float attenuation);
  50. /// Set stereo panning. -1.0 is full left and 1.0 is full right.
  51. /// @property
  52. void SetPanning(float panning);
  53. /// Set to remove either the sound source component or its owner node from the scene automatically on sound playback completion. Disabled by default.
  54. /// @property
  55. void SetAutoRemoveMode(AutoRemoveMode mode);
  56. /// Set new playback position.
  57. void SetPlayPosition(signed char* pos);
  58. /// Return sound.
  59. /// @property
  60. Sound* GetSound() const { return sound_; }
  61. /// Return playback position.
  62. volatile signed char* GetPlayPosition() const { return position_; }
  63. /// Return sound type, determines the master gain group.
  64. /// @property
  65. String GetSoundType() const { return soundType_; }
  66. /// Return playback time position.
  67. /// @property
  68. float GetTimePosition() const { return timePosition_; }
  69. /// Return frequency.
  70. /// @property
  71. float GetFrequency() const { return frequency_; }
  72. /// Return gain.
  73. /// @property
  74. float GetGain() const { return gain_; }
  75. /// Return attenuation.
  76. /// @property
  77. float GetAttenuation() const { return attenuation_; }
  78. /// Return stereo panning.
  79. /// @property
  80. float GetPanning() const { return panning_; }
  81. /// Return automatic removal mode on sound playback completion.
  82. /// @property
  83. AutoRemoveMode GetAutoRemoveMode() const { return autoRemove_; }
  84. /// Return whether is playing.
  85. /// @property
  86. bool IsPlaying() const;
  87. /// Update the sound source. Perform subclass specific operations. Called by Audio.
  88. virtual void Update(float timeStep);
  89. /// Mix sound source output to a 32-bit clipping buffer. Called by Audio.
  90. void Mix(int* dest, unsigned samples, int mixRate, bool stereo, bool interpolation);
  91. /// Update the effective master gain. Called internally and by Audio when the master gain changes.
  92. void UpdateMasterGain();
  93. /// Set sound attribute.
  94. void SetSoundAttr(const ResourceRef& value);
  95. /// Set sound position attribute.
  96. void SetPositionAttr(int value);
  97. /// Return sound attribute.
  98. ResourceRef GetSoundAttr() const;
  99. /// Set sound playing attribute.
  100. void SetPlayingAttr(bool value);
  101. /// Return sound position attribute.
  102. int GetPositionAttr() const;
  103. protected:
  104. /// Audio subsystem.
  105. WeakPtr<Audio> audio_;
  106. /// SoundSource type, determines the master gain group.
  107. String soundType_;
  108. /// SoundSource type hash.
  109. StringHash soundTypeHash_;
  110. /// Frequency.
  111. float frequency_;
  112. /// Gain.
  113. float gain_;
  114. /// Attenuation.
  115. float attenuation_;
  116. /// Stereo panning.
  117. float panning_;
  118. /// Effective master gain.
  119. float masterGain_{};
  120. /// Whether finished event should be sent on playback stop.
  121. bool sendFinishedEvent_;
  122. /// Automatic removal mode.
  123. AutoRemoveMode autoRemove_;
  124. private:
  125. /// Play a sound without locking the audio mutex. Called internally.
  126. void PlayLockless(Sound* sound);
  127. /// Play a sound stream without locking the audio mutex. Called internally.
  128. void PlayLockless(const SharedPtr<SoundStream>& stream);
  129. /// Stop sound without locking the audio mutex. Called internally.
  130. void StopLockless();
  131. /// Set new playback position without locking the audio mutex. Called internally.
  132. void SetPlayPositionLockless(signed char* pos);
  133. /// Mix mono sample to mono buffer.
  134. void MixMonoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  135. /// Mix mono sample to stereo buffer.
  136. void MixMonoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  137. /// Mix mono sample to mono buffer interpolated.
  138. void MixMonoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  139. /// Mix mono sample to stereo buffer interpolated.
  140. void MixMonoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  141. /// Mix stereo sample to mono buffer.
  142. void MixStereoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  143. /// Mix stereo sample to stereo buffer.
  144. void MixStereoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  145. /// Mix stereo sample to mono buffer interpolated.
  146. void MixStereoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  147. /// Mix stereo sample to stereo buffer interpolated.
  148. void MixStereoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  149. /// Advance playback pointer without producing audible output.
  150. void MixZeroVolume(Sound* sound, unsigned samples, int mixRate);
  151. /// Advance playback pointer to simulate audio playback in headless mode.
  152. void MixNull(float timeStep);
  153. /// Sound that is being played.
  154. SharedPtr<Sound> sound_;
  155. /// Sound stream that is being played.
  156. SharedPtr<SoundStream> soundStream_;
  157. /// Playback position.
  158. volatile signed char* position_;
  159. /// Playback fractional position.
  160. volatile int fractPosition_;
  161. /// Playback time position.
  162. volatile float timePosition_;
  163. /// Decode buffer.
  164. SharedPtr<Sound> streamBuffer_;
  165. /// Unused stream bytes from previous frame.
  166. int unusedStreamSize_;
  167. };
  168. }