BsAudioClip.h 6.8 KB

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