Kaynağa Gözat

init project

陈超 4 yıl önce
işleme
c3113d1067
9 değiştirilmiş dosya ile 196 ekleme ve 0 silme
  1. 2
    0
      .idea/.gitignore
  2. 6
    0
      .idea/misc.xml
  3. 8
    0
      .idea/modules.xml
  4. 9
    0
      .idea/tts.iml
  5. 6
    0
      .idea/vcs.xml
  6. 9
    0
      go.mod
  7. 59
    0
      go.sum
  8. 97
    0
      main.go
  9. BIN
      tts

+ 2
- 0
.idea/.gitignore Dosyayı Görüntüle

@@ -0,0 +1,2 @@
1
+# Default ignored files
2
+/workspace.xml

+ 6
- 0
.idea/misc.xml Dosyayı Görüntüle

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="JavaScriptSettings">
4
+    <option name="languageLevel" value="ES6" />
5
+  </component>
6
+</project>

+ 8
- 0
.idea/modules.xml Dosyayı Görüntüle

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ProjectModuleManager">
4
+    <modules>
5
+      <module fileurl="file://$PROJECT_DIR$/.idea/tts.iml" filepath="$PROJECT_DIR$/.idea/tts.iml" />
6
+    </modules>
7
+  </component>
8
+</project>

+ 9
- 0
.idea/tts.iml Dosyayı Görüntüle

@@ -0,0 +1,9 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module type="WEB_MODULE" version="4">
3
+  <component name="Go" enabled="true" />
4
+  <component name="NewModuleRootManager">
5
+    <content url="file://$MODULE_DIR$" />
6
+    <orderEntry type="inheritedJdk" />
7
+    <orderEntry type="sourceFolder" forTests="false" />
8
+  </component>
9
+</module>

+ 6
- 0
.idea/vcs.xml Dosyayı Görüntüle

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="VcsDirectoryMappings">
4
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+  </component>
6
+</project>

+ 9
- 0
go.mod Dosyayı Görüntüle

@@ -0,0 +1,9 @@
1
+module git.chenqinghe.com/jarvis/tts
2
+
3
+go 1.13
4
+
5
+require (
6
+	github.com/chenqinghe/baidu-ai-go-sdk v0.2.1
7
+	github.com/gin-gonic/gin v1.6.3
8
+	github.com/sirupsen/logrus v1.6.0
9
+)

+ 59
- 0
go.sum Dosyayı Görüntüle

@@ -0,0 +1,59 @@
1
+github.com/chenqinghe/baidu-ai-go-sdk v0.2.1 h1:GF+LSF7Z5PODkJBl2DDL7IhhjVFIuOqeHL3/rB08j2I=
2
+github.com/chenqinghe/baidu-ai-go-sdk v0.2.1/go.mod h1:FZqcKNXYC7Ysp53bi8zlLh6SjX5SBFOUaAMV7RBrW0E=
3
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6
+github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
7
+github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
8
+github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
9
+github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
10
+github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
11
+github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
12
+github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
13
+github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
14
+github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
15
+github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
16
+github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
17
+github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
18
+github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
19
+github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
20
+github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
21
+github.com/imroc/req v0.2.4 h1:8XbvaQpERLAJV6as/cB186DtH5f0m5zAOtHEaTQ4ac0=
22
+github.com/imroc/req v0.2.4/go.mod h1:J9FsaNHDTIVyW/b5r6/Df5qKEEEq2WzZKIgKSajd1AE=
23
+github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
24
+github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
25
+github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
26
+github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
27
+github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
28
+github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
29
+github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
30
+github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
31
+github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
32
+github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
33
+github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
34
+github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
35
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
36
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
37
+github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
38
+github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
39
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
40
+github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
41
+github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
42
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
43
+github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
44
+github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
45
+github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
46
+github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
47
+github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
48
+github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
49
+golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
50
+golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
51
+golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
52
+golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
53
+golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
54
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
55
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
56
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
57
+gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
58
+gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
59
+gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 97
- 0
main.go Dosyayı Görüntüle

@@ -0,0 +1,97 @@
1
+package main
2
+
3
+import (
4
+	"crypto/md5"
5
+	"encoding/hex"
6
+	"flag"
7
+	"fmt"
8
+	"io/ioutil"
9
+	"net/http"
10
+	"os"
11
+	"os/exec"
12
+	"sync"
13
+	"time"
14
+
15
+	"github.com/chenqinghe/baidu-ai-go-sdk/voice"
16
+	"github.com/gin-gonic/gin"
17
+	"github.com/sirupsen/logrus"
18
+)
19
+
20
+var client *voice.VoiceClient
21
+var addr string
22
+
23
+const (
24
+	apiKey    = "tVPKdPqxKwWOM9vsukPzseoH"
25
+	apiSecret = "VFusINaQ3YjDUC4GoIB1cENME9g2f4Gn"
26
+)
27
+
28
+func init() {
29
+	client = voice.NewVoiceClient(apiKey, apiSecret)
30
+
31
+	flag.StringVar(&addr, "addr", ":8080", "server address")
32
+
33
+}
34
+
35
+func main() {
36
+	flag.Parse()
37
+	if addr == "" {
38
+		flag.Usage()
39
+		os.Exit(1)
40
+	}
41
+
42
+	r := gin.Default()
43
+
44
+	handler := &handler{
45
+		l: &sync.Mutex{},
46
+	}
47
+	r.GET("/", handler.tts)
48
+	if err := r.Run(":8080"); err != nil {
49
+		logrus.Fatalln("start server error:", err)
50
+	}
51
+}
52
+
53
+type handler struct {
54
+	l *sync.Mutex // 确保串行,不然会同时播放多个语音
55
+}
56
+
57
+func (h *handler) tts(c *gin.Context) {
58
+	h.l.Lock()
59
+	defer h.l.Unlock()
60
+	content := c.Query("q")
61
+	logrus.Infoln("tts content:", content)
62
+
63
+	data, err := client.TextToSpeech(content)
64
+	if err != nil {
65
+		logrus.Errorln("sdk::TextToSpeech error:", err, "content:", content)
66
+		http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
67
+		return
68
+	}
69
+
70
+	filename := fmt.Sprintf("/tmp/%s.mp3", randomStr())
71
+	err = ioutil.WriteFile(filename, data, os.ModePerm)
72
+	if err != nil {
73
+		logrus.Errorln("write file error:", err, "path:", filename)
74
+		http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
75
+		return
76
+	}
77
+	defer os.Remove(filename)
78
+
79
+	cmd := exec.Command("mplayer", filename)
80
+	if err := cmd.Run(); err != nil {
81
+		logrus.Errorln("play error:", err)
82
+		http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
83
+		return
84
+	}
85
+	c.Writer.WriteHeader(http.StatusOK)
86
+	c.Writer.WriteString("ok")
87
+
88
+}
89
+
90
+func randomStr() string {
91
+	now := time.Now().Format("2006-01-02 15:04:05 9999")
92
+	h := md5.New()
93
+
94
+	h.Write([]byte(now))
95
+	r := h.Sum(nil)
96
+	return hex.EncodeToString(r)
97
+}