bevy_audio 0.18.1

Provides audio functionality for Bevy Engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use crate::Volume;
use bevy_ecs::component::Component;
use bevy_math::Vec3;
use bevy_transform::prelude::Transform;
use core::time::Duration;
pub use rodio::source::SeekError;
use rodio::{Sink, SpatialSink};

/// Common interactions with an audio sink.
pub trait AudioSinkPlayback {
    /// Gets the volume of the sound as a [`Volume`].
    ///
    /// If the sink is muted, this returns the managed volume rather than the
    /// sink's actual volume. This allows you to use the returned volume as if
    /// the sink were not muted, because a muted sink has a physical volume of
    /// 0.
    fn volume(&self) -> Volume;

    /// Changes the volume of the sound to the given [`Volume`].
    ///
    /// If the sink is muted, changing the volume won't unmute it, i.e. the
    /// sink's volume will remain "off" / "muted". However, the sink will
    /// remember the volume change and it will be used when
    /// [`unmute`](Self::unmute) is called. This allows you to control the
    /// volume even when the sink is muted.
    fn set_volume(&mut self, volume: Volume);

    /// Gets the speed of the sound.
    ///
    /// The value `1.0` is the "normal" speed (unfiltered input). Any value other than `1.0`
    /// will change the play speed of the sound.
    fn speed(&self) -> f32;

    /// Changes the speed of the sound.
    ///
    /// The value `1.0` is the "normal" speed (unfiltered input). Any value other than `1.0`
    /// will change the play speed of the sound.
    fn set_speed(&self, speed: f32);

    /// Resumes playback of a paused sink.
    ///
    /// No effect if not paused.
    fn play(&self);

    /// Returns the position of the sound that's being played.
    ///
    /// This takes into account any speedup or delay applied.
    ///
    /// Example: if you [`set_speed(2.0)`](Self::set_speed) and [`position()`](Self::position) returns *5s*,
    /// then the position in the recording is *10s* from its start.
    fn position(&self) -> Duration;

    /// Attempts to seek to a given position in the current source.
    ///
    /// This blocks between 0 and ~5 milliseconds.
    ///
    /// As long as the duration of the source is known, seek is guaranteed to saturate
    /// at the end of the source. For example given a source that reports a total duration
    /// of 42 seconds calling `try_seek()` with 60 seconds as argument will seek to
    /// 42 seconds.
    ///
    /// # Errors
    /// This function will return [`SeekError::NotSupported`] if one of the underlying
    /// sources does not support seeking.
    ///
    /// It will return an error if an implementation ran
    /// into one during the seek.
    ///
    /// When seeking beyond the end of a source, this
    /// function might return an error if the duration of the source is not known.
    fn try_seek(&self, pos: Duration) -> Result<(), SeekError>;

    /// Pauses playback of this sink.
    ///
    /// No effect if already paused.
    /// A paused sink can be resumed with [`play`](Self::play).
    fn pause(&self);

    /// Toggles playback of the sink.
    ///
    /// If the sink is paused, toggling playback resumes it. If the sink is
    /// playing, toggling playback pauses it.
    fn toggle_playback(&self) {
        if self.is_paused() {
            self.play();
        } else {
            self.pause();
        }
    }

    /// Returns true if the sink is paused.
    ///
    /// Sinks can be paused and resumed using [`pause`](Self::pause) and [`play`](Self::play).
    fn is_paused(&self) -> bool;

    /// Stops the sink.
    ///
    /// It won't be possible to restart it afterwards.
    fn stop(&self);

    /// Returns true if this sink has no more sounds to play.
    fn empty(&self) -> bool;

    /// Returns true if the sink is muted.
    fn is_muted(&self) -> bool;

    /// Mutes the sink.
    ///
    /// Muting a sink sets the volume to 0. Use [`unmute`](Self::unmute) to
    /// unmute the sink and restore the original volume.
    fn mute(&mut self);

    /// Unmutes the sink.
    ///
    /// Restores the volume to the value it was before it was muted.
    fn unmute(&mut self);

    /// Toggles whether the sink is muted or not.
    fn toggle_mute(&mut self) {
        if self.is_muted() {
            self.unmute();
        } else {
            self.mute();
        }
    }
}

