Expand description
Where a sound is, and what that does to its two ears.
Four games spatialise sound today with four hand-written formulas, and
three of them have a private square root. This module is the shared version:
a Curve turns distance into a level, a PanLaw splits the level over
the two ears, a ListenerRule picks which of several listeners (a split
screen) a sound is heard from, and gains runs the whole chain.
Everything is i32. Distances are clamped per axis before they are squared,
so the sum of squares stays below 2^31 and no 64-bit arithmetic is needed
(the PS1 has none, and a 64-bit divide costs hundreds of cycles).
§Coordinates
The horizontal plane is x and z; y is up and only adds to distance.
A listener with heading 0 faces +z with its right ear toward +x, and a
positive heading turns toward +x. A game with the other handedness negates
the x axis (or the heading) once when it builds its Listener and
Emitter values.
§Example
use psx_sfx::space::*;
let space = Space::new(0, PanLaw::EqualPower(Center::Unity));
let ears = Listeners::new([Listener::new([0, 0, 0], 0)]);
// A sound 400 units ahead and 400 to the right.
let source = Emitter::new([400, 0, 400]);
let curve = Curve::linear_clip(0x3FFF, 2000);
let g = gains(&space, &ears, ListenerRule::Loudest, &source, &curve).unwrap();
assert!(g.right > g.left);Structs§
- Emitter
- Where a sound comes from.
- Gains
- The result of placing a sound: the two ear levels and where they came from.
- Listener
- Where the ears are.
- Listeners
- The listeners in a scene: one, or two for a split screen.
- Space
- How a world position becomes a distance: one place for the world unit to ear unit shift, the pan law and the cull threshold.
Enums§
- Attn
- Quake’s attenuation classes, as the
Curveconstructors take them. - Center
- Which centre loss a
PanLaw::EqualPowerhas. - Curve
- Distance to level.
- Listener
Rule - Which listener a sound is heard from when there are several. There is one television and one stereo image, so there is no true answer, only a policy.
- PanLaw
- How a level is split over two ears.
Constants§
- AXIS_
LIMIT - Largest per-axis distance, after the world shift, that is squared as is.
Three of these squared sum to less than
2^31. - GOLDSRC_
IDLE - GoldSrc
ATTN_IDLE, in thousandths. - GOLDSRC_
NORM - GoldSrc
ATTN_NORM, in thousandths, forCurve::goldsrc. - GOLDSRC_
STATIC - GoldSrc
ATTN_STATIC, in thousandths. - PAN_
RIGHT - Full right in
PanLawinput: Q12 one.
Functions§
- gains
- Place
emitterfor a scene of listeners underrule, returning the ear levels, orNonewhen the sound is inaudible and should not take a voice.