Commit e6e6838f 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/TTS-client.iml" filepath="$PROJECT_DIR$/.idea/TTS-client.iml" />
</modules>
</component>
</project>
\ No newline at end of file
File added
File added
module TTS-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/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 (
"encoding/json"
"flag"
"fmt"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"os"
"sync"
"time"
)
func main() {
var (
t = flag.String("f", "欢迎来到北科瑞声。", "The text that used to Synthesize audio")
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")
wg sync.WaitGroup
)
logrus.SetReportCaller(true)
flag.Parse()
d := time.Now().Format("2006-01-02-15-04")
_ = os.MkdirAll(fmt.Sprintf("./log/%v/", d), 0755)
logrus.SetReportCaller(true)
cNumChan := make(chan int, *c)
i := 0
for {
cNumChan <- 1
wg.Add(1)
go func(idx int, data string) {
defer func() {
wg.Done()
<-cNumChan
}()
if e := connect(data, idx, *a, d); e != nil {
logrus.Errorln(e)
}
return
}(i, *t)
i++
}
wg.Wait()
}
func connect(d string, id int, addr string, dateStr string) error {
file, err := os.OpenFile(fmt.Sprintf("./log/%v/%v.log", dateStr, 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(`{"singal": "start"}`)); err != nil {
logrus.Errorln(err)
return err
}
var data []byte
if _, data, err = conn.ReadMessage(); err != nil {
return err
}
logger.Println(string(data))
if err = conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf(`{"singal": "text", "text": "%v", "voice_name": "xiaomu", "streaming": %v, "sample_rate": %v, "speed": %v}`, d, true, 16000, float32(1.0)))); err != nil {
logrus.Errorln(err)
return err
}
parseData := struct {
Status string `json:"status"`
Audio string `json:"audio:"`
UttId string `json:"utt_id"`
}{}
for {
if err = conn.SetReadDeadline(time.Now().Add(2 * time.Second)); err != nil {
logrus.Errorln()
goto ERR
}
if _, data, err = conn.ReadMessage(); err != nil {
logrus.Errorln(err)
goto ERR
}
if err = json.Unmarshal(data, &parseData); err != nil {
logrus.Errorln(err)
goto ERR
}
logrus.Println(fmt.Sprintf("timestamp=%v", time.Now().UnixMilli()), "status="+parseData.Status, "utt_id="+parseData.UttId)
logger.Println(fmt.Sprintf("timestamp=%v", time.Now().UnixMilli()), "status="+parseData.Status, "utt_id="+parseData.UttId)
if parseData.Status == "end" {
goto ERR
}
}
ERR:
if err = conn.WriteMessage(websocket.TextMessage, []byte(`{"singal": "end"}`)); err != nil {
logrus.Errorln(err)
return err
}
if _, data, err = conn.ReadMessage(); err != nil {
return err
}
logger.Println(string(data))
_ = 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