SoundSource.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Copyright (c) 2008-2014 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 "AudioDefs.h"
  24. #include "Component.h"
  25. namespace Urho3D
  26. {
  27. class Audio;
  28. class Sound;
  29. // Compressed audio decode buffer length in milliseconds
  30. static const int DECODE_BUFFER_LENGTH = 100;
  31. /// %Sound source component with stereo position.
  32. class URHO3D_API SoundSource : public Component
  33. {
  34. OBJECT(SoundSource);
  35. public:
  36. /// Construct.
  37. SoundSource(Context* context);
  38. /// Destruct. Remove self from the audio subsystem
  39. virtual ~SoundSource();
  40. /// Register object factory.
  41. static void RegisterObject(Context* context);
  42. /// Play a sound.
  43. void Play(Sound* sound);
  44. /// Play a sound with specified frequency.
  45. void Play(Sound* sound, float frequency);
  46. /// Play a sound with specified frequency and gain.
  47. void Play(Sound* sound, float frequency, float gain);
  48. /// Play a sound with specified frequency, gain and panning.
  49. void Play(Sound* sound, float frequency, float gain, float panning);
  50. /// Stop playback.
  51. void Stop();
  52. /// Set sound type, determines the master gain group.
  53. void SetSoundType(SoundType type);
  54. /// Set frequency.
  55. void SetFrequency(float frequency);
  56. /// Set gain. 0.0 is silence, 1.0 is full volume.
  57. void SetGain(float gain);
  58. /// Set attenuation. 1.0 is unaltered. Used for distance attenuated playback.
  59. void SetAttenuation(float attenuation);
  60. /// Set stereo panning. -1.0 is full left and 1.0 is full right.
  61. void SetPanning(float panning);
  62. /// Set whether sound source will be automatically removed from the scene node when playback stops.
  63. void SetAutoRemove(bool enable);
  64. /// Set new playback position.
  65. void SetPlayPosition(signed char* pos);
  66. /// Return sound.
  67. Sound* GetSound() const { return sound_; }
  68. /// Return playback position.
  69. volatile signed char* GetPlayPosition() const { return position_; }
  70. /// Return sound type, determines the master gain group.
  71. SoundType GetSoundType() const { return soundType_; }
  72. /// Return playback time position.
  73. float GetTimePosition() const { return timePosition_; }
  74. /// Return frequency.
  75. float GetFrequency() const { return frequency_; }
  76. /// Return gain.
  77. float GetGain() const { return gain_; }
  78. /// Return attenuation.
  79. float GetAttenuation() const { return attenuation_; }
  80. /// Return stereo panning.
  81. float GetPanning() const { return panning_; }
  82. /// Return autoremove mode.
  83. bool GetAutoRemove() const { return autoRemove_; }
  84. /// Return whether is playing.
  85. bool IsPlaying() const;
  86. /// Play a sound without locking the audio mutex. Called internally.
  87. void PlayLockless(Sound* sound);
  88. /// Stop sound without locking the audio mutex. Called internally.
  89. void StopLockless();
  90. /// Set new playback position without locking the audio mutex. Called internally.
  91. void SetPlayPositionLockless(signed char* position);
  92. /// Update the sound source. Perform subclass specific operations. Called by Audio.
  93. virtual void Update(float timeStep);
  94. /// Mix sound source output to a 32-bit clipping buffer. Called by Audio.
  95. void Mix(int* dest, unsigned samples, int mixRate, bool stereo, bool interpolation);
  96. /// Set sound attribute.
  97. void SetSoundAttr(ResourceRef value);
  98. /// Set sound position attribute.
  99. void SetPositionAttr(int value);
  100. /// Return sound attribute.
  101. ResourceRef GetSoundAttr() const;
  102. /// Set sound playing attribute
  103. void SetPlayingAttr(bool value);
  104. /// Return sound position attribute.
  105. int GetPositionAttr() const;
  106. protected:
  107. /// Audio subsystem.
  108. WeakPtr<Audio> audio_;
  109. /// SoundSource type, determines the master gain group.
  110. SoundType soundType_;
  111. /// Frequency.
  112. float frequency_;
  113. /// Gain.
  114. float gain_;
  115. /// Attenuation.
  116. float attenuation_;
  117. /// Stereo panning.
  118. float panning_;
  119. /// Autoremove timer.
  120. float autoRemoveTimer_;
  121. /// Autoremove flag.
  122. bool autoRemove_;
  123. private:
  124. /// Mix mono sample to mono buffer.
  125. void MixMonoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  126. /// Mix mono sample to stereo buffer.
  127. void MixMonoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  128. /// Mix mono sample to mono buffer interpolated.
  129. void MixMonoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  130. /// Mix mono sample to stereo buffer interpolated.
  131. void MixMonoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  132. /// Mix stereo sample to mono buffer.
  133. void MixStereoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  134. /// Mix stereo sample to stereo buffer.
  135. void MixStereoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  136. /// Mix stereo sample to mono buffer interpolated.
  137. void MixStereoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  138. /// Mix stereo sample to stereo buffer interpolated.
  139. void MixStereoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  140. /// Advance playback pointer without producing audible output.
  141. void MixZeroVolume(Sound* sound, unsigned samples, int mixRate);
  142. /// Advance playback pointer to simulate audio playback in headless mode.
  143. void MixNull(float timeStep);
  144. /// Free the decoder if any.
  145. void FreeDecoder();
  146. /// Sound.
  147. SharedPtr<Sound> sound_;
  148. /// Playback position.
  149. volatile signed char *position_;
  150. /// Playback fractional position.
  151. volatile int fractPosition_;
  152. /// Playback time position.
  153. volatile float timePosition_;
  154. /// Ogg Vorbis decoder.
  155. void* decoder_;
  156. /// Decode buffer.
  157. SharedPtr<Sound> decodeBuffer_;
  158. /// Previous decode buffer position.
  159. unsigned decodePosition_;
  160. };
  161. }