Add server-side spatial audio with HRTF, occlusion and adaptive quality
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
// Copyright 2026 Sverse contributors. Licensed under the Apache License, Version 2.0.
|
||||
package spatialaudio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/ebitengine/purego"
|
||||
)
|
||||
|
||||
// Opus owns a library handle for the process lifetime; individual codec states
|
||||
// are exclusively owned by the room's audio worker and destroyed on removal.
|
||||
type Opus struct {
|
||||
decoderCreate func(int32, int32, *int32) uintptr
|
||||
decoderDestroy func(uintptr)
|
||||
decode func(uintptr, *byte, int32, *float32, int32, int32) int32
|
||||
encoderCreate func(int32, int32, int32, *int32) uintptr
|
||||
encoderDestroy func(uintptr)
|
||||
encode func(uintptr, *float32, int32, *byte, int32) int32
|
||||
encoderCtl func(uintptr, int32, int32) int32
|
||||
packetSamples func(*byte, int32, int32) int32
|
||||
}
|
||||
|
||||
var opusLibraries = struct {
|
||||
sync.Mutex
|
||||
byPath map[string]*Opus
|
||||
}{byPath: make(map[string]*Opus)}
|
||||
|
||||
func LoadOpus(path string) (opus *Opus, err error) {
|
||||
if path == "" {
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
path = "opus.dll"
|
||||
case "darwin":
|
||||
path = "libopus.0.dylib"
|
||||
default:
|
||||
path = "libopus.so.0"
|
||||
}
|
||||
}
|
||||
opusLibraries.Lock()
|
||||
defer opusLibraries.Unlock()
|
||||
if existing := opusLibraries.byPath[path]; existing != nil {
|
||||
return existing, nil
|
||||
}
|
||||
lib, err := openOpusLibrary(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load libopus %q: %w", path, err)
|
||||
}
|
||||
defer func() {
|
||||
if p := recover(); p != nil {
|
||||
opus = nil
|
||||
err = fmt.Errorf("load libopus symbols: %v", p)
|
||||
}
|
||||
}()
|
||||
opus = &Opus{}
|
||||
purego.RegisterLibFunc(&opus.decoderCreate, lib, "opus_decoder_create")
|
||||
purego.RegisterLibFunc(&opus.decoderDestroy, lib, "opus_decoder_destroy")
|
||||
purego.RegisterLibFunc(&opus.decode, lib, "opus_decode_float")
|
||||
purego.RegisterLibFunc(&opus.encoderCreate, lib, "opus_encoder_create")
|
||||
purego.RegisterLibFunc(&opus.encoderDestroy, lib, "opus_encoder_destroy")
|
||||
purego.RegisterLibFunc(&opus.encode, lib, "opus_encode_float")
|
||||
purego.RegisterLibFunc(&opus.encoderCtl, lib, "opus_encoder_ctl")
|
||||
purego.RegisterLibFunc(&opus.packetSamples, lib, "opus_packet_get_nb_samples")
|
||||
opusLibraries.byPath[path] = opus
|
||||
return opus, nil
|
||||
}
|
||||
|
||||
type Decoder struct {
|
||||
opus *Opus
|
||||
state uintptr
|
||||
}
|
||||
|
||||
func (o *Opus) NewDecoder() (*Decoder, error) {
|
||||
var code int32
|
||||
state := o.decoderCreate(SampleRate, 1, &code)
|
||||
if state == 0 || code != 0 {
|
||||
return nil, fmt.Errorf("opus decoder: %d", code)
|
||||
}
|
||||
return &Decoder{o, state}, nil
|
||||
}
|
||||
func (d *Decoder) Decode(packet []byte, frameSize int, fec bool) ([]float32, error) {
|
||||
if frameSize < 120 || frameSize > 5760 {
|
||||
return nil, fmt.Errorf("invalid Opus frame size")
|
||||
}
|
||||
pcm := make([]float32, frameSize)
|
||||
var data *byte
|
||||
if len(packet) > 0 {
|
||||
data = &packet[0]
|
||||
}
|
||||
f := int32(0)
|
||||
if fec {
|
||||
f = 1
|
||||
}
|
||||
n := d.opus.decode(d.state, data, int32(len(packet)), &pcm[0], int32(frameSize), f)
|
||||
runtime.KeepAlive(packet)
|
||||
runtime.KeepAlive(pcm)
|
||||
if n < 0 {
|
||||
return nil, fmt.Errorf("opus decode: %d", n)
|
||||
}
|
||||
return pcm[:n], nil
|
||||
}
|
||||
func (d *Decoder) Close() {
|
||||
if d != nil && d.state != 0 {
|
||||
d.opus.decoderDestroy(d.state)
|
||||
d.state = 0
|
||||
}
|
||||
}
|
||||
|
||||
type Encoder struct {
|
||||
opus *Opus
|
||||
state uintptr
|
||||
channels int
|
||||
}
|
||||
|
||||
func (o *Opus) NewEncoder(channels, bitrate int) (*Encoder, error) {
|
||||
var code int32
|
||||
state := o.encoderCreate(SampleRate, int32(channels), 2049, &code)
|
||||
if state == 0 || code != 0 {
|
||||
return nil, fmt.Errorf("opus encoder: %d", code)
|
||||
}
|
||||
e := &Encoder{o, state, channels}
|
||||
if code = o.encoderCtl(state, 4002, int32(bitrate)); code != 0 {
|
||||
e.Close()
|
||||
return nil, fmt.Errorf("opus bitrate: %d", code)
|
||||
}
|
||||
// Force two-channel encoding for personalized output, including centered voices.
|
||||
if channels == 2 {
|
||||
if code = o.encoderCtl(state, 4022, 2); code != 0 {
|
||||
e.Close()
|
||||
return nil, fmt.Errorf("opus stereo: %d", code)
|
||||
}
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
func (e *Encoder) Encode(pcm []float32) ([]byte, error) {
|
||||
if len(pcm) != PacketSamples*e.channels {
|
||||
return nil, fmt.Errorf("expected 20 ms PCM")
|
||||
}
|
||||
b := make([]byte, 1275)
|
||||
n := e.opus.encode(e.state, &pcm[0], PacketSamples, &b[0], int32(len(b)))
|
||||
runtime.KeepAlive(pcm)
|
||||
runtime.KeepAlive(b)
|
||||
if n < 0 {
|
||||
return nil, fmt.Errorf("opus encode: %d", n)
|
||||
}
|
||||
return b[:n], nil
|
||||
}
|
||||
func (e *Encoder) Close() {
|
||||
if e.state != 0 {
|
||||
e.opus.encoderDestroy(e.state)
|
||||
e.state = 0
|
||||
}
|
||||
}
|
||||
func (o *Opus) Samples(b []byte) int {
|
||||
if len(b) == 0 {
|
||||
return 0
|
||||
}
|
||||
n := o.packetSamples(&b[0], int32(len(b)), SampleRate)
|
||||
runtime.KeepAlive(b)
|
||||
return int(n)
|
||||
}
|
||||
Reference in New Issue
Block a user