131 lines
3.9 KiB
Go
131 lines
3.9 KiB
Go
// Copyright 2026 Sverse contributors. Licensed under the Apache License, Version 2.0.
|
|
package spatialaudio
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/ebitengine/purego"
|
|
"math"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sync"
|
|
)
|
|
|
|
type HRTFConfig struct {
|
|
Enabled bool `yaml:"enabled,omitempty"`
|
|
Library string `yaml:"library,omitempty"`
|
|
Interpolation string `yaml:"interpolation,omitempty"`
|
|
}
|
|
type hrtfAPI struct {
|
|
create func(int32, int32, *uintptr) int32
|
|
destroy func(uintptr)
|
|
pairCreate func(uintptr, *uintptr) int32
|
|
pairDestroy func(uintptr)
|
|
apply func(uintptr, *float32, *float32, *float32, int32) int32
|
|
}
|
|
|
|
var hrtfLibraries = struct {
|
|
sync.Mutex
|
|
libraries map[string]*hrtfAPI
|
|
}{libraries: make(map[string]*hrtfAPI)}
|
|
|
|
type HRTFRenderer struct {
|
|
api *hrtfAPI
|
|
state uintptr
|
|
interpolation int32
|
|
}
|
|
type binauralPair struct {
|
|
renderer *HRTFRenderer
|
|
state uintptr
|
|
amplitude float64
|
|
direction [3]float32
|
|
output [2 * BlockSamples]float32
|
|
interpolation int32
|
|
}
|
|
|
|
func NewHRTFRenderer(c HRTFConfig) (renderer *HRTFRenderer, err error) {
|
|
if c.Library == "" {
|
|
return nil, fmt.Errorf("hrtf.library must name the native Steam Audio bridge")
|
|
}
|
|
path, err := filepath.Abs(c.Library)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hrtfLibraries.Lock()
|
|
defer hrtfLibraries.Unlock()
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
renderer = nil
|
|
err = fmt.Errorf("load HRTF symbols: %v", p)
|
|
}
|
|
}()
|
|
api := hrtfLibraries.libraries[path]
|
|
if api == nil {
|
|
lib, openErr := openHRTFLibrary(path)
|
|
if openErr != nil {
|
|
return nil, fmt.Errorf("load HRTF: %w", openErr)
|
|
}
|
|
api = &hrtfAPI{}
|
|
purego.RegisterLibFunc(&api.create, lib, "sverse_hrtf_create")
|
|
purego.RegisterLibFunc(&api.destroy, lib, "sverse_hrtf_destroy")
|
|
purego.RegisterLibFunc(&api.pairCreate, lib, "sverse_hrtf_pair_create")
|
|
purego.RegisterLibFunc(&api.pairDestroy, lib, "sverse_hrtf_pair_destroy")
|
|
purego.RegisterLibFunc(&api.apply, lib, "sverse_hrtf_apply")
|
|
hrtfLibraries.libraries[path] = api
|
|
}
|
|
renderer = &HRTFRenderer{api: api}
|
|
if c.Interpolation == "bilinear" {
|
|
renderer.interpolation = 1
|
|
}
|
|
if code := api.create(SampleRate, BlockSamples, &renderer.state); code != 0 {
|
|
return nil, fmt.Errorf("create HRTF renderer: %d", code)
|
|
}
|
|
return renderer, nil
|
|
}
|
|
func (r *HRTFRenderer) newPair() (*binauralPair, error) {
|
|
p := &binauralPair{renderer: r, direction: [3]float32{0, 0, -1}, interpolation: r.interpolation}
|
|
if code := r.api.pairCreate(r.state, &p.state); code != 0 {
|
|
return nil, fmt.Errorf("create HRTF pair: %d", code)
|
|
}
|
|
return p, nil
|
|
}
|
|
func (r *HRTFRenderer) Close() {
|
|
if r != nil && r.state != 0 {
|
|
r.api.destroy(r.state)
|
|
r.state = 0
|
|
}
|
|
}
|
|
func (p *binauralPair) Close() {
|
|
if p != nil && p.state != 0 {
|
|
p.renderer.api.pairDestroy(p.state)
|
|
p.state = 0
|
|
}
|
|
}
|
|
func (p *binauralPair) process(pcm []float32, direction [3]float32, smoothing float64) bool {
|
|
for i := range p.direction {
|
|
p.direction[i] += float32(smoothing) * (direction[i] - p.direction[i])
|
|
}
|
|
if len(pcm) != BlockSamples {
|
|
return false
|
|
}
|
|
code := p.renderer.api.apply(p.state, &pcm[0], &p.output[0], &p.direction[0], p.interpolation)
|
|
runtime.KeepAlive(pcm)
|
|
runtime.KeepAlive(p)
|
|
return code == 0
|
|
}
|
|
|
|
// Unreal-style head coordinates: forward +X, right +Y, up +Z. Steam Audio
|
|
// expects right +X, up +Y, forward -Z, relative to the listener's orientation.
|
|
func headDirection(listener, source Pose) [3]float32 {
|
|
q := listener.Rotation
|
|
f := Vec3{1 - 2*(q.Y*q.Y+q.Z*q.Z), 2 * (q.X*q.Y + q.Z*q.W), 2 * (q.X*q.Z - q.Y*q.W)}
|
|
right := q.Right()
|
|
up := Vec3{2 * (q.X*q.Z + q.Y*q.W), 2 * (q.Y*q.Z - q.X*q.W), 1 - 2*(q.X*q.X+q.Y*q.Y)}
|
|
d := Vec3{source.Position.X - listener.Position.X, source.Position.Y - listener.Position.Y, source.Position.Z - listener.Position.Z}
|
|
length := math.Sqrt(d.X*d.X + d.Y*d.Y + d.Z*d.Z)
|
|
if length < 1e-6 {
|
|
return [3]float32{0, 0, -1}
|
|
}
|
|
dot := func(v Vec3) float32 { return float32((d.X*v.X + d.Y*v.Y + d.Z*v.Z) / length) }
|
|
return [3]float32{dot(right), dot(up), -dot(f)}
|
|
}
|