-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Package ahsai AH Soft フリーテキスト音声合成 demo API | ||
package ahsai | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"sort" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/FloatTech/floatbox/file" | ||
ctrl "github.com/FloatTech/zbpctrl" | ||
"github.com/FloatTech/zbputils/control" | ||
ahsaitts "github.com/fumiama/ahsai" | ||
zero "github.com/wdvxdr1123/ZeroBot" | ||
"github.com/wdvxdr1123/ZeroBot/message" | ||
) | ||
|
||
var ( | ||
namelist = []string{"伊織弓鶴", "紲星あかり", "結月ゆかり", "京町セイカ", "東北きりたん", "東北イタコ", "ついなちゃん標準語", "ついなちゃん関西弁", "音街ウナ", "琴葉茜", "吉田くん", "民安ともえ", "桜乃そら", "月読アイ", "琴葉葵", "東北ずん子", "月読ショウタ", "水奈瀬コウ"} | ||
) | ||
|
||
func init() { | ||
engine := control.Register("ahsai", &ctrl.Options[*zero.Ctx]{ | ||
DisableOnDefault: false, | ||
Help: "ahsai tts\n- 使[伊織弓鶴|紲星あかり|結月ゆかり|京町セイカ|東北きりたん|東北イタコ|ついなちゃん標準語|ついなちゃん関西弁|音街ウナ|琴葉茜|吉田くん|民安ともえ|桜乃そら|月読アイ|琴葉葵|東北ずん子|月読ショウタ|水奈瀬コウ]说(日语)\n", | ||
PrivateDataFolder: "ahsai", | ||
}) | ||
cachePath := engine.DataFolder() + "cache/" | ||
_ = os.RemoveAll(cachePath) | ||
_ = os.MkdirAll(cachePath, 0755) | ||
engine.OnRegex("^使(.{0,10})说([A-Za-z\\s\\d\u3005\u3040-\u30ff\u4e00-\u9fff\uff11-\uff19\uff21-\uff3a\uff41-\uff5a\uff66-\uff9d\\pP]+)$", selectName).SetBlock(true).Handle(func(ctx *zero.Ctx) { | ||
ctx.SendChain(message.Text("少女祈祷中...")) | ||
uid := ctx.Event.UserID | ||
today := time.Now().Format("20060102150405") | ||
ahsaiFile := cachePath + strconv.FormatInt(uid, 10) + today + "ahsai.wav" | ||
s := ahsaitts.NewSpeaker() | ||
err := s.SetName(ctx.State["ahsainame"].(string)) | ||
if err != nil { | ||
ctx.SendChain(message.Text("Error:", err)) | ||
return | ||
} | ||
u, err := s.Speak(ctx.State["ahsaitext"].(string)) | ||
if err != nil { | ||
ctx.SendChain(message.Text("Error:", err)) | ||
return | ||
} | ||
err = ahsaitts.SaveOggToFile(u, ahsaiFile) | ||
if err != nil { | ||
ctx.SendChain(message.Text("Error:", err)) | ||
return | ||
} | ||
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + ahsaiFile)) | ||
}) | ||
} | ||
|
||
func selectName(ctx *zero.Ctx) bool { | ||
regexMatched := ctx.State["regex_matched"].([]string) | ||
ctx.State["ahsaitext"] = regexMatched[2] | ||
name := regexMatched[1] | ||
sort.Strings(namelist) | ||
index := sort.SearchStrings(namelist, name) | ||
if index < len(namelist) && namelist[index] == name { | ||
ctx.State["ahsainame"] = name | ||
return true | ||
} | ||
speaktext := "" | ||
for i, v := range namelist { | ||
speaktext += fmt.Sprintf("%d. %s\n", i, v) | ||
} | ||
ctx.SendChain(message.Text("输入的音源为空, 请输入音源序号\n", speaktext)) | ||
next, cancel := zero.NewFutureEvent("message", 999, false, ctx.CheckSession(), zero.RegexRule(`\d{0,2}`)).Repeat() | ||
defer cancel() | ||
for { | ||
select { | ||
case <-time.After(time.Second * 10): | ||
ctx.State["ahsainame"] = namelist[rand.Intn(len(namelist))] | ||
ctx.SendChain(message.Text("时间太久啦!", zero.BotConfig.NickName[0], "帮你选择", ctx.State["ahsainame"])) | ||
return true | ||
case c := <-next: | ||
msg := c.Event.Message.ExtractPlainText() | ||
num, _ := strconv.Atoi(msg) | ||
if num < 0 || num >= len(namelist) { | ||
ctx.SendChain(message.Text("序号非法!")) | ||
continue | ||
} | ||
ctx.State["ahsainame"] = namelist[num] | ||
return true | ||
} | ||
} | ||
} |
Oops, something went wrong.