Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Audio.h
Go to the documentation of this file.
1/**
2 * @file Audio.h
3 * @ingroup mnd_audio
4 * @brief Reference-counted audio clip with 3D positioning.
5 *
6 * A single mnd::Audio instance wraps one decoded miniaudio stream. Clips
7 * are loaded through the static factory and shared between callers via
8 * `std::shared_ptr<Audio>`. Hand them to a mnd::AudioComponent to
9 * trigger playback through the engine's mnd::AudioManager.
10 *
11 * @code
12 * auto shot = mnd::Audio::Load("sfx/laser.wav");
13 * shot->SetVolume(0.6F);
14 * shot->Play();
15 * @endcode
16 */
17
18#pragma once
19#include <memory>
20#include <string>
21#include <vector>
22
23#include "Common.h"
24
25struct ma_sound;
26struct ma_decoder;
27
28namespace mnd
29{
30/**
31 * @ingroup mnd_audio
32 * @brief Single playable audio clip backed by a decoded miniaudio stream.
33 */
34class Audio
35{
36 public:
37 ~Audio();
38
39 /// Place the emitter at a world-space position (3D spatialisation).
40 void SetPosition(const glm::vec3 &position);
41 /// Start playback. Pass @c true to loop forever.
42 void Play(bool loop = false);
43 /// Halt playback if currently playing; no-op otherwise.
44 void Stop();
45 /// True between Play() and natural end / Stop().
46 [[nodiscard]] bool IsPlaying() const;
47 /// Linear gain in [0, 1]. Values above 1 amplify but may clip.
48 void SetVolume(float volume);
49 [[nodiscard]] float GetVolume() const;
50
51 /**
52 * @brief Decode an audio file from disk and return a shared instance.
53 * @param path Path under the assets directory; resolved by FileSystem.
54 * @return Non-null shared pointer on success, null on decode failure.
55 */
56 static std::shared_ptr<Audio> Load(const std::string &path);
57
58 private:
59 std::unique_ptr<ma_sound> m_sound;
60 std::unique_ptr<ma_decoder> m_decoder;
61 std::vector<char> m_buffer;
62};
63} // namespace mnd
Single playable audio clip backed by a decoded miniaudio stream.
Definition Audio.h:35
void Play(bool loop=false)
Start playback. Pass true to loop forever.
Definition Audio.cpp:29
float GetVolume() const
Definition Audio.cpp:65
void Stop()
Halt playback if currently playing; no-op otherwise.
Definition Audio.cpp:39
void SetPosition(const glm::vec3 &position)
Place the emitter at a world-space position (3D spatialisation).
Definition Audio.cpp:21
void SetVolume(float volume)
Linear gain in [0, 1]. Values above 1 amplify but may clip.
Definition Audio.cpp:57
bool IsPlaying() const
True between Play() and natural end / Stop().
Definition Audio.cpp:48
static std::shared_ptr< Audio > Load(const std::string &path)
Decode an audio file from disk and return a shared instance.
Definition Audio.cpp:74