Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

fix: tts failed when use voice clone

沈浩9bbc6f4a1246
diff --git a/.gitignore b/.gitignore
index fae4b253..dc5b3209 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,5 +7,4 @@ bin/
models/
uploads/
app.log
-build/
-models/
\ No newline at end of file
+build/
\ No newline at end of file
diff --git a/go.mod b/go.mod
index 08b7db67..129f86c1 100644
--- a/go.mod
+++ b/go.mod
@@ -7,7 +7,6 @@ toolchain go1.22.10
require (
github.com/BurntSushi/toml v1.3.2
github.com/aliyun/alibaba-cloud-sdk-go v1.63.72
- github.com/aliyun/alibabacloud-nls-go-sdk v1.1.1
github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.1.3
github.com/gin-gonic/gin v1.10.0
github.com/go-resty/resty/v2 v2.7.0
@@ -41,7 +40,6 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
- github.com/satori/go.uuid v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/atomic v1.10.0 // indirect
diff --git a/go.sum b/go.sum
index 8929f23b..a8cfc57c 100644
--- a/go.sum
+++ b/go.sum
@@ -4,11 +4,8 @@ github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
-github.com/aliyun/alibaba-cloud-sdk-go v1.61.1376/go.mod h1:9CMdKNL3ynIGPpfTcdwTvIm8SGuAZYYC4jFVSSvE1YQ=
github.com/aliyun/alibaba-cloud-sdk-go v1.63.72 h1:HvFZUzEbNvfe8F2Mg0wBGv90bPhWDxgVtDHR5zoBOU0=
github.com/aliyun/alibaba-cloud-sdk-go v1.63.72/go.mod h1:SOSDHfe1kX91v3W5QiBsWSLqeLxImobbMX1mxrFHsVQ=
-github.com/aliyun/alibabacloud-nls-go-sdk v1.1.1 h1:LjItoNZuu5xHlsByFo+kr3nGa4LRIESCGWhfurayxBg=
-github.com/aliyun/alibabacloud-nls-go-sdk v1.1.1/go.mod h1:4BDMUKpEaP/Ct79w0ozR0nbnEj49g1k3mrgX/IKG5I4=
github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.1.3 h1:grJyLSdRJtfxKKhCTWSeJhnOQsp2WoLNdK8XA5FE9oo=
github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.1.3/go.mod h1:FTzydeQVmR24FI0D6XWUOMKckjXehM/jgMn1xC+DA9M=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
@@ -53,25 +50,19 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
-github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
-github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
-github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
-github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
-github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
-github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
@@ -86,7 +77,6 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
-github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
@@ -105,10 +95,6 @@ github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/sashabaranov/go-openai v1.36.0 h1:fcSrn8uGuorzPWCBp8L0aCR95Zjb/Dd+ZSML0YZy9EI=
github.com/sashabaranov/go-openai v1.36.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
-github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
-github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
-github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
-github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -157,7 +143,6 @@ golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMx
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
-golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
@@ -185,7 +170,6 @@ golang.org/x/time v0.4.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190206041539-40960b6deb8e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
-golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -197,19 +181,15 @@ gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZ
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
-gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
-gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
-gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/krillinai_test.exe b/krillinai_test.exe
new file mode 100644
index 00000000..915e5b29
Binary files /dev/null and b/krillinai_test.exe differ
diff --git a/pkg/aliyun/tts.go b/pkg/aliyun/tts.go
index 12f27ecb..3bc7f1e8 100644
--- a/pkg/aliyun/tts.go
+++ b/pkg/aliyun/tts.go
@@ -1,142 +1,200 @@
package aliyun
import (
- "errors"
+ "encoding/json"
"fmt"
- "io"
- "log"
+ "github.com/gorilla/websocket"
+ "go.uber.org/zap"
+ "krillin-ai/log"
+ "krillin-ai/pkg/util"
"os"
"time"
-
- nls "github.com/aliyun/alibabacloud-nls-go-sdk"
)
-type TtsUserParam struct {
- F io.Writer
- Logger *nls.NlsLogger
-}
-
-// TtsClient 语音合成客户端
type TtsClient struct {
- AccessKeyId string
+ AccessKeyID string
AccessKeySecret string
- AppKey string
+ Appkey string
+}
+
+type TtsHeader struct {
+ Appkey string `json:"appkey"`
+ MessageID string `json:"message_id"`
+ TaskID string `json:"task_id"`
+ Namespace string `json:"namespace"`
+ Name string `json:"name"`
+}
+
+type StartSynthesisPayload struct {
+ Voice string `json:"voice,omitempty"`
+ Format string `json:"format,omitempty"`
+ SampleRate int `json:"sample_rate,omitempty"`
+ Volume int `json:"volume,omitempty"`
+ SpeechRate int `json:"speech_rate,omitempty"`
+ PitchRate int `json:"pitch_rate,omitempty"`
+ EnableSubtitle bool `json:"enable_subtitle,omitempty"`
+ EnablePhonemeTimestamp bool `json:"enable_phoneme_timestamp,omitempty"`
+}
+
+type RunSynthesisPayload struct {
+ Text string `json:"text"`
+}
+
+type Message struct {
+ Header TtsHeader `json:"header"`
+ Payload interface{} `json:"payload,omitempty"`
}
-func NewTtsClient(accessKeyId, accessKeySecret, appKey string) *TtsClient {
+func NewTtsClient(accessKeyId, accessKeySecret, appkey string) *TtsClient {
return &TtsClient{
- AccessKeyId: accessKeyId,
+ AccessKeyID: accessKeyId,
AccessKeySecret: accessKeySecret,
- AppKey: appKey,
+ Appkey: appkey,
}
}
-// Text2Speech 将文本转换为语音并保存到文件
func (c *TtsClient) Text2Speech(text, voice, outputFile string) error {
- fout, err := os.OpenFile(outputFile, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666)
+ file, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY, 0666)
+ if err != nil {
+ return fmt.Errorf("failed to create file: %w", err)
+ }
+ defer file.Close()
+
+ var conn *websocket.Conn
+ token, _ := CreateToken(c.AccessKeyID, c.AccessKeySecret)
+ fullURL := "wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1?token=" + token
+ dialer := websocket.DefaultDialer
+ dialer.HandshakeTimeout = 10 * time.Second
+ conn, _, err = dialer.Dial(fullURL, nil)
if err != nil {
- return fmt.Errorf("failed to create output file: %w", err)
+ return err
}
- defer fout.Close()
+ _ = conn.SetReadDeadline(time.Now().Add(time.Second * 60))
+ defer c.Close(conn)
- logger := nls.NewNlsLogger(os.Stderr, "TTS", log.LstdFlags|log.Lmicroseconds)
- logger.SetLogSil(false)
- logger.SetDebug(true)
+ onTextMessage := func(message string) {
+ log.GetLogger().Info("Received text message", zap.String("Message", message))
+ }
- // 用户参数
- ttsUserParam := &TtsUserParam{
- F: fout,
- Logger: logger,
+ onBinaryMessage := func(data []byte) {
+ if _, err := file.Write(data); err != nil {
+ log.GetLogger().Error("Failed to write data to file", zap.Error(err))
+ }
}
- param := nls.DefaultSpeechSynthesisParam()
- param.Voice = voice // 设置语音类型
+ var (
+ synthesisStarted = make(chan struct{})
+ synthesisComplete = make(chan struct{})
+ )
+
+ startPayload := StartSynthesisPayload{
+ Voice: voice,
+ Format: "wav",
+ SampleRate: 44100,
+ Volume: 50,
+ SpeechRate: 0,
+ PitchRate: 0,
+ }
- var token string
- token, err = CreateToken(c.AccessKeyId, c.AccessKeySecret)
- config := nls.NewConnectionConfigWithToken(nls.DEFAULT_URL, c.AppKey, token)
+ go c.receiveMessages(conn, onTextMessage, onBinaryMessage, synthesisStarted, synthesisComplete)
- // 语音合成实例
- var tts *nls.SpeechSynthesis
- tts, err = nls.NewSpeechSynthesis(config, logger, false,
- onTaskFailed, onSynthesisResult, nil,
- onCompleted, onClose, ttsUserParam)
- if err != nil {
- return fmt.Errorf("failed to create speech synthesis instance: %w", err)
+ taskId := util.GenerateID()
+ if err := c.StartSynthesis(conn, taskId, startPayload, synthesisStarted); err != nil {
+ return fmt.Errorf("failed to start synthesis: %w", err)
}
- // 启动任务
- var ch chan bool
- ch, err = tts.Start(text, param, nil)
- if err != nil {
- return fmt.Errorf("failed to start speech synthesis: %w", err)
+ if err := c.RunSynthesis(conn, taskId, text); err != nil {
+ return fmt.Errorf("failed to run synthesis: %w", err)
}
- // 等待任务完成
- if err = waitReady(ch, logger); err != nil {
- return fmt.Errorf("speech synthesis task failed: %w", err)
+ if err := c.StopSynthesis(conn, taskId, synthesisComplete); err != nil {
+ return fmt.Errorf("failed to stop synthesis: %w", err)
}
- // 关闭实例
- tts.Shutdown()
- logger.Println("Speech synthesis completed and saved to", outputFile)
return nil
}
-// onTaskFailed 任务失败回调
-func onTaskFailed(text string, param any) {
- p, ok := param.(*TtsUserParam)
- if !ok {
- log.Default().Fatal("invalid logger")
- return
+func (c *TtsClient) sendMessage(conn *websocket.Conn, taskId, name string, payload interface{}) error {
+ message := Message{
+ Header: TtsHeader{
+ Appkey: c.Appkey,
+ MessageID: util.GenerateID(),
+ TaskID: taskId,
+ Namespace: "FlowingSpeechSynthesizer",
+ Name: name,
+ },
+ Payload: payload,
}
- p.Logger.Println("TaskFailed:", text)
+ jsonData, _ := json.Marshal(message)
+ log.GetLogger().Debug("SpeechClient sendMessage", zap.String("message", string(jsonData)))
+ return conn.WriteJSON(message)
}
-// onSynthesisResult 合成结果回调
-func onSynthesisResult(data []byte, param any) {
- p, ok := param.(*TtsUserParam)
- if !ok {
- log.Default().Fatal("invalid logger")
- return
- }
- if _, err := p.F.Write(data); err != nil {
- p.Logger.Println("Failed to write data to file:", err)
+func (c *TtsClient) StartSynthesis(conn *websocket.Conn, taskId string, payload StartSynthesisPayload, synthesisStarted chan struct{}) error {
+ err := c.sendMessage(conn, taskId, "StartSynthesis", payload)
+ if err != nil {
+ return err
}
+
+ // 阻塞等待 SynthesisStarted 事件
+ <-synthesisStarted
+
+ return nil
+}
+
+func (c *TtsClient) RunSynthesis(conn *websocket.Conn, taskId, text string) error {
+ return c.sendMessage(conn, taskId, "RunSynthesis", RunSynthesisPayload{Text: text})
}
-// onCompleted 任务完成回调
-func onCompleted(text string, param any) {
- p, ok := param.(*TtsUserParam)
- if !ok {
- log.Default().Fatal("invalid logger")
- return
+func (c *TtsClient) StopSynthesis(conn *websocket.Conn, taskId string, synthesisComplete chan struct{}) error {
+ err := c.sendMessage(conn, taskId, "StopSynthesis", nil)
+ if err != nil {
+ return err
}
- p.Logger.Println("onCompleted:", text)
+
+ // 阻塞等待 SynthesisCompleted 事件
+ <-synthesisComplete
+
+ return nil
}
-// onClose 连接关闭回调
-func onClose(param any) {
- p, ok := param.(*TtsUserParam)
- if !ok {
- log.Default().Fatal("invalid logger")
- return
+func (c *TtsClient) Close(conn *websocket.Conn) error {
+ err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
+ if err != nil {
+ return err
}
- p.Logger.Println("onClosed")
+ return conn.Close()
}
-// waitReady 等待任务完成
-func waitReady(ch chan bool, logger *nls.NlsLogger) error {
- select {
- case done := <-ch:
- if !done {
- logger.Println("Wait failed")
- return errors.New("wait failed")
+func (c *TtsClient) receiveMessages(conn *websocket.Conn, onTextMessage func(string), onBinaryMessage func([]byte), synthesisStarted, synthesisComplete chan struct{}) {
+ defer close(synthesisComplete)
+ for {
+ messageType, message, err := conn.ReadMessage()
+ if err != nil {
+ if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
+ log.GetLogger().Error("SpeechClient receiveMessages websocket非正常关闭", zap.Error(err))
+ return
+ }
+ return
+ }
+ if messageType == websocket.TextMessage {
+ var msg Message
+ if err := json.Unmarshal(message, &msg); err != nil {
+ log.GetLogger().Error("SpeechClient receiveMessages json解析失败", zap.Error(err))
+ return
+ }
+ if msg.Header.Name == "SynthesisCompleted" {
+ log.GetLogger().Info("SynthesisCompleted event received")
+ // 收到结束消息退出
+ break
+ } else if msg.Header.Name == "SynthesisStarted" {
+ log.GetLogger().Info("SynthesisStarted event received")
+ close(synthesisStarted)
+ } else {
+ onTextMessage(string(message))
+ }
+ } else if messageType == websocket.BinaryMessage {
+ onBinaryMessage(message)
}
- logger.Println("Wait done")
- case <-time.After(60 * time.Second):
- logger.Println("Wait timeout")
- return errors.New("wait timeout")
}
- return nil
}
diff --git a/pkg/aliyun/voice_clone.go b/pkg/aliyun/voice_clone.go
index 1004e179..d97076e4 100644
--- a/pkg/aliyun/voice_clone.go
+++ b/pkg/aliyun/voice_clone.go
@@ -107,7 +107,7 @@ func (c *VoiceCloneClient) CosyVoiceClone(voicePrefix, audioURL string) (string,
}
log.GetLogger().Info("CosyVoiceClone请求完毕", zap.String("Response", resp.String()))
if res.Message != "SUCCESS" {
- log.GetLogger().Error("CosyVoiceClone请求响应错误", zap.String("Message", res.Message))
+ log.GetLogger().Error("CosyVoiceClone请求响应错误", zap.String("Request Id", res.RequestId), zap.Int("Code", res.Code), zap.String("Message", res.Message))
return "", fmt.Errorf("CosyVoiceClone请求响应错误: %s", res.Message)
}
return res.VoiceName, nil