Skip to content

Commit

Permalink
feat: add tts design
Browse files Browse the repository at this point in the history
Signed-off-by: Xinwei Xiong <[email protected]>
  • Loading branch information
cubxxw committed Dec 20, 2024
1 parent 32e5050 commit 2f4f613
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ VOICEFLOW_VOLCENGINE_STT_RESOURCE_ID='volc.bigasr.sauc.duration'
VOICEFLOW_VOLCENGINE_TTS_WS_URL='wss://openspeech.bytedance.com/api/v1/tts/ws_binary' # TTS WebSocket URL
VOICEFLOW_VOLCENGINE_TTS_APP_ID='' # TTS 应用ID
VOICEFLOW_VOLCENGINE_TTS_TOKEN='' # TTS 令牌
VOICEFLOW_VOLCENGINE_TTS_CLUSTER='volcano_tts' # TTS 集群名称
VOICEFLOW_VOLCENGINE_TTS_VOICE_TYPE='zh_female_1' # TTS 音色类型
VOICEFLOW_VOLCENGINE_TTS_ENCODING='mp3' # TTS 音频编码
VOICEFLOW_VOLCENGINE_TTS_SPEED_RATIO='1.0' # TTS 语速比例
Expand Down
12 changes: 7 additions & 5 deletions configs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ server:

minio:
enabled: true
endpoint: ''
access_key: ""
secret_key: ""
bucket_name: 'telepace-pipeline'
endpoint: "localhost:9000"
access_key: "your_access_key"
secret_key: "your_secret_key"
bucket_name: "telepace-pipeline"
secure: true
storage_path: "voiceflow/audio/"

stt:
# 可选值:azure、google、local、assemblyai、volcengine、 aws
provider: volcengine
provider: assemblyai

tts:
# 可选值:azure、google、local、volcengine
Expand Down Expand Up @@ -65,6 +66,7 @@ volcengine:
ws_url: "wss://openspeech.bytedance.com/api/v1/tts/ws_binary"
app_id: "your_app_id"
token: "your_token"
cluster: "volcano_tts"
voice_type: "zh_female_sajiaonvyou_moon_bigtts"
encoding: "mp3"
speed_ratio: 1.0
Expand Down
8 changes: 7 additions & 1 deletion internal/storage/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"
"time"
"github.com/telepace/voiceflow/pkg/config"
"github.com/telepace/voiceflow/pkg/logger"
)

type LocalStorageService struct {
Expand All @@ -15,8 +17,12 @@ type LocalStorageService struct {

// NewLocalStorageService 创建并返回一个新的本地存储服务
func NewLocalStorageService() *LocalStorageService {
cfg, err := config.GetConfig()
if err != nil {
logger.Fatalf("配置初始化失败: %v", err)
}
return &LocalStorageService{
storagePath: "./audio_files", // 设置本地存储路径
storagePath: cfg.MinIO.StoragePath, // 使用相同的配置路径或单独配置
}
}

Expand Down
25 changes: 14 additions & 11 deletions internal/storage/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ package storage
import (
"context"
"fmt"
"github.com/google/uuid" // 用于生成唯一文件名
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/telepace/voiceflow/pkg/config"
"github.com/telepace/voiceflow/pkg/logger"
"io/ioutil"
"log"
"net/url"
"os"
"time"

"github.com/google/uuid" // 用于生成唯一文件名
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/telepace/voiceflow/pkg/config"
"github.com/telepace/voiceflow/pkg/logger"
)

type MinIOService struct {
client *minio.Client
bucketName string
client *minio.Client
bucketName string
storagePath string
}

// NewMinIOService 创建并返回 MinIO 客户端
Expand All @@ -38,8 +40,9 @@ func NewMinIOService() *MinIOService {
}

return &MinIOService{
client: minioClient,
bucketName: cfg.MinIO.BucketName,
client: minioClient,
bucketName: cfg.MinIO.BucketName,
storagePath: cfg.MinIO.StoragePath,
}
}

Expand All @@ -61,8 +64,8 @@ func (m *MinIOService) StoreAudio(audioData []byte) (string, error) {
return "", fmt.Errorf("error writing audio to temp file: %v", err)
}

// 生成唯一文件名
objectName := uuid.New().String() + ".wav"
// 生成唯一文件名,并添加存储路径前缀
objectName := m.storagePath + uuid.New().String() + ".wav"

// 上传文件到 MinIO
info, err := m.client.FPutObject(ctx, m.bucketName, objectName, tempFile.Name(), minio.PutObjectOptions{
Expand Down
4 changes: 3 additions & 1 deletion internal/tts/volcengine/volcengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type VolcengineTTS struct {
wsURL string
appID string
token string
cluster string
voiceType string
encoding string
speedRatio float64
Expand All @@ -38,6 +39,7 @@ func NewVolcengineTTS() *VolcengineTTS {
wsURL: ttsCfg.WsURL,
appID: ttsCfg.AppID,
token: ttsCfg.Token,
cluster: ttsCfg.Cluster,
voiceType: ttsCfg.VoiceType,
encoding: ttsCfg.Encoding,
speedRatio: ttsCfg.SpeedRatio,
Expand Down Expand Up @@ -70,7 +72,7 @@ func (v *VolcengineTTS) Synthesize(text string) ([]byte, error) {
"app": {
"appid": v.appID,
"token": v.token,
"cluster": "default",
"cluster": v.cluster,
},
"user": {
"uid": fmt.Sprintf("user_%d", time.Now().UnixNano()),
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Config struct {
WsURL string `mapstructure:"ws_url"`
AppID string `mapstructure:"app_id"`
Token string `mapstructure:"token"`
Cluster string `mapstructure:"cluster"`
VoiceType string `mapstructure:"voice_type"`
Encoding string `mapstructure:"encoding"`
SpeedRatio float64 `mapstructure:"speed_ratio"`
Expand All @@ -94,6 +95,7 @@ type Config struct {
SecretKey string `mapstructure:"secret_key"`
UseSSL bool `mapstructure:"use_ssl"`
Secure bool `mapstructure:"secure"`
StoragePath string `mapstructure:"storage_path"`
}
Logging struct {
Level string
Expand Down
Binary file removed test.mp3
Binary file not shown.

0 comments on commit 2f4f613

Please sign in to comment.