SoundSource.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Audio/AudioDefs.h"
  24. #include "../Scene/Component.h"
  25. namespace Urho3D
  26. {
  27. class Audio;
  28. class Sound;
  29. class SoundStream;
  30. /// Compressed audio decode buffer length in milliseconds.
  31. static const int STREAM_BUFFER_LENGTH = 100;
  32. /// %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.
  33. class URHO3D_API SoundSource : public Component
  34. {
  35. URHO3D_OBJECT(SoundSource, Component);
  36. public:
  37. /// Construct.
  38. explicit SoundSource(Context* context);
  39. /// Destruct. Remove self from the audio subsystem.
  40. ~SoundSource() override;
  41. /// Register object factory.
  42. static void RegisterObject(Context* context);
  43. /// Seek to time.
  44. void Seek(float seekTime);
  45. /// Play a sound.
  46. void Play(Sound* sound);
  47. /// Play a sound with specified frequency.
  48. void Play(Sound* sound, float frequency);
  49. /// Play a sound with specified frequency and gain.
  50. void Play(Sound* sound, float frequency, float gain);
  51. /// Play a sound with specified frequency, gain and panning.
  52. void Play(Sound* sound, float frequency, float gain, float panning);
  53. /// Start playing a sound stream.
  54. void Play(SoundStream* stream);
  55. /// Stop playback.
  56. void Stop();
  57. /// Set sound type, determines the master gain group.
  58. /// @property
  59. void SetSoundType(const String& type);
  60. /// Set frequency.
  61. /// @property
  62. void SetFrequency(float frequency);
  63. /// Set gain. 0.0 is silence, 1.0 is full volume.
  64. /// @property
  65. void SetGain(float gain);
  66. /// Set attenuation. 1.0 is unaltered. Used for distance attenuated playback.
  67. void SetAttenuation(float attenuation);
  68. /// Set stereo panning. -1.0 is full left and 1.0 is full right.
  69. /// @property
  70. void SetPanning(float panning);
  71. /// Set to remove either the sound source component or its owner node from the scene automatically on sound playback completion. Disabled by default.
  72. /// @property
  73. void SetAutoRemoveMode(AutoRemoveMode mode);
  74. /// Set new playback position.
  75. void SetPlayPosition(signed char* pos);
  76. /// Return sound.
  77. /// @property
  78. Sound* GetSound() const { return sound_; }
  79. /// Return playback position.
  80. volatile signed char* GetPlayPosition() const { return position_; }
  81. /// Return sound type, determines the master gain group.
  82. /// @property
  83. String GetSoundType() const { return soundType_; }
  84. /// Return playback time position.
  85. /// @property
  86. float GetTimePosition() const { return timePosition_; }
  87. /// Return frequency.
  88. /// @property
  89. float GetFrequency() const { return frequency_; }
  90. /// Return gain.
  91. /// @property
  92. float GetGain() const { return gain_; }
  93. /// Return attenuation.
  94. /// @property
  95. float GetAttenuation() const { return attenuation_; }
  96. /// Return stereo panning.
  97. /// @property
  98. float GetPanning() const { return panning_; }
  99. /// Return automatic removal mode on sound playback completion.
  100. /// @property
  101. AutoRemoveMode GetAutoRemoveMode() const { return autoRemove_; }
  102. /// Return whether is playing.
  103. /// @property
  104. bool IsPlaying() const;
  105. /// Update the sound source. Perform subclass specific operations. Called by Audio.
  106. virtual void Update(float timeStep);
  107. /// Mix sound source output to a 32-bit clipping buffer. Called by Audio.
  108. void Mix(int* dest, unsigned samples, int mixRate, bool stereo, bool interpolation);
  109. /// Update the effective master gain. Called internally and by Audio when the master gain changes.
  110. void UpdateMasterGain();
  111. /// Set sound attribute.
  112. void SetSoundAttr(const ResourceRef& value);
  113. /// Set sound position attribute.
  114. void SetPositionAttr(int value);
  115. /// Return sound attribute.
  116. ResourceRef GetSoundAttr() const;
  117. /// Set sound playing attribute.
  118. void SetPlayingAttr(bool value);
  119. /// Return sound position attribute.
  120. int GetPositionAttr() const;
  121. protected:
  122. /// Audio subsystem.
  123. WeakPtr<Audio> audio_;
  124. /// SoundSource type, determines the master gain group.
  125. String soundType_;
  126. /// SoundSource type hash.
  127. StringHash soundTypeHash_;
  128. /// Frequency.
  129. float frequency_;
  130. /// Gain.
  131. float gain_;
  132. /// Attenuation.
  133. float attenuation_;
  134. /// Stereo panning.
  135. float panning_;
  136. /// Effective master gain.
  137. float masterGain_{};
  138. /// Whether finished event should be sent on playback stop.
  139. bool sendFinishedEvent_;
  140. /// Automatic removal mode.
  141. AutoRemoveMode autoRemove_;
  142. private:
  143. /// Play a sound without locking the audio mutex. Called internally.
  144. void PlayLockless(Sound* sound);
  145. /// Play a sound stream without locking the audio mutex. Called internally.
  146. void PlayLockless(const SharedPtr<SoundStream>& stream);
  147. /// Stop sound without locking the audio mutex. Called internally.
  148. void StopLockless();
  149. /// Set new playback position without locking the audio mutex. Called internally.
  150. void SetPlayPositionLockless(signed char* pos);
  151. /// Mix mono sample to mono buffer.
  152. void MixMonoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  153. /// Mix mono sample to stereo buffer.
  154. void MixMonoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  155. /// Mix mono sample to mono buffer interpolated.
  156. void MixMonoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  157. /// Mix mono sample to stereo buffer interpolated.
  158. void MixMonoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  159. /// Mix stereo sample to mono buffer.
  160. void MixStereoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  161. /// Mix stereo sample to stereo buffer.
  162. void MixStereoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  163. /// Mix stereo sample to mono buffer interpolated.
  164. void MixStereoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  165. /// Mix stereo sample to stereo buffer interpolated.
  166. void MixStereoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  167. /// Advance playback pointer without producing audible output.
  168. void MixZeroVolume(Sound* sound, unsigned samples, int mixRate);
  169. /// Advance playback pointer to simulate audio playback in headless mode.
  170. void MixNull(float timeStep);
  171. /// Sound that is being played.
  172. SharedPtr<Sound> sound_;
  173. /// Sound stream that is being played.
  174. SharedPtr<SoundStream> soundStream_;
  175. /// Playback position.
  176. volatile signed char* position_;
  177. /// Playback fractional position.
  178. volatile int fractPosition_;
  179. /// Playback time position.
  180. volatile float timePosition_;
  181. /// Decode buffer.
  182. SharedPtr<Sound> streamBuffer_;
  183. /// Unused stream bytes from previous frame.
  184. int unusedStreamSize_;
  185. };
  186. }