feat(linux): the Go half of OpsLog builds for Linux
Measured rather than guessed: the whole repository was cross-compiled for
linux/amd64 and the gaps closed one by one. There were fewer than expected.
Flex and TCI were never Windows-specific — they carried //go:build windows by
inheritance and import nothing but net and gorilla/websocket. Untagged, no code
change. The two backends a Linux operator is most likely to own were already
portable.
Audio was 560 lines, not 2287: only devices.go and engine.go touch WASAPI, while
manager.go, recorder.go, wav.go and mp3.go were pure Go wearing the tag by
association. The whole platform surface is seven functions, now implemented a
second time on PulseAudio through github.com/jfreymuth/pulse — pure Go over the
server socket, so the no-cgo rule survives, and PipeWire answers the same
protocol. The fixed 16 kHz mono format and the server-side resampling mirror
what AUTOCONVERTPCM does on Windows, for the same reason.
OmniRig is the only real loss, and its backend still EXISTS off Windows rather
than being compiled out of app.go: a settings database is portable, so an
operator moving a profile across keeps "omnirig" saved and must be told to pick
a native backend instead of meeting a nil one.
The parts where Linux is not Windows, and where a compile-only stub would have
been a silent bug:
- data dir: still beside the binary, but ~/.local/share/OpsLog/data when that
folder belongs to the system — decided by trying the write, because /opt and
/usr/local are writable on some stations and not others.
- single instance: an flock, not a pid file. The kernel drops it however the
process dies, so a crash leaves nothing to delete by hand. This is the guard
that stops two instances fighting over the rig frequency.
- update: simpler here. Unix renames over a running binary, so the deferred
swap the Windows path needs a detached helper for is unreachable.
- tasklist/taskkill become /proc and SIGTERM; the boot log moves out of /tmp,
which is wiped exactly when the evidence is wanted.
- serial ports sorted naturally: /dev/ttyUSB10 was landing between USB1 and
USB2, the same trap COM10 fell into.
release.ps1 now cross-builds and vets for linux before it builds the exe, and
refuses the release if that fails — a port rots one unguarded x/sys/windows call
at a time.
Nothing has been executed on Linux yet: Wails needs webkit2gtk and cgo there, so
the binary must be built on Linux. scripts/linux-setup.sh checks the machine and
does it; BUILDING-LINUX.md is the manual version.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package audio
|
||||
|
||||
// Device is one audio endpoint (a capture input or a render output).
|
||||
//
|
||||
// ID is whatever the platform calls the endpoint and is PERSISTED in settings:
|
||||
// a WASAPI endpoint id on Windows, a PulseAudio source/sink name on Linux.
|
||||
// It is opaque to everything above this package, which only ever hands it back.
|
||||
type Device struct {
|
||||
ID string `json:"id"` // opaque platform endpoint id (persisted)
|
||||
Name string `json:"name"` // friendly name shown in dropdowns
|
||||
Default bool `json:"default"` // is this the system default endpoint
|
||||
}
|
||||
|
||||
// DeviceName resolves an endpoint id to its friendly name.
|
||||
//
|
||||
// Diagnostics quote the id that was CONFIGURED, which is a GUID — an operator
|
||||
// told "no audio at all from {0.0.1.00000000}.{6a27abfd…}" learns nothing they
|
||||
// can act on, while "no audio at all from DAX RX 1 (FlexRadio DAX)" points
|
||||
// straight at the DAX panel.
|
||||
//
|
||||
// Falls back to the id when the endpoint cannot be found, which is itself worth
|
||||
// seeing: a device that has disappeared explains an empty recording too.
|
||||
func DeviceName(id string) string {
|
||||
if id == "" {
|
||||
return "(none)"
|
||||
}
|
||||
for _, list := range []func() ([]Device, error){ListInputDevices, ListOutputDevices} {
|
||||
devs, err := list()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, d := range devs {
|
||||
if d.ID == id {
|
||||
return d.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
return id
|
||||
}
|
||||
@@ -15,13 +15,6 @@ import (
|
||||
"github.com/moutend/go-wca/pkg/wca"
|
||||
)
|
||||
|
||||
// Device is one audio endpoint (a capture input or a render output).
|
||||
type Device struct {
|
||||
ID string `json:"id"` // stable WASAPI endpoint id (persisted)
|
||||
Name string `json:"name"` // friendly name shown in dropdowns
|
||||
Default bool `json:"default"` // is this the system default endpoint
|
||||
}
|
||||
|
||||
// ListInputDevices returns the active capture endpoints — microphones,
|
||||
// line-in, and the soundcard input wired to the rig's audio out ("From Radio").
|
||||
func ListInputDevices() ([]Device, error) { return listEndpoints(wca.ECapture) }
|
||||
@@ -101,30 +94,3 @@ func endpointName(dev *wca.IMMDevice, fallback string) string {
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
// DeviceName resolves an endpoint id to its friendly name.
|
||||
//
|
||||
// Diagnostics quote the id that was CONFIGURED, which is a GUID — an operator
|
||||
// told "no audio at all from {0.0.1.00000000}.{6a27abfd…}" learns nothing they
|
||||
// can act on, while "no audio at all from DAX RX 1 (FlexRadio DAX)" points
|
||||
// straight at the DAX panel.
|
||||
//
|
||||
// Falls back to the id when the endpoint cannot be found, which is itself worth
|
||||
// seeing: a device that has disappeared explains an empty recording too.
|
||||
func DeviceName(id string) string {
|
||||
if id == "" {
|
||||
return "(none)"
|
||||
}
|
||||
for _, list := range []func() ([]Device, error){ListInputDevices, ListOutputDevices} {
|
||||
devs, err := list()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, d := range devs {
|
||||
if d.ID == id {
|
||||
return d.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
//go:build linux
|
||||
|
||||
package audio
|
||||
|
||||
// devices_linux.go — audio endpoints on Linux, through PulseAudio.
|
||||
//
|
||||
// PulseAudio and not ALSA, for two reasons that both matter here. ALSA's C
|
||||
// library needs cgo, and OpsLog is a pure-Go build; and PulseAudio is the API
|
||||
// that is actually present on a ham's desktop — PipeWire, which most current
|
||||
// distributions ship, answers the PulseAudio protocol through pipewire-pulse,
|
||||
// so one client speaks to both. github.com/jfreymuth/pulse implements that
|
||||
// protocol in Go over the server's Unix socket, so nothing is linked in.
|
||||
//
|
||||
// The endpoint id we persist is the sink/source NAME
|
||||
// ("alsa_input.usb-Icom_Inc._IC-7610-00.analog-stereo"), never the numeric
|
||||
// index: the index is assigned at boot in device-arrival order and moves the
|
||||
// moment a rig is plugged in before a headset.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jfreymuth/pulse"
|
||||
)
|
||||
|
||||
// pulseClient opens a short-lived connection to the local sound server. Each
|
||||
// call gets its own: the connection is a Unix socket to a server that may be
|
||||
// restarted underneath us (a PipeWire update, a user logging the session out
|
||||
// and in), and holding one open for the lifetime of the app means every later
|
||||
// call fails until OpsLog itself restarts.
|
||||
func pulseClient() (*pulse.Client, error) {
|
||||
c, err := pulse.NewClient(pulse.ClientApplicationName("OpsLog"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot reach the sound server (is PulseAudio or PipeWire running?): %w", err)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// ListInputDevices returns the capture sources.
|
||||
//
|
||||
// Monitor sources (".monitor", what a given output is playing) are kept rather
|
||||
// than filtered out. They look like clutter until you meet the operator whose
|
||||
// rig audio reaches OpsLog through a virtual cable — on Linux that is a
|
||||
// null-sink and its monitor, and hiding it would hide the only device that
|
||||
// works for them.
|
||||
func ListInputDevices() ([]Device, error) {
|
||||
c, err := pulseClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
srcs, err := c.ListSources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defID := ""
|
||||
if d, err := c.DefaultSource(); err == nil && d != nil {
|
||||
defID = d.ID()
|
||||
}
|
||||
out := make([]Device, 0, len(srcs))
|
||||
for _, s := range srcs {
|
||||
out = append(out, Device{ID: s.ID(), Name: endpointLabel(s.Name(), s.ID()), Default: s.ID() == defID})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListOutputDevices returns the render sinks.
|
||||
func ListOutputDevices() ([]Device, error) {
|
||||
c, err := pulseClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
sinks, err := c.ListSinks()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defID := ""
|
||||
if d, err := c.DefaultSink(); err == nil && d != nil {
|
||||
defID = d.ID()
|
||||
}
|
||||
out := make([]Device, 0, len(sinks))
|
||||
for _, s := range sinks {
|
||||
out = append(out, Device{ID: s.ID(), Name: endpointLabel(s.Name(), s.ID()), Default: s.ID() == defID})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// endpointLabel prefers the server's human description ("USB Audio CODEC
|
||||
// Analog Stereo") and falls back to the raw name, which is ugly but still
|
||||
// identifies the device — an empty entry in the dropdown identifies nothing.
|
||||
func endpointLabel(desc, id string) string {
|
||||
if d := strings.TrimSpace(desc); d != "" {
|
||||
return d
|
||||
}
|
||||
return id
|
||||
}
|
||||
@@ -5,7 +5,6 @@ package audio
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
@@ -281,63 +280,6 @@ func playPCM(deviceID string, pcm []byte, rate, ch, bits int, stop <-chan struct
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
//go:build linux
|
||||
|
||||
package audio
|
||||
|
||||
// engine_linux.go — the four calls the rest of the package makes into the sound
|
||||
// card, implemented on PulseAudio. The Windows half of this pair is engine.go
|
||||
// (WASAPI); nothing above these functions knows which one it is talking to.
|
||||
//
|
||||
// Capture is fixed at 16 kHz mono 16-bit, the format the DVK, the recorder and
|
||||
// the CW tap all share (see wav.go). We ask the server for it and let the
|
||||
// server resample from whatever the device really runs at — the same division
|
||||
// of labour as WASAPI's AUTOCONVERTPCM, and for the same reason: a rig codec
|
||||
// that only does 48 kHz must still feed a 16 kHz pipeline, and the sound
|
||||
// server's converter filters before it decimates, where a naive one folds the
|
||||
// receiver hiss above 8 kHz straight back on top of the voice.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/jfreymuth/pulse"
|
||||
"github.com/jfreymuth/pulse/proto"
|
||||
)
|
||||
|
||||
// chunkFrames is how much audio a playback reader hands over at once (20 ms).
|
||||
// It bounds how much silence a padded underrun can queue ahead of real audio,
|
||||
// which is what keeps live monitoring from drifting seconds behind the rig.
|
||||
const chunkFrames = sampleRate / 50
|
||||
|
||||
// channelMap describes n channels to the server. Only mono and stereo occur
|
||||
// here — capture is always mono, and playback follows the WAV being played.
|
||||
func channelMap(n int) proto.ChannelMap {
|
||||
if n >= 2 {
|
||||
return proto.ChannelMap{proto.ChannelLeft, proto.ChannelRight}
|
||||
}
|
||||
return proto.ChannelMap{proto.ChannelMono}
|
||||
}
|
||||
|
||||
// chunkWriter turns the record stream's byte deliveries into onChunk calls.
|
||||
// The server reuses its buffer between deliveries, so every chunk is copied
|
||||
// before it leaves: the recorder keeps the slices it is given.
|
||||
type chunkWriter struct{ onChunk func([]byte) }
|
||||
|
||||
func (w chunkWriter) Write(p []byte) (int, error) {
|
||||
if len(p) > 0 && w.onChunk != nil {
|
||||
cp := make([]byte, len(p))
|
||||
copy(cp, p)
|
||||
w.onChunk(cp)
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// recordPCM captures from a device into 16 kHz mono 16-bit PCM bytes until the
|
||||
// stop channel is closed.
|
||||
func recordPCM(deviceID string, stop <-chan struct{}) ([]byte, error) {
|
||||
out := make([]byte, 0, bytesPerSec*4)
|
||||
err := captureStream(deviceID, stop, func(chunk []byte) { out = append(out, chunk...) })
|
||||
return out, err
|
||||
}
|
||||
|
||||
// captureStream opens a device and calls onChunk with freshly-captured 16 kHz
|
||||
// mono 16-bit PCM as it arrives, until stop closes. onChunk receives a private
|
||||
// copy it may retain.
|
||||
func captureStream(deviceID string, stop <-chan struct{}, onChunk func([]byte)) error {
|
||||
c, err := pulseClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
// Rate and channels first, then latency — the latency option sizes its
|
||||
// buffer from both, so setting it earlier would size it from the defaults.
|
||||
//
|
||||
// 50 ms of fragment: the CW decoder is downstream of this and works on the
|
||||
// chunks as they arrive, so a server-chosen fragment of a quarter of a
|
||||
// second would make it decide about a dit long after the dit was over.
|
||||
opts := []pulse.RecordOption{
|
||||
pulse.RecordSampleRate(sampleRate),
|
||||
pulse.RecordChannels(channelMap(channels)),
|
||||
pulse.RecordLatency(0.05),
|
||||
pulse.RecordMediaName("OpsLog capture"),
|
||||
}
|
||||
// An empty id means "whatever the desktop calls the default", which is also
|
||||
// what an operator who has never opened the audio settings expects.
|
||||
if deviceID != "" {
|
||||
src, err := c.SourceByID(deviceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("no audio input %q: %w", deviceID, err)
|
||||
}
|
||||
opts = append(opts, pulse.RecordSource(src))
|
||||
}
|
||||
st, err := c.NewRecord(pulse.NewWriter(chunkWriter{onChunk}, proto.FormatInt16LE), opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open capture: %w", err)
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
st.Start()
|
||||
<-stop
|
||||
st.Stop()
|
||||
return st.Error()
|
||||
}
|
||||
|
||||
// playPCM plays a fixed buffer to a device and returns when it has been heard
|
||||
// (or when stop closes, which cuts it short).
|
||||
func playPCM(deviceID string, pcm []byte, rate, ch, bits int, stop <-chan struct{}) error {
|
||||
if len(pcm) == 0 {
|
||||
return nil
|
||||
}
|
||||
format, err := pulseFormat(bits)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
frameBytes := ch * bits / 8
|
||||
if frameBytes <= 0 || rate <= 0 {
|
||||
return fmt.Errorf("bad audio format")
|
||||
}
|
||||
|
||||
c, err := pulseClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
// The reader hands out the buffer a slice at a time and ends the stream
|
||||
// with EndOfData — the library's own sentinel. io.EOF would work as an end
|
||||
// too, but it is recorded as the stream's error, and a message finishing
|
||||
// normally must not look like a fault in the log.
|
||||
pos := 0
|
||||
read := func(buf []byte) (int, error) {
|
||||
select {
|
||||
case <-stop:
|
||||
return 0, pulse.EndOfData
|
||||
default:
|
||||
}
|
||||
if pos >= len(pcm) {
|
||||
return 0, pulse.EndOfData
|
||||
}
|
||||
n := copy(buf, pcm[pos:])
|
||||
n -= n % frameBytes // never hand the server a partial frame
|
||||
if n == 0 {
|
||||
return 0, pulse.EndOfData
|
||||
}
|
||||
pos += n
|
||||
return n, nil
|
||||
}
|
||||
|
||||
opts := []pulse.PlaybackOption{
|
||||
pulse.PlaybackSampleRate(rate),
|
||||
pulse.PlaybackChannels(channelMap(ch)),
|
||||
pulse.PlaybackLatency(0.1),
|
||||
pulse.PlaybackMediaName("OpsLog playback"),
|
||||
}
|
||||
if deviceID != "" {
|
||||
sink, err := c.SinkByID(deviceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("no audio output %q: %w", deviceID, err)
|
||||
}
|
||||
opts = append(opts, pulse.PlaybackSink(sink))
|
||||
}
|
||||
st, err := c.NewPlayback(pulse.NewReader(readerFunc(read), format), opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open playback: %w", err)
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
st.Start()
|
||||
|
||||
// Drain blocks until the server has played everything queued. Waiting on it
|
||||
// in a goroutine keeps stop responsive: a voice message must cut off the
|
||||
// instant the operator unkeys, not at the end of the buffer.
|
||||
drained := make(chan struct{})
|
||||
go func() { st.Drain(); close(drained) }()
|
||||
select {
|
||||
case <-drained:
|
||||
case <-stop:
|
||||
st.Stop()
|
||||
}
|
||||
return st.Error()
|
||||
}
|
||||
|
||||
// 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 server's clock steady so
|
||||
// live monitor audio flows smoothly even when the source stalls briefly.
|
||||
func renderStream(deviceID string, rate, ch, bits int, stop <-chan struct{}, src *pcmRing) error {
|
||||
format, err := pulseFormat(bits)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
frameBytes := ch * bits / 8
|
||||
if frameBytes <= 0 || rate <= 0 || src == nil {
|
||||
return fmt.Errorf("bad audio format")
|
||||
}
|
||||
|
||||
c, err := pulseClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
// Never return 0 bytes without an error: the library's playback loop would
|
||||
// spin on it. A stalled source therefore yields silence, which is also the
|
||||
// behaviour that keeps the clock running.
|
||||
chunk := chunkFrames * frameBytes
|
||||
read := func(buf []byte) (int, error) {
|
||||
select {
|
||||
case <-stop:
|
||||
return 0, pulse.EndOfData
|
||||
default:
|
||||
}
|
||||
n := len(buf)
|
||||
if n > chunk {
|
||||
n = chunk
|
||||
}
|
||||
n -= n % frameBytes
|
||||
if n == 0 {
|
||||
n = frameBytes
|
||||
}
|
||||
got := copy(buf[:n], src.pull(n))
|
||||
for i := got; i < n; i++ {
|
||||
buf[i] = 0
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
opts := []pulse.PlaybackOption{
|
||||
pulse.PlaybackSampleRate(rate),
|
||||
pulse.PlaybackChannels(channelMap(ch)),
|
||||
pulse.PlaybackLatency(0.1),
|
||||
pulse.PlaybackMediaName("OpsLog monitor"),
|
||||
}
|
||||
if deviceID != "" {
|
||||
sink, err := c.SinkByID(deviceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("no audio output %q: %w", deviceID, err)
|
||||
}
|
||||
opts = append(opts, pulse.PlaybackSink(sink))
|
||||
}
|
||||
st, err := c.NewPlayback(pulse.NewReader(readerFunc(read), format), opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open monitor: %w", err)
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
st.Start()
|
||||
<-stop
|
||||
st.Stop()
|
||||
// Give the server a moment to notice the stream stopped before the client
|
||||
// socket goes away, so the last fragment is heard instead of clipped.
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
return st.Error()
|
||||
}
|
||||
|
||||
// pulseFormat maps a WAV bit depth onto the server's sample formats. 8 and 16
|
||||
// bit cover everything OpsLog produces or reads; anything else is refused by
|
||||
// name rather than played as noise.
|
||||
func pulseFormat(bits int) (byte, error) {
|
||||
switch bits {
|
||||
case 8:
|
||||
return proto.FormatUint8, nil
|
||||
case 16:
|
||||
return proto.FormatInt16LE, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("unsupported sample size %d-bit (8 or 16 expected)", bits)
|
||||
}
|
||||
}
|
||||
|
||||
// readerFunc adapts a read closure to io.Reader.
|
||||
type readerFunc func([]byte) (int, error)
|
||||
|
||||
func (f readerFunc) Read(p []byte) (int, error) { return f(p) }
|
||||
|
||||
var _ io.Reader = readerFunc(nil)
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package audio
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package audio
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package audio
|
||||
|
||||
import (
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package audio
|
||||
|
||||
import "sync"
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package audio
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import (
|
||||
@@ -9,8 +7,6 @@ import (
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// FlexRadio is one radio found by discovery.
|
||||
@@ -38,7 +34,7 @@ func DiscoverFlex(timeout time.Duration) ([]FlexRadio, error) {
|
||||
Control: func(_, _ string, c syscall.RawConn) error {
|
||||
var serr error
|
||||
_ = c.Control(func(fd uintptr) {
|
||||
serr = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1)
|
||||
serr = setSocketReuse(fd)
|
||||
})
|
||||
return serr
|
||||
},
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import (
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//go:build !windows
|
||||
|
||||
package cat
|
||||
|
||||
import "errors"
|
||||
|
||||
// OmniRig is COM automation against a Windows-only application, so off Windows
|
||||
// there is nothing to talk to. The backend still EXISTS here rather than being
|
||||
// compiled out of app.go, because a settings database is portable: an operator
|
||||
// who moves a profile from Windows to Linux keeps "omnirig" as their saved CAT
|
||||
// backend, and OpsLog must start and say why the rig is silent instead of
|
||||
// failing to build or panicking on a nil backend.
|
||||
//
|
||||
// The fix for those operators is a native backend (Icom, Yaesu, Kenwood/
|
||||
// Elecraft, Flex, TCI, Xiegu all speak to the radio directly) or Hamlib.
|
||||
type OmniRig struct{ CWLower bool }
|
||||
|
||||
var errOmniRigWindowsOnly = errors.New("OmniRig runs only on Windows — pick a native CAT backend (Icom, Yaesu, Kenwood/Elecraft, FlexRadio, TCI, Xiegu) in Settings ▸ CAT")
|
||||
|
||||
func NewOmniRig(rigNum int, forceVFO string, cwLower bool) *OmniRig {
|
||||
return &OmniRig{CWLower: cwLower}
|
||||
}
|
||||
|
||||
func (o *OmniRig) Name() string { return "omnirig" }
|
||||
func (o *OmniRig) Connect() error { return errOmniRigWindowsOnly }
|
||||
func (o *OmniRig) Disconnect() {}
|
||||
func (o *OmniRig) ReadState() (RigState, error) { return RigState{}, errOmniRigWindowsOnly }
|
||||
func (o *OmniRig) SetFrequency(hz int64) error { return errOmniRigWindowsOnly }
|
||||
func (o *OmniRig) SetMode(mode string) error { return errOmniRigWindowsOnly }
|
||||
func (o *OmniRig) SetPTT(on bool) error { return errOmniRigWindowsOnly }
|
||||
|
||||
// SetCWLower satisfies OmniRigController so the preference push at startup is a
|
||||
// no-op here rather than a "backend does not support this" error in the log.
|
||||
func (o *OmniRig) SetCWLower(on bool) { o.CWLower = on }
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import "testing"
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
//go:build !windows
|
||||
|
||||
package cat
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
// setSocketReuse is the Unix half of the Windows SO_REUSEADDR above. Linux
|
||||
// wants SO_REUSEPORT as well before two processes may share a bound UDP port;
|
||||
// it is not defined on every Unix, so a refusal there is ignored.
|
||||
func setSocketReuse(fd uintptr) error {
|
||||
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import "golang.org/x/sys/windows"
|
||||
|
||||
// setSocketReuse enables SO_REUSEADDR before bind, so discovery can listen on
|
||||
// :4992 while SmartSDR is already listening for the same radio broadcast.
|
||||
// Without it the second bind fails with WSAEADDRINUSE and the operator has to
|
||||
// type the radio's IP by hand.
|
||||
func setSocketReuse(fd uintptr) error {
|
||||
return windows.SetsockoptInt(windows.Handle(fd),
|
||||
windows.SOL_SOCKET, windows.SO_REUSEADDR, 1)
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
// TCI audio — receiving the radio's audio over the same WebSocket that carries
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import "fmt"
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
// The TCI control panel: what the radio already tells us, gathered up.
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import "testing"
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cat
|
||||
|
||||
import "testing"
|
||||
|
||||
@@ -69,7 +69,7 @@ func Configured(svc Service, cfg ExternalServices) error {
|
||||
return missing("Cloudlog / Wavelog", need...)
|
||||
}
|
||||
case ServiceLoTW:
|
||||
add(set(cfg.LoTW.TQSLPath), "the path to tqsl.exe")
|
||||
add(set(cfg.LoTW.TQSLPath), "the path to TQSL")
|
||||
add(set(cfg.LoTW.StationLocation), "the TQSL station location")
|
||||
if len(need) > 0 {
|
||||
return missing("LoTW", need...)
|
||||
|
||||
+2
-25
@@ -297,29 +297,6 @@ func ListStationLocations(stationDataPath string) ([]StationLocation, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// DefaultTQSLPath returns the usual tqsl.exe install path on Windows, or ""
|
||||
// if not found.
|
||||
func DefaultTQSLPath() string {
|
||||
for _, p := range []string{
|
||||
`C:\Program Files (x86)\TrustedQSL\tqsl.exe`,
|
||||
`C:\Program Files\TrustedQSL\tqsl.exe`,
|
||||
} {
|
||||
if fileExists(p) {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// DefaultStationDataPath returns TQSL's station_data location (%APPDATA%\
|
||||
// TrustedQSL\station_data on Windows), or "" if APPDATA isn't set.
|
||||
func DefaultStationDataPath() string {
|
||||
if appData := os.Getenv("APPDATA"); appData != "" {
|
||||
return filepath.Join(appData, "TrustedQSL", "station_data")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func fileExists(p string) bool {
|
||||
info, err := os.Stat(p)
|
||||
return err == nil && !info.IsDir()
|
||||
@@ -375,7 +352,7 @@ func UploadLoTW(ctx context.Context, cfg ServiceConfig, tempDir, adifRecord stri
|
||||
case tqsl == "":
|
||||
return UploadResult{}, fmt.Errorf("lotw: TQSL path not set")
|
||||
case !fileExists(tqsl):
|
||||
return UploadResult{}, fmt.Errorf("lotw: tqsl.exe not found at %q", tqsl)
|
||||
return UploadResult{}, fmt.Errorf("lotw: TQSL not found at %q", tqsl)
|
||||
case loc == "":
|
||||
return UploadResult{}, fmt.Errorf("lotw: station location not set")
|
||||
case strings.TrimSpace(adifRecord) == "":
|
||||
@@ -515,7 +492,7 @@ func TestLoTW(cfg ServiceConfig, stationDataPath string) (string, error) {
|
||||
tqsl := strings.TrimSpace(cfg.TQSLPath)
|
||||
loc := strings.TrimSpace(cfg.StationLocation)
|
||||
if tqsl == "" || !fileExists(tqsl) {
|
||||
return "", fmt.Errorf("lotw: tqsl.exe not found (set the TQSL path)")
|
||||
return "", fmt.Errorf("lotw: TQSL not found (set the TQSL path)")
|
||||
}
|
||||
if loc == "" {
|
||||
return "", fmt.Errorf("lotw: pick a station location")
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
//go:build !windows
|
||||
|
||||
package extsvc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// DefaultTQSLPath finds TrustedQSL, or returns "" so the operator can point at
|
||||
// it by hand.
|
||||
//
|
||||
// PATH is asked FIRST, unlike the Windows side where two fixed install folders
|
||||
// are the whole story. On Linux tqsl comes from the distribution's package
|
||||
// manager, a Flatpak or a self-built copy, and each puts it somewhere
|
||||
// different; whichever one the operator installed is the one their shell finds.
|
||||
// The fixed list below is only for a desktop session that started without a
|
||||
// useful PATH.
|
||||
func DefaultTQSLPath() string {
|
||||
if p, err := exec.LookPath("tqsl"); err == nil && fileExists(p) {
|
||||
return p
|
||||
}
|
||||
candidates := []string{
|
||||
"/usr/bin/tqsl",
|
||||
"/usr/local/bin/tqsl",
|
||||
"/var/lib/flatpak/exports/bin/org.arrl.tqsl",
|
||||
filepath.Join(os.Getenv("HOME"), ".local/share/flatpak/exports/bin/org.arrl.tqsl"),
|
||||
}
|
||||
if runtime.GOOS == "darwin" {
|
||||
candidates = append(candidates, "/Applications/TrustedQSL/tqsl.app/Contents/MacOS/tqsl")
|
||||
}
|
||||
for _, p := range candidates {
|
||||
if fileExists(p) {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// DefaultStationDataPath returns TQSL's station_data location.
|
||||
//
|
||||
// ~/.tqsl is where the Unix build of TrustedQSL keeps its configuration. A
|
||||
// Flatpak install redirects it into the sandbox
|
||||
// (~/.var/app/org.arrl.tqsl/data/tqsl), so that is tried too — an operator on a
|
||||
// Flatpak TQSL otherwise sees an empty station-location list with nothing to
|
||||
// explain it.
|
||||
func DefaultStationDataPath() string {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil || home == "" {
|
||||
return ""
|
||||
}
|
||||
for _, p := range []string{
|
||||
filepath.Join(home, ".tqsl", "station_data"),
|
||||
filepath.Join(home, ".var", "app", "org.arrl.tqsl", "data", "tqsl", "station_data"),
|
||||
} {
|
||||
if fileExists(p) {
|
||||
return p
|
||||
}
|
||||
}
|
||||
// Nothing found: name the ordinary location anyway. The settings field then
|
||||
// shows the path TQSL would create on its first run, which is a better
|
||||
// starting point for the operator than an empty box.
|
||||
return filepath.Join(home, ".tqsl", "station_data")
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//go:build windows
|
||||
|
||||
package extsvc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// DefaultTQSLPath returns the usual tqsl.exe install path, or "" if not found.
|
||||
func DefaultTQSLPath() string {
|
||||
for _, p := range []string{
|
||||
`C:\Program Files (x86)\TrustedQSL\tqsl.exe`,
|
||||
`C:\Program Files\TrustedQSL\tqsl.exe`,
|
||||
} {
|
||||
if fileExists(p) {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// DefaultStationDataPath returns TQSL's station_data location
|
||||
// (%APPDATA%\TrustedQSL\station_data), or "" if APPDATA isn't set.
|
||||
func DefaultStationDataPath() string {
|
||||
if appData := os.Getenv("APPDATA"); appData != "" {
|
||||
return filepath.Join(appData, "TrustedQSL", "station_data")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user