Add server-side spatial audio with HRTF, occlusion and adaptive quality
This commit is contained in:
@@ -0,0 +1,636 @@
|
||||
// Copyright 2026 Sverse contributors. Licensed under the Apache License, Version 2.0.
|
||||
package spatialaudio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RTPInput struct {
|
||||
Sequence uint16
|
||||
Timestamp, SSRC uint32
|
||||
Payload []byte
|
||||
}
|
||||
type Input struct {
|
||||
packets chan RTPInput
|
||||
closed atomic.Bool
|
||||
dropped atomic.Uint64
|
||||
received atomic.Uint64
|
||||
}
|
||||
|
||||
// Push never waits for the audio worker. The SFU's packet buffer is reused, so
|
||||
// the payload must be copied before returning from its observer callback.
|
||||
func (i *Input) Push(p RTPInput) {
|
||||
if i.closed.Load() || len(p.Payload) == 0 || len(p.Payload) > 1500 {
|
||||
return
|
||||
}
|
||||
p.Payload = append([]byte(nil), p.Payload...)
|
||||
i.received.Add(1)
|
||||
select {
|
||||
case i.packets <- p:
|
||||
return
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-i.packets:
|
||||
i.dropped.Add(1)
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case i.packets <- p:
|
||||
default:
|
||||
i.dropped.Add(1)
|
||||
}
|
||||
}
|
||||
|
||||
// PrimaryRED extracts the primary payload of RFC 2198. Redundant blocks are
|
||||
// skipped here; Opus in-band FEC is still usable by the playout buffer.
|
||||
func PrimaryRED(payload []byte) ([]byte, error) {
|
||||
offset, skip := 0, 0
|
||||
for {
|
||||
if offset >= len(payload) {
|
||||
return nil, errors.New("truncated RED header")
|
||||
}
|
||||
if payload[offset]&0x80 == 0 {
|
||||
offset++
|
||||
break
|
||||
}
|
||||
if offset+4 > len(payload) {
|
||||
return nil, errors.New("truncated RED block")
|
||||
}
|
||||
skip += int(payload[offset+2]&3)<<8 | int(payload[offset+3])
|
||||
offset += 4
|
||||
}
|
||||
if offset+skip >= len(payload) {
|
||||
return nil, errors.New("truncated RED primary")
|
||||
}
|
||||
return payload[offset+skip:], nil
|
||||
}
|
||||
|
||||
type source struct {
|
||||
id, track string
|
||||
in *Input
|
||||
decoder *Decoder
|
||||
opus *Opus
|
||||
queue map[int64]RTPInput
|
||||
anchor uint32
|
||||
ssrc uint32
|
||||
started bool
|
||||
ready time.Time
|
||||
expected int64
|
||||
pcm []float32
|
||||
block [BlockSamples]float32
|
||||
plc uint64
|
||||
retiring bool
|
||||
fade float64
|
||||
fadeStep float64
|
||||
remaining int
|
||||
latePackets uint64
|
||||
rebuffers uint64
|
||||
decodeErrors uint64
|
||||
lateStreak int
|
||||
lastLate int64
|
||||
}
|
||||
|
||||
func (s *source) reset(p RTPInput, now time.Time, jitter time.Duration) {
|
||||
s.anchor = p.Timestamp
|
||||
s.ssrc = p.SSRC
|
||||
s.expected = 0
|
||||
s.ready = now.Add(jitter)
|
||||
s.started = true
|
||||
clear(s.queue)
|
||||
s.pcm = nil
|
||||
s.lateStreak = 0
|
||||
s.decoder.Close()
|
||||
var err error
|
||||
s.decoder, err = s.opus.NewDecoder()
|
||||
if err != nil {
|
||||
s.decodeErrors++
|
||||
}
|
||||
}
|
||||
func (s *source) read(now time.Time, jitter time.Duration) []float32 {
|
||||
clear(s.block[:])
|
||||
// Rebase before the signed 32-bit timestamp delta can overflow in long
|
||||
// sessions. Decoder state and buffered PCM remain continuous.
|
||||
if s.expected > 1<<30 {
|
||||
offset := s.expected
|
||||
s.anchor += uint32(offset)
|
||||
s.expected = 0
|
||||
s.lastLate -= offset
|
||||
queue := make(map[int64]RTPInput, len(s.queue))
|
||||
for timestamp, packet := range s.queue {
|
||||
queue[timestamp-offset] = packet
|
||||
}
|
||||
s.queue = queue
|
||||
}
|
||||
for n := 0; n < 64; n++ {
|
||||
select {
|
||||
case p := <-s.in.packets:
|
||||
if samples := s.opus.Samples(p.Payload); samples < 120 || samples > 5760 {
|
||||
s.decodeErrors++
|
||||
continue
|
||||
}
|
||||
if !s.started || p.SSRC != s.ssrc {
|
||||
s.reset(p, now, jitter)
|
||||
}
|
||||
t := int64(int32(p.Timestamp - s.anchor))
|
||||
if t-s.expected > SampleRate*2 || t-s.expected < -SampleRate*2 {
|
||||
s.reset(p, now, jitter)
|
||||
t = 0
|
||||
}
|
||||
if now.Before(s.ready) && t < s.expected {
|
||||
s.expected = t
|
||||
}
|
||||
if t < s.expected {
|
||||
s.latePackets++
|
||||
// After a burst of delay (or clock drift), every new packet may
|
||||
// be late by the same amount. Dropping them forever locks the
|
||||
// decoder into PLC despite an otherwise healthy incoming stream.
|
||||
// Three advancing late packets with no future data trigger a
|
||||
// new playout anchor. Keep Opus state; rebuild the jitter reserve.
|
||||
if len(s.queue) == 0 && (s.lateStreak == 0 || t > s.lastLate) {
|
||||
s.lateStreak++
|
||||
s.lastLate = t
|
||||
}
|
||||
if s.lateStreak < 3 {
|
||||
continue
|
||||
}
|
||||
s.expected = t
|
||||
s.ready = now.Add(jitter)
|
||||
s.pcm = nil
|
||||
clear(s.queue)
|
||||
s.rebuffers++
|
||||
}
|
||||
s.lateStreak = 0
|
||||
if t >= s.expected && len(s.queue) < 96 {
|
||||
s.queue[t] = p
|
||||
}
|
||||
default:
|
||||
n = 64
|
||||
}
|
||||
}
|
||||
if !s.started || now.Before(s.ready) || s.decoder == nil {
|
||||
return s.block[:]
|
||||
}
|
||||
for len(s.pcm) < BlockSamples {
|
||||
var pcm []float32
|
||||
var err error
|
||||
if p, ok := s.queue[s.expected]; ok {
|
||||
delete(s.queue, s.expected)
|
||||
pcm, err = s.decoder.Decode(p.Payload, 5760, false)
|
||||
} else {
|
||||
// Attempt in-band FEC only when the following packet fits the gap.
|
||||
var next int64 = -1
|
||||
for t := range s.queue {
|
||||
if t > s.expected && (next < 0 || t < next) {
|
||||
next = t
|
||||
}
|
||||
}
|
||||
gap := next - s.expected
|
||||
if next >= 0 && gap >= 120 && gap <= 5760 && gap%120 == 0 && s.opus.Samples(s.queue[next].Payload) == int(gap) {
|
||||
pcm, err = s.decoder.Decode(s.queue[next].Payload, int(gap), true)
|
||||
} else {
|
||||
pcm, err = s.decoder.Decode(nil, BlockSamples, false)
|
||||
}
|
||||
s.plc++
|
||||
}
|
||||
if err != nil || len(pcm) == 0 {
|
||||
s.decodeErrors++
|
||||
pcm = make([]float32, BlockSamples)
|
||||
}
|
||||
s.expected += int64(len(pcm))
|
||||
s.pcm = append(s.pcm, pcm...)
|
||||
for t := range s.queue {
|
||||
if t < s.expected {
|
||||
delete(s.queue, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
copy(s.block[:], s.pcm[:BlockSamples])
|
||||
s.pcm = s.pcm[BlockSamples:]
|
||||
return s.block[:]
|
||||
}
|
||||
|
||||
type OutputFunc func(payload []byte, timestamp uint32) error
|
||||
type listener struct {
|
||||
id string
|
||||
encoder *Encoder
|
||||
mixer *Mixer
|
||||
output OutputFunc
|
||||
pcm []float32
|
||||
block [2 * BlockSamples]float32
|
||||
}
|
||||
type Stats struct {
|
||||
AdaptiveEnabled bool
|
||||
QualityLevel int
|
||||
LoadPressure float64
|
||||
QualityTransitions uint64
|
||||
PoseInterval time.Duration
|
||||
ActiveObstacles int
|
||||
EffectiveHRTF string
|
||||
HRTFEnabled bool
|
||||
HRTFPairs int
|
||||
HRTFErrors uint64
|
||||
LatePackets, Rebuffers, DecodeErrors uint64
|
||||
SceneRevision uint64
|
||||
Obstacles int
|
||||
OcclusionEnabled bool
|
||||
TickDuration, MaxTickDuration time.Duration
|
||||
MatrixCalculations uint64
|
||||
InputPeak, OutputPeak float64
|
||||
Sources, Listeners int
|
||||
Poses int
|
||||
ReceivedPackets uint64
|
||||
QueueDrops, ConcealedFrames, MissedTicks, OutputErrors uint64
|
||||
}
|
||||
type Engine struct {
|
||||
config Config
|
||||
opus *Opus
|
||||
Poses *PoseStore
|
||||
commands chan func()
|
||||
stop chan struct{}
|
||||
done chan struct{}
|
||||
stopped atomic.Bool
|
||||
sources map[string]*source
|
||||
listeners map[string]*listener
|
||||
stats Stats
|
||||
matrix *GainMatrix
|
||||
scene *CompiledScene
|
||||
hrtf *HRTFRenderer
|
||||
hrtfEnabled bool
|
||||
adaptive adaptiveController
|
||||
geometryDirty bool
|
||||
}
|
||||
|
||||
func NewEngine(c Config) (*Engine, error) {
|
||||
c = c.Defaults()
|
||||
if err := c.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opus, err := LoadOpus(c.Library)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e := &Engine{config: c, opus: opus, Poses: NewPoseStore(), commands: make(chan func(), 64), stop: make(chan struct{}), done: make(chan struct{}), sources: make(map[string]*source), listeners: make(map[string]*listener), matrix: NewGainMatrix(c)}
|
||||
if c.HRTF.Enabled {
|
||||
e.hrtf, err = NewHRTFRenderer(c.HRTF)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.hrtfEnabled = true
|
||||
}
|
||||
e.adaptive = adaptiveController{config: c.Adaptive, enabled: c.Adaptive.Enabled}
|
||||
go e.run()
|
||||
return e, nil
|
||||
}
|
||||
func (e *Engine) command(f func()) error {
|
||||
if e.stopped.Load() {
|
||||
return errors.New("spatial audio engine closed")
|
||||
}
|
||||
select {
|
||||
case e.commands <- f:
|
||||
return nil
|
||||
case <-e.done:
|
||||
return errors.New("spatial audio engine closed")
|
||||
}
|
||||
}
|
||||
|
||||
// ApplyScene validates a complete immutable snapshot before queuing it. A bad
|
||||
// or old revision cannot partially replace the previous room geometry.
|
||||
func (e *Engine) ApplyScene(payload []byte) (uint64, error) {
|
||||
scene, err := ParseScene(payload, e.config.Occlusion.MaxObjects)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if !e.config.Occlusion.Enabled && (scene.Enabled || len(scene.Objects) > 0) {
|
||||
return scene.Revision, errors.New("occlusion disabled in server configuration")
|
||||
}
|
||||
if scene.HRTF != nil && *scene.HRTF && e.hrtf == nil {
|
||||
return scene.Revision, errors.New("HRTF disabled in server configuration")
|
||||
}
|
||||
result := make(chan error, 1)
|
||||
if err = e.command(func() {
|
||||
if scene.Adaptive != nil && *scene.Adaptive && !e.config.Adaptive.Enabled {
|
||||
result <- errors.New("adaptive quality disabled in server configuration")
|
||||
return
|
||||
}
|
||||
if e.scene != nil && scene.Revision <= e.scene.Revision {
|
||||
result <- errors.New("scene revision must increase")
|
||||
return
|
||||
}
|
||||
e.scene = scene
|
||||
if scene.Adaptive != nil {
|
||||
e.adaptive.setEnabled(*scene.Adaptive)
|
||||
}
|
||||
if scene.HRTF != nil {
|
||||
e.hrtfEnabled = *scene.HRTF
|
||||
}
|
||||
e.matrix.SetScene(scene)
|
||||
e.matrix.SetMinPriority(e.adaptive.minWallPriority())
|
||||
e.geometryDirty = true
|
||||
result <- nil
|
||||
}); err != nil {
|
||||
return scene.Revision, err
|
||||
}
|
||||
select {
|
||||
case err := <-result:
|
||||
return scene.Revision, err
|
||||
case <-e.done:
|
||||
return scene.Revision, errors.New("engine closed")
|
||||
}
|
||||
}
|
||||
func (e *Engine) AddListener(id string, output OutputFunc) error {
|
||||
result := make(chan error, 1)
|
||||
if err := e.command(func() {
|
||||
if _, ok := e.listeners[id]; ok {
|
||||
result <- nil
|
||||
return
|
||||
}
|
||||
if len(e.listeners) >= e.config.MaxParticipants {
|
||||
result <- errors.New("spatial participant limit reached")
|
||||
return
|
||||
}
|
||||
enc, err := e.opus.NewEncoder(2, e.config.Bitrate)
|
||||
if err == nil {
|
||||
mixer := NewMixer(e.config)
|
||||
mixer.hrtf = e.hrtf
|
||||
e.listeners[id] = &listener{id: id, encoder: enc, mixer: mixer, output: output}
|
||||
}
|
||||
result <- err
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case err := <-result:
|
||||
return err
|
||||
case <-e.done:
|
||||
return errors.New("engine closed")
|
||||
}
|
||||
}
|
||||
func (e *Engine) AddSource(id, track string) (*Input, error) {
|
||||
result := make(chan struct {
|
||||
input *Input
|
||||
err error
|
||||
}, 1)
|
||||
if err := e.command(func() {
|
||||
if _, exists := e.sources[id]; !exists && len(e.sources) >= e.config.MaxParticipants {
|
||||
// A participant may enter immediately after another has left. Do
|
||||
// not let a short retiring tail consume its source capacity.
|
||||
for sourceID, s := range e.sources {
|
||||
if s.retiring {
|
||||
e.disposeSource(s)
|
||||
delete(e.sources, sourceID)
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, exists := e.sources[id]; !exists && len(e.sources) >= e.config.MaxParticipants {
|
||||
result <- struct {
|
||||
input *Input
|
||||
err error
|
||||
}{nil, errors.New("spatial source limit reached")}
|
||||
return
|
||||
}
|
||||
if old := e.sources[id]; old != nil {
|
||||
e.disposeSource(old)
|
||||
delete(e.sources, id)
|
||||
}
|
||||
dec, err := e.opus.NewDecoder()
|
||||
var input *Input
|
||||
if err == nil {
|
||||
input = &Input{packets: make(chan RTPInput, 64)}
|
||||
e.sources[id] = &source{id: id, track: track, in: input, decoder: dec, opus: e.opus, queue: make(map[int64]RTPInput)}
|
||||
}
|
||||
result <- struct {
|
||||
input *Input
|
||||
err error
|
||||
}{input, err}
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
select {
|
||||
case r := <-result:
|
||||
return r.input, r.err
|
||||
case <-e.done:
|
||||
return nil, errors.New("engine closed")
|
||||
}
|
||||
}
|
||||
func (e *Engine) RemoveSource(id, track string) {
|
||||
_ = e.command(func() {
|
||||
if s := e.sources[id]; s != nil && s.track == track {
|
||||
e.retireSource(s)
|
||||
}
|
||||
})
|
||||
}
|
||||
func (e *Engine) RemoveParticipant(id string) {
|
||||
e.Poses.Remove(id)
|
||||
_ = e.command(func() {
|
||||
if s := e.sources[id]; s != nil {
|
||||
e.retireSource(s)
|
||||
}
|
||||
if l := e.listeners[id]; l != nil {
|
||||
l.mixer.Close()
|
||||
l.encoder.Close()
|
||||
delete(e.listeners, id)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Preserve decoder continuity briefly after unpublishing. PLC supplies the
|
||||
// tail while an envelope fades it out; the input accepts no new packets.
|
||||
func (e *Engine) retireSource(s *source) {
|
||||
if s.retiring {
|
||||
return
|
||||
}
|
||||
s.in.closed.Store(true)
|
||||
s.retiring = true
|
||||
s.fade = 1
|
||||
s.fadeStep = math.Exp(-1 / (SampleRate * float64(e.config.SmoothingMs) / 1000))
|
||||
s.remaining = 5 * e.config.SmoothingMs * SampleRate / 1000
|
||||
clear(s.queue)
|
||||
for {
|
||||
select {
|
||||
case <-s.in.packets:
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) disposeSource(s *source) {
|
||||
s.in.closed.Store(true)
|
||||
s.decoder.Close()
|
||||
e.stats.ReceivedPackets += s.in.received.Load()
|
||||
e.stats.QueueDrops += s.in.dropped.Load()
|
||||
e.stats.ConcealedFrames += s.plc
|
||||
e.stats.LatePackets += s.latePackets
|
||||
e.stats.Rebuffers += s.rebuffers
|
||||
e.stats.DecodeErrors += s.decodeErrors
|
||||
}
|
||||
func (e *Engine) SnapshotStats() Stats {
|
||||
result := make(chan Stats, 1)
|
||||
if e.command(func() {
|
||||
s := e.stats
|
||||
s.AdaptiveEnabled = e.adaptive.enabled
|
||||
s.QualityLevel = e.adaptive.level
|
||||
s.LoadPressure = e.adaptive.pressure
|
||||
s.QualityTransitions = e.adaptive.transitions
|
||||
s.PoseInterval = e.adaptive.poseInterval()
|
||||
s.ActiveObstacles = e.scene.activeObjects(e.adaptive.minWallPriority())
|
||||
s.EffectiveHRTF = e.adaptive.hrtfMode(e.hrtfEnabled, e.config.HRTF.Interpolation)
|
||||
s.HRTFEnabled = e.hrtfEnabled
|
||||
if e.scene != nil {
|
||||
s.SceneRevision = e.scene.Revision
|
||||
s.Obstacles = len(e.scene.Objects)
|
||||
s.OcclusionEnabled = e.scene.Enabled
|
||||
}
|
||||
s.MatrixCalculations = e.matrix.Calculations()
|
||||
s.Sources = len(e.sources)
|
||||
s.Listeners = len(e.listeners)
|
||||
s.Poses = len(e.Poses.Snapshot(time.Now(), e.config.PoseTimeout()))
|
||||
for _, src := range e.sources {
|
||||
s.ReceivedPackets += src.in.received.Load()
|
||||
s.QueueDrops += src.in.dropped.Load()
|
||||
s.ConcealedFrames += src.plc
|
||||
s.LatePackets += src.latePackets
|
||||
s.Rebuffers += src.rebuffers
|
||||
s.DecodeErrors += src.decodeErrors
|
||||
for _, v := range src.block {
|
||||
s.InputPeak = math.Max(s.InputPeak, math.Abs(float64(v)))
|
||||
}
|
||||
}
|
||||
for _, l := range e.listeners {
|
||||
s.HRTFPairs += len(l.mixer.pairs)
|
||||
for _, v := range l.block {
|
||||
s.OutputPeak = math.Max(s.OutputPeak, math.Abs(float64(v)))
|
||||
}
|
||||
}
|
||||
result <- s
|
||||
}) != nil {
|
||||
return Stats{}
|
||||
}
|
||||
select {
|
||||
case s := <-result:
|
||||
return s
|
||||
case <-e.done:
|
||||
return Stats{}
|
||||
}
|
||||
}
|
||||
func (e *Engine) Close() {
|
||||
if !e.stopped.Swap(true) {
|
||||
close(e.stop)
|
||||
}
|
||||
<-e.done
|
||||
}
|
||||
func (e *Engine) run() {
|
||||
defer forgetSpatialLoad(e)
|
||||
defer close(e.done)
|
||||
defer func() {
|
||||
for _, s := range e.sources {
|
||||
s.in.closed.Store(true)
|
||||
s.decoder.Close()
|
||||
}
|
||||
for _, l := range e.listeners {
|
||||
l.mixer.Close()
|
||||
l.encoder.Close()
|
||||
}
|
||||
e.hrtf.Close()
|
||||
}()
|
||||
ticker := time.NewTicker(10 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
start := time.Now()
|
||||
var last int64
|
||||
var nextGeometry time.Time
|
||||
var spatialPoses map[string]Pose
|
||||
for {
|
||||
select {
|
||||
case f := <-e.commands:
|
||||
f()
|
||||
case <-e.stop:
|
||||
return
|
||||
case now := <-ticker.C:
|
||||
step := now.Sub(start).Milliseconds() / 10
|
||||
if step <= last {
|
||||
continue
|
||||
}
|
||||
if step-last > 1 {
|
||||
e.stats.MissedTicks += uint64(step - last - 1)
|
||||
for _, l := range e.listeners {
|
||||
l.pcm = nil
|
||||
}
|
||||
}
|
||||
missed := uint64(max(step-last-1, 0))
|
||||
last = step
|
||||
tickStarted := time.Now()
|
||||
// Pose expiration must not wait for the next geometry update.
|
||||
fresh := e.Poses.Snapshot(time.Now(), e.config.PoseTimeout())
|
||||
for id := range spatialPoses {
|
||||
if _, exists := fresh[id]; !exists {
|
||||
e.geometryDirty = true
|
||||
}
|
||||
}
|
||||
if e.geometryDirty || !now.Before(nextGeometry) {
|
||||
spatialPoses = fresh
|
||||
e.matrix.Update(spatialPoses)
|
||||
e.geometryDirty = false
|
||||
nextGeometry = now.Add(e.adaptive.poseInterval())
|
||||
}
|
||||
hrtfMode := e.adaptive.hrtfMode(e.hrtfEnabled, e.config.HRTF.Interpolation)
|
||||
pcm := make(map[string][]float32, len(e.sources))
|
||||
ids := make([]string, 0, len(e.sources))
|
||||
for id := range e.sources {
|
||||
if s := e.sources[id]; s.retiring && s.remaining <= 0 {
|
||||
e.disposeSource(s)
|
||||
delete(e.sources, id)
|
||||
continue
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
for _, id := range ids {
|
||||
s := e.sources[id]
|
||||
block := s.read(now, time.Duration(e.config.JitterMs)*time.Millisecond)
|
||||
if s.retiring {
|
||||
for n := range block {
|
||||
s.fade *= s.fadeStep
|
||||
block[n] *= float32(s.fade)
|
||||
}
|
||||
s.remaining -= BlockSamples
|
||||
}
|
||||
pcm[id] = block
|
||||
}
|
||||
for _, l := range e.listeners {
|
||||
l.mixer.hrtfNearest = hrtfMode == "nearest"
|
||||
e.stats.HRTFErrors += l.mixer.MixSpatial(l.id, spatialPoses, e.matrix.Row(l.id), pcm, l.block[:], hrtfMode != "off")
|
||||
l.pcm = append(l.pcm, l.block[:]...)
|
||||
if len(l.pcm) < 2*PacketSamples {
|
||||
continue
|
||||
}
|
||||
packet, err := l.encoder.Encode(l.pcm[:2*PacketSamples])
|
||||
l.pcm = l.pcm[2*PacketSamples:]
|
||||
if err == nil {
|
||||
err = l.output(packet, uint32((step-2)*BlockSamples))
|
||||
}
|
||||
if err != nil {
|
||||
e.stats.OutputErrors++
|
||||
}
|
||||
}
|
||||
e.stats.TickDuration = time.Since(tickStarted)
|
||||
if e.stats.TickDuration > e.stats.MaxTickDuration {
|
||||
e.stats.MaxTickDuration = e.stats.TickDuration
|
||||
}
|
||||
if e.adaptive.observe(e, time.Now(), e.stats.TickDuration, missed) {
|
||||
e.matrix.SetMinPriority(e.adaptive.minWallPriority())
|
||||
e.geometryDirty = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) String() string {
|
||||
s := e.SnapshotStats()
|
||||
return fmt.Sprintf("sources=%d listeners=%d poses=%d received=%d input_peak=%.4f output_peak=%.4f dropped=%d concealed=%d late=%d rebuffers=%d decode_errors=%d missed=%d output_errors=%d matrix_calculations=%d tick=%s max_tick=%s occlusion=%t obstacles=%d scene_revision=%d hrtf=%t hrtf_pairs=%d hrtf_errors=%d adaptive=%t quality_level=%d load_pressure=%.3f pose_interval=%s active_obstacles=%d effective_hrtf=%s quality_transitions=%d", s.Sources, s.Listeners, s.Poses, s.ReceivedPackets, s.InputPeak, s.OutputPeak, s.QueueDrops, s.ConcealedFrames, s.LatePackets, s.Rebuffers, s.DecodeErrors, s.MissedTicks, s.OutputErrors, s.MatrixCalculations, s.TickDuration, s.MaxTickDuration, s.OcclusionEnabled, s.Obstacles, s.SceneRevision, s.HRTFEnabled, s.HRTFPairs, s.HRTFErrors, s.AdaptiveEnabled, s.QualityLevel, s.LoadPressure, s.PoseInterval, s.ActiveObstacles, s.EffectiveHRTF, s.QualityTransitions)
|
||||
}
|
||||
Reference in New Issue
Block a user