Skip to content

Commit

Permalink
fix tts
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Dec 14, 2022
1 parent b6026ee commit e9e0498
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
52 changes: 20 additions & 32 deletions plugin/ai_reply/ai_tts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package aireply
import (
"errors"
"net/url"
"sync"

"github.com/RomiChan/syncx"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"

"github.com/FloatTech/AnimeAPI/aireply"
Expand Down Expand Up @@ -99,9 +100,8 @@ var ttsModes = func() []string {
}()

type ttsmode struct {
sync.Mutex `json:"-"`
APIKey string // APIKey is for genshin vits
mode map[int64]int64 `json:"-"` // mode grp index
APIKey string // APIKey is for genshin vits
mode syncx.Map[int64, int64] `json:"-"` // mode grp index
}

func list(list []string, num int) string {
Expand All @@ -119,44 +119,35 @@ func list(list []string, num int) string {

func newttsmode() *ttsmode {
t := &ttsmode{}
t.Lock()
defer t.Unlock()
m, ok := control.Lookup("tts")
t.mode = make(map[int64]int64, 2*len(genshin.SoundList))
t.mode[defaultttsindexkey] = 0
t.mode = syncx.Map[int64, int64]{}
t.mode.Store(defaultttsindexkey, 0)
if ok {
index := m.GetData(defaultttsindexkey)
msk := index & 0xff
if msk >= 0 && (msk < int64(len(genshin.SoundList)) || msk == baiduttsindex || msk == mockingbirdttsindex) {
t.mode[defaultttsindexkey] = index
t.mode.Store(defaultttsindexkey, index)
}
}
return t
}

func (t *ttsmode) getAPIKey(ctx *zero.Ctx) string {
if t.APIKey == "" {
t.Lock()
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
_ = m.Manager.GetExtra(gid, &t)
t.Unlock()
}
logrus.Debugln("[tts] get api key:", t.APIKey)
return url.QueryEscape(t.APIKey)
}

func (t *ttsmode) setAPIKey(m *ctrl.Control[*zero.Ctx], grp int64, key string) error {
t.Lock()
defer t.Unlock()
err := m.Manager.SetExtra(grp, &key)
if err != nil {
return err
}
t.APIKey = key
return nil
return m.Manager.SetExtra(grp, t)
}

func (t *ttsmode) setSoundMode(ctx *zero.Ctx, name string, baiduper, mockingsynt int) error {
Expand Down Expand Up @@ -186,9 +177,7 @@ func (t *ttsmode) setSoundMode(ctx *zero.Ctx, name string, baiduper, mockingsynt
}
}
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
t.Lock()
defer t.Unlock()
t.mode[gid] = index
t.mode.Store(gid, index)
return m.SetData(gid, (m.GetData(gid)&^0xffff00)|((index<<8)&0xff00)|((int64(baiduper)<<16)&0x0f0000)|((int64(mockingsynt)<<20)&0xf00000))
}

Expand All @@ -197,16 +186,14 @@ func (t *ttsmode) getSoundMode(ctx *zero.Ctx) (tts.TTS, error) {
if gid == 0 {
gid = -ctx.Event.UserID
}
t.Lock()
defer t.Unlock()
i, ok := t.mode[gid]
i, ok := t.mode.Load(gid)
if !ok {
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
i = m.GetData(gid) >> 8
}
m := i & 0xff
if m < 0 || (m >= int64(len(genshin.SoundList)) && m != baiduttsindex && m != mockingbirdttsindex) {
i = t.mode[defaultttsindexkey]
i, _ = t.mode.Load(defaultttsindexkey)
m = i & 0xff
}
mode := ttsModes[m]
Expand All @@ -222,8 +209,13 @@ func (t *ttsmode) getSoundMode(ctx *zero.Ctx) (tts.TTS, error) {
return nil, err
}
default: // 原神
ins = genshin.NewGenshin(int(m), t.getAPIKey(ctx))
ttsins[mode] = ins
k := t.getAPIKey(ctx)
if k != "" {
ins = genshin.NewGenshin(int(m), t.getAPIKey(ctx))
ttsins[mode] = ins
} else {
return nil, errors.New("no valid speaker")
}
}
}
return ins, nil
Expand All @@ -234,8 +226,6 @@ func (t *ttsmode) resetSoundMode(ctx *zero.Ctx) error {
if gid == 0 {
gid = -ctx.Event.UserID
}
t.Lock()
defer t.Unlock()
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
index := m.GetData(defaultttsindexkey)
return m.SetData(gid, (m.GetData(gid)&0xff)|((index&^0xff)<<8)) // 重置数据
Expand Down Expand Up @@ -263,12 +253,10 @@ func (t *ttsmode) setDefaultSoundMode(name string, baiduper, mockingsynt int) er
return errors.New("语音人物" + name + "未注册index")
}
}
t.Lock()
defer t.Unlock()
m, ok := control.Lookup("tts")
if !ok {
return errors.New("[tts] service not found")
}
t.mode[defaultttsindexkey] = index
t.mode.Store(defaultttsindexkey, index)
return m.SetData(defaultttsindexkey, (index&0xff)|((int64(baiduper)<<8)&0x0f00)|((int64(mockingsynt)<<12)&0xf000))
}
6 changes: 5 additions & 1 deletion plugin/ai_reply/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() { // 插件主体
"- 设置语音模式[原神人物/百度/拟声鸟] 数字(百度/拟声鸟模式)\n" +
"- 设置默认语音模式[原神人物/百度/拟声鸟] 数字(百度/拟声鸟模式)\n" +
"- 恢复成默认语音模式\n" +
"- 为群 xxx 设置原神语音 api key xxxxxx (key请加开发群获得)\n" +
"- 为群 xxx 设置原神语音 api key xxxxxx (key请加开发群获得, 群号不代表设置之后只能在该群使用, 而是设置后, 必须在该群触发过语音后, 其他位置才会正常)\n" +
"当前适用的原神人物含有以下:\n" + list(genshin.SoundList[:], 5),
})

Expand Down Expand Up @@ -196,23 +196,27 @@ func init() { // 插件主体
}
}
// 保存设置
logrus.Debugln("[tts] t.setSoundMode( ctx", param, n, n, ")")
err = t.setSoundMode(ctx, param, n, n)
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err))
return
}
if banner, ok := genshin.TestRecord[param]; ok {
logrus.Debugln("[tts] banner:", banner, "get sound mode...")
// 设置验证
speaker, err := t.getSoundMode(ctx)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
logrus.Debugln("[tts] got sound mode, speaking...")
rec, err := speaker.Speak(ctx.Event.UserID, func() string { return banner })
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
logrus.Debugln("[tts] sending...")
if id := ctx.SendChain(message.Record(rec).Add("cache", 0)); id.ID() == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("无法发送测试语音,请重试。"))
return
Expand Down

0 comments on commit e9e0498

Please sign in to comment.