# ttshub **Repository Path**: cg33/ttshub ## Basic Information - **Project Name**: ttshub - **Description**: this is a ttshub - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-17 - **Last Updated**: 2025-11-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ttshub 一个开源的 Golang TTS API 汇总集成库,统一集成各大厂商的文本转语音(TTS)API。 ## 功能特性 - 🎯 **统一接口**:提供统一的 TTS 接口,简化多厂商集成 - 🔌 **多厂商支持**:支持 Google、OpenAI、阿里云、Azure、腾讯云等主流 TTS 服务 - 🎤 **语音克隆**:支持阿里云和 Azure 的语音克隆功能 - 🚀 **易于使用**:通过简单的工厂方法即可创建 TTS 实例 - 📦 **轻量级**:无额外依赖,仅使用 Go 标准库 ## 支持的厂商 - ✅ Google Cloud Text-to-Speech - ✅ OpenAI TTS API - ✅ 阿里云智能语音(支持语音克隆) - ✅ Azure Cognitive Services Speech(支持语音克隆) - ✅ 腾讯云语音合成 ## 安装 ```bash go get github.com/ttshub/ttshub ``` ## 快速开始 ### 基本使用 ```go package main import ( "context" "github.com/ttshub/ttshub" ) func main() { // 创建认证信息 credentials := &ttshub.Credentials{ APIKey: "your-api-key", } // 创建 TTS 实例(以 OpenAI 为例) tts, err := ttshub.NewTTS(ttshub.ProviderOpenAI, credentials) if err != nil { panic(err) } // 设置合成选项 options := ttshub.DefaultSynthesisOptions() options.Language = "zh-CN" options.Voice = "alloy" options.Speed = 1.0 // 合成语音 audio, err := tts.Synthesize(context.Background(), "你好,世界!", options) if err != nil { panic(err) } // 保存音频文件 // ioutil.WriteFile("output.mp3", audio, 0644) } ``` ### 使用不同厂商 #### Google TTS ```go credentials := &ttshub.Credentials{ APIKey: "your-google-api-key", } tts, err := ttshub.NewTTS(ttshub.ProviderGoogle, credentials) ``` #### OpenAI TTS ```go credentials := &ttshub.Credentials{ APIKey: "your-openai-api-key", } tts, err := ttshub.NewTTS(ttshub.ProviderOpenAI, credentials) ``` #### 阿里云 TTS ```go credentials := &ttshub.Credentials{ AccessKeyID: "your-access-key-id", AccessKeySecret: "your-access-key-secret", Region: "cn-shanghai", } tts, err := ttshub.NewTTS(ttshub.ProviderAliyun, credentials) ``` #### Azure TTS ```go credentials := &ttshub.Credentials{ APIKey: "your-azure-subscription-key", Region: "eastus", } tts, err := ttshub.NewTTS(ttshub.ProviderAzure, credentials) ``` #### 腾讯云 TTS ```go credentials := &ttshub.Credentials{ AccessKeyID: "your-secret-id", AccessKeySecret: "your-secret-key", Region: "ap-beijing", } tts, err := ttshub.NewTTS(ttshub.ProviderTencent, credentials) ``` ### 列出可用语音 ```go voices, err := tts.ListVoices(context.Background()) if err != nil { panic(err) } for _, voice := range voices { fmt.Printf("ID: %s, Name: %s, Language: %s, Gender: %s\n", voice.ID, voice.Name, voice.Language, voice.Gender) } ``` ### 流式合成 ```go reader, err := tts.SynthesizeStream(context.Background(), "长文本内容...", options) if err != nil { panic(err) } // 读取音频流 buffer := make([]byte, 4096) for { n, err := reader.Read(buffer) if err == io.EOF { break } // 处理音频数据 } ``` ### 语音克隆(仅阿里云和 Azure 支持) ```go // 准备音频样本 samples := []ttshub.VoiceSample{ { AudioData: audioBytes, // 从文件读取的音频数据 AudioFormat: "wav", Text: "这是对应的文本内容", // 可选 }, } // 设置克隆选项 cloneOptions := ttshub.DefaultCloneOptions() cloneOptions.Name = "我的自定义语音" cloneOptions.Description = "这是一个测试语音" cloneOptions.Language = "zh-CN" // 创建克隆语音 clonedVoiceID, err := tts.CloneVoice(context.Background(), samples, cloneOptions) if err != nil { panic(err) } fmt.Printf("克隆语音 ID: %s\n", clonedVoiceID) // 使用克隆语音进行合成 audio, err := tts.SynthesizeWithClonedVoice( context.Background(), "使用克隆语音合成的文本", clonedVoiceID, options, ) if err != nil { panic(err) } // 列出所有克隆语音 clonedVoices, err := tts.ListClonedVoices(context.Background()) if err != nil { panic(err) } for _, voice := range clonedVoices { fmt.Printf("ID: %s, Name: %s, Status: %s\n", voice.ID, voice.Name, voice.Status) } // 删除克隆语音 err = tts.DeleteClonedVoice(context.Background(), clonedVoiceID) if err != nil { panic(err) } ``` **注意**:Google、OpenAI 和腾讯云不支持语音克隆功能,调用相关方法会返回 `ErrVoiceCloningNotSupported` 错误。 ## API 文档 ### TTS 接口 ```go type TTS interface { // Synthesize 将文本转换为语音 Synthesize(ctx context.Context, text string, options *SynthesisOptions) ([]byte, error) // SynthesizeStream 将文本转换为语音流 SynthesizeStream(ctx context.Context, text string, options *SynthesisOptions) (io.Reader, error) // ListVoices 列出可用的语音列表 ListVoices(ctx context.Context) ([]Voice, error) // CloneVoice 创建语音克隆模型 CloneVoice(ctx context.Context, samples []VoiceSample, options *CloneOptions) (string, error) // SynthesizeWithClonedVoice 使用克隆的语音进行合成 SynthesizeWithClonedVoice(ctx context.Context, text string, clonedVoiceID string, options *SynthesisOptions) ([]byte, error) // ListClonedVoices 列出已创建的克隆语音列表 ListClonedVoices(ctx context.Context) ([]ClonedVoice, error) // DeleteClonedVoice 删除克隆的语音模型 DeleteClonedVoice(ctx context.Context, clonedVoiceID string) error } ``` ### SynthesisOptions ```go type SynthesisOptions struct { Language string // 语言代码,如 "zh-CN", "en-US" Voice string // 语音名称或 ID Speed float64 // 语速,通常范围 0.5-2.0 Pitch float64 // 音调,通常范围 -20.0 到 20.0 Volume float64 // 音量,通常范围 0.0-1.0 AudioFormat string // 音频格式,如 "mp3", "wav", "pcm" SampleRate int // 采样率,如 16000, 22050, 44100 AdditionalOptions map[string]interface{} // 各厂商特定的额外选项 } ``` ## 注意事项 1. **认证信息**:不同厂商需要不同的认证信息,请参考各厂商的文档获取相应的密钥 2. **参数映射**:各厂商的参数范围可能不同,库会自动进行映射和转换 3. **错误处理**:请务必处理所有可能的错误 4. **上下文取消**:支持通过 context 取消请求 5. **语音克隆**:目前仅阿里云和 Azure 支持语音克隆功能,其他厂商调用相关方法会返回错误 6. **语音克隆训练**:语音克隆需要上传音频样本进行训练,训练时间可能较长,请耐心等待 ## 服务提供商信息 关于各厂商的 API 简介、文档链接和价格信息,请参考: - [服务提供商信息 (中文)](PROVIDERS_CN.md) - [Provider Information (English)](PROVIDERS.md) ## 贡献 欢迎提交 Issue 和 Pull Request! ## 许可证 MIT License