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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
use bevy_ecs::prelude::*;
use bevy_math::ops;
use bevy_reflect::prelude::*;

/// Use this [`Resource`] to control the global volume of all audio.
///
/// Note: Changing [`GlobalVolume`] does not affect already playing audio.
#[derive(Resource, Debug, Default, Clone, Copy, Reflect)]
#[reflect(Resource, Debug, Default, Clone)]
pub struct GlobalVolume {
    /// The global volume of all audio.
    pub volume: Volume,
}

impl From<Volume> for GlobalVolume {
    fn from(volume: Volume) -> Self {
        Self { volume }
    }
}

impl GlobalVolume {
    /// Create a new [`GlobalVolume`] with the given volume.
    pub fn new(volume: Volume) -> Self {
        Self { volume }
    }
}

/// A [`Volume`] represents an audio source's volume level.
///
/// To create a new [`Volume`] from a linear scale value, use
/// [`Volume::Linear`].
///
/// To create a new [`Volume`] from decibels, use [`Volume::Decibels`].
#[derive(Clone, Copy, Debug, Reflect)]
#[reflect(Clone, Debug, PartialEq)]
pub enum Volume {
    /// Create a new [`Volume`] from the given volume in the linear scale.
    ///
    /// In a linear scale, the value `1.0` represents the "normal" volume,
    /// meaning the audio is played at its original level. Values greater than
    /// `1.0` increase the volume, while values between `0.0` and `1.0` decrease
    /// the volume. A value of `0.0` effectively mutes the audio.
    ///
    /// # Examples
    ///
    /// ```
    /// # use bevy_audio::Volume;
    /// # use bevy_math::ops;
    /// #
    /// # const EPSILON: f32 = 0.01;
    ///
    /// let volume = Volume::Linear(0.5);
    /// assert_eq!(volume.to_linear(), 0.5);
    /// assert!(ops::abs(volume.to_decibels() - -6.0206) < EPSILON);
    ///
    /// let volume = Volume::Linear(0.0);
    /// assert_eq!(volume.to_linear(), 0.0);
    /// assert_eq!(volume.to_decibels(), f32::NEG_INFINITY);
    ///
    /// let volume = Volume::Linear(1.0);
    /// assert_eq!(volume.to_linear(), 1.0);
    /// assert!(ops::abs(volume.to_decibels() - 0.0) < EPSILON);
    /// ```
    Linear(f32),
    /// Create a new [`Volume`] from the given volume in decibels.
    ///
    /// In a decibel scale, the value `0.0` represents the "normal" volume,
    /// meaning the audio is played at its original level. Values greater than
    /// `0.0` increase the volume, while values less than `0.0` decrease the
    /// volume. A value of [`f32::NEG_INFINITY`] decibels effectively mutes the
    /// audio.
    ///
    /// # Examples
    ///
    /// ```
    /// # use bevy_audio::Volume;
    /// # use bevy_math::ops;
    /// #
    /// # const EPSILON: f32 = 0.01;
    ///
    /// let volume = Volume::Decibels(-5.998);
    /// assert!(ops::abs(volume.to_linear() - 0.5) < EPSILON);
    ///
    /// let volume = Volume::Decibels(f32::NEG_INFINITY);
    /// assert_eq!(volume.to_linear(), 0.0);
    ///
    /// let volume = Volume::Decibels(0.0);
    /// assert_eq!(volume.to_linear(), 1.0);
    ///
    /// let volume = Volume::Decibels(20.0);
    /// assert_eq!(volume.to_linear(), 10.0);
    /// ```
    Decibels(f32),
}

impl Default for Volume {
    fn default() -> Self {
        Self::Linear(1.0)
    }
}

impl PartialEq for Volume {
    fn eq(&self, other: &Self) -> bool {
        use Volume::{Decibels, Linear};

        match (self, other) {
            (Linear(a), Linear(b)) => a.abs() == b.abs(),
            (Decibels(a), Decibels(b)) => a == b,
            (a, b) => a.to_decibels() == b.to_decibels(),
        }
    }
}

