mirror of
https://github.com/hanzoai/go-cosyvoice.git
synced 2026-09-30 21:53:53 +00:00
main
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers, and CI tests on 1.27.1 alone; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
CosyVoice SDK for Golang
This library provides unofficial Go clients for cosyvoice-websocket-api.
Installation
go get github.com/WqyJh/go-cosyvoice
Currently, go-cosyvoice requires Go version 1.19 or greater.
Usage
Async Synthesizer (Streaming)
import (
"context"
"fmt"
"os"
"github.com/WqyJh/go-cosyvoice"
)
func main() {
client := cosyvoice.NewClient("your-api-key")
ctx := context.Background()
asyncSynthesizer, err := client.AsyncSynthesizer(ctx)
if err != nil {
log.Fatal(err)
}
defer asyncSynthesizer.Close()
output, err := synthesizer.RunTask(ctx)
if err != nil {
log.Println("run task err", err)
return
}
texts := []string{"Hello, world!", "你好,世界!"}
for _, text := range texts {
err := synthesizer.SendText(ctx, text)
if err != nil {
log.Println("send text err", err)
return
}
}
err = synthesizer.FinishTask(ctx)
if err != nil {
log.Println("finish task err", err)
return
}
for result := range output {
if result.Err != nil {
log.Println("result err", result.Err)
return
}
// handle audio data
log.Println("received audio", len(result.Data))
}
}
Sync Synthesizer (Non-Streaming)
import (
"context"
"fmt"
"os"
"github.com/WqyJh/go-cosyvoice"
)
func main() {
client := cosyvoice.NewClient("your-api-key")
ctx := context.Background()
syncSynthesizer, err := client.SyncSynthesizer(ctx)
if err != nil {
log.Fatal(err)
}
defer syncSynthesizer.Close()
output, err := syncSynthesizer.Call(ctx, "Hello, world!")
if err != nil {
log.Println("call err", err)
return
}
for result := range output {
if result.Err != nil {
log.Println("result err", result.Err)
return
}
// handle audio data
log.Println("received audio", len(result.Data))
}
// another call
output, err = syncSynthesizer.Call(ctx, "你好,世界!")
if err != nil {
log.Println("call err", err)
return
}
}
Use another WebSocket dialer
The default websocket library is coder/websocket. You can use another websocket library by setting a custom dialer.
import (
gorilla "github.com/WqyJh/go-cosyvoice/contrib/ws-gorilla"
)
func main() {
syncSynthesizer, err := client.SyncSynthesizer(ctx,
cosyvoice.WithDialer(gorilla.NewWebSocketDialer(gorilla.WebSocketOptions{})),
)
}
ADDATIONS
The conditions for WebSocket disconnection
- If task execution fails, the server sends the task-failed event and closes the websocketConnection.
- If the interval between two tasks exceeds 60 seconds, the connection will be disconnected due to timeout. In this package, after the Asynthesizer/Synthesizer object is created, a goroutine is started for health detection by send pingMessage. Developers don't need to worry about this condition.
- In task, if the interval between two commands exceeds 23S, the server triggers a timeout error and disconnects. When you use Asyncthesizer, if your business is likely to exceed 23S, periodically call SendText() to send empty characters or punctuation marks to prevent the connection breaking. Synthesizer is not needed.
Languages
Go
98.9%
Makefile
1.1%