Text to Speech Speech to Text

text_to_speech.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package handler
  2. import (
  3. "errors"
  4. "fmt"
  5. "git.links123.net/Slate/CorpusAI/config"
  6. "git.links123.net/Slate/CorpusAI/service"
  7. "git.links123.net/Slate/CorpusAI/service/store/cache"
  8. "git.links123.net/Slate/CorpusAI/service/store/mysql"
  9. "git.links123.net/links123.com/pkg/request"
  10. "github.com/gin-gonic/gin"
  11. "net/http"
  12. "net/url"
  13. "strconv"
  14. "strings"
  15. )
  16. // TextToSpeech godoc
  17. // @Tags 语料库
  18. // @Summary 文本转语音
  19. // @Description Text to speech
  20. // @Accept json
  21. // @Produce json
  22. // @Param text query string false "单词/短语/句子"
  23. // @Param languageCode query string false "en-US(美音)/en-GB(英音)" Enums(en-US, en-GB)
  24. // @Param gender query string false "MALE(男音)/FEMALE(女音)" Enums(MALE, FEMALE)
  25. // @Success 302 {string} json "https://lnks123-campus-tts-hk.oss-cn-hongkong.aliyuncs.com/xxxx.mp3"
  26. // @Failure 400 {string} json "{"msg": "error info"}"
  27. // @Failure 500 {string} json "{"msg": "error info"}"
  28. // @Router /en/tts [get]
  29. func TextToSpeechMain(ctx *gin.Context) {
  30. //Text string, LanguageCode, Gender string
  31. //voiceName := ctx.Query("voiceName")
  32. speed, err := strconv.ParseFloat(ctx.DefaultQuery("speed", "1.00"), 64)
  33. pitch, err := strconv.ParseFloat(ctx.DefaultQuery("pitch", "0.00"), 64)
  34. // 获取参数内容,没有则返回空字符串
  35. text, err := url.QueryUnescape(ctx.Query("text"))
  36. languageCode, err := url.QueryUnescape(ctx.Query("languageCode"))
  37. gender, err := url.QueryUnescape(ctx.Query("gender"))
  38. voiceName := GetDefaultVoiceName(languageCode, gender)
  39. if err != nil || voiceName == "" || text == "" {
  40. request.Fail(ctx, 400, errors.New("text or voiceName error"))
  41. return
  42. }
  43. text = strings.Trim(text , "")
  44. ossObjectKey := service.GetTtsOssKey(text, voiceName, languageCode, speed, pitch)
  45. textKey := cache.GetTextKey(ossObjectKey)
  46. textKeyExists := cache.TextKeyExists(textKey)
  47. //fmt.Println("TextToSpeechMain %s%s%s%s", text,ossObjectKey,textKey,textKeyExists)
  48. if textKeyExists == true {
  49. //get url
  50. mp3Url := service.GetUrl(ossObjectKey)
  51. ctx.Redirect(302, mp3Url)
  52. return
  53. } else {
  54. appConfig := config.C.App
  55. if appConfig.InChina == true {
  56. text = url.QueryEscape(text)
  57. resp, err := http.Get(appConfig.ApiHost + "/v1/en/tts?text="+text+"&languageCode="+languageCode+"&gender="+gender)
  58. if err != nil || (resp.StatusCode != http.StatusFound && resp.StatusCode != http.StatusOK) {
  59. fmt.Println("service.Get Api", err , appConfig.ApiHost + "/v1/en/tts?text="+text+"&languageCode="+languageCode+"&gender="+gender)
  60. fmt.Println("service.Get Api resp", resp, err)
  61. request.Fail(ctx, 400, errors.New("Get Api error"))
  62. return
  63. }
  64. mp3Url := service.GetUrl(ossObjectKey)
  65. //set cache
  66. cache.SetOssObject(textKey, ossObjectKey)
  67. ctx.Redirect(302, mp3Url)
  68. return
  69. } else {
  70. //todo 先从db查一下缓存, 线上数据尽量走其它方式缓存到DB
  71. AudioContent, err := service.TextToSpeech(text, voiceName, languageCode, speed, pitch)
  72. if err != nil {
  73. fmt.Println("service.TextToSpeech Error", err)
  74. request.Fail(ctx, 400, errors.New("Error: service.TextToSpeech"))
  75. return
  76. }
  77. // todo 因hk地区和国内oss不通
  78. service.UploadHkOss(ossObjectKey, AudioContent)
  79. uploadResult, err := service.UploadOss(ossObjectKey, AudioContent)
  80. if err != nil {
  81. fmt.Println("service.UploadHkOss Error", err)
  82. request.Fail(ctx, 400, errors.New("Error: service.UploadOss"))
  83. return
  84. }
  85. if uploadResult == true {
  86. //get url
  87. mp3Url := service.GetUrl(ossObjectKey)
  88. //set cache
  89. cache.SetOssObject(textKey, ossObjectKey)
  90. //set db
  91. mysql.CreateCorpusTts(text, textKey, languageCode, voiceName, ossObjectKey, speed, pitch)
  92. ctx.Redirect(302, mp3Url)
  93. return
  94. }
  95. //set cache
  96. }
  97. }
  98. }
  99. func GetDefaultVoiceName(LanguageCode, Gender string) string {
  100. //todo
  101. switch LanguageCode {
  102. case "en-GB":
  103. if Gender == "FEMALE" {
  104. return "en-GB-Wavenet-C"
  105. } else {
  106. return "en-GB-Wavenet-B"
  107. }
  108. case "en-US":
  109. if Gender == "FEMALE" {
  110. return "en-US-Wavenet-C"
  111. } else {
  112. return "en-US-Wavenet-B"
  113. }
  114. }
  115. return ""
  116. //en-GB en-GB-Wavenet-B MALE
  117. //en-GB en-GB-Wavenet-C FEMALE
  118. //en-US en-US-Wavenet-B MALE
  119. //en-US en-US-Wavenet-C FEMALE
  120. //通过键值获取默认配置: languageCode + gender
  121. }