Commit e8513da0 authored by Mrs4s's avatar Mrs4s Committed by GitHub

Merge pull request #345 from wdvxdr1123/dev

feature support silk encoder
parents 7ae9c2d2 2a5f7849
...@@ -369,6 +369,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message. ...@@ -369,6 +369,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.
return nil, errors.New("private voice unsupported now") return nil, errors.New("private voice unsupported now")
} }
data, err := bot.Client.GetTts(d["text"]) data, err := bot.Client.GetTts(d["text"])
ioutil.WriteFile("tts.silk", data, 777)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -383,7 +384,10 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message. ...@@ -383,7 +384,10 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.
return nil, err return nil, err
} }
if !global.IsAMRorSILK(data) { if !global.IsAMRorSILK(data) {
return nil, errors.New("unsupported voice file format (please use AMR file for now)") data, err = global.Encoder(data)
if err != nil {
return nil, err
}
} }
return &message.VoiceElement{Data: data}, nil return &message.VoiceElement{Data: data}, nil
case "face": case "face":
......
package global
import (
"crypto/md5"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/wdvxdr1123/go-silk/silk"
"io/ioutil"
"path"
"sync"
)
var codec silk.Encoder
var useCodec = true
var once sync.Once
func InitCodec() {
once.Do(func() {
log.Info("正在加载silk编码器...")
err := codec.Init("data/cache", "codec")
if err != nil {
log.Error(err)
useCodec = false
}
})
}
func Encoder(data []byte) ([]byte, error) {
if useCodec == false {
return nil, errors.New("no silk encoder")
}
h := md5.New()
h.Write(data)
tempName := fmt.Sprintf("%x", h.Sum(nil))
if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) {
return ioutil.ReadFile(silkPath)
}
slk, err := codec.EncodeToSilk(data, tempName, true)
if err != nil {
return nil, err
}
return slk, nil
}
...@@ -3,7 +3,7 @@ module github.com/Mrs4s/go-cqhttp ...@@ -3,7 +3,7 @@ module github.com/Mrs4s/go-cqhttp
go 1.14 go 1.14
require ( require (
github.com/Mrs4s/MiraiGo v0.0.0-20201003051902-8a968656c116 github.com/Mrs4s/MiraiGo v0.0.0-20201005155759-f9b3c399e5e0
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/gin-gonic/gin v1.6.3 github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.4.0 // indirect github.com/go-playground/validator/v10 v10.4.0 // indirect
...@@ -22,10 +22,13 @@ require ( ...@@ -22,10 +22,13 @@ require (
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
github.com/tebeka/strftime v0.1.5 // indirect github.com/tebeka/strftime v0.1.5 // indirect
github.com/tidwall/gjson v1.6.1 github.com/tidwall/gjson v1.6.1
github.com/ugorji/go v1.1.10 // indirect
github.com/wdvxdr1123/go-silk v0.0.0-20201006020916-0398076200ea
github.com/xujiajun/nutsdb v0.5.0 github.com/xujiajun/nutsdb v0.5.0
github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189 github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189
golang.org/x/net v0.0.0-20200930145003-4acb6c075d10 // indirect golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f // indirect golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c // indirect
golang.org/x/sys v0.0.0-20201005172224-997123666555 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
gopkg.in/yaml.v2 v2.3.0 // indirect gopkg.in/yaml.v2 v2.3.0 // indirect
) )
This diff is collapsed.
...@@ -157,6 +157,7 @@ func (s *webServer) Dologin() { ...@@ -157,6 +157,7 @@ func (s *webServer) Dologin() {
} }
log.Info("正在加载事件过滤器.") log.Info("正在加载事件过滤器.")
global.BootFilter() global.BootFilter()
global.InitCodec()
coolq.IgnoreInvalidCQCode = conf.IgnoreInvalidCQCode coolq.IgnoreInvalidCQCode = conf.IgnoreInvalidCQCode
coolq.ForceFragmented = conf.ForceFragmented coolq.ForceFragmented = conf.ForceFragmented
log.Info("资源初始化完成, 开始处理信息.") log.Info("资源初始化完成, 开始处理信息.")
......
...@@ -22,7 +22,7 @@ import ( ...@@ -22,7 +22,7 @@ import (
type httpServer struct { type httpServer struct {
engine *gin.Engine engine *gin.Engine
bot *coolq.CQBot bot *coolq.CQBot
Http *http.Server Http *http.Server
} }
type httpClient struct { type httpClient struct {
...@@ -81,9 +81,9 @@ func (s *httpServer) Run(addr, authToken string, bot *coolq.CQBot) { ...@@ -81,9 +81,9 @@ func (s *httpServer) Run(addr, authToken string, bot *coolq.CQBot) {
go func() { go func() {
log.Infof("CQ HTTP 服务器已启动: %v", addr) log.Infof("CQ HTTP 服务器已启动: %v", addr)
s.Http=&http.Server{ s.Http = &http.Server{
Addr:addr, Addr: addr,
Handler:s.engine, Handler: s.engine,
} }
if err := s.Http.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := s.Http.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error(err) log.Error(err)
...@@ -553,4 +553,4 @@ func (s *httpServer) ShutDown() { ...@@ -553,4 +553,4 @@ func (s *httpServer) ShutDown() {
log.Println("timeout of 5 seconds.") log.Println("timeout of 5 seconds.")
} }
log.Println("http Server exiting") log.Println("http Server exiting")
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment