-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
64 lines (47 loc) · 1.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"log"
"bytes"
"regexp"
"net/http"
"os"
"io/ioutil"
"encoding/json"
"github.com/aperturescience/go-speech/utils"
)
func main() {
var err error
// 1. read audio file in memory
audio, err := ioutil.ReadFile("test.wav")
if err != nil {
log.Fatalf("Error reading file:\n%v\n", err)
}
reader := bytes.NewReader(audio)
// 2. send binary data to API endpoint
endpoint := "https://www.google.com/speech-api/v2/recognize?"
params := map[string]string {
"output" : "json",
"lang" : "en-us",
"key" : os.Getenv("GOOGLE_SPEECH_API_KEY"),
}
resp, err := http.Post(endpoint + utils.Queryify(params), "audio/l16; rate=16000;", reader)
if err != nil {
log.Fatalf("Error POST-ing data to Google Speech Endpoint:\n %v\n", err)
}
// 3. read response from API and unmarshal to struct
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
log.Fatalf("Google Speech Error:\n%v\n", string(body))
}
// 4. remove false response
rgx := regexp.MustCompile("{\"result\":\\[\\]}")
sanitized := rgx.ReplaceAllLiteralString(string(body), "")
// 5. create response struct via unmarshal
var response utils.Response
err = json.Unmarshal([]byte(sanitized), &response)
if err != nil {
log.Fatalf("Error Unmarshaling JSON:\n%v\n", err)
}
fmt.Printf("%+v\n", response)
}