7 Commits

Author SHA1 Message Date
e36af2c059 add 0.2.1 to changelog 2024-11-07 19:39:03 +00:00
5a5a6fa893 fix pointer 2024-11-07 19:36:56 +00:00
be11239d39 header now uses reusable buffer 2024-11-07 19:34:44 +00:00
d72c6a2d17 rename client struct to udpConn 2024-11-05 18:35:39 +00:00
c063feb919 fix default config.toml dir in README 2024-11-04 14:37:32 +00:00
ae170ca572 move indexOf into the vbantxt package.
improve the warning message on invalid bps value
2024-11-03 16:15:05 +00:00
7a844e3624 add note about functional options to README 2024-11-03 15:54:34 +00:00
9 changed files with 71 additions and 61 deletions

View File

@@ -11,12 +11,19 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
- [x]
# [0.2.1] - 2024-11-07
### Fixed
- {packet}.header() now uses a reusable buffer.
# [0.2.0] - 2024-10-27
### Added
- `config` flag (shorthand `C`), you may now specify a custom config directory. It defaults to `home directory / .config / vbantxt_cli /`.
- please note, the default directory has changed from v0.1.0
- Functional options `WithRateLimit` and `WithBPSOpt` and `WithChannel` added. Use them to configure the vbantxt client. See the [included vbantxt cli][vbantxt-cli] for an example of usage.
### Changed
@@ -41,3 +48,5 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
- Initial release, package implements VBAN PROTOCOL TXT with a basic CLI for configuring options.
- Ability to load configuration settings from a config.toml.
[vbantxt-cli]: https://github.com/onyx-and-iris/vbantxt/blob/main/cmd/vbantxt/main.go

View File