impl PartialOrd for Volume {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        use Volume::{Decibels, Linear};

        Some(match (self, other) {
            (Linear(a), Linear(b)) => a.abs().total_cmp(&b.abs()),
            (Decibels(a), Decibels(b)) => a.total_cmp(b),
            (a, b) => a.to_decibels().total_cmp(&b.to_decibels()),
        })
    }
}

#[inline]
fn decibels_to_linear(decibels: f32) -> f32 {
    ops::powf(10.0f32, decibels / 20.0)
}

#[inline]
fn linear_to_decibels(linear: f32) -> f32 {
    20.0 * ops::log10(linear.abs())
}

impl Volume {
    /// Returns the volume in linear scale as a float.
    pub fn to_linear(&self) -> f32 {
        match self {
            Self::Linear(v) => v.abs(),
            Self::Decibels(v) => decibels_to_linear(*v),
        }
    }

    /// Returns the volume in decibels as a float.
    ///
    /// If the volume is silent / off / muted, i.e., its underlying linear scale
    /// is `0.0`, this method returns negative infinity.
    pub fn to_decibels(&self) -> f32 {
        match self {
            Self::Linear(v) => linear_to_decibels(*v),
            Self::Decibels(v) => *v,
        }
    }

    /// The silent volume. Also known as "off" or "muted".
    pub const SILENT: Self = Volume::Linear(0.0);

    /// Increases the volume by the specified percentage.
    ///
    /// This method works in the linear domain, where a 100% increase
    /// means doubling the volume (equivalent to +6.02dB).
    ///
    /// # Arguments
    /// * `percentage` - The percentage to increase (50.0 means 50% increase)
    ///
    /// # Examples
    /// ```
    /// use bevy_audio::Volume;
    ///
    /// let volume = Volume::Linear(1.0);
    /// let increased = volume.increase_by_percentage(100.0);
    /// assert_eq!(increased.to_linear(), 2.0);
    /// ```
    pub fn increase_by_percentage(&self, percentage: f32) -> Self {
        let factor = 1.0 + (percentage / 100.0);
        Volume::Linear(self.to_linear() * factor)
    }

    /// Decreases the volume by the specified percentage.
    ///
    /// This method works in the linear domain, where a 50% decrease
    /// means halving the volume (equivalent to -6.02dB).
    ///
    /// # Arguments
    /// * `percentage` - The percentage to decrease (50.0 means 50% decrease)
    ///
    /// # Examples
    /// ```
    /// use bevy_audio::Volume;
    ///
    /// let volume = Volume::Linear(1.0);
    /// let decreased = volume.decrease_by_percentage(50.0);
    /// assert_eq!(decreased.to_linear(), 0.5);
    /// ```
    pub fn decrease_by_percentage(&self, percentage: f32) -> Self {
        let factor = 1.0 - (percentage / 100.0).clamp(0.0, 1.0);
        Volume::Linear(self.to_linear() * factor)
    }

    /// Scales the volume to a specific linear factor relative to the current volume.
    ///
    /// This is different from `adjust_by_linear` as it sets the volume to be
    /// exactly the factor times the original volume, rather than applying
    /// the factor to the current volume.
    ///
    /// # Arguments
    /// * `factor` - The scaling factor (2.0 = twice as loud, 0.5 = half as loud)
    ///
    /// # Examples
    /// ```
    /// use bevy_audio::Volume;
    ///
    /// let volume = Volume::Linear(0.8);
    /// let scaled = volume.scale_to_factor(1.25);
    /// assert_eq!(scaled.to_linear(), 1.0);
    /// ```
    pub fn scale_to_factor(&self, factor: f32) -> Self {
        Volume::Linear(self.to_linear() * factor)
    }

