diff --git a/handlers/handlers.go b/handlers/handlers.go index 68de88e..8feabf9 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -50,7 +50,7 @@ func SynthesizeVoice(c *gin.Context) { pitch := c.DefaultQuery("p", "0") outputFormat := c.DefaultQuery("o", "audio-24khz-48kbitrate-mono-mp3") - voice, err := utils.GetVoice(text, voiceName, rate, pitch, outputFormat) + voice, err := utils.GetVoice(text, voiceName, rate, pitch, outputFormat, c.Query("s")) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return @@ -65,12 +65,25 @@ func Index(c *gin.Context) { }) } +func ApiDoc(c *gin.Context) { + c.HTML(http.StatusOK, "api-doc.html", gin.H{ + "title": "TTS", + }) +} + type SynthesizeVoiceRequest struct { Text string `json:"t"` VoiceName string `json:"v"` Rate string `json:"r"` Pitch string `json:"p"` OutputFormat string `json:"o"` + Style string `json:"s"` +} + +type SynthesizeVoiceOpenAIRequest struct { + Model string `json:"model"` + Input string `json:"input"` + Voice string `json:"voice"` } func SynthesizeVoicePost(c *gin.Context) { @@ -80,7 +93,7 @@ func SynthesizeVoicePost(c *gin.Context) { return } - voice, err := utils.GetVoice(request.Text, request.VoiceName, request.Rate, request.Pitch, request.OutputFormat) + voice, err := utils.GetVoice(request.Text, request.VoiceName, request.Rate, request.Pitch, request.OutputFormat, request.Style) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return @@ -88,3 +101,19 @@ func SynthesizeVoicePost(c *gin.Context) { c.Data(http.StatusOK, "audio/mpeg", voice) } + +func SynthesizeVoiceOpenAI(c *gin.Context) { + var request SynthesizeVoiceOpenAIRequest + if err := c.BindJSON(&request); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + voice, err := utils.GetVoice(request.Input, request.Voice, c.Query("r"), c.Query("p"), c.Query("o"), c.Query("s")) + + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + c.Data(http.StatusOK, "audio/mpeg", voice) +} diff --git a/routes/routes.go b/routes/routes.go index 0c29669..d8d0083 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,8 +1,9 @@ package routes import ( - "github.com/gin-gonic/gin" "tts/handlers" + + "github.com/gin-gonic/gin" ) func SetupRouter() *gin.Engine { @@ -14,7 +15,9 @@ func SetupRouter() *gin.Engine { router.GET("/voices", handlers.GetVoiceList) router.POST("/tts", handlers.SynthesizeVoicePost) router.GET("/tts", handlers.SynthesizeVoice) + router.GET("/v1/audio/speech", handlers.SynthesizeVoiceOpenAI) router.GET("/", handlers.Index) + router.GET("/doc", handlers.ApiDoc) return router } diff --git a/templates/api-doc.html b/templates/api-doc.html new file mode 100644 index 0000000..5c5f7dd --- /dev/null +++ b/templates/api-doc.html @@ -0,0 +1,37 @@ + + +
+ ++参数列表: +1. t: 文本内容 (必填) +2. v: 语音名称 (可选), 默认为 zh-CN-XiaoxiaoMultilingualNeural +3. r: 语速 (可选), 默认为 0 +4. p: 语调 (可选), 默认为 0 +5. o: 输出格式 (可选), 默认为audio-24khz-48kbitrate-mono-mp3 ++ + +
+参数列表: +1. l: 语言区域 (可选), 使用 contains 匹配,如 l=zh +2. d: 显示详细信息 (可选) , 默认为 false, 如需显示详细信息, 请添加参数d , 如 /voices?d ++ + diff --git a/templates/index.html b/templates/index.html index 8b478a2..9500d00 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,36 +2,120 @@ -