BsAudioClip.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsResource.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Audio
  9. * @{
  10. */
  11. /** Valid internal engine audio formats. */
  12. enum class AudioFormat
  13. {
  14. PCM, /**< Pulse code modulation audio ("raw" uncompressed audio). */
  15. VORBIS /**< Vorbis compressed audio. */
  16. };
  17. /** Modes that determine how and when is audio data read. */
  18. enum class AudioReadMode
  19. {
  20. /** Entire audio clip will be loaded and decompressed. Uses most memory but has lowest CPU impact. */
  21. LoadDecompressed,
  22. /**
  23. * Entire audio clip will be loaded, but will be decompressed while playing. Uses medium amount of memory and has
  24. * the highest CPU impact.
  25. */
  26. LoadCompressed,
  27. /**
  28. * Audio will be slowly streamed from the disk, and decompressed if needed. Uses very little memory, and has either
  29. * low or high CPU impact depending if the audio is in a compressed format. Since data is streamed from the disk,
  30. * read speeds could also be a bottleneck.
  31. */
  32. Stream
  33. };
  34. /** Descriptor used for initializing an audio clip. */
  35. struct AUDIO_CLIP_DESC
  36. {
  37. /** Determines how is audio data read. */
  38. AudioReadMode readMode = AudioReadMode::LoadDecompressed;
  39. /** Determines in which format is the audio data in. */
  40. AudioFormat format = AudioFormat::PCM;
  41. /** Sample rate (frequency) of the audio data. */
  42. UINT32 frequency = 44100;
  43. /** Number of bits per sample. Not used for compressed formats. */
  44. UINT32 bitDepth = 16;
  45. /** Number of channels. Each channel has its own step of samples. */
  46. UINT32 numChannels = 2;
  47. /** Determines should the audio clip be played using 3D positioning. Only valid for mono audio. */
  48. bool is3D = true;
  49. /**
  50. * Determines should the audio clip keep the original data in memory after creation. For example if the audio data
  51. * is normally compressed, but audio clip uncompresses it on load, the original compressed data will be lost unless
  52. * this is enabled. This will cause extra memory to be used, but can be useful in certain circumstances (for example
  53. * you might require that data to save the audio clip on disk).
  54. *
  55. * When loading audio clip directly from disk, this properly is controlled by the ResourceLoadFlag::KeepSourceData.
  56. */
  57. bool keepSourceData = true;
  58. };
  59. /**
  60. * Audio clip stores audio data in a compressed or uncompressed format. Clips can be provided to audio sources or
  61. * other audio methods to be played.
  62. */
  63. class BS_CORE_EXPORT AudioClip : public Resource
  64. {
  65. public:
  66. virtual ~AudioClip() { }
  67. /** Returns the size of a single sample, in bits. */
  68. UINT32 getBitDepth() const { return mDesc.bitDepth; }
  69. /** Returns how many samples per second is the audio encoded in. */
  70. UINT32 getFrequency() const { return mDesc.frequency; }
  71. /** Returns the number of channels provided by the clip. */
  72. UINT32 getNumChannels() const { return mDesc.numChannels; }
  73. /**
  74. * Returns in which format is audio data stored in.
  75. *
  76. * @see AudioFormat
  77. */
  78. AudioFormat getFormat() const { return mDesc.format; }
  79. /**
  80. * Returns how is the audio data read/decoded.
  81. *
  82. * @see AudioReadMode
  83. */
  84. AudioReadMode getReadMode() const { return mDesc.readMode; }
  85. /** Returns the length of the audio clip, in seconds. */
  86. float getLength() const { return mLength; }
  87. /** Returns the total number of samples in the clip (includes all channels). */
  88. UINT32 getNumSamples() const { return mNumSamples; }
  89. /** Determines will the clip be played a spatial 3D sound, or as a normal sound (for example music). */
  90. bool is3D() const { return mDesc.is3D; }
  91. /**
  92. * Creates a new AudioClip and populates it with provided samples.
  93. *
  94. * @param[in] samples Data streams containing the samples to load. Data will be read starting from the current
  95. * position in the stream. The samples should be in audio format as specified in the
  96. * @p desc parameter. Ownership of the data stream is taken by the audio clip and the
  97. * caller must not close it manually.
  98. * @param[in] streamSize Number of bytes to read from the @p samples stream.
  99. * @param[in] numSamples Total number of samples (including all channels).
  100. * @param[in] desc Descriptor containing meta-data for the provided samples.
  101. *
  102. * @note If the provided samples are in PCM format, they should be signed integers of provided bit depth.
  103. */
  104. static HAudioClip create(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples,
  105. const AUDIO_CLIP_DESC& desc);
  106. public: // ***** INTERNAL ******
  107. /** @name Internal
  108. * @{
  109. */
  110. /** Creates a new AudioClip without initializing it. Use create() for normal use. */
  111. static SPtr<AudioClip> _createPtr(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples,
  112. const AUDIO_CLIP_DESC& desc);
  113. /** @} */
  114. protected:
  115. AudioClip(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples, const AUDIO_CLIP_DESC& desc);
  116. /** @copydoc Resource::initialize */
  117. void initialize() override;
  118. /** Returns original audio data. Only available if @p keepSourceData has been provided on creation. */
  119. virtual SPtr<DataStream> getSourceStream(UINT32& size) = 0;
  120. protected:
  121. AUDIO_CLIP_DESC mDesc;
  122. UINT32 mNumSamples;
  123. UINT32 mStreamSize;
  124. UINT32 mStreamOffset;
  125. float mLength;
  126. SPtr<DataStream> mStreamData;
  127. /************************************************************************/
  128. /* SERIALIZATION */
  129. /************************************************************************/
  130. public:
  131. friend class AudioClipRTTI;
  132. static RTTITypeBase* getRTTIStatic();
  133. RTTITypeBase* getRTTI() const override;
  134. /**
  135. * Creates an AudioClip with no samples. You must populate its data manually followed by a call to initialize().
  136. *
  137. * @note For serialization use only.
  138. */
  139. static SPtr<AudioClip> createEmpty();
  140. };
  141. /** @} */
  142. }