BsAudioClip.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /**
  51. * Audio clip stores audio data in a compressed or uncompressed format. Clips can be provided to audio sources or
  52. * or other audio methods to be played.
  53. */
  54. class BS_CORE_EXPORT AudioClip : public Resource
  55. {
  56. public:
  57. virtual ~AudioClip() { }
  58. UINT32 getBitDepth() const { return mDesc.bitDepth; }
  59. UINT32 getFrequency() const { return mDesc.frequency; }
  60. UINT32 getNumChannels() const { return mDesc.numChannels; }
  61. AudioFormat getFormat() const { return mDesc.format; }
  62. AudioReadMode getReadMode() const { return mDesc.readMode; }
  63. float getLength() const { return mLength; }
  64. UINT32 getNumSamples() const { return mNumSamples; }
  65. bool is3D() const { return mDesc.is3D; }
  66. /**
  67. * Returns audio samples in PCM format, channel data interleaved. Sample read pointer is advanced by the read
  68. * amount. Only available if the audio data has been created with AudioReadMode::Stream,
  69. * AudioReadMode::LoadCompressed (and the format is compressed), or if @p keepSourceData was enabled on creation.
  70. *
  71. * @param[in] samples Previously allocated buffer to contain the samples.
  72. * @param[in] count Number of samples to read (should be a multiple of number of channels).
  73. *
  74. * @note Implementation must be thread safe as this will get called from audio streaming thread.
  75. */
  76. virtual void getSamples(UINT8* samples, UINT32 count) const = 0;
  77. /**
  78. * Moves the read location from which the getSamples method retrieves samples. Only available if the audio data
  79. * has been created with AudioReadMode::Stream, AudioReadMode::LoadCompressed (and the format is compressed),
  80. * or if @p keepSourceData was enabled on creation.
  81. *
  82. * @param[in] offset Offset in number of samples at which to start reading (should be a multiple of number
  83. * of channels).
  84. *
  85. * @note Implementation must be thread safe as this will get called from audio streaming thread.
  86. */
  87. virtual void seekSamples(UINT32 offset) = 0;
  88. static HAudioClip create(UINT32 streamSize, UINT32 numSamples, const AUDIO_CLIP_DESC& desc);
  89. static HAudioClip create(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples,
  90. const AUDIO_CLIP_DESC& desc); // Note that ownership of stream is taken by the AudioClip
  91. public: // ***** INTERNAL ******
  92. /** @name Internal
  93. * @{
  94. */
  95. static SPtr<AudioClip> _createPtr(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples,
  96. const AUDIO_CLIP_DESC& desc);
  97. /** @} */
  98. protected:
  99. AudioClip(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples, const AUDIO_CLIP_DESC& desc);
  100. /** @copydoc Resource::initialize */
  101. void initialize() override;
  102. /** Returns original audio data. Only available if @p keepSourceData has been provided on creation. */
  103. virtual SPtr<DataStream> getSourceStream(UINT32& size) = 0;
  104. protected:
  105. AUDIO_CLIP_DESC mDesc;
  106. UINT32 mNumSamples;
  107. UINT32 mStreamSize;
  108. UINT32 mStreamOffset;
  109. float mLength;
  110. bool mKeepSourceData;
  111. SPtr<DataStream> mStreamData;
  112. /************************************************************************/
  113. /* SERIALIZATION */
  114. /************************************************************************/
  115. public:
  116. friend class AudioClipRTTI;
  117. static RTTITypeBase* getRTTIStatic();
  118. RTTITypeBase* getRTTI() const override;
  119. static SPtr<AudioClip> createEmpty();
  120. };
  121. /** @} */
  122. }