//go:build linux && !android && (amd64 || arm64) && !android package audio import ( "errors" "fmt" "os" "path/filepath" "runtime" "sync" "time" "unsafe" "github.com/ebitengine/purego" ) // Linux plays through PulseAudio's simple API, which PipeWire serves as // well, or else ALSA's default device. Both block while the device holds // enough, from the output's goroutine. const linuxFrames = sampleRate / 100 // per write: 10 ms var ( pulseLoad sync.Once pulseErr error paSimpleNew func(server, name *byte, dir int32, dev, stream *byte, spec *paSampleSpec, chmap, attr unsafe.Pointer, err *int32) uintptr paSimpleWrite uintptr // through a cCaller, on every write paSimpleFree func(s uintptr) paStrerror func(err int32) string alsaLoad sync.Once alsaErr error sndPcmOpen func(pcm *uintptr, name *byte, stream int32, mode int32) int32 sndPcmSetParams func(pcm uintptr, format, access int32, channels, rate uint32, softResample int32, latency uint32) int32 sndPcmWritei uintptr // through a cCaller, on every write sndPcmRecover func(pcm uintptr, err int32, silent int32) int32 sndPcmClose func(pcm uintptr) int32 sndStrerror func(err int32) string ) type paSampleSpec struct { format int32 rate uint32 channels uint8 } type paBufferAttr struct { maxLength, tLength, preBuf, minReq, fragSize uint32 } const ( paStreamPlayback = 1 paSampleFloat32LE = 5 sndPcmStreamPlayback = 0 sndPcmFormatFloatLE = 14 sndPcmAccessRWInterleaved = 3 ) func cstr(s string) *byte { b := append([]byte(s), 0) return &b[0] } func appName() string { if exe, err := os.Executable(); err == nil { return filepath.Base(exe) } return "mygo" } func openDevice(ahead time.Duration) (device, int, error) { d, perr := openPulse(ahead) if perr == nil { return d, linuxFrames, nil } d, aerr := openALSA(ahead) if aerr == nil { return d, linuxFrames, nil } return nil, 0, errors.Join(perr, aerr) } type pulseDevice struct { s uintptr code int32 // pa_simple_write's error c cCaller } func openPulse(ahead time.Duration) (device, error) { pulseLoad.Do(func() { lib, err := purego.Dlopen("libpulse-simple.so.0", purego.RTLD_NOW|purego.RTLD_GLOBAL) if err != nil { pulseErr = err return } purego.RegisterLibFunc(&paSimpleNew, lib, "pa_simple_new") paSimpleWrite = cFunc(lib, "pa_simple_write") purego.RegisterLibFunc(&paSimpleFree, lib, "pa_simple_free") purego.RegisterLibFunc(&paStrerror, lib, "pa_strerror") }) if pulseErr != nil { return nil, pulseErr } spec := paSampleSpec{format: paSampleFloat32LE, rate: sampleRate, channels: channels} // ahead, asking for more every 10 ms. const frame = channels * 4 attr := paBufferAttr{ maxLength: ^uint32(0), tLength: uint32(int64(ahead) * sampleRate / int64(time.Second) * frame), preBuf: ^uint32(0), minReq: linuxFrames * frame, fragSize: ^uint32(0), } var code int32 name := appName() s := paSimpleNew(nil, cstr(name), paStreamPlayback, nil, cstr(name), &spec, nil, unsafe.Pointer(&attr), &code) if s == 0 { return nil, fmt.Errorf("audio: PulseAudio: %s", paStrerror(code)) } return &pulseDevice{s: s}, nil } func (d *pulseDevice) write(samples []float32) error { // samples and d.code are on the heap, as a cCaller needs. r := int32(d.c.call(paSimpleWrite, d.s, uintptr(unsafe.Pointer(&samples[0])), uintptr(len(samples)*4), uintptr(unsafe.Pointer(&d.code)))) runtime.KeepAlive(samples) if r < 0 { return fmt.Errorf("audio: PulseAudio: %s", paStrerror(d.code)) } return nil } func (d *pulseDevice) close() { paSimpleFree(d.s) } type alsaDevice struct { pcm uintptr c cCaller } func openALSA(ahead time.Duration) (device, error) { alsaLoad.Do(func() { lib, err := purego.Dlopen("libasound.so.2", purego.RTLD_NOW|purego.RTLD_GLOBAL) if err != nil { alsaErr = err return } purego.RegisterLibFunc(&sndPcmOpen, lib, "snd_pcm_open") purego.RegisterLibFunc(&sndPcmSetParams, lib, "snd_pcm_set_params") sndPcmWritei = cFunc(lib, "snd_pcm_writei") purego.RegisterLibFunc(&sndPcmRecover, lib, "snd_pcm_recover") purego.RegisterLibFunc(&sndPcmClose, lib, "snd_pcm_close") purego.RegisterLibFunc(&sndStrerror, lib, "snd_strerror") }) if alsaErr != nil { return nil, alsaErr } var pcm uintptr if e := sndPcmOpen(&pcm, cstr("default"), sndPcmStreamPlayback, 0); e < 0 { return nil, fmt.Errorf("audio: ALSA: %s", sndStrerror(e)) } if e := sndPcmSetParams(pcm, sndPcmFormatFloatLE, sndPcmAccessRWInterleaved, channels, sampleRate, 1, uint32(ahead.Microseconds())); e < 0 { sndPcmClose(pcm) return nil, fmt.Errorf("audio: ALSA: %s", sndStrerror(e)) } return &alsaDevice{pcm: pcm}, nil } func (d *alsaDevice) write(samples []float32) error { for len(samples) > 0 { // samples is on the heap, as a cCaller needs. n := int(d.c.call(sndPcmWritei, d.pcm, uintptr(unsafe.Pointer(&samples[0])), uintptr(len(samples)/channels))) runtime.KeepAlive(samples) if n < 0 { // An underrun or a suspend, recovered from by preparing the // device again. if e := sndPcmRecover(d.pcm, int32(n), 1); e < 0 { return fmt.Errorf("audio: ALSA: %s", sndStrerror(e)) } continue } samples = samples[n*channels:] } return nil } func (d *alsaDevice) close() { sndPcmClose(d.pcm) }