]> Sergey Matveev's repositories - vors.git/commitdiff
vors-vad automatically starts rec
authorSergey Matveev <stargrave@stargrave.org>
Tue, 16 Apr 2024 18:46:44 +0000 (21:46 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 16 Apr 2024 19:43:31 +0000 (22:43 +0300)
cmd/client/main.go
cmd/vad/main.go
doc/vad.texi
internal/audio.go
internal/cmd.go [new file with mode: 0644]

index 511c2c0e0d262afd4e6038ada55348cc8e3ce08a98b253bf5384116adf63d9da..44e5f63ee0a873ed6f6c3761249fc87c9a64d0cc88dc1a96cb9d585fbc2aaeec 100644 (file)
@@ -68,14 +68,6 @@ func parseSID(s string) byte {
        return byte(n)
 }
 
-func makeCmd(cmd string) *exec.Cmd {
-       args := strings.Fields(cmd)
-       if len(args) == 1 {
-               return exec.Command(args[0])
-       }
-       return exec.Command(args[0], args[1:]...)
-}
-
 func incr(data []byte) {
        for i := len(data) - 1; i >= 0; i-- {
                data[i]++
@@ -86,14 +78,12 @@ func incr(data []byte) {
        panic("overflow")
 }
 
-const soxParams = "--no-show-progress --buffer 1920 --channels 1 --endian little --encoding signed --rate 48000 --bits 16 --type raw -"
-
 func main() {
        srvAddr := flag.String("srv", "vors.home.arpa:"+strconv.Itoa(vors.DefaultPort),
                "host:TCP/UDP port to connect to")
        srvPubB64 := flag.String("pub", "", "server's public key, Base64")
-       recCmd := flag.String("rec", "rec "+soxParams, "rec command")
-       playCmd := flag.String("play", "play "+soxParams, "play command")
+       recCmd := flag.String("rec", "rec "+vors.SoxParams, "rec command")
+       playCmd := flag.String("play", "play "+vors.SoxParams, "play command")
        vadRaw := flag.Uint("vad", 0, "VAD threshold")
        passwd := flag.String("passwd", "", "protected room's password")
        muteToggle := flag.String("mute-toggle", "", "path to FIFO to toggle mute")
@@ -143,7 +133,7 @@ func main() {
        opusEnc := newOpusEnc()
        var mic io.ReadCloser
        if *recCmd != "" {
-               cmd := makeCmd(*recCmd)
+               cmd := vors.MakeCmd(*recCmd)
                mic, err = cmd.StdoutPipe()
                if err != nil {
                        log.Fatal(err)
@@ -377,7 +367,7 @@ func main() {
                                        playerTx := make(chan []byte, 5)
                                        var cmd *exec.Cmd
                                        if *playCmd != "" {
-                                               cmd = makeCmd(*playCmd)
+                                               cmd = vors.MakeCmd(*playCmd)
                                                player, err = cmd.StdinPipe()
                                                if err != nil {
                                                        log.Fatal(err)
index 20b117091362536f9578520994f9dc623dcf1c16f214ac434e1fe92528e02aa6..c207d79c393e80fd68afb1d164856dc0d956a46f875a2a2b697bcc4f75b6cd1d 100644 (file)
@@ -19,28 +19,46 @@ import (
        "flag"
        "fmt"
        "io"
+       "log"
        "os"
        "strconv"
 
        vors "go.stargrave.org/vors/internal"
 )
 
+func usage() {
+       fmt.Fprintf(os.Stderr, "Usage: vors-vad [-rec ...] THRESHOLD\n")
+       flag.PrintDefaults()
+}
+
 func main() {
+       recCmd := flag.String("rec", "rec "+vors.SoxParams, "rec command")
+       flag.Usage = usage
        flag.Parse()
+       log.SetFlags(log.Lmicroseconds | log.Lshortfile)
        if len(flag.Args()) != 1 {
-               fmt.Fprintf(os.Stderr, "Usage: rec | vors-vad THRES\n")
+               usage()
                os.Exit(1)
        }
        thres, err := strconv.ParseUint(flag.Arg(0), 10, 64)
        if err != nil {
-               panic(err)
+               log.Fatal(err)
        }
        buf := make([]byte, 2*vors.FrameLen)
        pcm := make([]int16, vors.FrameLen)
+       cmd := vors.MakeCmd(*recCmd)
+       mic, err := cmd.StdoutPipe()
+       if err != nil {
+               log.Fatal(err)
+       }
+       err = cmd.Start()
+       if err != nil {
+               log.Fatal(err)
+       }
        var rms uint64
        var i int
        for {
-               _, err = io.ReadFull(os.Stdin, buf)
+               _, err = io.ReadFull(mic, buf)
                if err != nil {
                        panic(err)
                }
@@ -49,7 +67,7 @@ func main() {
                }
                rms = vors.RMS(pcm)
                if rms > thres {
-                       println(rms)
+                       fmt.Println(rms)
                }
        }
 }
index 14719bda8a3450bbf81b00be12d73bd27f8fbdacca3a5d77432fb3fffe0111e5..6a9f156cedabaac1b8c2d7c3d101221621196ed34173ae4ae811c97039dda3c6 100644 (file)
@@ -2,19 +2,18 @@
 @unnumbered VAD
 
 Voice Activity Detection is off by default. You can use the
-@command{vors-vad THRES} command with feeding audio record in its stdin.
-It will print calculated sound RMS value if it exceeds the
+@command{vors-vad [-rec ...] THRES} command with feeding audio record in
+its stdin. It will print calculated sound RMS value if it exceeds the
 @option{THRES} threshold. So you can manually try various @option{THRES}
 values to see when it is comfortable to detect your voice. Pass the
-desired @option{THRES} value to @option{-vad} option of
-@command{vors-client}.
+desired @option{THRES} value to @option{-vad} option of @command{vors-client}.
 
 @example
-$ rec [...] | vors-vad 100
+$ vors-vad 100
 [talk and see if threshold is low/high enough]
 [it is too sensible, let's try higher one]
 
-$ rec [...] | vors-vad 200
+$ vors-vad 200
 [perfect!]
 
 $ vors-client -vad 200 [...]
index fc0a266f1f5fccb2d10dbc52c976fa75b8fb53dc3e3f3fcbcff7aabea3961fda..a27386f66c4d7461d87c15ffaf1f47d0d669c2053d0b4ab3699373857420896a 100644 (file)
@@ -6,4 +6,6 @@ const (
        Bitrate  = 24000
        FrameLen = FrameMs * Rate / 1000
        MaxLost  = 32
+
+       SoxParams = "--no-show-progress --buffer 1920 --channels 1 --endian little --encoding signed --rate 48000 --bits 16 --type raw -"
 )
diff --git a/internal/cmd.go b/internal/cmd.go
new file mode 100644 (file)
index 0000000..0e31dcd
--- /dev/null
@@ -0,0 +1,14 @@
+package internal
+
+import (
+       "os/exec"
+       "strings"
+)
+
+func MakeCmd(cmd string) *exec.Cmd {
+       args := strings.Fields(cmd)
+       if len(args) == 1 {
+               return exec.Command(args[0])
+       }
+       return exec.Command(args[0], args[1:]...)
+}