Commit 47f37d0d by zhengcheng.wang

Initial commit

parents
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/engine-client.iml" filepath="$PROJECT_DIR$/.idea/engine-client.iml" />
</modules>
</component>
</project>
\ No newline at end of file
File added
module engine-client
go 1.21.3
require (
github.com/gorilla/websocket v1.5.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
)
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
package main
import (
"flag"
"fmt"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"os"
"sync"
"time"
)
func main() {
var (
f = flag.String("f", "test.wav", "The address where the audio file is located")
c = flag.Int("c", 1, "Number of concurrent channels")
a = flag.String("a", "ws://127.0.0.1:20086", "The connection address of the engine service")
lock sync.RWMutex
wg sync.WaitGroup
subContainer []chan []byte
)
flag.Parse()
_ = os.MkdirAll(fmt.Sprintf("./log/%v/", time.Now().Format("2006-01-02-15-04")), 0755)
logrus.SetReportCaller(true)
file, err := os.OpenFile(*f, os.O_RDONLY, os.ModePerm|os.ModeDir)
if err != nil {
logrus.Fatalln(err)
}
defer func() {
_ = file.Close()
}()
go func() {
var (
b = make([]byte, 3200)
n = 0
e error
w sync.WaitGroup
)
time.Sleep(1 * time.Second)
for {
if n, e = file.Read(b); e != nil {
goto ERR
//_ = file.Close()
//file, err = os.OpenFile(*f, os.O_RDONLY, os.ModePerm|os.ModeDir)
//if err != nil {
// logrus.Fatalln(err)
//}
//continue
}
lock.RLock()
for _, ch := range subContainer {
w.Add(1)
go func(c chan []byte, d []byte) {
defer w.Done()
select {
case c <- d:
}
return
}(ch, b[:n])
}
lock.RUnlock()
time.Sleep(100 * time.Millisecond)
}
ERR:
w.Wait()
lock.Lock()
for _, ch := range subContainer {
close(ch)
}
lock.Unlock()
return
}()
wg.Add(*c)
for i := 0; i < *c; i++ {
lock.Lock()
ch := make(chan []byte)
subContainer = append(subContainer, ch)
lock.Unlock()
go func(idx int, c chan []byte) {
defer wg.Done()
if e := connect(c, idx, *a); e != nil {
logrus.Errorln(e)
}
return
}(i, ch)
}
wg.Wait()
}
func connect(c chan []byte, id int, addr string) error {
file, err := os.OpenFile(fmt.Sprintf("./log/%v/%v.log", time.Now().Format("2006-01-02-15-04"), id), os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755)
if err != nil {
logrus.Errorln(err)
return err
}
defer func() {
_ = file.Close()
}()
logger := logrus.New()
logger.SetOutput(file)
conn, _, err := websocket.DefaultDialer.Dial(addr, nil)
if err != nil {
logrus.Errorln(addr, err)
return err
}
if err = conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf(`{"signal": "start", "continuous_decoding": %v, "nbest": 10, "enable_vad": %v, "chunk_size": 12, "two_pass": %v, "context_update":%v,"context_score": 5.0}`, true, true, true, true))); err != nil {
logrus.Errorln(err)
return err
}
go func() {
var (
b []byte
)
for b = range c {
if e := conn.WriteMessage(websocket.BinaryMessage, b); e != nil {
goto ERR
}
}
if e := conn.WriteMessage(websocket.TextMessage, []byte(`{"signal" : "end"}`)); e != nil {
goto ERR
}
time.Sleep(1 * time.Second)
ERR:
_ = conn.Close()
return
}()
var data []byte
for {
if _, data, err = conn.ReadMessage(); err != nil {
goto ERR
}
logger.Println(string(data))
}
ERR:
_ = conn.Close()
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment