bevy/crates
mgi388 2660ddc4c5
Support decibels in bevy_audio::Volume (#17605)
# Objective

- Allow users to configure volume using decibels by changing the
`Volume` type from newtyping an `f32` to an enum with `Linear` and
`Decibels` variants.
- Fixes #9507.
- Alternative reworked version of closed #9582.

## Solution

Compared to https://github.com/bevyengine/bevy/pull/9582, this PR has
the following main differences:

1. It uses the term "linear scale" instead of "amplitude" per
https://github.com/bevyengine/bevy/pull/9582/files#r1513529491.
2. Supports `ops` for doing `Volume` arithmetic. Can add two volumes,
e.g. to increase/decrease the current volume. Can multiply two volumes,
e.g. to get the “effective” volume of an audio source considering global
volume.

[requested and blessed on Discord]:
https://discord.com/channels/691052431525675048/749430447326625812/1318272597003341867

## Testing

- Ran `cargo run --example soundtrack`.
- Ran `cargo run --example audio_control`.
- Ran `cargo run --example spatial_audio_2d`.
- Ran `cargo run --example spatial_audio_3d`.
- Ran `cargo run --example pitch`.
- Ran `cargo run --example decodable`.
- Ran `cargo run --example audio`.

---

## Migration Guide

Audio volume can now be configured using decibel values, as well as
using linear scale values. To enable this, some types and functions in
`bevy_audio` have changed.

- `Volume` is now an enum with `Linear` and `Decibels` variants.

Before:

```rust
let v = Volume(1.0);
```

After:

```rust
let volume = Volume::Linear(1.0);
let volume = Volume::Decibels(0.0); // or now you can deal with decibels if you prefer
```

- `Volume::ZERO` has been renamed to the more semantically correct
`Volume::SILENT` because `Volume` now supports decibels and "zero
volume" in decibels actually means "normal volume".
- The `AudioSinkPlayback` trait's volume-related methods now deal with
`Volume` types rather than `f32`s. `AudioSinkPlayback::volume()` now
returns a `Volume` rather than an `f32`. `AudioSinkPlayback::set_volume`
now receives a `Volume` rather than an `f32`. This affects the
`AudioSink` and `SpatialAudioSink` implementations of the trait. The
previous `f32` values are equivalent to the volume converted to linear
scale so the `Volume:: Linear` variant should be used to migrate between
`f32`s and `Volume`.
- The `GlobalVolume::new` function now receives a `Volume` instead of an
`f32`.

---------

Co-authored-by: Zachary Harrold <zac@harrold.com.au>
2025-02-10 21:26:43 +00:00
..
bevy_a11y Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_animation Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_app Harden proc macro path resolution and add integration tests. (#17330) 2025-02-09 19:45:45 +00:00
bevy_asset Remove labeled_assets from LoadedAsset and ErasedLoadedAsset (#15481) 2025-02-10 21:06:37 +00:00
bevy_audio Support decibels in bevy_audio::Volume (#17605) 2025-02-10 21:26:43 +00:00
bevy_color Upgrade to wgpu v24 (#17542) 2025-02-09 19:40:53 +00:00
bevy_core_pipeline Upgrade to wgpu v24 (#17542) 2025-02-09 19:40:53 +00:00
bevy_derive Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_dev_tools Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_diagnostic Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_dylib Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_ecs Encapsulate cfg(feature = "track_location") in a type. (#17602) 2025-02-10 21:21:20 +00:00
bevy_encase_derive Harden proc macro path resolution and add integration tests. (#17330) 2025-02-09 19:45:45 +00:00
bevy_gilrs Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_gizmos Harden proc macro path resolution and add integration tests. (#17330) 2025-02-09 19:45:45 +00:00
bevy_gltf Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_image Upgrade to wgpu v24 (#17542) 2025-02-09 19:40:53 +00:00
bevy_input Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_input_focus Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_internal Upgrade to wgpu v24 (#17542) 2025-02-09 19:40:53 +00:00
bevy_log Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_macro_utils Harden proc macro path resolution and add integration tests. (#17330) 2025-02-09 19:45:45 +00:00
bevy_math Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_mesh Upgrade to wgpu v24 (#17542) 2025-02-09 19:40:53 +00:00
bevy_mikktspace Move #![warn(clippy::allow_attributes, clippy::allow_attributes_without_reason)] to the workspace Cargo.toml (#17374) 2025-01-15 01:14:58 +00:00
bevy_pbr Fix meshlets when bindless disabled. (#17770) 2025-02-10 09:46:10 +00:00
bevy_picking Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_platform_support Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_ptr Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_reflect Harden proc macro path resolution and add integration tests. (#17330) 2025-02-09 19:45:45 +00:00
bevy_remote Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_render Improved Spawn APIs and Bundle Effects (#17521) 2025-02-09 23:32:56 +00:00
bevy_scene Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_sprite Upgrade to wgpu v24 (#17542) 2025-02-09 19:40:53 +00:00
bevy_state Harden proc macro path resolution and add integration tests. (#17330) 2025-02-09 19:45:45 +00:00
bevy_tasks Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_text Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_time Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_transform Improved Spawn APIs and Bundle Effects (#17521) 2025-02-09 23:32:56 +00:00
bevy_ui Store UI render target info locally per node (#17579) 2025-02-10 07:27:58 +00:00
bevy_utils Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_window Cleanup publish process (#17728) 2025-02-09 17:46:19 +00:00
bevy_winit Upgrade to wgpu v24 (#17542) 2025-02-09 19:40:53 +00:00