Audio.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "../Container/ArrayPtr.h"
  25. #include "../Container/HashSet.h"
  26. #include "../Core/Mutex.h"
  27. #include "../Core/Object.h"
  28. namespace Atomic
  29. {
  30. class AudioImpl;
  31. class Sound;
  32. class SoundListener;
  33. class SoundSource;
  34. /// %Audio subsystem.
  35. class ATOMIC_API Audio : public Object
  36. {
  37. ATOMIC_OBJECT(Audio, Object);
  38. public:
  39. /// Construct.
  40. Audio(Context* context);
  41. /// Destruct. Terminate the audio thread and free the audio buffer.
  42. virtual ~Audio();
  43. /// Initialize sound output with specified buffer length and output mode.
  44. bool SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpolation = true);
  45. /// Run update on sound sources. Not required for continued playback, but frees unused sound sources & sounds and updates 3D positions.
  46. void Update(float timeStep);
  47. /// Restart sound output.
  48. bool Play();
  49. /// Suspend sound output.
  50. void Stop();
  51. /// Set master gain on a specific sound type such as sound effects, music or voice.
  52. void SetMasterGain(const String& type, float gain);
  53. /// 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.
  54. void PauseSoundType(const String& type);
  55. /// Resume playback of specific sound type.
  56. void ResumeSoundType(const String& type);
  57. /// Resume playback of all sound types.
  58. void ResumeAll();
  59. /// Set active sound listener for 3D sounds.
  60. void SetListener(SoundListener* listener);
  61. /// Stop any sound source playing a certain sound clip.
  62. void StopSound(Sound* sound);
  63. /// Return byte size of one sample.
  64. unsigned GetSampleSize() const { return sampleSize_; }
  65. /// Return mixing rate.
  66. int GetMixRate() const { return mixRate_; }
  67. /// Return whether output is interpolated.
  68. bool GetInterpolation() const { return interpolation_; }
  69. /// Return whether output is stereo.
  70. bool IsStereo() const { return stereo_; }
  71. /// Return whether audio is being output.
  72. bool IsPlaying() const { return playing_; }
  73. /// Return whether an audio stream has been reserved.
  74. bool IsInitialized() const { return deviceID_ != 0; }
  75. /// Return master gain for a specific sound source type. Unknown sound types will return full gain (1).
  76. float GetMasterGain(const String& type) const;
  77. /// Return whether specific sound type has been paused.
  78. bool IsSoundTypePaused(const String& type) const;
  79. /// Return active sound listener.
  80. SoundListener* GetListener() const;
  81. /// Return all sound sources.
  82. const PODVector<SoundSource*>& GetSoundSources() const { return soundSources_; }
  83. /// Return whether the specified master gain has been defined.
  84. bool HasMasterGain(const String& type) const { return masterGain_.Contains(type); }
  85. /// Add a sound source to keep track of. Called by SoundSource.
  86. void AddSoundSource(SoundSource* soundSource);
  87. /// Remove a sound source. Called by SoundSource.
  88. void RemoveSoundSource(SoundSource* soundSource);
  89. /// Return audio thread mutex.
  90. Mutex& GetMutex() { return audioMutex_; }
  91. /// Return sound type specific gain multiplied by master gain.
  92. float GetSoundSourceMasterGain(StringHash typeHash) const;
  93. /// Mix sound sources into the buffer.
  94. void MixOutput(void* dest, unsigned samples);
  95. /// Final multiplier for audio byte conversion.
  96. #ifdef __EMSCRIPTEN__
  97. static const int SAMPLE_SIZE_MUL = 2;
  98. #else
  99. static const int SAMPLE_SIZE_MUL = 1;
  100. #endif
  101. private:
  102. /// Handle render update event.
  103. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  104. /// Stop sound output and release the sound buffer.
  105. void Release();
  106. /// Actually update sound sources with the specific timestep. Called internally.
  107. void UpdateInternal(float timeStep);
  108. /// Clipping buffer for mixing.
  109. SharedArrayPtr<int> clipBuffer_;
  110. /// Audio thread mutex.
  111. Mutex audioMutex_;
  112. /// SDL audio device ID.
  113. unsigned deviceID_;
  114. /// Sample size.
  115. unsigned sampleSize_;
  116. /// Clip buffer size in samples.
  117. unsigned fragmentSize_;
  118. /// Mixing rate.
  119. int mixRate_;
  120. /// Mixing interpolation flag.
  121. bool interpolation_;
  122. /// Stereo flag.
  123. bool stereo_;
  124. /// Playing flag.
  125. bool playing_;
  126. /// Master gain by sound source type.
  127. HashMap<StringHash, Variant> masterGain_;
  128. /// Paused sound types.
  129. HashSet<StringHash> pausedSoundTypes_;
  130. /// Sound sources.
  131. PODVector<SoundSource*> soundSources_;
  132. /// Sound listener.
  133. WeakPtr<SoundListener> listener_;
  134. };
  135. /// Register Audio library objects.
  136. void ATOMIC_API RegisterAudioLibrary(Context* context);
  137. }