Audio.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Audio/AudioDefs.h"
  5. #include "../Container/ArrayPtr.h"
  6. #include "../Container/HashSet.h"
  7. #include "../Core/Mutex.h"
  8. #include "../Core/Object.h"
  9. namespace Urho3D
  10. {
  11. class AudioImpl;
  12. class Sound;
  13. class SoundListener;
  14. class SoundSource;
  15. /// %Audio subsystem.
  16. class URHO3D_API Audio : public Object
  17. {
  18. URHO3D_OBJECT(Audio, Object);
  19. public:
  20. /// Construct.
  21. explicit Audio(Context* context);
  22. /// Destruct. Terminate the audio thread and free the audio buffer.
  23. ~Audio() override;
  24. /// Initialize sound output with specified buffer length and output mode.
  25. bool SetMode(i32 bufferLengthMSec, i32 mixRate, bool stereo, bool interpolation = true);
  26. /// Run update on sound sources. Not required for continued playback, but frees unused sound sources & sounds and updates 3D positions.
  27. void Update(float timeStep);
  28. /// Restart sound output.
  29. bool Play();
  30. /// Suspend sound output.
  31. void Stop();
  32. /// Set master gain on a specific sound type such as sound effects, music or voice.
  33. /// @property
  34. void SetMasterGain(const String& type, float gain);
  35. /// Pause playback of specific sound type. This allows to suspend e.g. sound effects or voice when the game is paused. By default all sound types are unpaused.
  36. void PauseSoundType(const String& type);
  37. /// Resume playback of specific sound type.
  38. void ResumeSoundType(const String& type);
  39. /// Resume playback of all sound types.
  40. void ResumeAll();
  41. /// Set active sound listener for 3D sounds.
  42. /// @property
  43. void SetListener(SoundListener* listener);
  44. /// Stop any sound source playing a certain sound clip.
  45. void StopSound(Sound* sound);
  46. /// Return byte size of one sample.
  47. /// @property
  48. u32 GetSampleSize() const { return sampleSize_; }
  49. /// Return mixing rate.
  50. /// @property
  51. i32 GetMixRate() const { return mixRate_; }
  52. /// Return whether output is interpolated.
  53. /// @property
  54. bool GetInterpolation() const { return interpolation_; }
  55. /// Return whether output is stereo.
  56. /// @property
  57. bool IsStereo() const { return stereo_; }
  58. /// Return whether audio is being output.
  59. /// @property
  60. bool IsPlaying() const { return playing_; }
  61. /// Return whether an audio stream has been reserved.
  62. /// @property
  63. bool IsInitialized() const { return deviceID_ != 0; }
  64. /// Return master gain for a specific sound source type. Unknown sound types will return full gain (1).
  65. /// @property
  66. float GetMasterGain(const String& type) const;
  67. /// Return whether specific sound type has been paused.
  68. bool IsSoundTypePaused(const String& type) const;
  69. /// Return active sound listener.
  70. /// @property
  71. SoundListener* GetListener() const;
  72. /// Return all sound sources.
  73. const Vector<SoundSource*>& GetSoundSources() const { return soundSources_; }
  74. /// Return whether the specified master gain has been defined.
  75. bool HasMasterGain(const String& type) const { return masterGain_.Contains(type); }
  76. /// Add a sound source to keep track of. Called by SoundSource.
  77. void AddSoundSource(SoundSource* soundSource);
  78. /// Remove a sound source. Called by SoundSource.
  79. void RemoveSoundSource(SoundSource* soundSource);
  80. /// Return audio thread mutex.
  81. Mutex& GetMutex() { return audioMutex_; }
  82. /// Return sound type specific gain multiplied by master gain.
  83. float GetSoundSourceMasterGain(StringHash typeHash) const;
  84. /// Mix sound sources into the buffer.
  85. void MixOutput(void* dest, u32 samples);
  86. private:
  87. /// Handle render update event.
  88. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  89. /// Stop sound output and release the sound buffer.
  90. void Release();
  91. /// Actually update sound sources with the specific timestep. Called internally.
  92. void UpdateInternal(float timeStep);
  93. /// Clipping buffer for mixing.
  94. SharedArrayPtr<i32> clipBuffer_;
  95. /// Audio thread mutex.
  96. Mutex audioMutex_;
  97. /// SDL audio device ID.
  98. u32 deviceID_{};
  99. /// Sample size.
  100. u32 sampleSize_{};
  101. /// Clip buffer size in samples.
  102. u32 fragmentSize_{};
  103. /// Mixing rate.
  104. i32 mixRate_{};
  105. /// Mixing interpolation flag.
  106. bool interpolation_{};
  107. /// Stereo flag.
  108. bool stereo_{};
  109. /// Playing flag.
  110. bool playing_{};
  111. /// Master gain by sound source type.
  112. HashMap<StringHash, Variant> masterGain_;
  113. /// Paused sound types.
  114. HashSet<StringHash> pausedSoundTypes_;
  115. /// Sound sources.
  116. Vector<SoundSource*> soundSources_;
  117. /// Sound listener.
  118. WeakPtr<SoundListener> listener_;
  119. };
  120. /// Register Audio library objects.
  121. /// @nobind
  122. void URHO3D_API RegisterAudioLibrary(Context* context);
  123. }