    /// Creates a fade effect by interpolating between current volume and target volume.
    ///
    /// This method performs linear interpolation in the linear domain, which
    /// provides a more natural-sounding fade effect.
    ///
    /// # Arguments
    /// * `target` - The target volume to fade towards
    /// * `factor` - The interpolation factor (0.0 = current volume, 1.0 = target volume)
    ///
    /// # Examples
    /// ```
    /// use bevy_audio::Volume;
    ///
    /// let current = Volume::Linear(1.0);
    /// let target = Volume::Linear(0.0);
    /// let faded = current.fade_towards(target, 0.5);
    /// assert_eq!(faded.to_linear(), 0.5);
    /// ```
    pub fn fade_towards(&self, target: Volume, factor: f32) -> Self {
        let current_linear = self.to_linear();
        let target_linear = target.to_linear();
        let factor_clamped = factor.clamp(0.0, 1.0);

        let interpolated = current_linear + (target_linear - current_linear) * factor_clamped;
        Volume::Linear(interpolated)
    }
}

impl core::ops::Mul<Self> for Volume {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self {
        use Volume::{Decibels, Linear};

        match (self, rhs) {
            (Linear(a), Linear(b)) => Linear(a * b),
            (Decibels(a), Decibels(b)) => Decibels(a + b),
            // {Linear, Decibels} favors the left hand side of the operation by
            // first converting the right hand side to the same type as the left
            // hand side and then performing the operation.
            (Linear(..), Decibels(db)) => self * Linear(decibels_to_linear(db)),
            (Decibels(..), Linear(l)) => self * Decibels(linear_to_decibels(l)),
        }
    }
}

impl core::ops::MulAssign<Self> for Volume {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl core::ops::Div<Self> for Volume {
    type Output = Self;

    fn div(self, rhs: Self) -> Self {
        use Volume::{Decibels, Linear};

        match (self, rhs) {
            (Linear(a), Linear(b)) => Linear(a / b),
            (Decibels(a), Decibels(b)) => Decibels(a - b),
            // {Linear, Decibels} favors the left hand side of the operation by
            // first converting the right hand side to the same type as the left
            // hand side and then performing the operation.
            (Linear(..), Decibels(db)) => self / Linear(decibels_to_linear(db)),
            (Decibels(..), Linear(l)) => self / Decibels(linear_to_decibels(l)),
        }
    }
}

impl core::ops::DivAssign<Self> for Volume {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

#[cfg(test)]
mod tests {
    use super::Volume::{self, Decibels, Linear};

    /// Based on [Wikipedia's Decibel article].
    ///
    /// [Wikipedia's Decibel article]: https://web.archive.org/web/20230810185300/https://en.wikipedia.org/wiki/Decibel
    const DECIBELS_LINEAR_TABLE: [(f32, f32); 27] = [
        (100., 100000.),
        (90., 31623.),
        (80., 10000.),
        (70., 3162.),
        (60., 1000.),
        (50., 316.2),
        (40., 100.),
        (30., 31.62),
        (20., 10.),
        (10., 3.162),
        (5.998, 1.995),
        (3.003, 1.413),
        (1.002, 1.122),
        (0., 1.),
        (-1.002, 0.891),
        (-3.003, 0.708),
        (-5.998, 0.501),
        (-10., 0.3162),
        (-20., 0.1),
        (-30., 0.03162),
        (-40., 0.01),
        (-50., 0.003162),
        (-60., 0.001),
        (-70., 0.0003162),
        (-80., 0.0001),
        (-90., 0.00003162),
        (-100., 0.00001),
    ];

