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]>
97 lines
3.1 KiB
Go
97 lines
3.1 KiB
Go
//go:build windows
|
|
|
|
// Package audio drives Windows audio endpoints via WASAPI (through go-ole /
|
|
// go-wca) — pure Go, no CGO, the same COM stack OmniRig already uses. It
|
|
// powers the Digital Voice Keyer (record/play voice messages to the rig) and
|
|
// the QSO recorder (rolling-buffer capture saved as WAV).
|
|
package audio
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"sort"
|
|
|
|
"github.com/go-ole/go-ole"
|
|
"github.com/moutend/go-wca/pkg/wca"
|
|
)
|
|
|
|
// 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) }
|
|
|
|
// ListOutputDevices returns the active render endpoints — speakers and the
|
|
// soundcard output wired to the rig's mic/data input ("To Radio").
|
|
func ListOutputDevices() ([]Device, error) { return listEndpoints(wca.ERender) }
|
|
|
|
// listEndpoints enumerates active endpoints for a data-flow direction. COM is
|
|
// thread-affine, so we lock the OS thread and Co(Un)Initialize around the work
|
|
// — this is a one-shot call from a Wails binding, not a long-lived session.
|
|
func listEndpoints(flow uint32) (out []Device, err error) {
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
|
|
if e := ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED); e != nil {
|
|
// 0x1 = S_FALSE → already initialised on this thread, fine.
|
|
if oe, ok := e.(*ole.OleError); !ok || oe.Code() != 0x00000001 {
|
|
return nil, fmt.Errorf("CoInitializeEx: %w", e)
|
|
}
|
|
}
|
|
defer ole.CoUninitialize()
|
|
|
|
var mmde *wca.IMMDeviceEnumerator
|
|
if e := wca.CoCreateInstance(wca.CLSID_MMDeviceEnumerator, 0, wca.CLSCTX_ALL,
|
|
wca.IID_IMMDeviceEnumerator, &mmde); e != nil {
|
|
return nil, fmt.Errorf("create MMDeviceEnumerator: %w", e)
|
|
}
|
|
defer mmde.Release()
|
|
|
|
// Record the default endpoint id so the UI can flag it.
|
|
var defID string
|
|
var defDev *wca.IMMDevice
|
|
if e := mmde.GetDefaultAudioEndpoint(flow, wca.EConsole, &defDev); e == nil && defDev != nil {
|
|
_ = defDev.GetId(&defID)
|
|
defDev.Release()
|
|
}
|
|
|
|
var coll *wca.IMMDeviceCollection
|
|
if e := mmde.EnumAudioEndpoints(flow, wca.DEVICE_STATE_ACTIVE, &coll); e != nil {
|
|
return nil, fmt.Errorf("enum endpoints: %w", e)
|
|
}
|
|
defer coll.Release()
|
|
|
|
var count uint32
|
|
if e := coll.GetCount(&count); e != nil {
|
|
return nil, fmt.Errorf("count endpoints: %w", e)
|
|
}
|
|
for i := uint32(0); i < count; i++ {
|
|
var dev *wca.IMMDevice
|
|
if coll.Item(i, &dev) != nil || dev == nil {
|
|
continue
|
|
}
|
|
var id string
|
|
_ = dev.GetId(&id)
|
|
name := endpointName(dev, id)
|
|
dev.Release()
|
|
out = append(out, Device{ID: id, Name: name, Default: id != "" && id == defID})
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
|
return out, nil
|
|
}
|
|
|
|
// endpointName reads PKEY_Device_FriendlyName, falling back to the raw id.
|
|
func endpointName(dev *wca.IMMDevice, fallback string) string {
|
|
var ps *wca.IPropertyStore
|
|
if dev.OpenPropertyStore(wca.STGM_READ, &ps) != nil || ps == nil {
|
|
return fallback
|
|
}
|
|
defer ps.Release()
|
|
var pv wca.PROPVARIANT
|
|
if ps.GetValue(&wca.PKEY_Device_FriendlyName, &pv) != nil {
|
|
return fallback
|
|
}
|
|
if s := pv.String(); s != "" {
|
|
return s
|
|
}
|
|
return fallback
|
|
}
|