feat: added full support in USB (local) & ethernet (local or remote) of audio for Icom
This commit is contained in:
@@ -5,6 +5,7 @@ package audio
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
@@ -269,3 +270,151 @@ func playPCM(deviceID string, pcm []byte, rate, ch, bits int, stop <-chan struct
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// pcmRing is a thread-safe, latency-bounded FIFO of PCM bytes feeding a live
|
||||
// render stream. Producers (a USB-codec capture, or a decoded network audio
|
||||
// stream) Push freshly-arrived samples; the render loop Pulls. It is the shared
|
||||
// hand-off point between "where the audio comes from" (USB device / UDP 50003)
|
||||
// and "where it's heard" (any WASAPI output) — so the transport can be swapped
|
||||
// without touching the render side, mirroring the civTransport split on the CAT
|
||||
// side. On overflow the oldest audio is dropped to keep latency bounded; on
|
||||
// underrun Pull simply returns short and the render loop pads with silence.
|
||||
type pcmRing struct {
|
||||
mu sync.Mutex
|
||||
buf []byte
|
||||
max int // hard cap in bytes (drops oldest beyond this → bounded latency)
|
||||
}
|
||||
|
||||
// newPCMRing makes a ring whose backlog is capped at maxBytes. Size it from the
|
||||
// acceptable latency: bytesPerSec (=32000) worth ≈ 1 s.
|
||||
func newPCMRing(maxBytes int) *pcmRing {
|
||||
if maxBytes <= 0 {
|
||||
maxBytes = bytesPerSec // 1 s default
|
||||
}
|
||||
return &pcmRing{max: maxBytes}
|
||||
}
|
||||
|
||||
// Push appends samples, dropping the oldest audio if the backlog would exceed
|
||||
// the cap (a slow/absent consumer never makes the producer block or grow without
|
||||
// bound). A short glitch beats runaway latency for live monitoring.
|
||||
func (r *pcmRing) Push(p []byte) {
|
||||
if len(p) == 0 {
|
||||
return
|
||||
}
|
||||
r.mu.Lock()
|
||||
r.buf = append(r.buf, p...)
|
||||
if len(r.buf) > r.max {
|
||||
drop := len(r.buf) - r.max
|
||||
r.buf = append(r.buf[:0], r.buf[drop:]...)
|
||||
}
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
// pull removes and returns up to maxBytes of queued PCM (a private copy), or nil
|
||||
// when empty. The render loop pads any shortfall with silence.
|
||||
func (r *pcmRing) pull(maxBytes int) []byte {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if len(r.buf) == 0 || maxBytes <= 0 {
|
||||
return nil
|
||||
}
|
||||
n := maxBytes
|
||||
if n > len(r.buf) {
|
||||
n = len(r.buf)
|
||||
}
|
||||
out := make([]byte, n)
|
||||
copy(out, r.buf[:n])
|
||||
r.buf = append(r.buf[:0], r.buf[n:]...)
|
||||
return out
|
||||
}
|
||||
|
||||
// renderStream continuously renders PCM pulled from src to a device until stop
|
||||
// closes — the streaming counterpart to playPCM's fixed buffer. On underrun it
|
||||
// writes silence rather than glitching, keeping the WASAPI clock steady so live
|
||||
// monitor audio flows smoothly even when the source stalls briefly. Runs on a
|
||||
// COM-initialised, OS-locked thread.
|
||||
func renderStream(deviceID string, rate, ch, bits int, stop <-chan struct{}, src *pcmRing) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
if err := coInit(); err != nil {
|
||||
return fmt.Errorf("CoInitialize: %w", err)
|
||||
}
|
||||
defer ole.CoUninitialize()
|
||||
|
||||
dev, err := openDevice(wca.ERender, deviceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dev.Release()
|
||||
|
||||
var ac *wca.IAudioClient
|
||||
if err := dev.Activate(wca.IID_IAudioClient, wca.CLSCTX_ALL, nil, &ac); err != nil {
|
||||
return fmt.Errorf("activate render: %w", err)
|
||||
}
|
||||
defer ac.Release()
|
||||
|
||||
frameBytes := ch * bits / 8
|
||||
if frameBytes <= 0 {
|
||||
return fmt.Errorf("bad audio format")
|
||||
}
|
||||
wfx := &wca.WAVEFORMATEX{
|
||||
WFormatTag: 1, NChannels: uint16(ch), NSamplesPerSec: uint32(rate),
|
||||
NAvgBytesPerSec: uint32(rate * frameBytes), NBlockAlign: uint16(frameBytes),
|
||||
WBitsPerSample: uint16(bits), CbSize: 0,
|
||||
}
|
||||
if err := ac.Initialize(wca.AUDCLNT_SHAREMODE_SHARED, autoConvert,
|
||||
wca.REFERENCE_TIME(bufferDuration100ns), 0, wfx, nil); err != nil {
|
||||
return fmt.Errorf("initialize render: %w", err)
|
||||
}
|
||||
var bufFrames uint32
|
||||
if err := ac.GetBufferSize(&bufFrames); err != nil {
|
||||
return err
|
||||
}
|
||||
var arc *wca.IAudioRenderClient
|
||||
if err := ac.GetService(wca.IID_IAudioRenderClient, &arc); err != nil {
|
||||
return fmt.Errorf("get render service: %w", err)
|
||||
}
|
||||
defer arc.Release()
|
||||
|
||||
// feed fills up to `frames` render frames: as much real audio as the ring
|
||||
// has, the remainder silence (so the buffer stays full and the clock steady).
|
||||
feed := func(frames int) error {
|
||||
if frames <= 0 {
|
||||
return nil
|
||||
}
|
||||
var data *byte
|
||||
if err := arc.GetBuffer(uint32(frames), &data); err != nil {
|
||||
return err
|
||||
}
|
||||
dst := unsafe.Slice(data, frames*frameBytes)
|
||||
got := src.pull(frames * frameBytes)
|
||||
n := copy(dst, got)
|
||||
for i := n; i < len(dst); i++ {
|
||||
dst[i] = 0 // silence-fill the shortfall
|
||||
}
|
||||
arc.ReleaseBuffer(uint32(frames), 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := feed(int(bufFrames)); err != nil { // pre-fill to avoid a start glitch
|
||||
return err
|
||||
}
|
||||
if err := ac.Start(); err != nil {
|
||||
return fmt.Errorf("start render: %w", err)
|
||||
}
|
||||
defer ac.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return nil
|
||||
default:
|
||||
}
|
||||
var padding uint32
|
||||
ac.GetCurrentPadding(&padding)
|
||||
if err := feed(int(bufFrames - padding)); err != nil {
|
||||
return err
|
||||
}
|
||||
time.Sleep(8 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user