Skip to content

Commit

Permalink
✨ 添加文静合成器 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
guohuiyuan authored May 10, 2022
1 parent b9aee01 commit 693d870
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
data
data
tts/mockingbird/data
.idea
31 changes: 24 additions & 7 deletions tts/mockingbird/mocking_bird.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mockingbird

import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -24,6 +25,7 @@ const (
dbpath = "data/MockingBird/"
cachePath = dbpath + "cache/"
azfile = dbpath + "az.wav"
wjfile = dbpath + "wj.wav"
ysgfile = dbpath + "ysg.wav"
baseURL = "http://aaquatri.com/sound"
synthesizersURL = baseURL + "/api/synthesizers/"
Expand All @@ -33,8 +35,8 @@ const (

var (
vocoderList = []string{"WaveRNN", "HifiGAN"}
mockingbirdModes = map[int]string{0: "阿梓", 1: "药水哥"}
exampleFileMap = map[int]string{0: azfile, 1: ysgfile}
mockingbirdModes = map[int]string{0: "阿梓", 1: "文静", 2: "药水哥"}
exampleFileMap = map[int]string{0: azfile, 1: wjfile, 2: ysgfile}
)

// MockingBirdTTS 类
Expand All @@ -61,6 +63,11 @@ func NewMockingBirdTTS(synt int) (*MockingBirdTTS, error) {
return nil, err
}
case 1:
_, err := file.GetLazyData(wjfile, true)
if err != nil {
return nil, err
}
case 2:
_, err := file.GetLazyData(ysgfile, true)
if err != nil {
return nil, err
Expand All @@ -70,7 +77,7 @@ func NewMockingBirdTTS(synt int) (*MockingBirdTTS, error) {
}

// Speak 返回音频本地路径
func (tts *MockingBirdTTS) Speak(uid int64, text func() string) string {
func (tts *MockingBirdTTS) Speak(uid int64, text func() string) (fileName string, err error) {
// 异步
rch := make(chan string, 1)
sch := make(chan string, 1)
Expand All @@ -82,9 +89,13 @@ func (tts *MockingBirdTTS) Speak(uid int64, text func() string) string {
go func() {
sch <- tts.getSyntPath()
}()
fileName := tts.getWav(<-rch, <-sch, vocoderList[tts.vocoder], uid)
fileName, err = tts.getWav(<-rch, <-sch, vocoderList[tts.vocoder], uid)
if err != nil {
return
}
fileName = "file:///" + file.BOTPATH + "/" + cachePath + fileName
// 回复
return "file:///" + file.BOTPATH + "/" + cachePath + fileName
return
}

func (tts *MockingBirdTTS) getSyntPath() (syntPath string) {
Expand All @@ -96,7 +107,11 @@ func (tts *MockingBirdTTS) getSyntPath() (syntPath string) {
return
}

func (tts *MockingBirdTTS) getWav(text, syntPath, vocoder string, uid int64) (fileName string) {
func (tts *MockingBirdTTS) getWav(text, syntPath, vocoder string, uid int64) (fileName string, err error) {
if syntPath == "" {
err = errors.New("获取合成器失败")
return
}
fileName = strconv.FormatInt(uid, 10) + time.Now().Format("20060102150405") + "_mockingbird.wav"
var b bytes.Buffer
w := multipart.NewWriter(&b)
Expand Down Expand Up @@ -131,7 +146,7 @@ func (tts *MockingBirdTTS) getWav(text, syntPath, vocoder string, uid int64) (fi
if _, err = fw.Write([]byte(vocoder)); err != nil {
log.Errorln("[mockingbird]:", err)
}
w.Close()
_ = w.Close()
// Now that you have a form, you can submit it to your handler.
req, err := http.NewRequest("POST", synthesizeURL, &b)
if err != nil {
Expand All @@ -149,6 +164,8 @@ func (tts *MockingBirdTTS) getWav(text, syntPath, vocoder string, uid int64) (fi
// Check the response
if res.StatusCode != http.StatusOK {
log.Errorf("[mockingbird]bad status: %s", res.Status)
err = errors.New("bad status:" + res.Status)
return
}
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
Expand Down
25 changes: 16 additions & 9 deletions tts/mockingbird/mocking_bird_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ import (
)

func TestNewMockingBirdTTS(t *testing.T) {
tts, err := NewMockingBirdTTS(0)
//tts, err := NewMockingBirdTTS(0)
//if err != nil {
// t.Fatal(err)
//}
//fmt.Println(tts.Speak(int64(1), func() string {
// return "我爱你"
//}))
tts, err := NewMockingBirdTTS(1)
if err != nil {
t.Fatal(err)
}
fmt.Println(tts.Speak(int64(1), func() string {
return "我爱你"
}))
tts, err = NewMockingBirdTTS(1)
if err != nil {
t.Fatal(err)
}
fmt.Println(tts.Speak(int64(1), func() string {
return "我爱你"
return "大家要好好吃饭"
}))
//tts, err := NewMockingBirdTTS(2)
//if err != nil {
// t.Fatal(err)
//}
//fmt.Println(tts.Speak(int64(1), func() string {
// return "我爱你"
//}))
}

0 comments on commit 693d870

Please sign in to comment.