SoundSource.h 6.5 KB

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