zeekayandHanzo Dev 8a019ba36f Go 1.27.1, and the code it can say more simply
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]>
2026-09-16 18:54:36 -07:00
2025-02-11 16:04:05 +08:00
2025-02-11 16:04:05 +08:00
2025-02-11 16:04:05 +08:00
2025-02-11 16:03:19 +08:00
2025-02-11 16:55:07 +08:00
2025-02-20 19:53:05 +08:00
2025-02-11 16:04:05 +08:00

CosyVoice SDK for Golang

Go Reference Go Report Card codecov

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

  1. If task execution fails, the server sends the task-failed event and closes the websocketConnection.
  2. 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.
  3. 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.
S
Description
No description provided
Readme MIT
68 KiB
Languages
Go 98.9%
Makefile 1.1%