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/BsAudioSource.h" |
7 | |
8 | namespace bs |
9 | { |
10 | /** @addtogroup OpenAudio |
11 | * @{ |
12 | */ |
13 | |
14 | /** OpenAL implementation of an AudioSource. */ |
15 | class OAAudioSource : public AudioSource |
16 | { |
17 | public: |
18 | OAAudioSource(); |
19 | virtual ~OAAudioSource(); |
20 | |
21 | /** @copydoc SceneActor::setTransform */ |
22 | void setTransform(const Transform& transform) override; |
23 | |
24 | /** @copydoc AudioSource::setClip */ |
25 | void setClip(const HAudioClip& clip) override; |
26 | |
27 | /** @copydoc AudioSource::setVelocity */ |
28 | void setVelocity(const Vector3& velocity) override; |
29 | |
30 | /** @copydoc AudioSource::setVolume */ |
31 | void setVolume(float volume) override; |
32 | |
33 | /** @copydoc AudioSource::setPitch */ |
34 | void setPitch(float pitch) override; |
35 | |
36 | /** @copydoc AudioSource::setIsLooping */ |
37 | void setIsLooping(bool loop) override; |
38 | |
39 | /** @copydoc AudioSource::setPriority */ |
40 | void setPriority(INT32 priority) override; |
41 | |
42 | /** @copydoc AudioSource::setMinDistance */ |
43 | void setMinDistance(float distance) override; |
44 | |
45 | /** @copydoc AudioSource::setAttenuation */ |
46 | void setAttenuation(float attenuation) override; |
47 | |
48 | /** @copydoc AudioSource::setTime */ |
49 | void setTime(float time) override; |
50 | |
51 | /** @copydoc AudioSource::getTime */ |
52 | float getTime() const override; |
53 | |
54 | /** @copydoc AudioSource::play */ |
55 | void play() override; |
56 | |
57 | /** @copydoc AudioSource::pause */ |
58 | void pause() override; |
59 | |
60 | /** @copydoc AudioSource::stop */ |
61 | void stop() override; |
62 | |
63 | /** @copydoc AudioSource::getState */ |
64 | AudioSourceState getState() const override; |
65 | |
66 | private: |
67 | friend class OAAudio; |
68 | |
69 | /** Destroys the internal representation of the audio source. */ |
70 | void clear(); |
71 | |
72 | /** Rebuilds the internal representation of an audio source. */ |
73 | void rebuild(); |
74 | |
75 | /** Streams new data into the source audio buffer, if needed. */ |
76 | void stream(); |
77 | |
78 | /** Same as stream(), but without a mutex lock (up to the caller to lock it). */ |
79 | void streamUnlocked(); |
80 | |
81 | /** Starts data streaming from the currently attached audio clip. */ |
82 | void startStreaming(); |
83 | |
84 | /** Stops streaming data from the currently attached audio clip. */ |
85 | void stopStreaming(); |
86 | |
87 | /** Pauses or resumes audio playback due to the global pause setting. */ |
88 | void setGlobalPause(bool pause); |
89 | |
90 | /** |
91 | * Returns true if the sound source is three dimensional (volume and pitch varies based on listener distance |
92 | * and velocity). |
93 | */ |
94 | bool is3D() const; |
95 | |
96 | /** |
97 | * Returns true if the audio source is receiving audio data from a separate thread (as opposed to loading it all |
98 | * at once. |
99 | */ |
100 | bool requiresStreaming() const; |
101 | |
102 | /** Fills the provided buffer with streaming data. */ |
103 | bool fillBuffer(UINT32 buffer, AudioDataInfo& info, UINT32 maxNumSamples); |
104 | |
105 | /** Makes the current audio clip active. Should be called whenever the audio clip changes. */ |
106 | void applyClip(); |
107 | |
108 | /** @copydoc IResourceListener::onClipChanged */ |
109 | void onClipChanged() override; |
110 | |
111 | Vector<UINT32> mSourceIDs; |
112 | float mSavedTime = 0.0f; |
113 | AudioSourceState mSavedState = AudioSourceState::Stopped; |
114 | bool mGloballyPaused = false; |
115 | |
116 | static const UINT32 StreamBufferCount = 3; // Maximum 32 |
117 | UINT32 mStreamBuffers[StreamBufferCount]; |
118 | UINT32 mBusyBuffers[StreamBufferCount]; |
119 | UINT32 mStreamProcessedPosition = 0; |
120 | UINT32 mStreamQueuedPosition = 0; |
121 | bool mIsStreaming = false; |
122 | mutable Mutex mMutex; |
123 | }; |
124 | |
125 | /** @} */ |
126 | } |
127 | |