1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsOAPrerequisites.h"
6#include "Audio/BsAudioClip.h"
7#include "BsOggVorbisDecoder.h"
8
9namespace bs
10{
11 /** @addtogroup OpenAudio
12 * @{
13 */
14
15 /** OpenAudio implementation of an AudioClip. */
16 class OAAudioClip : public AudioClip
17 {
18 public:
19 OAAudioClip(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples, const AUDIO_CLIP_DESC& desc);
20 virtual ~OAAudioClip();
21
22 /**
23 * Returns audio samples in PCM format, channel data interleaved. Only available if the audio data has been created
24 * with AudioReadMode::Stream, AudioReadMode::LoadCompressed (and the format is compressed), or if @p keepSourceData
25 * was enabled on creation.
26 *
27 * @param[in] samples Previously allocated buffer to contain the samples.
28 * @param[in] offset Offset in number of samples at which to start reading (should be a multiple of number
29 * of channels).
30 * @param[in] count Number of samples to read (should be a multiple of number of channels).
31 *
32 * @note Implementation must be thread safe as this will get called from audio streaming thread.
33 */
34 void getSamples(UINT8* samples, UINT32 offset, UINT32 count) const;
35
36 /** @name Internal
37 * @{
38 */
39
40 /** Returns the internal OpenAL buffer. Only valid if the audio clip was created without AudioReadMode::Stream. */
41 UINT32 _getOpenALBuffer() const { return mBufferId; }
42
43 /** @} */
44 protected:
45 /** @copydoc Resource::initialize */
46 void initialize() override;
47
48 /** @copydoc AudioClip::getSourceStream */
49 SPtr<DataStream> getSourceStream(UINT32& size) override;
50 private:
51 mutable Mutex mMutex;
52 mutable OggVorbisDecoder mVorbisReader;
53 bool mNeedsDecompression = false;
54 UINT32 mBufferId = (UINT32)-1;
55
56 // These streams exist to save original audio data in case it's needed later (usually for saving with the editor, or
57 // manual data manipulation). In normal usage (in-game) these will be null so no memory is wasted.
58 SPtr<DataStream> mSourceStreamData;
59 UINT32 mSourceStreamSize = 0;
60 };
61
62 /** @} */
63}