From e9e0498b34305c8b6f7ec48d1bc3672ac3818c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 14 Dec 2022 20:26:37 +0800 Subject: [PATCH] fix tts --- plugin/ai_reply/ai_tts.go | 52 +++++++++++++++------------------------ plugin/ai_reply/main.go | 6 ++++- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/plugin/ai_reply/ai_tts.go b/plugin/ai_reply/ai_tts.go index 0633e76a..3f6bf950 100644 --- a/plugin/ai_reply/ai_tts.go +++ b/plugin/ai_reply/ai_tts.go @@ -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" @@ -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 { @@ -119,16 +119,14 @@ 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 @@ -136,27 +134,20 @@ func newttsmode() *ttsmode { 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 { @@ -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)) } @@ -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] @@ -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 @@ -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)) // 重置数据 @@ -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)) } diff --git a/plugin/ai_reply/main.go b/plugin/ai_reply/main.go index 030d81ae..ab79d19c 100644 --- a/plugin/ai_reply/main.go +++ b/plugin/ai_reply/main.go @@ -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), }) @@ -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