Add server-side spatial audio with HRTF, occlusion and adaptive quality
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
// Copyright 2026 Sverse contributors. Licensed under the Apache License, Version 2.0.
|
||||
package spatialaudio
|
||||
|
||||
// GainMatrix belongs to the room's audio worker. Heartbeat sequence numbers and
|
||||
// client timestamps do not invalidate geometry. Rotation changes a listener's
|
||||
// row; position changes its row and its column. Missing poses silence the pair.
|
||||
type GainMatrix struct {
|
||||
config Config
|
||||
poses map[string]Pose
|
||||
rows map[string]map[string]Gain
|
||||
calculations uint64
|
||||
scene *CompiledScene
|
||||
dirty bool
|
||||
minPriority int
|
||||
}
|
||||
|
||||
func NewGainMatrix(c Config) *GainMatrix {
|
||||
return &GainMatrix{config: c, poses: make(map[string]Pose), rows: make(map[string]map[string]Gain)}
|
||||
}
|
||||
|
||||
func (m *GainMatrix) Update(poses map[string]Pose) {
|
||||
moved := make(map[string]bool)
|
||||
for id, pose := range poses {
|
||||
previous, exists := m.poses[id]
|
||||
if !exists || pose.Position != previous.Position {
|
||||
moved[id] = true
|
||||
}
|
||||
}
|
||||
for id := range m.poses {
|
||||
if _, exists := poses[id]; !exists {
|
||||
delete(m.rows, id)
|
||||
for _, row := range m.rows {
|
||||
delete(row, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
for listenerID, listener := range poses {
|
||||
previous, exists := m.poses[listenerID]
|
||||
row := m.rows[listenerID]
|
||||
if row == nil {
|
||||
row = make(map[string]Gain, len(poses))
|
||||
m.rows[listenerID] = row
|
||||
}
|
||||
if m.dirty || !exists || moved[listenerID] || listener.Rotation != previous.Rotation {
|
||||
for sourceID, source := range poses {
|
||||
if sourceID == listenerID {
|
||||
continue
|
||||
}
|
||||
row[sourceID] = m.gains(listener, source)
|
||||
m.calculations++
|
||||
}
|
||||
} else {
|
||||
for sourceID := range moved {
|
||||
if sourceID == listenerID {
|
||||
continue
|
||||
}
|
||||
row[sourceID] = m.gains(listener, poses[sourceID])
|
||||
m.calculations++
|
||||
}
|
||||
}
|
||||
}
|
||||
m.poses = poses
|
||||
m.dirty = false
|
||||
}
|
||||
|
||||
func (m *GainMatrix) SetScene(scene *CompiledScene) { m.scene = scene; m.dirty = true }
|
||||
func (m *GainMatrix) SetMinPriority(priority int) {
|
||||
if m.minPriority != priority {
|
||||
m.minPriority = priority
|
||||
m.dirty = true
|
||||
}
|
||||
}
|
||||
func (m *GainMatrix) gains(listener, source Pose) Gain {
|
||||
g := Gains(listener, source, m.config)
|
||||
if g == (Gain{}) {
|
||||
return g
|
||||
}
|
||||
transmission := m.scene.transmission(listener.Position, source.Position, m.minPriority)
|
||||
g.Left *= transmission
|
||||
g.Right *= transmission
|
||||
return g
|
||||
}
|
||||
|
||||
// Row is read-only to callers and valid until the next Update.
|
||||
func (m *GainMatrix) Row(listenerID string) map[string]Gain { return m.rows[listenerID] }
|
||||
func (m *GainMatrix) Calculations() uint64 { return m.calculations }
|
||||
Reference in New Issue
Block a user