Sound.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/ArrayPtr.h"
  5. #include "../Resource/Resource.h"
  6. namespace Urho3D
  7. {
  8. class SoundStream;
  9. /// %Sound resource.
  10. class URHO3D_API Sound : public ResourceWithMetadata
  11. {
  12. URHO3D_OBJECT(Sound, ResourceWithMetadata);
  13. public:
  14. /// Construct.
  15. explicit Sound(Context* context);
  16. /// Destruct and free sound data.
  17. ~Sound() override;
  18. /// Register object factory.
  19. /// @nobind
  20. static void RegisterObject(Context* context);
  21. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  22. bool BeginLoad(Deserializer& source) override;
  23. /// Load raw sound data.
  24. bool LoadRaw(Deserializer& source);
  25. /// Load WAV format sound data.
  26. bool LoadWav(Deserializer& source);
  27. /// Load Ogg Vorbis format sound data. Does not decode at load, but will rather be decoded while playing.
  28. bool LoadOggVorbis(Deserializer& source);
  29. /// Set sound size in bytes. Also resets the sound to be uncompressed and one-shot.
  30. void SetSize(unsigned dataSize);
  31. /// Set uncompressed sound data.
  32. void SetData(const void* data, unsigned dataSize);
  33. /// Set uncompressed sound data format.
  34. void SetFormat(unsigned frequency, bool sixteenBit, bool stereo);
  35. /// Set loop on/off. If loop is enabled, sets the full sound as loop range.
  36. /// @property
  37. void SetLooped(bool enable);
  38. /// Define loop.
  39. void SetLoop(unsigned repeatOffset, unsigned endOffset);
  40. /// Return a new instance of a decoder sound stream. Used by compressed sounds.
  41. SharedPtr<SoundStream> GetDecoderStream() const;
  42. /// Return shared sound data.
  43. SharedArrayPtr<signed char> GetData() const { return data_; }
  44. /// Return sound data start.
  45. signed char* GetStart() const { return data_.Get(); }
  46. /// Return loop start.
  47. signed char* GetRepeat() const { return repeat_; }
  48. /// Return sound data end.
  49. signed char* GetEnd() const { return end_; }
  50. /// Return length in seconds.
  51. /// @property
  52. float GetLength() const;
  53. /// Return total sound data size.
  54. unsigned GetDataSize() const { return dataSize_; }
  55. /// Return sample size.
  56. /// @property
  57. unsigned GetSampleSize() const;
  58. /// Return default frequency as a float.
  59. /// @property
  60. float GetFrequency() const { return (float)frequency_; }
  61. /// Return default frequency as an integer.
  62. unsigned GetIntFrequency() const { return frequency_; }
  63. /// Return whether is looped.
  64. /// @property
  65. bool IsLooped() const { return looped_; }
  66. /// Return whether data is sixteen bit.
  67. /// @property
  68. bool IsSixteenBit() const { return sixteenBit_; }
  69. /// Return whether data is stereo.
  70. /// @property
  71. bool IsStereo() const { return stereo_; }
  72. /// Return whether is compressed.
  73. /// @property
  74. bool IsCompressed() const { return compressed_; }
  75. /// Fix interpolation by copying data from loop start to loop end (looped), or adding silence (oneshot). Called internally, does not normally need to be called, unless the sound data is modified manually on the fly.
  76. void FixInterpolation();
  77. private:
  78. /// Load optional parameters from an XML file.
  79. void LoadParameters();
  80. /// Sound data.
  81. SharedArrayPtr<signed char> data_;
  82. /// Loop start.
  83. signed char* repeat_;
  84. /// Sound data end.
  85. signed char* end_;
  86. /// Sound data size in bytes.
  87. unsigned dataSize_;
  88. /// Default frequency.
  89. unsigned frequency_;
  90. /// Looped flag.
  91. bool looped_;
  92. /// Sixteen bit flag.
  93. bool sixteenBit_;
  94. /// Stereo flag.
  95. bool stereo_;
  96. /// Compressed flag.
  97. bool compressed_;
  98. /// Compressed sound length.
  99. float compressedLength_;
  100. };
  101. }