Audio.h 4.8 KB

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