SoundSource.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // Copyright (c) 2008-2015 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.
  33. class ATOMIC_API SoundSource : public Component
  34. {
  35. OBJECT(SoundSource);
  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. /// Play a sound.
  44. void Play(Sound* sound);
  45. /// Play a sound with specified frequency.
  46. void Play(Sound* sound, float frequency);
  47. /// Play a sound with specified frequency and gain.
  48. void Play(Sound* sound, float frequency, float gain);
  49. /// Play a sound with specified frequency, gain and panning.
  50. void Play(Sound* sound, float frequency, float gain, float panning);
  51. /// Start playing a sound stream.
  52. void Play(SoundStream* stream);
  53. /// Stop playback.
  54. void Stop();
  55. /// Set sound type, determines the master gain group.
  56. void SetSoundType(const String& type);
  57. /// Set frequency.
  58. void SetFrequency(float frequency);
  59. /// Set gain. 0.0 is silence, 1.0 is full volume.
  60. void SetGain(float gain);
  61. /// Set attenuation. 1.0 is unaltered. Used for distance attenuated playback.
  62. void SetAttenuation(float attenuation);
  63. /// Set stereo panning. -1.0 is full left and 1.0 is full right.
  64. void SetPanning(float panning);
  65. /// Set whether sound source will be automatically removed from the scene node when playback stops.
  66. void SetAutoRemove(bool enable);
  67. /// Set new playback position.
  68. void SetPlayPosition(signed char* pos);
  69. // BEGIN ATOMIC
  70. /// Return sound.
  71. void SetSound(Sound* sound);
  72. // END ATOMIC
  73. /// Return sound.
  74. Sound* GetSound() const { return sound_; }
  75. /// Return playback position.
  76. volatile signed char* GetPlayPosition() const { return position_; }
  77. /// Return sound type, determines the master gain group.
  78. String GetSoundType() const { return soundType_; }
  79. /// Return playback time position.
  80. float GetTimePosition() const { return timePosition_; }
  81. /// Return frequency.
  82. float GetFrequency() const { return frequency_; }
  83. /// Return gain.
  84. float GetGain() const { return gain_; }
  85. /// Return attenuation.
  86. float GetAttenuation() const { return attenuation_; }
  87. /// Return stereo panning.
  88. float GetPanning() const { return panning_; }
  89. /// Return autoremove mode.
  90. bool GetAutoRemove() const { return autoRemove_; }
  91. /// Return whether is playing.
  92. bool IsPlaying() const;
  93. /// Update the sound source. Perform subclass specific operations. Called by Audio.
  94. virtual void Update(float timeStep);
  95. /// Mix sound source output to a 32-bit clipping buffer. Called by Audio.
  96. void Mix(int* dest, unsigned samples, int mixRate, bool stereo, bool interpolation);
  97. /// Update the effective master gain. Called internally and by Audio when the master gain changes.
  98. void UpdateMasterGain();
  99. /// Set sound attribute.
  100. void SetSoundAttr(const ResourceRef& value);
  101. /// Set sound position attribute.
  102. void SetPositionAttr(int value);
  103. /// Return sound attribute.
  104. ResourceRef GetSoundAttr() const;
  105. /// Set sound playing attribute
  106. void SetPlayingAttr(bool value);
  107. /// Return sound position attribute.
  108. int GetPositionAttr() const;
  109. protected:
  110. /// Audio subsystem.
  111. WeakPtr<Audio> audio_;
  112. /// SoundSource type, determines the master gain group.
  113. String soundType_;
  114. /// SoundSource type hash.
  115. StringHash soundTypeHash_;
  116. /// Frequency.
  117. float frequency_;
  118. /// Gain.
  119. float gain_;
  120. /// Attenuation.
  121. float attenuation_;
  122. /// Stereo panning.
  123. float panning_;
  124. /// Autoremove timer.
  125. float autoRemoveTimer_;
  126. /// Effective master gain.
  127. float masterGain_;
  128. /// Autoremove flag.
  129. bool autoRemove_;
  130. // BEGIN ATOMIC
  131. bool autoPlay_;
  132. bool hasAutoPlayed_;
  133. // END ATOMIC
  134. private:
  135. /// Play a sound without locking the audio mutex. Called internally.
  136. void PlayLockless(Sound* sound);
  137. /// Play a sound stream without locking the audio mutex. Called internally.
  138. void PlayLockless(SharedPtr<SoundStream> stream);
  139. /// Stop sound without locking the audio mutex. Called internally.
  140. void StopLockless();
  141. /// Set new playback position without locking the audio mutex. Called internally.
  142. void SetPlayPositionLockless(signed char* position);
  143. /// Mix mono sample to mono buffer.
  144. void MixMonoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  145. /// Mix mono sample to stereo buffer.
  146. void MixMonoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  147. /// Mix mono sample to mono buffer interpolated.
  148. void MixMonoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  149. /// Mix mono sample to stereo buffer interpolated.
  150. void MixMonoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  151. /// Mix stereo sample to mono buffer.
  152. void MixStereoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  153. /// Mix stereo sample to stereo buffer.
  154. void MixStereoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  155. /// Mix stereo sample to mono buffer interpolated.
  156. void MixStereoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  157. /// Mix stereo sample to stereo buffer interpolated.
  158. void MixStereoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  159. /// Advance playback pointer without producing audible output.
  160. void MixZeroVolume(Sound* sound, unsigned samples, int mixRate);
  161. /// Advance playback pointer to simulate audio playback in headless mode.
  162. void MixNull(float timeStep);
  163. /// Sound that is being played.
  164. SharedPtr<Sound> sound_;
  165. /// Sound stream that is being played.
  166. SharedPtr<SoundStream> soundStream_;
  167. /// Playback position.
  168. volatile signed char* position_;
  169. /// Playback fractional position.
  170. volatile int fractPosition_;
  171. /// Playback time position.
  172. volatile float timePosition_;
  173. /// Decode buffer.
  174. SharedPtr<Sound> streamBuffer_;
  175. /// Unused stream bytes from previous frame.
  176. int unusedStreamSize_;
  177. };
  178. }