@@ -41,13 +41,13 @@ func main() {
streamname string = "onyx"
)
vbantxtClient, err := vbantxt.New(host, port, streamname)
client, err := vbantxt.New(host, port, streamname)
if err != nil {
log.Fatal(err)
}
defer vbantxtClient.Close()
defer client.Close()
err = vbantxtClient.Send("strip[0].mute=0")
err = client.Send("strip[0].mute=0")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s", err)
os.Exit(1)
@@ -60,18 +60,18 @@ func main() {
Pass `host`, `port` and `streamname` as flags, for example:
```
vbantxt-cli -h="gamepc.local" -p=6980 -s=Command1 "strip[0].mute=1 strip[1].mono=1"
vbantxt -h="gamepc.local" -p=6980 -s=Command1 "strip[0].mute=1 strip[1].mono=1"
```
You may also store them in a `config.toml` located in `home directory / .config / vbantxt_cli /`
You may also store them in a `config.toml` located in `home directory / .config / vbantxt /`
A valid `config.toml` might look like this:
```toml
[connection]
Host="gamepc.local"
Port=6980
Streamname="Command1"
host="gamepc.local"
port=6980
streamname="Command1"
```
- `host` defaults to "localhost"
@@ -88,7 +88,7 @@ The vbantxt-cli utility accepts a single string request or an array of string re
For example, in Windows with Powershell you could:
`vbantxt-cli $(Get-Content .\script.txt)`
`vbantxt $(Get-Content .\script.txt)`
Or with Bash:
@@ -109,7 +109,7 @@ bus[3].eq.On=0
Sending commands to VB-Audio Matrix is also possible, for example:
```
vbantxt-cli -s=streamname "Point(ASIO128.IN[2],ASIO128.OUT[1]).dBGain = -8"
vbantxt -s=streamname "Point(ASIO128.IN[2],ASIO128.OUT[1]).dBGain = -8"
```
---

View File

@@ -82,12 +82,12 @@ func main() {
}
}
func createClient(host string, port int, streamname string, bps int, channel, ratelimit int) (*vbantxt.VbanTxt, error) {
func createClient(host string, port int, streamname string, bps, channel, ratelimit int) (*vbantxt.VbanTxt, error) {
client, err := vbantxt.New(
host,
port,
streamname,
vbantxt.WithBPSOpt(indexOf(vbantxt.BpsOpts, bps)),
vbantxt.WithBPSOpt(bps),
vbantxt.WithChannel(channel),
vbantxt.WithRateLimit(time.Duration(ratelimit)*time.Millisecond))
if err != nil {

View File

@@ -15,12 +15,3 @@ func flagsPassed(flags []string) bool {
})
return found
}
func indexOf[T comparable](collection []T, e T) int {
for i, x := range collection {
if x == e {
return i
}
}
return -1
}

View File

@@ -16,18 +16,19 @@ func WithRateLimit(ratelimit time.Duration) Option {
}
}
// WithBPSOpt is a functional option to set the bps index for {VbanTx}.{Packet}.bpsIndex
func WithBPSOpt(bpsIndex int) Option {
// WithBPSOpt is a functional option to set the bps index for {VbanTxt}.packet
func WithBPSOpt(bps int) Option {
return func(vt *VbanTxt) {
if bpsIndex < 0 || bpsIndex >= len(BpsOpts) {
log.Warnf("invalid bpsIndex %d, defaulting to 0", bpsIndex)
bpsIndex := indexOf(BpsOpts, bps)
if bpsIndex == -1 {
log.Warnf("invalid bps value %d, expected one of %v, defaulting to 0", bps, BpsOpts)
return
}
vt.packet.bpsIndex = bpsIndex
}
}
// WithChannel is a functional option to set the bps index for {VbanTx}.{Packet}.channel
// WithChannel is a functional option to set the channel for {VbanTxt}.packet
func WithChannel(channel int) Option {
return func(vt *VbanTxt) {
vt.packet.channel = channel

View File

@@ -1,6 +1,7 @@
package vbantxt
import (
"bytes"
"encoding/binary"
log "github.com/sirupsen/logrus"
@@ -17,19 +18,24 @@ var BpsOpts = []int{0, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200,
1000000, 1500000, 2000000, 3000000}
type packet struct {
name string
streamname []byte
bpsIndex int
channel int
framecounter []byte
hbuf *bytes.Buffer
}
// newPacket returns a packet struct with default values, framecounter at 0.
func newPacket(streamname string) packet {
streamnameBuf := make([]byte, streamNameSz)
copy(streamnameBuf, streamname)
return packet{
name: streamname,
streamname: streamnameBuf,
bpsIndex: 0,
channel: 0,
framecounter: make([]byte, 4),
hbuf: bytes.NewBuffer(make([]byte, headerSz)),
}
}
@@ -43,24 +49,17 @@ func (p *packet) nbc() byte {
return byte(p.channel)
}
// streamname defines the stream name of the text request
func (p *packet) streamname() []byte {
b := make([]byte, streamNameSz)
copy(b, p.name)
return b
}
// header returns a fully formed packet header
func (p *packet) header() []byte {
h := make([]byte, 0, headerSz)
h = append(h, []byte("VBAN")...)
h = append(h, p.sr())
h = append(h, byte(0))
h = append(h, p.nbc())
h = append(h, byte(0x10))
h = append(h, p.streamname()...)
h = append(h, p.framecounter...)
return h
p.hbuf.Reset()
p.hbuf.WriteString("VBAN")
p.hbuf.WriteByte(p.sr())
p.hbuf.WriteByte(byte(0))
p.hbuf.WriteByte(p.nbc())
p.hbuf.WriteByte(byte(0x10))
p.hbuf.Write(p.streamname)
p.hbuf.Write(p.framecounter)
return p.hbuf.Bytes()
}
// bumpFrameCounter increments the frame counter by 1

View File

@@ -7,40 +7,40 @@ import (
log "github.com/sirupsen/logrus"
)
// client represents the UDP client
type client struct {
// udpConn represents the UDP client
type udpConn struct {
conn *net.UDPConn
}
// NewClient returns a UDP client
func newClient(host string, port int) (client, error) {
// newUDPConn returns a UDP client
func newUDPConn(host string, port int) (udpConn, error) {
udpAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", host, port))
if err != nil {
return client{}, err
return udpConn{}, err
}
conn, err := net.DialUDP("udp4", nil, udpAddr)
if err != nil {
return client{}, err
return udpConn{}, err
}
log.Infof("Outgoing address %s", conn.RemoteAddr())
return client{conn: conn}, nil
return udpConn{conn: conn}, nil
}
// Write implements the io.WriteCloser interface
func (c client) Write(buf []byte) (int, error) {
n, err := c.conn.Write(buf)
func (u udpConn) Write(buf []byte) (int, error) {
n, err := u.conn.Write(buf)
if err != nil {
return 0, err
}
log.Debugf("Sending '%s' to: %s", string(buf), c.conn.RemoteAddr())
log.Debugf("Sending '%s' to: %s", string(buf), u.conn.RemoteAddr())
return n, nil
}
// Close implements the io.WriteCloser interface
func (c client) Close() error {
err := c.conn.Close()
func (u udpConn) Close() error {
err := u.conn.Close()
if err != nil {
return err
}

10
util.go Normal file
View File

@@ -0,0 +1,10 @@
package vbantxt
func indexOf[T comparable](collection []T, e T) int {
for i, x := range collection {
if x == e {
return i
}
}
return -1
}

View File

@@ -8,7 +8,7 @@ import (
// VbanTxt is used to send VBAN-TXT requests to a distant Voicemeeter/Matrix.
type VbanTxt struct {
client io.WriteCloser
udpConn io.WriteCloser
packet packet
ratelimit time.Duration
}
@@ -16,13 +16,13 @@ type VbanTxt struct {
// New constructs a fully formed VbanTxt instance. This is the package's entry point.
// It sets default values for it's fields and then runs the option functions.
func New(host string, port int, streamname string, options ...Option) (*VbanTxt, error) {
client, err := newClient(host, port)
udpConn, err := newUDPConn(host, port)
if err != nil {
return nil, fmt.Errorf("error creating UDP client for (%s:%d): %w", host, port, err)
}
vt := &VbanTxt{
client: client,
udpConn: udpConn,
packet: newPacket(streamname),
ratelimit: time.Duration(20) * time.Millisecond,
}
@@ -37,7 +37,7 @@ func New(host string, port int, streamname string, options ...Option) (*VbanTxt,
// Send is resonsible for firing each VBAN-TXT request.
// It waits for {vt.ratelimit} time before returning.
func (vt VbanTxt) Send(cmd string) error {
_, err := vt.client.Write(append(vt.packet.header(), []byte(cmd)...))
_, err := vt.udpConn.Write(append(vt.packet.header(), []byte(cmd)...))
if err != nil {
return fmt.Errorf("error sending command (%s): %w", cmd, err)
}
@@ -51,7 +51,7 @@ func (vt VbanTxt) Send(cmd string) error {
// Close is responsible for closing the UDP Client connection
func (vt VbanTxt) Close() error {
err := vt.client.Close()
err := vt.udpConn.Close()
if err != nil {
return fmt.Errorf("error attempting to close UDP Client: %w", err)
}