    #[test]
    fn volume_conversion() {
        for (db, linear) in DECIBELS_LINEAR_TABLE {
            for volume in [Linear(linear), Decibels(db), Linear(-linear)] {
                let db_test = volume.to_decibels();
                let linear_test = volume.to_linear();

                let db_delta = db_test - db;
                let linear_relative_delta = (linear_test - linear) / linear;

                assert!(
                    db_delta.abs() < 1e-2,
                    "Expected ~{db}dB, got {db_test}dB (delta {db_delta})",
                );
                assert!(
                    linear_relative_delta.abs() < 1e-3,
                    "Expected ~{linear}, got {linear_test} (relative delta {linear_relative_delta})",
                );
            }
        }
    }

    #[test]
    fn volume_conversion_special() {
        assert!(
            Decibels(f32::INFINITY).to_linear().is_infinite(),
            "Infinite decibels is equivalent to infinite linear scale"
        );
        assert!(
            Linear(f32::INFINITY).to_decibels().is_infinite(),
            "Infinite linear scale is equivalent to infinite decibels"
        );

        assert!(
            Linear(f32::NEG_INFINITY).to_decibels().is_infinite(),
            "Negative infinite linear scale is equivalent to infinite decibels"
        );
        assert_eq!(
            Decibels(f32::NEG_INFINITY).to_linear().abs(),
            0.0,
            "Negative infinity decibels is equivalent to zero linear scale"
        );

        assert!(
            Linear(0.0).to_decibels().is_infinite(),
            "Zero linear scale is equivalent to negative infinity decibels"
        );
        assert!(
            Linear(-0.0).to_decibels().is_infinite(),
            "Negative zero linear scale is equivalent to negative infinity decibels"
        );

        assert!(
            Decibels(f32::NAN).to_linear().is_nan(),
            "NaN decibels is equivalent to NaN linear scale"
        );
        assert!(
            Linear(f32::NAN).to_decibels().is_nan(),
            "NaN linear scale is equivalent to NaN decibels"
        );
    }

    #[test]
    fn test_increase_by_percentage() {
        let volume = Linear(1.0);

        // 100% increase should double the volume
        let increased = volume.increase_by_percentage(100.0);
        assert_eq!(increased.to_linear(), 2.0);

        // 50% increase
        let increased = volume.increase_by_percentage(50.0);
        assert_eq!(increased.to_linear(), 1.5);
    }

    #[test]
    fn test_decrease_by_percentage() {
        let volume = Linear(1.0);

        // 50% decrease should halve the volume
        let decreased = volume.decrease_by_percentage(50.0);
        assert_eq!(decreased.to_linear(), 0.5);

        // 25% decrease
        let decreased = volume.decrease_by_percentage(25.0);
        assert_eq!(decreased.to_linear(), 0.75);

        // 100% decrease should result in silence
        let decreased = volume.decrease_by_percentage(100.0);
        assert_eq!(decreased.to_linear(), 0.0);
    }

    #[test]
    fn test_scale_to_factor() {
        let volume = Linear(0.8);
        let scaled = volume.scale_to_factor(1.25);
        assert_eq!(scaled.to_linear(), 1.0);
    }

    #[test]
    fn test_fade_towards() {
        let current = Linear(1.0);
        let target = Linear(0.0);

        // 50% fade should result in 0.5 linear volume
        let faded = current.fade_towards(target, 0.5);
        assert_eq!(faded.to_linear(), 0.5);

        // 0% fade should keep current volume
        let faded = current.fade_towards(target, 0.0);
        assert_eq!(faded.to_linear(), 1.0);

        // 100% fade should reach target volume
        let faded = current.fade_towards(target, 1.0);
        assert_eq!(faded.to_linear(), 0.0);
    }

    #[test]
    fn test_decibel_math_properties() {
        let volume = Linear(1.0);

        // Adding 20dB should multiply linear volume by 10
        let adjusted = volume * Decibels(20.0);
        assert_approx_eq(adjusted, Linear(10.0));

        // Subtracting 20dB should divide linear volume by 10
        let adjusted = volume / Decibels(20.0);
        assert_approx_eq(adjusted, Linear(0.1));
    }

