SoundSource.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  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 ATOMIC_API SoundSource : public Component
  34. {
  35. ATOMIC_OBJECT(SoundSource, Component);
  36. public:
  37. /// Construct.
  38. SoundSource(Context* context);
  39. /// Destruct. Remove self from the audio subsystem
  40. virtual ~SoundSource();
  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. void SetSoundType(const String& type);
  59. /// Set frequency.
  60. void SetFrequency(float frequency);
  61. /// Set gain. 0.0 is silence, 1.0 is full volume.
  62. void SetGain(float gain);
  63. /// Set attenuation. 1.0 is unaltered. Used for distance attenuated playback.
  64. void SetAttenuation(float attenuation);
  65. /// Set stereo panning. -1.0 is full left and 1.0 is full right.
  66. void SetPanning(float panning);
  67. /// Set to remove either the sound source component or its owner node from the scene automatically on sound playback completion. Disabled by default.
  68. void SetAutoRemoveMode(AutoRemoveMode mode);
  69. /// Set new playback position.
  70. void SetPlayPosition(signed char* pos);
  71. /// Return sound.
  72. Sound* GetSound() const { return sound_; }
  73. /// Return playback position.
  74. volatile signed char* GetPlayPosition() const { return position_; }
  75. /// Return sound type, determines the master gain group.
  76. String GetSoundType() const { return soundType_; }
  77. /// Return playback time position.
  78. float GetTimePosition() const { return timePosition_; }
  79. /// Return frequency.
  80. float GetFrequency() const { return frequency_; }
  81. /// Return gain.
  82. float GetGain() const { return gain_; }
  83. /// Return attenuation.
  84. float GetAttenuation() const { return attenuation_; }
  85. /// Return stereo panning.
  86. float GetPanning() const { return panning_; }
  87. /// Return automatic removal mode on sound playback completion.
  88. AutoRemoveMode GetAutoRemoveMode() const { return autoRemove_; }
  89. /// Return whether is playing.
  90. bool IsPlaying() const;
  91. /// Update the sound source. Perform subclass specific operations. Called by Audio.
  92. virtual void Update(float timeStep);
  93. /// Mix sound source output to a 32-bit clipping buffer. Called by Audio.
  94. void Mix(int* dest, unsigned samples, int mixRate, bool stereo, bool interpolation);
  95. /// Update the effective master gain. Called internally and by Audio when the master gain changes.
  96. void UpdateMasterGain();
  97. /// Set sound attribute.
  98. void SetSoundAttr(const ResourceRef& value);
  99. /// Set sound position attribute.
  100. void SetPositionAttr(int value);
  101. /// Return sound attribute.
  102. ResourceRef GetSoundAttr() const;
  103. /// Set sound playing attribute
  104. void SetPlayingAttr(bool value);
  105. /// Return sound position attribute.
  106. int GetPositionAttr() const;
  107. protected:
  108. /// Audio subsystem.
  109. WeakPtr<Audio> audio_;
  110. /// SoundSource type, determines the master gain group.
  111. String soundType_;
  112. /// SoundSource type hash.
  113. StringHash soundTypeHash_;
  114. /// Frequency.
  115. float frequency_;
  116. /// Gain.
  117. float gain_;
  118. /// Attenuation.
  119. float attenuation_;
  120. /// Stereo panning.
  121. float panning_;
  122. /// Effective master gain.
  123. float masterGain_;
  124. /// Whether finished event should be sent on playback stop.
  125. bool sendFinishedEvent_;
  126. /// Automatic removal mode.
  127. AutoRemoveMode autoRemove_;
  128. private:
  129. /// Play a sound without locking the audio mutex. Called internally.
  130. void PlayLockless(Sound* sound);
  131. /// Play a sound stream without locking the audio mutex. Called internally.
  132. void PlayLockless(SharedPtr<SoundStream> stream);
  133. /// Stop sound without locking the audio mutex. Called internally.
  134. void StopLockless();
  135. /// Set new playback position without locking the audio mutex. Called internally.
  136. void SetPlayPositionLockless(signed char* position);
  137. /// Mix mono sample to mono buffer.
  138. void MixMonoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  139. /// Mix mono sample to stereo buffer.
  140. void MixMonoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  141. /// Mix mono sample to mono buffer interpolated.
  142. void MixMonoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  143. /// Mix mono sample to stereo buffer interpolated.
  144. void MixMonoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  145. /// Mix stereo sample to mono buffer.
  146. void MixStereoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  147. /// Mix stereo sample to stereo buffer.
  148. void MixStereoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  149. /// Mix stereo sample to mono buffer interpolated.
  150. void MixStereoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  151. /// Mix stereo sample to stereo buffer interpolated.
  152. void MixStereoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  153. /// Advance playback pointer without producing audible output.
  154. void MixZeroVolume(Sound* sound, unsigned samples, int mixRate);
  155. /// Advance playback pointer to simulate audio playback in headless mode.
  156. void MixNull(float timeStep);
  157. /// Sound that is being played.
  158. SharedPtr<Sound> sound_;
  159. /// Sound stream that is being played.
  160. SharedPtr<SoundStream> soundStream_;
  161. /// Playback position.
  162. volatile signed char* position_;
  163. /// Playback fractional position.
  164. volatile int fractPosition_;
  165. /// Playback time position.
  166. volatile float timePosition_;
  167. /// Decode buffer.
  168. SharedPtr<Sound> streamBuffer_;
  169. /// Unused stream bytes from previous frame.
  170. int unusedStreamSize_;
  171. };
  172. }