/// Used to control audio during playback.
///
/// Bevy inserts this component onto your entities when it begins playing an audio source.
/// Use [`AudioPlayer`][crate::AudioPlayer] to trigger that to happen.
///
/// You can use this component to modify the playback settings while the audio is playing.
///
/// If this component is removed from an entity, and an [`AudioSource`][crate::AudioSource] is
/// attached to that entity, that [`AudioSource`][crate::AudioSource] will start playing. If
/// that source is unchanged, that translates to the audio restarting.
#[derive(Component)]
pub struct AudioSink {
    pub(crate) sink: Sink,

    /// Managed volume allows the sink to be muted without losing the user's
    /// intended volume setting.
    ///
    /// This is used to restore the volume when [`unmute`](Self::unmute) is
    /// called.
    ///
    /// If the sink is not muted, this is `None`.
    ///
    /// If the sink is muted, this is `Some(volume)` where `volume` is the
    /// user's intended volume setting, even if the underlying sink's volume is
    /// 0.
    pub(crate) managed_volume: Option<Volume>,
}

impl AudioSink {
    /// Create a new audio sink.
    pub fn new(sink: Sink) -> Self {
        Self {
            sink,
            managed_volume: None,
        }
    }
}

impl AudioSinkPlayback for AudioSink {
    fn volume(&self) -> Volume {
        self.managed_volume
            .unwrap_or_else(|| Volume::Linear(self.sink.volume()))
    }

    fn set_volume(&mut self, volume: Volume) {
        if self.is_muted() {
            self.managed_volume = Some(volume);
        } else {
            self.sink.set_volume(volume.to_linear());
        }
    }

    fn speed(&self) -> f32 {
        self.sink.speed()
    }

    fn set_speed(&self, speed: f32) {
        self.sink.set_speed(speed);
    }

    fn play(&self) {
        self.sink.play();
    }

    fn position(&self) -> Duration {
        self.sink.get_pos()
    }

    fn try_seek(&self, pos: Duration) -> Result<(), SeekError> {
        self.sink.try_seek(pos)
    }

    fn pause(&self) {
        self.sink.pause();
    }

    fn is_paused(&self) -> bool {
        self.sink.is_paused()
    }

    fn stop(&self) {
        self.sink.stop();
    }

    fn empty(&self) -> bool {
        self.sink.empty()
    }

    fn is_muted(&self) -> bool {
        self.managed_volume.is_some()
    }

    fn mute(&mut self) {
        self.managed_volume = Some(self.volume());
        self.sink.set_volume(0.0);
    }

    fn unmute(&mut self) {
        if let Some(volume) = self.managed_volume.take() {
            self.sink.set_volume(volume.to_linear());
        }
    }
}

/// Used to control spatial audio during playback.
///
/// Bevy inserts this component onto your entities when it begins playing an audio source
/// that's configured to use spatial audio.
///
/// You can use this component to modify the playback settings while the audio is playing.
///
/// If this component is removed from an entity, and a [`AudioSource`][crate::AudioSource] is
/// attached to that entity, that [`AudioSource`][crate::AudioSource] will start playing. If
/// that source is unchanged, that translates to the audio restarting.
#[derive(Component)]
pub struct SpatialAudioSink {
    pub(crate) sink: SpatialSink,

    /// Managed volume allows the sink to be muted without losing the user's
    /// intended volume setting.
    ///
    /// This is used to restore the volume when [`unmute`](Self::unmute) is
    /// called.
    ///
    /// If the sink is not muted, this is `None`.
    ///
    /// If the sink is muted, this is `Some(volume)` where `volume` is the
    /// user's intended volume setting, even if the underlying sink's volume is
    /// 0.
    pub(crate) managed_volume: Option<Volume>,
}

impl SpatialAudioSink {
    /// Create a new spatial audio sink.
    pub fn new(sink: SpatialSink) -> Self {
        Self {
            sink,
            managed_volume: None,
        }
    }
}

impl AudioSinkPlayback for SpatialAudioSink {
    fn volume(&self) -> Volume {
        self.managed_volume
            .unwrap_or_else(|| Volume::Linear(self.sink.volume()))
    }

