Unravel Engine C++ Reference
Loading...
Searching...
No Matches
audio_source_component.cpp
Go to the documentation of this file.
2#include <audiopp/exception.h>
3#include <limits>
4
5namespace unravel
6{
7
9{
10 source_.reset();
11 auto_play_consumed_ = false;
12}
14{
15 auto_play_consumed_ = true;
16 if(source_)
17 {
18 source_->stop();
19 source_.reset();
20 }
21}
22
24{
25 if(get_autoplay() && !auto_play_consumed_)
26 {
27 if(play())
28 {
29 auto_play_consumed_ = true;
30 }
31 }
32
33 if(!source_)
34 {
35 return;
36 }
37
38 source_->update(std::chrono::milliseconds(16));
39 auto pos = t.get_position();
40 auto forward = t.z_unit_axis();
41 auto up = t.y_unit_axis();
42 source_->set_position({{pos.x, pos.y, pos.z}});
43 source_->set_orientation({{forward.x, forward.y, forward.z}}, {{up.x, up.y, up.z}});
44
45 if(source_->is_stopped())
46 {
47 source_.reset();
48 }
49}
50
52{
53 loop_ = on;
54
55 if(!source_)
56 {
57 return;
58 }
59 source_->set_loop(on);
60}
61
63{
64 volume_ =math::clamp(volume, 0.0f, 1.0f);
65
66 if(!source_)
67 {
68 return;
69 }
70 source_->set_volume(volume_);
71}
72
74{
75 pitch_ = math::clamp(pitch, 0.0f, 5.0f);
76
77 if(!source_)
78 {
79 return;
80 }
81 source_->set_pitch(pitch_);
82}
83
85{
86 volume_rolloff_ = math::clamp(rolloff, 0.0f, 10.0f);
87
88 if(!source_)
89 {
90 return;
91 }
92 source_->set_volume_rolloff(rolloff);
93}
94
96{
97 range_ = range;
98 range_.min = math::clamp(range_.min, 0.0f, range_.max);
99 range_.max = math::clamp(range_.max, range_.min, std::numeric_limits<float>::max());
100
101
102 if(!source_)
103 {
104 return;
105 }
106 source_->set_distance(range_.min, range_.max);
107}
108
110{
111 auto_play_ = on;
112}
113
115{
116 return auto_play_;
117}
118
120{
121 return volume_;
122}
123
125{
126 return pitch_;
127}
128
130{
131 return volume_rolloff_;
132}
133
135{
136 return range_;
137}
138
140{
141 if(!source_)
142 {
143 return;
144 }
145 source_->set_playback_position(offset);
146}
147
149{
150 if(!source_)
151 {
152 return {};
153 }
154 return source_->get_playback_position();
155}
156
158{
159 if(!source_)
160 {
161 return {};
162 }
163
164 return source_->get_playback_duration();
165}
166
168{
169 if(!source_)
170 {
171 if(!create_source())
172 {
173 return false;
174 }
175 }
176
177 if(source_ && sound_)
178 {
179 source_->bind(*sound_.get());
180 source_->play();
181 return true;
182 }
183
184 return false;
185}
186
188{
189 if(!source_)
190 {
191 return;
192 }
193 source_->stop();
194 source_.reset();
195}
196
198{
199 if(!source_)
200 {
201 return;
202 }
203 source_->pause();
204}
205
207{
208 if(!source_)
209 {
210 return;
211 }
212 source_->resume();
213}
214
216{
217 muted_ = mute;
218
219 if(!source_)
220 {
221 return;
222 }
223
224 if(mute)
225 {
226 source_->mute();
227 }
228 else
229 {
230 source_->unmute();
231 }
232}
233
235{
236 if(!source_)
237 {
238 return false;
239 }
240 return source_->is_muted();
241}
242
244{
245 if(!source_)
246 {
247 return false;
248 }
249 return source_->is_playing();
250}
251
253{
254 if(!source_)
255 {
256 return false;
257 }
258 return source_->is_paused();
259}
260
262{
263 return loop_;
264}
265
267{
268 stop();
269
270 sound_ = clip;
271
272 apply_all();
273}
274
276{
277 return sound_;
278}
279
281{
282 if(!source_)
283 {
284 return false;
285 }
286
287 return source_->has_bound_sound();
288}
289
290void audio_source_component::apply_all()
291{
292 set_loop(loop_);
293 set_volume(volume_);
294 set_pitch(pitch_);
295 set_volume_rolloff(volume_rolloff_);
296 set_range(range_);
297 set_autoplay(auto_play_);
298 set_mute(muted_);
299
300}
301
302auto audio_source_component::is_sound_valid() const -> bool
303{
304 return sound_;
305}
306
307auto audio_source_component::create_source() -> bool
308{
309 try
310 {
311 source_ = std::make_shared<audio::source>();
312 apply_all();
313 return true;
314 }
315 catch(const audio::exception& e)
316 {
317 APPLOG_ERROR(e.what());
318 return false;
319 }
320}
321
322} // namespace unravel
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
auto get_range() const -> const frange_t &
Gets the range of the audio source.
void set_volume(float volume)
Sets the volume of the audio source.
void set_autoplay(bool on)
Sets whether the audio source should autoplay.
void set_clip(const asset_handle< audio_clip > &clip)
Sets the audio clip for the audio source.
void set_range(const frange_t &range)
Sets the range of the audio source.
void update(const math::transform &t, delta_t dt)
Updates the audio source with the given transform and delta time.
void set_loop(bool on)
Sets whether the audio source should loop.
auto is_playing() const -> bool
Checks if the audio source is currently playing.
auto get_clip() const -> const asset_handle< audio_clip > &
Gets the audio clip of the audio source.
void resume()
Resumes playing the audio source.
void set_mute(bool mute)
Sets whether the audio source is muted.
void on_play_begin()
Called when audio playback begins.
void stop()
Stops playing the audio source.
auto is_muted() const -> bool
Checks if the audio source is muted.
auto get_volume_rolloff() const -> float
Gets the volume rolloff factor of the audio source.
void on_play_end()
Called when audio playback ends.
auto is_paused() const -> bool
Checks if the audio source is currently paused.
auto has_bound_sound() const -> bool
Checks if the audio source has a valid sound bound.
auto get_pitch() const -> float
Gets the pitch of the audio source.
void set_volume_rolloff(float rolloff)
Sets the volume rolloff factor of the audio source.
void set_playback_position(audio::duration_t offset)
Sets the playback position of the audio source.
void set_pitch(float pitch)
Sets the pitch of the audio source.
auto get_autoplay() const -> bool
Gets whether the audio source is set to autoplay.
auto is_looping() const -> bool
Checks if the audio source is set to loop.
auto get_volume() const -> float
Gets the volume of the audio source.
auto get_playback_position() const -> audio::duration_t
Gets the playback position of the audio source.
void pause()
Pauses the audio source.
auto get_playback_duration() const -> audio::duration_t
Gets the total playback duration of the audio source.
auto play() -> bool
Starts playing the audio source.
std::chrono::duration< float > delta_t
#define APPLOG_ERROR(...)
Definition logging.h:20
Represents a handle to an asset, providing access and management functions.
Struct representing an audio clip.
Definition audio_clip.h:16