-
Notifications
You must be signed in to change notification settings - Fork 0
/
speechAPI.go
96 lines (91 loc) · 2.52 KB
/
speechAPI.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Sample speech-quickstart uses the Google Cloud Speech API to transcribe
// audio.
package googleApi
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
// Imports the Google Cloud Speech API client package.
"golang.org/x/net/context"
speech "cloud.google.com/go/speech/apiv1"
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1"
)
func CreateDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
}
}
func SpeechAPI() {
fmt.Println("====Start shell Script====")
cmdStr := "./shell/convertVoiveFile.sh"
cmd := exec.Command("bash", cmdStr)
_, err := cmd.Output()
if err != nil {
log.Println(err.Error())
return
}
fmt.Println("====Create Client====")
ctx := context.Background()
// Creates a client.
client, err := speech.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
fmt.Println("====Search Audio File====")
i := 1
for {
var pathFile string
pathFile = "./audioFile_python/test" + strconv.Itoa(i) + ".flac"
fmt.Println(pathFile)
if _, err := os.Stat(pathFile); err == nil {
// Reads the audio file into memory.
data, err := ioutil.ReadFile(pathFile)
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
// Detects speech in the audio file.
resp, err := client.Recognize(ctx, &speechpb.RecognizeRequest{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_FLAC,
SampleRateHertz: 16000,
LanguageCode: "en-US",
},
Audio: &speechpb.RecognitionAudio{
AudioSource: &speechpb.RecognitionAudio_Content{Content: data},
},
})
if err != nil {
log.Fatalf("failed to recognize: %v", err)
}
// Prints the results.
fmt.Printf("Response" + strconv.Itoa(i) + "\nresult.txt file create\n")
createFile := "./resultText/result" + strconv.Itoa(i) + ".txt"
CreateDirIfNotExist("./resultText")
f, err := os.Create(createFile)
check(err)
defer f.Close()
w := bufio.NewWriter(f)
for _, result := range resp.Results {
for _, alt := range result.Alternatives {
w.WriteString(alt.Transcript + "\n")
// fmt.Printf("\"%v\" (confidence=%3f)\n", , alt.Confidence)
}
w.Flush()
}
fmt.Println(" Clear " + strconv.Itoa(i))
i++
} else {
fmt.Println("No File" + strconv.Itoa(i))
break
}
}
// Sets the name of the audio file to transcribe.
// filename := "/Users/jinhyuk/go/src/github.com/JinHyukParkk/CapstoneProject/audioFile/res.flac"
}