// Copyright 2026 Sverse contributors. Licensed under the Apache License, Version 2.0. package spatialaudio import ( "encoding/binary" "errors" "math" "sync" "time" ) const PoseTopic = "sverse.spatial.pose.v1" const PosePacketSize = 44 type Vec3 struct{ X, Y, Z float64 } type Quaternion struct{ X, Y, Z, W float64 } type Pose struct { Sequence uint32 ClientTimeMs float64 Position Vec3 Rotation Quaternion } func finite(v float64) bool { return !math.IsNaN(v) && !math.IsInf(v, 0) } func (p Pose) Valid() bool { for _, v := range []float64{p.ClientTimeMs, p.Position.X, p.Position.Y, p.Position.Z, p.Rotation.X, p.Rotation.Y, p.Rotation.Z, p.Rotation.W} { if !finite(v) { return false } } n := p.Rotation.X*p.Rotation.X + p.Rotation.Y*p.Rotation.Y + p.Rotation.Z*p.Rotation.Z + p.Rotation.W*p.Rotation.W return n > 0.5 && n < 1.5 && math.Abs(p.Position.X) < 1e7 && math.Abs(p.Position.Y) < 1e7 && math.Abs(p.Position.Z) < 1e7 } // Layout: version/u8, reserved/3, sequence/u32, monotonic milliseconds/f64, // position/3*f32, quaternion x,y,z,w/4*f32. All values are little endian. func ParsePose(b []byte) (Pose, error) { if len(b) != PosePacketSize || b[0] != 1 || b[1] != 0 || b[2] != 0 || b[3] != 0 { return Pose{}, errors.New("invalid spatial pose packet") } f := func(i int) float64 { return float64(math.Float32frombits(binary.LittleEndian.Uint32(b[i : i+4]))) } p := Pose{Sequence: binary.LittleEndian.Uint32(b[4:8]), ClientTimeMs: math.Float64frombits(binary.LittleEndian.Uint64(b[8:16])), Position: Vec3{f(16), f(20), f(24)}, Rotation: Quaternion{f(28), f(32), f(36), f(40)}} if !p.Valid() { return Pose{}, errors.New("non-finite or invalid spatial pose") } q := &p.Rotation n := math.Sqrt(q.X*q.X + q.Y*q.Y + q.Z*q.Z + q.W*q.W) q.X /= n q.Y /= n q.Z /= n q.W /= n return p, nil } func (p Pose) MarshalBinary() []byte { b := make([]byte, PosePacketSize) b[0] = 1 binary.LittleEndian.PutUint32(b[4:8], p.Sequence) binary.LittleEndian.PutUint64(b[8:16], math.Float64bits(p.ClientTimeMs)) for i, v := range []float64{p.Position.X, p.Position.Y, p.Position.Z, p.Rotation.X, p.Rotation.Y, p.Rotation.Z, p.Rotation.W} { binary.LittleEndian.PutUint32(b[16+i*4:], math.Float32bits(float32(v))) } return b } func (q Quaternion) Right() Vec3 { return Vec3{2 * (q.X*q.Y - q.Z*q.W), 1 - 2*(q.X*q.X+q.Z*q.Z), 2 * (q.Y*q.Z + q.X*q.W)} } type timedPose struct { pose Pose at time.Time } type PoseStore struct { mu sync.RWMutex poses map[string]timedPose } func NewPoseStore() *PoseStore { return &PoseStore{poses: make(map[string]timedPose)} } func (s *PoseStore) Update(id string, p Pose, now time.Time) bool { if !p.Valid() { return false } s.mu.Lock() defer s.mu.Unlock() old, ok := s.poses[id] if ok && int32(p.Sequence-old.pose.Sequence) <= 0 { return false } s.poses[id] = timedPose{p, now} return true } func (s *PoseStore) Remove(id string) { s.mu.Lock(); delete(s.poses, id); s.mu.Unlock() } func (s *PoseStore) Snapshot(now time.Time, timeout time.Duration) map[string]Pose { s.mu.RLock() defer s.mu.RUnlock() out := make(map[string]Pose, len(s.poses)) for id, p := range s.poses { if now.Sub(p.at) <= timeout { out[id] = p.pose } } return out }