    fn assert_approx_eq(a: Volume, b: Volume) {
        const EPSILON: f32 = 0.0001;

        match (a, b) {
            (Decibels(a), Decibels(b)) | (Linear(a), Linear(b)) => assert!(
                (a - b).abs() < EPSILON,
                "Expected {a:?} to be approximately equal to {b:?}",
            ),
            (a, b) => assert!(
                (a.to_decibels() - b.to_decibels()).abs() < EPSILON,
                "Expected {a:?} to be approximately equal to {b:?}",
            ),
        }
    }

    #[test]
    fn volume_ops_mul() {
        // Linear to Linear.
        assert_approx_eq(Linear(0.5) * Linear(0.5), Linear(0.25));
        assert_approx_eq(Linear(0.5) * Linear(0.1), Linear(0.05));
        assert_approx_eq(Linear(0.5) * Linear(-0.5), Linear(-0.25));

        // Decibels to Decibels.
        assert_approx_eq(Decibels(0.0) * Decibels(0.0), Decibels(0.0));
        assert_approx_eq(Decibels(6.0) * Decibels(6.0), Decibels(12.0));
        assert_approx_eq(Decibels(-6.0) * Decibels(-6.0), Decibels(-12.0));

        // {Linear, Decibels} favors the left hand side of the operation.
        assert_approx_eq(Linear(0.5) * Decibels(0.0), Linear(0.5));
        assert_approx_eq(Decibels(0.0) * Linear(0.501), Decibels(-6.003246));
    }

    #[test]
    fn volume_ops_mul_assign() {
        // Linear to Linear.
        let mut volume = Linear(0.5);
        volume *= Linear(0.5);
        assert_approx_eq(volume, Linear(0.25));

        // Decibels to Decibels.
        let mut volume = Decibels(6.0);
        volume *= Decibels(6.0);
        assert_approx_eq(volume, Decibels(12.0));

        // {Linear, Decibels} favors the left hand side of the operation.
        let mut volume = Linear(0.5);
        volume *= Decibels(0.0);
        assert_approx_eq(volume, Linear(0.5));
        let mut volume = Decibels(0.0);
        volume *= Linear(0.501);
        assert_approx_eq(volume, Decibels(-6.003246));
    }

    #[test]
    fn volume_ops_div() {
        // Linear to Linear.
        assert_approx_eq(Linear(0.5) / Linear(0.5), Linear(1.0));
        assert_approx_eq(Linear(0.5) / Linear(0.1), Linear(5.0));
        assert_approx_eq(Linear(0.5) / Linear(-0.5), Linear(-1.0));

        // Decibels to Decibels.
        assert_approx_eq(Decibels(0.0) / Decibels(0.0), Decibels(0.0));
        assert_approx_eq(Decibels(6.0) / Decibels(6.0), Decibels(0.0));
        assert_approx_eq(Decibels(-6.0) / Decibels(-6.0), Decibels(0.0));

        // {Linear, Decibels} favors the left hand side of the operation.
        assert_approx_eq(Linear(0.5) / Decibels(0.0), Linear(0.5));
        assert_approx_eq(Decibels(0.0) / Linear(0.501), Decibels(6.003246));
    }

    #[test]
    fn volume_ops_div_assign() {
        // Linear to Linear.
        let mut volume = Linear(0.5);
        volume /= Linear(0.5);
        assert_approx_eq(volume, Linear(1.0));

        // Decibels to Decibels.
        let mut volume = Decibels(6.0);
        volume /= Decibels(6.0);
        assert_approx_eq(volume, Decibels(0.0));

        // {Linear, Decibels} favors the left hand side of the operation.
        let mut volume = Linear(0.5);
        volume /= Decibels(0.0);
        assert_approx_eq(volume, Linear(0.5));
        let mut volume = Decibels(0.0);
        volume /= Linear(0.501);
        assert_approx_eq(volume, Decibels(6.003246));
    }
}