mirror of
https://github.com/wahyd4/say-it.git
synced 2026-08-09 05:06:41 +10:00
Add ability to read the words under Mac OS
This commit is contained in:
@@ -2,15 +2,29 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
|
||||
"time"
|
||||
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
Mp3FileName = "say-it.mp3"
|
||||
)
|
||||
|
||||
var (
|
||||
person int
|
||||
speed int
|
||||
pitch int
|
||||
token string = "24.85efc4bbd8b8315255dcb59cd82e1ac4.2592000.1501048297.282335-9739014"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -26,8 +40,7 @@ func main() {
|
||||
return nil
|
||||
}
|
||||
words := c.Args().Get(0)
|
||||
fmt.Println(words)
|
||||
|
||||
fetchVoiceAndSpeak(words)
|
||||
fmt.Println(person, speed, pitch)
|
||||
return nil
|
||||
}
|
||||
@@ -57,3 +70,47 @@ func setFlags(app *cli.App) {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func fetchVoiceAndSpeak(text string) {
|
||||
urlObject := buildURL(text)
|
||||
response, err := http.Get(urlObject.String())
|
||||
|
||||
if err != nil {
|
||||
log.Error("Fetch voice failed:" + err.Error())
|
||||
}
|
||||
// fmt.Println(response.StatusCode)
|
||||
|
||||
defer response.Body.Close()
|
||||
out, err := os.Create(Mp3FileName)
|
||||
if err != nil {
|
||||
log.Fatal("Create file failed:" + err.Error())
|
||||
}
|
||||
defer out.Close()
|
||||
io.Copy(out, response.Body)
|
||||
|
||||
speak()
|
||||
}
|
||||
|
||||
func speak() {
|
||||
command := exec.Command("afplay", Mp3FileName)
|
||||
if err := command.Run(); err != nil {
|
||||
log.Error("Failed to say the words: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func buildURL(text string) *url.URL {
|
||||
baseURL := "http://tsn.baidu.com/text2audio"
|
||||
urlObject, _ := url.Parse(baseURL)
|
||||
|
||||
queries := url.Values{}
|
||||
queries.Add("tex", text)
|
||||
queries.Add("lan", "zh")
|
||||
queries.Add("cuid", strconv.FormatInt(time.Now().Unix(), 10))
|
||||
queries.Add("ctp", "1")
|
||||
queries.Add("tok", token)
|
||||
queries.Add("per", strconv.Itoa(person))
|
||||
queries.Add("spd", strconv.Itoa(speed))
|
||||
queries.Add("pit", strconv.Itoa(pitch))
|
||||
urlObject.RawQuery = queries.Encode()
|
||||
return urlObject
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user