Skip to content

Commit

Permalink
Merge pull request #141 from NiklasEi/easing-functions-for-spatial-vo…
Browse files Browse the repository at this point in the history
…lume

Easing functions for spatial damping
  • Loading branch information
NiklasEi authored Jan 4, 2025
2 parents f3401ab + 7ce501d commit 6c32627
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Changelog

- update Kira to 0.9
- removed `playback_region` from `SoundSettings`
- Update Kira to 0.9
- Removed `playback_region` from `SoundSettings`
- Add `android_shared_stdcxx` feature for Android Builds
- fix spatial audio when position of receiver and emitter are the same ([#135](https://github.com/NiklasEi/bevy_kira_audio/issues/135))
- add `SpatialRadius` to control spatial audio distance range per entity
- rename `SpatialAudio` to `GlobalSpatialRadius` and rename it's field `max_distance` to `radius`
- Fix spatial audio when position of receiver and emitter are the same ([#135](https://github.com/NiklasEi/bevy_kira_audio/issues/135))
- Add `SpatialRadius` to control spatial audio distance range per entity
- Rename `SpatialAudio` to `GlobalSpatialRadius` and rename its field `max_distance` to `radius`
- Add `SpatialDampingCurve` component to control the damping of spatial audio over distance using Bevy easing functions

## v0.21.0 - 30.11.2024
- Update to Bevy `0.15`
Expand Down
27 changes: 20 additions & 7 deletions src/spatial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use bevy::ecs::{
schedule::IntoSystemConfigs,
system::{Query, Resource},
};
use bevy::math::Vec3;
use bevy::math::{Curve, Vec3};
use bevy::prelude::{EaseFunction, EasingCurve};
use bevy::transform::components::{GlobalTransform, Transform};
use std::f32::consts::PI;

Expand Down Expand Up @@ -36,7 +37,7 @@ impl Plugin for SpatialAudioPlugin {
/// Add [`Handle<AudioInstance>`]s to control their pan and volume based on emitter
/// and receiver positions.
#[derive(Component)]
#[require(Transform)]
#[require(Transform, SpatialDampingCurve)]
pub struct SpatialAudioEmitter {
/// Audio instances that are played by this emitter
///
Expand Down Expand Up @@ -77,23 +78,35 @@ pub struct SpatialRadius {
pub radius: f32,
}

#[derive(Component)]
struct SpatialDampingCurve(EaseFunction);

impl Default for SpatialDampingCurve {
fn default() -> Self {
SpatialDampingCurve(EaseFunction::Linear)
}
}

fn run_spatial_audio(
spatial_audio: Res<DefaultSpatialRadius>,
receiver: Query<&GlobalTransform, With<SpatialAudioReceiver>>,
emitters: Query<(
&GlobalTransform,
&SpatialAudioEmitter,
&SpatialDampingCurve,
Option<&SpatialRadius>,
)>,
mut audio_instances: ResMut<Assets<AudioInstance>>,
) {
if let Ok(receiver_transform) = receiver.get_single() {
for (emitter_transform, emitter, range) in emitters.iter() {
for (emitter_transform, emitter, damping_curve, range) in emitters.iter() {
let sound_path = emitter_transform.translation() - receiver_transform.translation();
let volume = (1.
- sound_path.length() / range.map_or(spatial_audio.radius, |r| r.radius))
.clamp(0., 1.)
.powi(2);
let progress = (sound_path.length() / range.map_or(spatial_audio.radius, |r| r.radius))
.clamp(0., 1.);
let volume: f32 = 1.
- EasingCurve::new(0., 1., damping_curve.0)
.sample_unchecked(progress)
.clamp(0., 1.);

let right_ear_angle = if sound_path == Vec3::ZERO {
PI / 2.
Expand Down

0 comments on commit 6c32627

Please sign in to comment.