MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_audio_source.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_AUDIO_SOURCE_H_1610744589
12#define INCGUARD_MGL_AUDIO_SOURCE_H_1610744589
13
16#include <mgl/mgl_environment.h>
17
18namespace MGL::Audio
19{
21class Source
22{
23public:
24 /* ------------------------------------------------------------------------- */
29 /* ------------------------------------------------------------------------- */
30 Source(const SharedSourceInstance &sourceInstance) noexcept
31 : _sourceInstance(sourceInstance)
32 {
33 }
34
35 /* ------------------------------------------------------------------------- */
39 /* ------------------------------------------------------------------------- */
40 constexpr Source() noexcept = default;
41
42 /* ------------------------------------------------------------------------- */
48 /* ------------------------------------------------------------------------- */
49 Source(VoiceKey voiceKey, bool isAutoRemove = true) noexcept
50 : Source()
51 {
52 Attach(voiceKey, isAutoRemove);
53 }
54
55 /* ------------------------------------------------------------------------- */
59 /* ------------------------------------------------------------------------- */
60 ~Source() noexcept
61 {
62 Detach();
63 }
64
65 /* ------------------------------------------------------------------------- */
73 /* ------------------------------------------------------------------------- */
74 bool Attach(VoiceKey voiceKey, bool isAutoRemove) noexcept
75 {
76 Detach();
77 _sourceInstance = Player::GetInstance().MakeSourceInstance(voiceKey, isAutoRemove);
78
79 return true;
80 }
81
82 /* ------------------------------------------------------------------------- */
86 /* ------------------------------------------------------------------------- */
87 void Detach() noexcept
88 {
89 if (auto source = _sourceInstance.lock(); source != nullptr)
90 {
91 if (!source->IsPlaying() || source->IsPaused())
92 {
93 source->Remove();
94 }
95 else
96 {
97 source->SetAutoRemove(true);
98 }
99
100 _sourceInstance = WeakSourceInstance();
101 }
102 }
103
104 /* ------------------------------------------------------------------------- */
110 /* ------------------------------------------------------------------------- */
111 [[nodiscard]] bool IsAttached() const noexcept
112 {
113 return !_sourceInstance.expired();
114 }
115
116 /* ------------------------------------------------------------------------- */
127 /* ------------------------------------------------------------------------- */
128 bool Play(VoiceKey voiceKey, bool isAutoRemove = true, uint32_t trackIndex = 0, LoopType loopType = LoopType::ResourceDefault, float volume = 1.0f) noexcept
129 {
130 if (!Attach(voiceKey, isAutoRemove))
131 {
132 return false;
133 }
134
135 return Play(trackIndex, loopType, volume);
136 }
137
138 /* ------------------------------------------------------------------------- */
147 /* ------------------------------------------------------------------------- */
148 bool Play(uint32_t trackIndex = 0, LoopType loopType = LoopType::ResourceDefault, float volume = 1.0f) noexcept
149 {
150 if (auto source = _sourceInstance.lock(); source != nullptr)
151 {
152 return source->Play(trackIndex, loopType, volume);
153 }
154
155 return false;
156 }
157
158 /* ------------------------------------------------------------------------- */
163 /* ------------------------------------------------------------------------- */
164 void Pause(bool isEnabled = true) noexcept
165 {
166 if (auto source = _sourceInstance.lock(); source != nullptr)
167 {
168 source->Pause(isEnabled);
169 }
170 }
171
172 /* ------------------------------------------------------------------------- */
177 /* ------------------------------------------------------------------------- */
178 void Resume() noexcept
179 {
180 Pause(false);
181 }
182
183 /* ------------------------------------------------------------------------- */
189 /* ------------------------------------------------------------------------- */
190 [[nodiscard]] bool IsPaused() const noexcept
191 {
192 if (const auto source = _sourceInstance.lock(); source != nullptr)
193 {
194 return source->IsPaused();
195 }
196
197 return false;
198 }
199
200 /* ------------------------------------------------------------------------- */
204 /* ------------------------------------------------------------------------- */
205 void Stop() noexcept
206 {
207 if (const auto source = _sourceInstance.lock(); source != nullptr)
208 {
209 source->Remove();
210 _sourceInstance = WeakSourceInstance();
211 }
212 }
213
214 /* ------------------------------------------------------------------------- */
220 /* ------------------------------------------------------------------------- */
221 void SetVolume(float volume) noexcept
222 {
223 if (const auto source = _sourceInstance.lock(); source != nullptr)
224 {
225 source->SetVolume(volume);
226 }
227 }
228
229 /* ------------------------------------------------------------------------- */
237 /* ------------------------------------------------------------------------- */
238 void SetVolume(float volume, float fadeTimeSec, bool isAutoStop = false) noexcept
239 {
240 if (const auto source = _sourceInstance.lock(); source != nullptr)
241 {
242 auto samplesPerSec = Player::GetInstance().GetOutputFormat().samplesPerSec;
243 source->SetVolume(volume, fadeTimeSec, samplesPerSec, isAutoStop);
244 }
245 }
246
247 /* ------------------------------------------------------------------------- */
253 /* ------------------------------------------------------------------------- */
254 void Fadeout(float fadeTimeSec, bool isAutoStop = true) noexcept
255 {
256 SetVolume(0.0f, fadeTimeSec, isAutoStop);
257 }
258
259 /* ------------------------------------------------------------------------- */
264 /* ------------------------------------------------------------------------- */
265 [[nodiscard]] float GetVolume() const noexcept
266 {
267 if (const auto source = _sourceInstance.lock(); source != nullptr)
268 {
269 return source->GetVolume();
270 }
271
272 return 0.0f;
273 }
274
275 /* ------------------------------------------------------------------------- */
281 /* ------------------------------------------------------------------------- */
282 [[nodiscard]] bool IsPlaying() const noexcept
283 {
284 if (const auto source = _sourceInstance.lock(); source != nullptr)
285 {
286 return source->IsPlaying();
287 }
288
289 return false;
290 }
291
292 /* ------------------------------------------------------------------------- */
298 /* ------------------------------------------------------------------------- */
299 [[nodiscard]] bool IsFading() const noexcept
300 {
301 if (const auto source = _sourceInstance.lock(); source != nullptr)
302 {
303 return source->IsFading();
304 }
305
306 return false;
307 }
308
309 // コピー禁止
310 Source(const Source &) = delete;
311 Source &operator=(const Source &) = delete;
312
313 /* ------------------------------------------------------------------------- */
318 /* ------------------------------------------------------------------------- */
319 Source(Source &&other) noexcept
320 : _sourceInstance(std::move(other._sourceInstance))
321 {
322 other._sourceInstance = WeakSourceInstance();
323 }
324
325 /* ------------------------------------------------------------------------- */
330 /* ------------------------------------------------------------------------- */
331 Source &operator=(Source &&other) noexcept
332 {
333 if (this != &other)
334 {
335 Detach();
336 _sourceInstance = std::move(other._sourceInstance);
337 other._sourceInstance = WeakSourceInstance();
338 }
339
340 return *this;
341 }
342
343private:
344 WeakSourceInstance _sourceInstance;
345};
346} // namespace MGL::Audio
347#endif // INCGUARD_MGL_AUDIO_SOURCE_H_1610744589
348
349// vim: et ts=4 sw=4 sts=4
オーディオソースクラス
Definition mgl_audio_source.h:22
Source(Source &&other) noexcept
ムーブコンストラクタ
Definition mgl_audio_source.h:319
bool IsAttached() const noexcept
ソースがボイスと関連付けられているかを取得
Definition mgl_audio_source.h:111
bool IsPlaying() const noexcept
再生中かどうかを取得
Definition mgl_audio_source.h:282
Source(const SharedSourceInstance &sourceInstance) noexcept
コンストラクタ
Definition mgl_audio_source.h:30
void Pause(bool isEnabled=true) noexcept
一時停止
Definition mgl_audio_source.h:164
bool Play(VoiceKey voiceKey, bool isAutoRemove=true, uint32_t trackIndex=0, LoopType loopType=LoopType::ResourceDefault, float volume=1.0f) noexcept
アタッチして再生
Definition mgl_audio_source.h:128
void SetVolume(float volume) noexcept
音量の設定
Definition mgl_audio_source.h:221
void Resume() noexcept
再開
Definition mgl_audio_source.h:178
void Stop() noexcept
停止要求
Definition mgl_audio_source.h:205
void SetVolume(float volume, float fadeTimeSec, bool isAutoStop=false) noexcept
変化時間を指定して音量を設定
Definition mgl_audio_source.h:238
constexpr Source() noexcept=default
コンストラクタ
bool Play(uint32_t trackIndex=0, LoopType loopType=LoopType::ResourceDefault, float volume=1.0f) noexcept
アタッチ済みのボイスを再生
Definition mgl_audio_source.h:148
bool IsFading() const noexcept
フェード中かどうかを取得
Definition mgl_audio_source.h:299
bool IsPaused() const noexcept
一時停止の状態を取得
Definition mgl_audio_source.h:190
~Source() noexcept
デストラクタ
Definition mgl_audio_source.h:60
Source & operator=(Source &&other) noexcept
ムーブ代入演算
Definition mgl_audio_source.h:331
bool Attach(VoiceKey voiceKey, bool isAutoRemove) noexcept
ボイスとソースの関連付け
Definition mgl_audio_source.h:74
float GetVolume() const noexcept
ボリュームの取得
Definition mgl_audio_source.h:265
void Detach() noexcept
ボイスとソースの関連付けを解除
Definition mgl_audio_source.h:87
void Fadeout(float fadeTimeSec, bool isAutoStop=true) noexcept
フェードアウト
Definition mgl_audio_source.h:254
static Player & GetInstance() noexcept
Definition mgl_singleton.h:74
VoiceKey
ボイスキー
Definition mgl_audio_defs.h:22
LoopType
ループタイプ
Definition mgl_audio_defs.h:71
MGL オーディオプレイヤー
MGL オーディオソースインスタンス
std::weak_ptr< SourceInstance > WeakSourceInstance
弱参照のソースインスタンス
Definition mgl_audio_source_instance.h:190
std::shared_ptr< SourceInstance > SharedSourceInstance
共有ソースインスタンス
Definition mgl_audio_source_instance.h:187
MGL 環境定義