SoundSource.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "AudioDefs.h"
  25. #include "Component.h"
  26. class Audio;
  27. class Sound;
  28. // Compressed audio decode buffer length in milliseconds
  29. static const int DECODE_BUFFER_LENGTH = 100;
  30. /// %Sound source component with stereo position.
  31. class SoundSource : public Component
  32. {
  33. OBJECT(SoundSource);
  34. public:
  35. /// Construct.
  36. SoundSource(Context* context);
  37. /// Destruct. Remove self from the audio subsystem
  38. virtual ~SoundSource();
  39. /// Register object factory.
  40. static void RegisterObject(Context* context);
  41. /// Play a sound.
  42. void Play(Sound* sound);
  43. /// Play a sound with specified frequency.
  44. void Play(Sound* sound, float frequency);
  45. /// Play a sound with specified frequency and gain.
  46. void Play(Sound* sound, float frequency, float gain);
  47. /// Play a sound with specified frequency, gain and panning.
  48. void Play(Sound* sound, float frequency, float gain, float panning);
  49. /// Stop playback.
  50. void Stop();
  51. /// %Set sound type, determines the master gain group.
  52. void SetSoundType(SoundType type);
  53. /// %Set frequency.
  54. void SetFrequency(float frequency);
  55. /// %Set gain. 0.0 is silence, 1.0 is full volume.
  56. void SetGain(float gain);
  57. /// %Set attenuation. 1.0 is unaltered. Used for distance attenuated playback.
  58. void SetAttenuation(float attenuation);
  59. /// %Set stereo panning. -1.0 is full left and 1.0 is full right.
  60. void SetPanning(float panning);
  61. /// %Set whether sound source will be automatically removed from the scene node when playback stops.
  62. void SetAutoRemove(bool enable);
  63. /// %Set new playback position.
  64. void SetPlayPosition(signed char* pos);
  65. /// Return sound.
  66. Sound* GetSound() const { return sound_; }
  67. /// Return playback position.
  68. volatile signed char* GetPlayPosition() const { return position_; }
  69. /// Return sound type, determines the master gain group.
  70. SoundType GetSoundType() const { return soundType_; }
  71. /// Return playback time position.
  72. float GetTimePosition() const { return timePosition_; }
  73. /// Return frequency.
  74. float GetFrequency() const { return frequency_; }
  75. /// Return gain.
  76. float GetGain() const { return gain_; }
  77. /// Return attenuation.
  78. float GetAttenuation() const { return attenuation_; }
  79. /// Return stereo panning.
  80. float GetPanning() const { return panning_; }
  81. /// Return autoremove mode.
  82. bool GetAutoRemove() const { return autoRemove_; }
  83. /// Return whether is playing.
  84. bool IsPlaying() const;
  85. /// Play a sound without locking the audio mutex. Called internally.
  86. void PlayLockless(Sound* sound);
  87. /// Stop sound without locking the audio mutex. Called internally.
  88. void StopLockless();
  89. /// %Set new playback position without locking the audio mutex. Called internally.
  90. void SetPlayPositionLockless(signed char* position);
  91. /// Update the sound source. Perform subclass specific operations. Called by Sound.
  92. virtual void Update(float timeStep);
  93. /// Mix sound source output to a 32-bit clipping buffer. Called by Sound.
  94. void Mix(int* dest, unsigned samples, int mixRate, bool stereo, bool interpolate);
  95. /// %Set sound attribute.
  96. void SetSoundAttr(ResourceRef value);
  97. /// %Set sound position attribute.
  98. void SetPositionAttr(int value);
  99. /// Return sound attribute.
  100. ResourceRef GetSoundAttr() const;
  101. /// Return sound position attribute.
  102. int GetPositionAttr() const;
  103. protected:
  104. /// Audio subsystem.
  105. WeakPtr<Audio> audio_;
  106. /// SoundSource type, determines the master gain group.
  107. SoundType soundType_;
  108. /// Frequency.
  109. float frequency_;
  110. /// Gain.
  111. float gain_;
  112. /// Attenuation.
  113. float attenuation_;
  114. /// Stereo panning.
  115. float panning_;
  116. /// Autoremove timer.
  117. float autoRemoveTimer_;
  118. /// Autoremove flag.
  119. bool autoRemove_;
  120. private:
  121. /// Mix mono sample to mono buffer.
  122. void MixMonoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  123. /// Mix mono sample to stereo buffer.
  124. void MixMonoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  125. /// Mix mono sample to mono buffer interpolated.
  126. void MixMonoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  127. /// Mix mono sample to stereo buffer interpolated.
  128. void MixMonoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  129. /// Mix stereo sample to mono buffer.
  130. void MixStereoToMono(Sound* sound, int* dest, unsigned samples, int mixRate);
  131. /// Mix stereo sample to stereo buffer.
  132. void MixStereoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate);
  133. /// Mix stereo sample to mono buffer interpolated.
  134. void MixStereoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  135. /// Mix stereo sample to stereo buffer interpolated.
  136. void MixStereoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate);
  137. /// Advance playback pointer without producing audible output.
  138. void MixZeroVolume(Sound* sound, unsigned samples, int mixRate);
  139. /// Advance playback pointer to simulate audio playback in headless mode.
  140. void MixNull(float timeStep);
  141. /// Free the decoder if any.
  142. void FreeDecoder();
  143. /// Sound.
  144. SharedPtr<Sound> sound_;
  145. /// Playback position.
  146. volatile signed char *position_;
  147. /// Playback fractional position.
  148. volatile int fractPosition_;
  149. /// Playback time position.
  150. volatile float timePosition_;
  151. /// Ogg Vorbis decoder.
  152. void* decoder_;
  153. /// Decode buffer.
  154. SharedPtr<Sound> decodeBuffer_;
  155. /// Previous decode buffer position.
  156. unsigned decodePosition_;
  157. };