feat(icom): phase 5 — the voice keyer plays to the radio over the LAN
The audio session opens its TX side alongside RX (txenable in the conninfo, 16 kHz both ways), the stream gains a paced sender — 320 samples every 20 ms, the frame mirroring what the rig itself sends on this socket, IDs swapped — and the network Icom takes the same NetworkPlayer slot a TCI radio does: pick 'Radio (network audio)' as the To-radio device and the voice keyer needs no cable and no virtual sound card. PTT brackets the message through the same code as every other audio path.
This commit is contained in:
@@ -22,6 +22,7 @@ package cat
|
||||
// audio stream is opt-in and entirely separate from control/CI-V.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -236,3 +237,58 @@ func (a *icomAudio) sendRetransmitReq() {
|
||||
_, _ = a.conn.Write(b)
|
||||
}
|
||||
}
|
||||
|
||||
// PlayTX sends one already-decoded message out over the audio session, paced
|
||||
// at the stream's own 20 ms / 320-sample cadence, and returns when the last
|
||||
// packet has gone or stop is closed.
|
||||
//
|
||||
// The frame mirrors what the rig itself sends on this socket byte for byte —
|
||||
// ident 0x81 0x01 (LPCM mono 16-bit), a big-endian send sequence at 0x12, the
|
||||
// payload length at 0x14, PCM from 0x18 — with the IDs swapped for direction.
|
||||
// PTT is the caller's business, exactly as on the TCI and sound-card paths.
|
||||
func (a *icomAudio) PlayTX(pcm []byte, rate, ch, bits int, stop <-chan struct{}) error {
|
||||
mono := decodeToMono(pcm, ch, bits)
|
||||
if len(mono) == 0 {
|
||||
return fmt.Errorf("the message is empty")
|
||||
}
|
||||
if rate > 0 && rate != 16000 {
|
||||
mono = resampleLinear(mono, rate, 16000)
|
||||
}
|
||||
const frame = 320 // samples per packet: 20 ms at 16 kHz, the rig's own cadence
|
||||
tick := time.NewTicker(20 * time.Millisecond)
|
||||
defer tick.Stop()
|
||||
var outerSeq uint16 = 1
|
||||
var sendSeq uint16
|
||||
for pos := 0; pos < len(mono); pos += frame {
|
||||
select {
|
||||
case <-stop:
|
||||
return nil
|
||||
case <-a.done:
|
||||
return fmt.Errorf("the audio stream closed mid-message")
|
||||
case <-tick.C:
|
||||
}
|
||||
pkt := make([]byte, 0x18+frame*2)
|
||||
icnLE.PutUint32(pkt[0:], uint32(len(pkt)))
|
||||
icnLE.PutUint16(pkt[6:], outerSeq)
|
||||
icnLE.PutUint32(pkt[8:], a.aID)
|
||||
icnLE.PutUint32(pkt[12:], a.aRemote)
|
||||
pkt[0x10], pkt[0x11] = 0x81, 0x01
|
||||
icnBE.PutUint16(pkt[0x12:], sendSeq)
|
||||
icnBE.PutUint32(pkt[0x14:], frame*2)
|
||||
for i := 0; i < frame && pos+i < len(mono); i++ {
|
||||
v := mono[pos+i] * 32767
|
||||
if v > 32767 {
|
||||
v = 32767
|
||||
} else if v < -32768 {
|
||||
v = -32768
|
||||
}
|
||||
icnLE.PutUint16(pkt[0x18+i*2:], uint16(int16(v)))
|
||||
}
|
||||
if _, err := a.conn.Write(pkt); err != nil {
|
||||
return fmt.Errorf("sending audio to the rig: %w", err)
|
||||
}
|
||||
outerSeq++
|
||||
sendSeq++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user