Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将mockingbird统一到tts #131

Merged
merged 6 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/fumiama/sqlite3 v1.14.6
github.com/jinzhu/gorm v1.9.16
github.com/mroth/weightedrand v0.4.1
github.com/pkumza/numcn v1.0.0
github.com/shirou/gopsutil/v3 v3.21.12
github.com/sirupsen/logrus v1.8.1
github.com/tidwall/gjson v1.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkumza/numcn v1.0.0 h1:ZT5cf9IJkUZgRgEtCiNNykk0RwsrKXSTsvDHOwUTzgE=
github.com/pkumza/numcn v1.0.0/go.mod h1:QSeH+al9dWCd8di5HZM/ZqHqhZmUKfph572e9Ev/ETc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
Expand Down
16 changes: 8 additions & 8 deletions plugin_ai_reply/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
)

const (
serviceName = "aireply"
replyServiceName = "aireply"
)

var modes = [...]string{"青云客", "小爱"}
var replyModes = [...]string{"青云客", "小爱"}

func init() { // 插件主体
engine := control.Register(serviceName, order.AcquirePrio(), &control.Options{
engine := control.Register(replyServiceName, order.AcquirePrio(), &control.Options{
DisableOnDefault: false,
Help: "人工智能回复\n" +
"- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客 | 小爱]\n- ",
Expand Down Expand Up @@ -59,7 +59,7 @@ func setReplyMode(ctx *zero.Ctx, name string) error {
}
var ok bool
var index int64
for i, s := range modes {
for i, s := range replyModes {
if s == name {
ok = true
index = int64(i)
Expand All @@ -69,7 +69,7 @@ func setReplyMode(ctx *zero.Ctx, name string) error {
if !ok {
return errors.New("no such mode")
}
m, ok := control.Lookup(serviceName)
m, ok := control.Lookup(replyServiceName)
if !ok {
return errors.New("no such plugin")
}
Expand All @@ -81,11 +81,11 @@ func getReplyMode(ctx *zero.Ctx) (name string) {
if gid == 0 {
gid = -ctx.Event.UserID
}
m, ok := control.Lookup(serviceName)
m, ok := control.Lookup(replyServiceName)
if ok {
index := m.GetData(gid)
if int(index) < len(modes) {
return modes[index]
if int(index) < len(replyModes) {
return replyModes[index]
}
}
return "青云客"
Expand Down
108 changes: 103 additions & 5 deletions plugin_ai_reply/tts.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,125 @@
package aireply

import (
"errors"
"github.com/pkumza/numcn"
log "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"regexp"
"strconv"

"github.com/FloatTech/AnimeAPI/aireply"
"github.com/FloatTech/AnimeAPI/tts"
"github.com/FloatTech/AnimeAPI/tts/baidutts"
"github.com/FloatTech/AnimeAPI/tts/mockingbird"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"

"github.com/FloatTech/zbputils/control/order"
)

const (
ttsServiceName = "tts"
)

var (
reNumber = "(\\-|\\+)?\\d+(\\.\\d+)?"
t *ttsInstances
)

type ttsInstances struct {
m map[string]tts.TTS
l []string
}

func (t *ttsInstances) List() []string {
return t.l
}

func init() {
control.Register("mockingbird", order.AcquirePrio(), &control.Options{
t = &ttsInstances{
m: map[string]tts.TTS{
"百度女声": baidutts.NewBaiduTTS(0),
"百度男声": baidutts.NewBaiduTTS(1),
"百度度逍遥": baidutts.NewBaiduTTS(3),
"百度度丫丫": baidutts.NewBaiduTTS(4),
"拟声鸟阿梓": mockingbird.NewMockingBirdTTS(0),
"拟声鸟药水哥": mockingbird.NewMockingBirdTTS(1),
},
l: []string{"拟声鸟阿梓", "拟声鸟药水哥", "百度女声", "百度男声", "百度度逍遥", "百度度丫丫"},
}
engine := control.Register(ttsServiceName, order.AcquirePrio(), &control.Options{
DisableOnDefault: false,
Help: "拟声鸟\n- @Bot 任意文本(任意一句话回复)",
}).OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
Help: "语音回复(包括拟声鸟和百度)\n- @Bot 任意文本(任意一句话回复)\n- 设置语音模式拟声鸟阿梓 | 设置语音模式拟声鸟药水哥 | 设置语音模式百度女声 | 设置语音模式百度男声| 设置语音模式百度度逍遥 | 设置语音模式百度度丫丫",
})
engine.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
msg := ctx.ExtractPlainText()
r := aireply.NewAIReply(getReplyMode(ctx))
ctx.SendChain(message.Record(mockingbird.NewMockingBirdTTS(1).Speak(ctx.Event.UserID, func() string {
return r.TalkPlain(msg, zero.BotConfig.NickName[0])
tts := newTTS(getSoundMode(ctx))
ctx.SendChain(message.Record(tts.Speak(ctx.Event.UserID, func() string {
reply := r.TalkPlain(msg, zero.BotConfig.NickName[0])
re := regexp.MustCompile(reNumber)
reply = re.ReplaceAllStringFunc(reply, func(s string) string {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
log.Errorln("[tts]:", err)
return s
}
return numcn.EncodeFromFloat64(f)
})
log.Println("[tts]:", reply)
return reply
})))
})
engine.OnRegex(`^设置语音模式(.*)$`, ctxext.FirstValueInList(t)).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
param := ctx.State["regex_matched"].([]string)[1]
err := setSoundMode(ctx, param)
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err))
return
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("成功"))
})
}

// newTTS 语音简单工厂
func newTTS(name string) tts.TTS {
return t.m[name]
}

func setSoundMode(ctx *zero.Ctx, name string) error {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
var index int64
for i, s := range t.l {
if s == name {
index = int64(i)
break
}
}
m, ok := control.Lookup(ttsServiceName)
if !ok {
return errors.New("no such plugin")
}
return m.SetData(gid, index)
}

func getSoundMode(ctx *zero.Ctx) (name string) {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
m, ok := control.Lookup(ttsServiceName)
if ok {
index := m.GetData(gid)
if int(index) < len(t.l) {
return t.l[index]
}
}
return "拟声鸟阿梓"
}