Sound.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  26. {
  27. class SoundStream;
  28. /// %Sound resource.
  29. class ATOMIC_API Sound : public ResourceWithMetadata
  30. {
  31. ATOMIC_OBJECT(Sound, ResourceWithMetadata);
  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. /// Return a new instance of a decoder sound stream. Used by compressed sounds.
  58. SharedPtr<SoundStream> GetDecoderStream() const;
  59. /// Return shared sound data.
  60. SharedArrayPtr<signed char> GetData() const { return data_; }
  61. /// Return sound data start.
  62. signed char* GetStart() const { return data_.Get(); }
  63. /// Return loop start.
  64. signed char* GetRepeat() const { return repeat_; }
  65. /// Return sound data end.
  66. signed char* GetEnd() const { return end_; }
  67. /// Return length in seconds.
  68. float GetLength() const;
  69. /// Return total sound data size.
  70. unsigned GetDataSize() const { return dataSize_; }
  71. /// Return sample size.
  72. unsigned GetSampleSize() const;
  73. /// Return default frequency as a float.
  74. float GetFrequency() const { return (float)frequency_; }
  75. /// Return default frequency as an integer.
  76. unsigned GetIntFrequency() const { return frequency_; }
  77. /// Return whether is looped.
  78. bool IsLooped() const { return looped_; }
  79. /// Return whether data is sixteen bit.
  80. bool IsSixteenBit() const { return sixteenBit_; }
  81. /// Return whether data is stereo.
  82. bool IsStereo() const { return stereo_; }
  83. /// Return whether is compressed.
  84. bool IsCompressed() const { return compressed_; }
  85. /// 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.
  86. void FixInterpolation();
  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. }