    fn set_volume(&mut self, volume: Volume) {
        if self.is_muted() {
            self.managed_volume = Some(volume);
        } else {
            self.sink.set_volume(volume.to_linear());
        }
    }

    fn speed(&self) -> f32 {
        self.sink.speed()
    }

    fn set_speed(&self, speed: f32) {
        self.sink.set_speed(speed);
    }

    fn play(&self) {
        self.sink.play();
    }

    fn position(&self) -> Duration {
        self.sink.get_pos()
    }

    fn try_seek(&self, pos: Duration) -> Result<(), SeekError> {
        self.sink.try_seek(pos)
    }

    fn pause(&self) {
        self.sink.pause();
    }

    fn is_paused(&self) -> bool {
        self.sink.is_paused()
    }

    fn stop(&self) {
        self.sink.stop();
    }

    fn empty(&self) -> bool {
        self.sink.empty()
    }

    fn is_muted(&self) -> bool {
        self.managed_volume.is_some()
    }

    fn mute(&mut self) {
        self.managed_volume = Some(self.volume());
        self.sink.set_volume(0.0);
    }

    fn unmute(&mut self) {
        if let Some(volume) = self.managed_volume.take() {
            self.sink.set_volume(volume.to_linear());
        }
    }
}

impl SpatialAudioSink {
    /// Set the two ears position.
    pub fn set_ears_position(&self, left_position: Vec3, right_position: Vec3) {
        self.sink.set_left_ear_position(left_position.to_array());
        self.sink.set_right_ear_position(right_position.to_array());
    }

    /// Set the listener position, with an ear on each side separated by `gap`.
    pub fn set_listener_position(&self, position: Transform, gap: f32) {
        self.set_ears_position(
            position.translation + position.left() * gap / 2.0,
            position.translation + position.right() * gap / 2.0,
        );
    }

    /// Set the emitter position.
    pub fn set_emitter_position(&self, position: Vec3) {
        self.sink.set_emitter_position(position.to_array());
    }
}

#[cfg(test)]
mod tests {
    use rodio::Sink;

    use super::*;

    fn test_audio_sink_playback<T: AudioSinkPlayback>(mut audio_sink: T) {
        // Test volume
        assert_eq!(audio_sink.volume(), Volume::Linear(1.0)); // default volume
        audio_sink.set_volume(Volume::Linear(0.5));
        assert_eq!(audio_sink.volume(), Volume::Linear(0.5));
        audio_sink.set_volume(Volume::Linear(1.0));
        assert_eq!(audio_sink.volume(), Volume::Linear(1.0));

        // Test speed
        assert_eq!(audio_sink.speed(), 1.0); // default speed
        audio_sink.set_speed(0.5);
        assert_eq!(audio_sink.speed(), 0.5);
        audio_sink.set_speed(1.0);
        assert_eq!(audio_sink.speed(), 1.0);

        // Test playback
        assert!(!audio_sink.is_paused()); // default pause state
        audio_sink.pause();
        assert!(audio_sink.is_paused());
        audio_sink.play();
        assert!(!audio_sink.is_paused());

        // Test toggle playback
        audio_sink.pause(); // start paused
        audio_sink.toggle_playback();
        assert!(!audio_sink.is_paused());
        audio_sink.toggle_playback();
        assert!(audio_sink.is_paused());

        // Test mute
        assert!(!audio_sink.is_muted()); // default mute state
        audio_sink.mute();
        assert!(audio_sink.is_muted());
        audio_sink.unmute();
        assert!(!audio_sink.is_muted());

        // Test volume with mute
        audio_sink.set_volume(Volume::Linear(0.5));
        audio_sink.mute();
        assert_eq!(audio_sink.volume(), Volume::Linear(0.5)); // returns managed volume even though sink volume is 0
        audio_sink.unmute();
        assert_eq!(audio_sink.volume(), Volume::Linear(0.5)); // managed volume is restored

        // Test toggle mute
        audio_sink.toggle_mute();
        assert!(audio_sink.is_muted());
        audio_sink.toggle_mute();
        assert!(!audio_sink.is_muted());
    }

    #[test]
    fn test_audio_sink() {
        let (sink, _queue_rx) = Sink::new_idle();
        let audio_sink = AudioSink::new(sink);
        test_audio_sink_playback(audio_sink);
    }
}