initial commit

This commit is contained in:
zuoban
2024-04-17 14:14:19 +08:00
commit f2455f92b6
14 changed files with 555 additions and 0 deletions

74
handlers/handlers.go Normal file
View File

@@ -0,0 +1,74 @@
package handlers
import (
"github.com/gin-gonic/gin"
"net/http"
"tts/utils"
)
func GetVoiceList(c *gin.Context) {
locale := c.Query("locale")
voices, err := utils.VoiceList()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if locale != "" {
filteredVoices := make([]interface{}, 0)
for _, voice := range voices {
if voice.(map[string]interface{})["Locale"].(string) == locale {
filteredVoices = append(filteredVoices, voice)
}
}
voices = filteredVoices
}
c.JSON(http.StatusOK, gin.H{"voices": voices})
}
func SynthesizeVoice(c *gin.Context) {
text := c.Query("t")
voiceName := c.DefaultQuery("v", "zh-CN-XiaoxiaoMultilingualNeural")
rate := c.DefaultQuery("r", "0")
pitch := c.DefaultQuery("p", "0")
outputFormat := c.DefaultQuery("o", "audio-24khz-48kbitrate-mono-mp3")
voice, err := utils.GetVoice(text, voiceName, rate, pitch, outputFormat)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.Data(http.StatusOK, "audio/mpeg", voice)
}
func Index(c *gin.Context) {
c.HTML(http.StatusOK, "index.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"`
}
func SynthesizeVoicePost(c *gin.Context) {
var request SynthesizeVoiceRequest
if err := c.BindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
voice, err := utils.GetVoice(request.Text, request.VoiceName, request.Rate, request.Pitch, request.OutputFormat)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.Data(http.StatusOK, "audio/mpeg", voice)
}