Text to Speech Speech to Text

speech_to_text.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package service
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/base64"
  6. "encoding/hex"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io/ioutil"
  11. "mime/multipart"
  12. "net/http"
  13. "net/url"
  14. "strings"
  15. "time"
  16. )
  17. // Xfyun 讯飞云调用对象
  18. type Xfyun struct {
  19. URL string
  20. ID string
  21. KEY string
  22. }
  23. // XfyunResult 讯飞云返回结果
  24. type XfyunResult struct {
  25. Code string `json:"code"`
  26. Data string `json:"data"`
  27. Desc string `json:"desc"`
  28. Sid string `json:"sid"`
  29. }
  30. // XfyunParam 讯飞云请求参数
  31. type XfyunParam struct {
  32. Aue string `json:"aue"`
  33. EngineType string `json:"engine_type"`
  34. }
  35. // SuccessCode 讯飞云成功代码
  36. const SuccessCode = "success"
  37. // SpeechToText speech to text input File
  38. func SpeechToText(file multipart.File, lang string, rateStr string) (string, error) {
  39. xfyun := Xfyun{
  40. URL: "http://api.xfyun.cn/v1/service/v1/iat",
  41. ID: "5bbaeda1",
  42. KEY: "25c68c82b45309d35a7f14069107746b",
  43. }
  44. wav, err := ioutil.ReadAll(file)
  45. if err != nil {
  46. return "", err
  47. }
  48. //
  49. base64Audio := base64.StdEncoding.EncodeToString(wav)
  50. u := url.Values{}
  51. u.Set("audio", base64Audio)
  52. body := u.Encode()
  53. xfyunParam := XfyunParam{
  54. Aue: "raw",
  55. EngineType: "sms",
  56. }
  57. if lang != "zh" {
  58. xfyunParam.EngineType += "-en"
  59. }
  60. if rateStr == "8" {
  61. xfyunParam.EngineType += "8k"
  62. } else {
  63. xfyunParam.EngineType += "16k"
  64. }
  65. xfyunParamString, _ := json.Marshal(xfyunParam)
  66. fmt.Println(string(xfyunParamString))
  67. xParam := base64.StdEncoding.EncodeToString(xfyunParamString)
  68. xTime := time.Now().Unix()
  69. h := md5.New()
  70. h.Write([]byte(fmt.Sprintf("%s%d%s", xfyun.KEY, xTime, xParam)))
  71. xChecksum := hex.EncodeToString(h.Sum(nil))
  72. client := &http.Client{}
  73. req, _ := http.NewRequest("POST", xfyun.URL, bytes.NewBuffer([]byte(body)))
  74. req.Header.Set("Accept-Encoding", "identity")
  75. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  76. req.Header.Add("X-Appid", xfyun.ID)
  77. req.Header.Add("X-CurTime", fmt.Sprintf("%d", xTime))
  78. req.Header.Add("X-Param", xParam)
  79. req.Header.Add("X-CheckSum", xChecksum)
  80. resp, err := client.Do(req)
  81. if err != nil {
  82. return "", nil
  83. }
  84. html, err := ioutil.ReadAll(resp.Body)
  85. if err != nil {
  86. return "", err
  87. }
  88. xfyunResult := XfyunResult{}
  89. err = json.Unmarshal(html, &xfyunResult)
  90. if err != nil {
  91. return "", nil
  92. }
  93. var text string
  94. if xfyunResult.Desc == SuccessCode {
  95. text = xfyunResult.Data
  96. } else {
  97. err = errors.New(xfyunResult.Desc)
  98. return "", err
  99. }
  100. return strings.TrimSpace(text), nil
  101. }