SoundSource.h 7.8 KB

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