Sound.h 4.6 KB

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