Sound.h 4.7 KB

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