From: Sergey Matveev Date: Tue, 16 Apr 2024 18:46:44 +0000 (+0300) Subject: vors-vad automatically starts rec X-Git-Tag: v2.3.0~3 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=861d36a0e0e561b7162ab49320ba3530ad338171c649f8074b4235b196116f54;p=vors.git vors-vad automatically starts rec --- diff --git a/cmd/client/main.go b/cmd/client/main.go index 511c2c0..44e5f63 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -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) diff --git a/cmd/vad/main.go b/cmd/vad/main.go index 20b1170..c207d79 100644 --- a/cmd/vad/main.go +++ b/cmd/vad/main.go @@ -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) } } } diff --git a/doc/vad.texi b/doc/vad.texi index 14719bd..6a9f156 100644 --- a/doc/vad.texi +++ b/doc/vad.texi @@ -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 [...] diff --git a/internal/audio.go b/internal/audio.go index fc0a266..a27386f 100644 --- a/internal/audio.go +++ b/internal/audio.go @@ -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 index 0000000..0e31dcd --- /dev/null +++ b/internal/cmd.go @@ -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:]...) +}