Sound.h 4.6 KB

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