Audio.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "ArrayPtr.h"
  25. #include "AudioDefs.h"
  26. #include "Mutex.h"
  27. #include "Object.h"
  28. #include "Quaternion.h"
  29. #include "Thread.h"
  30. class AudioImpl;
  31. class Sound;
  32. class SoundSource;
  33. /// %Audio subsystem. Uses either DirectSound or PortAudio for the actual sound output.
  34. class 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 interpolate = 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 listener position.
  53. void SetListenerPosition(const Vector3& position);
  54. /// %Set listener rotation.
  55. void SetListenerRotation(const Quaternion& rotation);
  56. /// %Set listener position and rotation.
  57. void SetListenerTransform(const Vector3& position, const Quaternion& rotation);
  58. /// Stop any sound source playing a certain sound clip.
  59. void StopSound(Sound* sound);
  60. /// Return byte size of one sample.
  61. unsigned GetSampleSize() const { return sampleSize_; }
  62. /// Return mixing rate.
  63. int GetMixRate() const { return mixRate_; }
  64. /// Return whether output is stereo.
  65. bool IsStereo() const { return stereo_; }
  66. /// Return whether output is interpolated.
  67. bool IsInterpolated() const { return interpolate_; }
  68. /// Return whether audio is being output.
  69. bool IsPlaying() const { return playing_; }
  70. /// Return whether an audio stream has been reserved.
  71. bool IsInitialized() const { return stream_ != 0; }
  72. /// Return master gain for a specific sound source type.
  73. float GetMasterGain(SoundType type) const;
  74. /// Return listener position.
  75. const Vector3& GetListenerPosition() const { return listenerPosition_; }
  76. /// Return listener rotation.
  77. const Quaternion& GetListenerRotation() const { return listenerRotation_; }
  78. /// Return all sound sources.
  79. const PODVector<SoundSource*>& GetSoundSources() const { return soundSources_; }
  80. /// Add a sound source to keep track of. Called by SoundSource.
  81. void AddSoundSource(SoundSource* soundSource);
  82. /// Remove a sound source. Called by SoundSource.
  83. void RemoveSoundSource(SoundSource* soundSource);
  84. /// Return audio thread mutex
  85. Mutex& GetMutex() { return audioMutex_; }
  86. /// Return sound type specific gain multiplied by master gain.
  87. float GetSoundSourceMasterGain(SoundType type) const { return masterGain_[SOUND_MASTER] * masterGain_[type]; }
  88. /// Mix sound sources into the buffer.
  89. void MixOutput(void *dest, unsigned samples);
  90. private:
  91. /// Handle render update event.
  92. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  93. /// Stop sound output and release the sound buffer.
  94. void Release();
  95. /// Sound output stream.
  96. void* stream_;
  97. /// Clipping buffer for mixing.
  98. SharedArrayPtr<int> clipBuffer_;
  99. /// Audio thread mutex.
  100. Mutex audioMutex_;
  101. /// Sample size.
  102. unsigned sampleSize_;
  103. /// Clip buffer size in samples.
  104. unsigned fragmentSize_;
  105. /// Mixing rate.
  106. int mixRate_;
  107. /// Stereo flag.
  108. bool stereo_;
  109. /// Interpolation flag.
  110. bool interpolate_;
  111. /// Playing flag.
  112. bool playing_;
  113. /// Master gain by sound source type.
  114. float masterGain_[MAX_SOUND_TYPES];
  115. /// Sound sources.
  116. PODVector<SoundSource*> soundSources_;
  117. /// Listener position.
  118. Vector3 listenerPosition_;
  119. /// Listener rotation.
  120. Quaternion listenerRotation_;
  121. };
  122. /// Register Sound library objects.
  123. void RegisterAudioLibrary(Context* context);