BsAudioClip.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * Returns audio samples in PCM format, channel data interleaved. Sample read pointer is advanced by the read
  93. * amount. Only available if the audio data has been created with AudioReadMode::Stream,
  94. * AudioReadMode::LoadCompressed (and the format is compressed), or if @p keepSourceData was enabled on creation.
  95. *
  96. * @param[in] samples Previously allocated buffer to contain the samples.
  97. * @param[in] count Number of samples to read (should be a multiple of number of channels).
  98. *
  99. * @note Implementation must be thread safe as this will get called from audio streaming thread.
  100. */
  101. virtual void getSamples(UINT8* samples, UINT32 count) const = 0;
  102. /**
  103. * Moves the read location from which the getSamples method retrieves samples. Only available if the audio data
  104. * has been created with AudioReadMode::Stream, AudioReadMode::LoadCompressed (and the format is compressed),
  105. * or if @p keepSourceData was enabled on creation.
  106. *
  107. * @param[in] offset Offset in number of samples at which to start reading (should be a multiple of number
  108. * of channels).
  109. *
  110. * @note Implementation must be thread safe as this will get called from audio streaming thread.
  111. */
  112. virtual void seekSamples(UINT32 offset) = 0;
  113. /**
  114. * Creates a new AudioClip and populates it with provided samples.
  115. *
  116. * @param[in] samples Data streams containing the samples to load. Data will be read starting from the current
  117. * position in the stream. The samples should be in audio format as specified in the
  118. * @p desc parameter.
  119. * @param[in] streamSize Number of bytes to read from the @p samples stream.
  120. * @param[in] numSamples Total number of samples (including all channels).
  121. * @param[in] desc Descriptor containing meta-data for the provided samples.
  122. *
  123. * @note If the provided samples are in PCM format, they should be unsigned for 8-bit data, and signed for
  124. * higher bit depths.
  125. */
  126. static HAudioClip create(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples,
  127. const AUDIO_CLIP_DESC& desc); // Note that ownership of stream is taken by the AudioClip
  128. public: // ***** INTERNAL ******
  129. /** @name Internal
  130. * @{
  131. */
  132. /** Creates a new AudioClip without initializing it. Use create() for normal use. */
  133. static SPtr<AudioClip> _createPtr(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples,
  134. const AUDIO_CLIP_DESC& desc);
  135. /** @} */
  136. protected:
  137. AudioClip(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples, const AUDIO_CLIP_DESC& desc);
  138. /** @copydoc Resource::initialize */
  139. void initialize() override;
  140. /** Returns original audio data. Only available if @p keepSourceData has been provided on creation. */
  141. virtual SPtr<DataStream> getSourceStream(UINT32& size) = 0;
  142. protected:
  143. AUDIO_CLIP_DESC mDesc;
  144. UINT32 mNumSamples;
  145. UINT32 mStreamSize;
  146. UINT32 mStreamOffset;
  147. float mLength;
  148. SPtr<DataStream> mStreamData;
  149. /************************************************************************/
  150. /* SERIALIZATION */
  151. /************************************************************************/
  152. public:
  153. friend class AudioClipRTTI;
  154. static RTTITypeBase* getRTTIStatic();
  155. RTTITypeBase* getRTTI() const override;
  156. /**
  157. * Creates an AudioClip with no samples. You must populate its data manually followed by a call to initialize().
  158. *
  159. * @note For serialization use only.
  160. */
  161. static SPtr<AudioClip> createEmpty();
  162. };
  163. /** @} */
  164. }