NcEngine
AudioSource.h
Go to the documentation of this file.
1
5#pragma once
6
7#include "ncengine/asset/Assets.h"
9
10#include <span>
11
12namespace nc
13{
14namespace audio
15{
16class NcAudioImpl;
17} // namespace audio
18
21{
22 static constexpr uint8_t None = 0b00000000;
23 static constexpr uint8_t Play = 0b00000001;
24 static constexpr uint8_t Loop = 0b00000010;
25 static constexpr uint8_t Spatial = 0b00000100;
26};
27
30{
31 float gain = 1.0f;
32 float innerRadius = 1.0f;
33 float outerRadius = 15.0f;
34 uint8_t flags = AudioSourceFlags::None;
35};
36
38constexpr auto NullClipIndex = UINT32_MAX;
39
47{
48 public:
49 AudioSource(Entity entity,
50 std::vector<asset::AudioClipView> clips = {},
52
54 void Play(uint32_t clipIndex = 0ull);
55
57 void PlayNext();
58
60 auto IsPlaying() const noexcept -> bool { return m_properties.flags & AudioSourceFlags::Play; }
61
66 void Queue(uint32_t clipIndex);
67
72 auto GetQueuedClipIndex() const noexcept -> uint32_t { return m_currentClipIndex; }
73
75 void Stop() noexcept { m_properties.flags &= ~AudioSourceFlags::Play; }
76
81 void Resume();
82
84 void ResetPlayPosition() { m_currentSampleIndex = 0; }
85
90 auto AddClip(const asset::AudioClipView& clip) -> uint32_t;
91
93 void SetClip(uint32_t clipIndex, const asset::AudioClipView& clip);
94
99 void RemoveClip(uint32_t clipIndex);
100
101 auto GetClips() const noexcept -> const std::vector<asset::AudioClipView>& { return m_clips; }
102 auto GetProperties() const noexcept -> const AudioSourceProperties& { return m_properties; }
103 auto GetGain() const noexcept -> float { return m_properties.gain; }
104 auto GetInnerRadius() const noexcept -> float { return m_properties.innerRadius; }
105 auto GetOuterRadius() const noexcept -> float { return m_properties.outerRadius; }
106 auto IsSpatial() const noexcept -> bool { return m_properties.flags & AudioSourceFlags::Spatial; }
107 auto IsLooping() const noexcept -> bool { return m_properties.flags & AudioSourceFlags::Loop; }
108
109 void SetProperties(const AudioSourceProperties& properties) noexcept { m_properties = properties; }
110 void SetGain(float gain) noexcept { m_properties.gain = gain; }
111 void SetInnerRadius(float radius) noexcept { m_properties.innerRadius = radius; }
112 void SetOuterRadius(float radius) noexcept { m_properties.outerRadius = radius; }
113
114 void SetSpatial(bool spatialize) noexcept
115 {
116 m_properties.flags = spatialize
117 ? m_properties.flags | AudioSourceFlags::Spatial
118 : m_properties.flags & ~AudioSourceFlags::Spatial;
119 }
120
121 void SetLooping(bool loop) noexcept
122 {
123 m_properties.flags = loop
124 ? m_properties.flags | AudioSourceFlags::Loop
125 : m_properties.flags & ~AudioSourceFlags::Loop;
126 }
127
128 private:
129 std::vector<asset::AudioClipView> m_clips;
130 uint32_t m_currentClipIndex = NullClipIndex;
131 uint32_t m_currentSampleIndex = 0u;
132 AudioSourceProperties m_properties;
133
134 void SetPlaying() noexcept { m_properties.flags |= AudioSourceFlags::Play; }
135 void SetStopped() noexcept { m_properties.flags &= ~AudioSourceFlags::Play; }
136 void WriteSpatialSamples(double* buffer, size_t frames, const Vector3& sourcePosition, const Vector3& listenerPosition, const Vector3& rightEar);
137 void WriteNonSpatialSamples(double* buffer, size_t frames);
138
139 friend class audio::NcAudioImpl;
140};
141} // namespace nc
constexpr auto NullClipIndex
Indicates an invalid audio clip index.
Definition: AudioSource.h:38
Component managing audio clips.
Definition: AudioSource.h:47
auto AddClip(const asset::AudioClipView &clip) -> uint32_t
Add an audio clip.
void Resume()
Continue playing from the last position.
auto GetQueuedClipIndex() const noexcept -> uint32_t
Get the index of the most recently queued or played clip.
Definition: AudioSource.h:72
void PlayNext()
Play the audio clip following the one most recently played (round-robin).
void Play(uint32_t clipIndex=0ull)
Play the audio clip at a given index.
void RemoveClip(uint32_t clipIndex)
Remove the audio clip at a given index.
auto IsPlaying() const noexcept -> bool
Check if a clip is currently playing.
Definition: AudioSource.h:60
void ResetPlayPosition()
Go back the beginning of the current playing or queued clip.
Definition: AudioSource.h:84
void SetClip(uint32_t clipIndex, const asset::AudioClipView &clip)
Replace the audio clip at a given index.
void Stop() noexcept
Stop the currently playing audio clip, preserving the current position in the clip.
Definition: AudioSource.h:75
void Queue(uint32_t clipIndex)
Preemptively set the next clip to be played. This can be started with Resume() instead of Play().
Optional base class for components.
Definition: Component.h:27
Identifies an object in the registry.
Definition: Entity.h:18
Flags applying to all clips in an AudioSource.
Definition: AudioSource.h:21
Properties applying to all clips in an AudioSource.
Definition: AudioSource.h:30
Definition: AssetViews.h:25