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
#[cfg(not(feature = "std"))]
use num_traits::Float;
use core::num::NonZeroU32;
use firewheel_macros::{Diff, Patch};
use crate::{
dsp::{
coeff_update::{CoeffUpdateFactor, CoeffUpdateMask},
filter::single_pole_iir::{OnePoleIirLPFCoeff, OnePoleIirLPFCoeffSimd, OnePoleIirLPFSimd},
},
param::smoother::{SmoothedParam, SmootherConfig},
};
pub const MUFFLE_CUTOFF_HZ_MIN: f32 = 20.0;
pub const MUFFLE_CUTOFF_HZ_MAX: f32 = 20_480.0;
const MUFFLE_CUTOFF_HZ_RANGE_RECIP: f32 = 1.0 / (MUFFLE_CUTOFF_HZ_MAX - MUFFLE_CUTOFF_HZ_MIN);
/// The method in which to calculate the volume of a sound based on the distance from
/// the listener.
///
/// Based on <https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/distanceModel>
///
/// Interactive graph of the different models: <https://www.desmos.com/calculator/g1pbsc5m9y>
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Diff, Patch)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DistanceModel {
#[default]
/// A linear distance model calculates the gain by:
///
/// `reference_distance / (reference_distance + rolloff_factor * (max(distance, reference_distance) - reference_distance))`
///
/// This mostly closely matches how sound is attenuated in the real world, and is the default model.
Inverse,
/// A linear distance model calculates the gain by:
///
/// `(1.0 - rolloff_factor * (distance - reference_distance) / (max_distance - reference_distance)).clamp(0.0, 1.0)`
Linear,
/// An exponential distance model calculates the gain by:
///
/// `pow((max(distance, reference_distance) / reference_distance, -rolloff_factor)`
///
/// This is equivalent to [`DistanceModel::Inverse`] when `rolloff_factor = 1.0`.
Exponential,
}
impl DistanceModel {
fn calculate_gain(
&self,
distance: f32,
distance_gain_factor: f32,
reference_distance: f32,
maximum_distance: f32,
) -> f32 {
if distance <= reference_distance || distance_gain_factor <= 0.00001 {
return 1.0;
}
match self {
DistanceModel::Inverse => {
reference_distance
/ (reference_distance
+ (distance_gain_factor * (distance - reference_distance)))
}
DistanceModel::Linear => {
if maximum_distance <= reference_distance {
1.0
} else {
(1.0 - (distance_gain_factor * (distance - reference_distance)
/ (maximum_distance - reference_distance)))
.clamp(0.0, 1.0)
}
}
DistanceModel::Exponential => {
(distance / reference_distance).powf(-distance_gain_factor)
}
}
}
}
/// The parameters which describe how to attenuate a sound based on its distance from
/// the listener.
#[derive(Diff, Patch, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DistanceAttenuation {
/// The method in which to calculate the volume of a sound based on the distance from
/// the listener.
///
/// by default this is set to [`DistanceModel::Inverse`].
///
/// Based on <https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/distanceModel>
///
/// Interactive graph of the different models: <https://www.desmos.com/calculator/g1pbsc5m9y>
pub distance_model: DistanceModel,
/// The factor by which the sound gets quieter the farther away it is from the
/// listener.
///
/// Values less than `1.0` will attenuate the sound less per unit distance, and values
/// greater than `1.0` will attenuate the sound more per unit distance.
///
/// Set to a value `<= 0.00001` to disable attenuating the sound.
///
/// By default this is set to `1.0`.
///
/// See <https://www.desmos.com/calculator/g1pbsc5m9y> for an interactive graph of
/// how these parameters affect the final volume of a sound for each distance model.
pub distance_gain_factor: f32,
/// The minimum distance at which a sound is considered to be at the maximum volume.
/// (Distances less than this value will be clamped at the maximum volume).
///
/// If this value is `< 0.00001`, then it will be clamped to `0.00001`.
///
/// By default this is set to `5.0`.
///
/// See <https://www.desmos.com/calculator/g1pbsc5m9y> for an interactive graph of
/// how these parameters affect the final volume of a sound for each distance model.
pub reference_distance: f32,
/// When using [`DistanceModel::Linear`], the maximum reference distance (at a
/// rolloff factor of `1.0`) of a sound before it is considered to be "silent".
/// (Distances greater than this value will be clamped to silence).
///
/// If this value is `< 0.0`, then it will be clamped to `0.0`.
///
/// By default this is set to `200.0`.
///
/// See <https://www.desmos.com/calculator/g1pbsc5m9y> for an interactive graph of
/// how these parameters affect the final volume of a sound for each distance model.
pub max_distance: f32,
/// The factor which determines the curve of the high frequency damping (lowpass)
/// in relation to distance.
///
/// Higher values dampen the high frequencies faster, while smaller values dampen
/// the high frequencies slower.
///
/// Set to a value `<= 0.00001` to disable muffling the sound based on distance.
///
/// By default this is set to `1.9`.
///
/// See <https://www.desmos.com/calculator/jxp8t9ero4> for an interactive graph of
/// how these parameters affect the final lowpass cuttoff frequency.
pub distance_muffle_factor: f32,
/// The distance at which the high frequencies of a sound become fully muffled
/// (lowpassed).
///
/// Distances less than `reference_distance` will have no muffling.
///
/// This has no effect if `muffle_factor` is `None`.
///
/// By default this is set to `200.0`.
///
/// See <https://www.desmos.com/calculator/jxp8t9ero4> for an interactive graph of
/// how these parameters affect the final lowpass cuttoff frequency.
pub max_muffle_distance: f32,
/// The amount of muffling (lowpass) at `max_muffle_distance` in the range
/// `[20.0, 20_480.0]`, where `20_480.0` is no muffling and `20.0` is maximum
/// muffling.
///
/// This has no effect if `muffle_factor` is `None`.
///
/// By default this is set to `20.0`.
///
/// See <https://www.desmos.com/calculator/jxp8t9ero4> for an interactive graph of
/// how these parameters affect the final lowpass cuttoff frequency.
pub max_distance_muffle_cutoff_hz: f32,
}
impl Default for DistanceAttenuation {
fn default() -> Self {
Self {
distance_model: DistanceModel::Inverse,
distance_gain_factor: 1.0,
reference_distance: 5.0,
max_distance: 200.0,
distance_muffle_factor: 1.9,
max_muffle_distance: 200.0,
max_distance_muffle_cutoff_hz: 20.0,
}
}
}
pub struct DistanceAttenuatorStereoDsp {
pub gain: SmoothedParam,
pub muffle_cutoff_hz: SmoothedParam,
pub damping_disabled: bool,
pub filter: OnePoleIirLPFSimd<2>,
coeff_update_mask: CoeffUpdateMask,
}
impl DistanceAttenuatorStereoDsp {
pub fn new(
smoother_config: SmootherConfig,
sample_rate: NonZeroU32,
coeff_update_factor: CoeffUpdateFactor,
) -> Self {
Self {
gain: SmoothedParam::new(1.0, smoother_config, sample_rate),
muffle_cutoff_hz: SmoothedParam::new(
MUFFLE_CUTOFF_HZ_MAX,
smoother_config,
sample_rate,
),
damping_disabled: true,
filter: OnePoleIirLPFSimd::default(),
coeff_update_mask: coeff_update_factor.mask(),
}
}
pub fn set_coeff_update_factor(&mut self, coeff_update_factor: CoeffUpdateFactor) {
self.coeff_update_mask = coeff_update_factor.mask();
}
pub fn is_silent(&self) -> bool {
self.gain.target_value() == 0.0 && !self.gain.is_smoothing()
}
pub fn compute_values(
&mut self,
distance: f32,
params: &DistanceAttenuation,
muffle_cutoff_hz: f32,
min_gain: f32,
) {
let reference_distance = params.reference_distance.max(0.00001);
let max_distance = params.max_distance.max(0.0);
let max_distance_muffle_cutoff_hz = params
.max_distance_muffle_cutoff_hz
.max(MUFFLE_CUTOFF_HZ_MIN);
let distance_gain = params.distance_model.calculate_gain(
distance,
params.distance_gain_factor,
reference_distance,
max_distance,
);
let gain = if distance_gain <= min_gain {
0.0
} else {
distance_gain
};
let distance_cutoff_norm = if params.distance_muffle_factor <= 0.00001
|| distance <= reference_distance
|| params.max_muffle_distance <= reference_distance
|| max_distance_muffle_cutoff_hz >= MUFFLE_CUTOFF_HZ_MAX
{
1.0
} else {
let num = distance - reference_distance;
let den = params.max_muffle_distance - reference_distance;
let norm = 1.0 - (num / den).powf(params.distance_muffle_factor.recip());
let min_norm = (max_distance_muffle_cutoff_hz - MUFFLE_CUTOFF_HZ_MIN)
* MUFFLE_CUTOFF_HZ_RANGE_RECIP;
norm.max(min_norm)
};
let muffle_cutoff_hz = if (muffle_cutoff_hz < MUFFLE_CUTOFF_HZ_MAX - 0.01)
|| distance_cutoff_norm < 1.0
{
let hz = if distance_cutoff_norm < 1.0 {
let muffle_cutoff_norm =
(muffle_cutoff_hz - MUFFLE_CUTOFF_HZ_MIN) * MUFFLE_CUTOFF_HZ_RANGE_RECIP;
let final_norm = muffle_cutoff_norm * distance_cutoff_norm;
(final_norm * (MUFFLE_CUTOFF_HZ_MAX - MUFFLE_CUTOFF_HZ_MIN)) + MUFFLE_CUTOFF_HZ_MIN
} else {
muffle_cutoff_hz
};
Some(hz.clamp(MUFFLE_CUTOFF_HZ_MIN, MUFFLE_CUTOFF_HZ_MAX))
} else {
None
};
self.gain.set_value(gain);
if let Some(cutoff_hz) = muffle_cutoff_hz {
self.muffle_cutoff_hz.set_value(cutoff_hz);
self.damping_disabled = false;
} else {
self.muffle_cutoff_hz.set_value(MUFFLE_CUTOFF_HZ_MAX);
self.damping_disabled = true;
}
}
/// Returns `true` if the output buffers should be cleared with silence, `false`
/// otherwise.
pub fn process(
&mut self,
frames: usize,
out1: &mut [f32],
out2: &mut [f32],
sample_rate_recip: f64,
) -> bool {
// Make doubly sure that the compiler optimizes away the bounds checking
// in the loop.
let out1 = &mut out1[..frames];
let out2 = &mut out2[..frames];
if !self.gain.is_smoothing() && !self.muffle_cutoff_hz.is_smoothing() {
if !self.gain.is_smoothing() && self.gain.target_value() == 0.0 {
self.gain.reset_to_target();
self.muffle_cutoff_hz.reset_to_target();
self.filter.reset();
return true;
} else if self.damping_disabled {
for i in 0..frames {
out1[i] = out1[i] * self.gain.target_value();
out2[i] = out2[i] * self.gain.target_value();
}
} else {
// The cutoff parameter is not currently smoothing, so we can optimize by
// only updating the filter coefficients once.
let coeff = OnePoleIirLPFCoeffSimd::splat(OnePoleIirLPFCoeff::new(
self.muffle_cutoff_hz.target_value(),
sample_rate_recip as f32,
));
for i in 0..frames {
let s = [
out1[i] * self.gain.target_value(),
out2[i] * self.gain.target_value(),
];
let [l, r] = self.filter.process(s, &coeff);
out1[i] = l;
out2[i] = r;
}
}
} else {
if self.damping_disabled && !self.muffle_cutoff_hz.is_smoothing() {
for i in 0..frames {
let gain = self.gain.next_smoothed();
out1[i] = out1[i] * gain;
out2[i] = out2[i] * gain;
}
} else {
let mut coeff = OnePoleIirLPFCoeffSimd::default();
for i in 0..frames {
let cutoff_hz = self.muffle_cutoff_hz.next_smoothed();
let gain = self.gain.next_smoothed();
// Because recalculating filter coefficients is expensive, a trick like
// this can be used to only recalculate them every few frames.
//
// TODO: use core::hint::cold_path() once that stabilizes
//
// TODO: Alternatively, this could be optimized using a lookup table
if self.coeff_update_mask.do_update(i) {
coeff = OnePoleIirLPFCoeffSimd::splat(OnePoleIirLPFCoeff::new(
cutoff_hz,
sample_rate_recip as f32,
));
}
let s = [out1[i] * gain, out2[i] * gain];
let [l, r] = self.filter.process(s, &coeff);
out1[i] = l;
out2[i] = r;
}
}
self.gain.settle();
self.muffle_cutoff_hz.settle();
}
false
}
pub fn reset(&mut self) {
self.gain.reset_to_target();
self.muffle_cutoff_hz.reset_to_target();
self.filter.reset();
}
pub fn set_smooth_seconds(&mut self, seconds: f32, sample_rate: NonZeroU32) {
self.gain.set_smooth_seconds(seconds, sample_rate);
self.muffle_cutoff_hz
.set_smooth_seconds(seconds, sample_rate);
}
pub fn update_sample_rate(&mut self, sample_rate: NonZeroU32) {
self.gain.update_sample_rate(sample_rate);
self.muffle_cutoff_hz.update_sample_rate(sample_rate);
}
}