-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (99 loc) · 2.67 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
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
97
98
99
100
101
102
103
104
105
package main
import (
speech "cloud.google.com/go/speech/apiv1"
texttospeech "cloud.google.com/go/texttospeech/apiv1"
"context"
"fmt"
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1"
texttospeechpb "google.golang.org/genproto/googleapis/cloud/texttospeech/v1"
"io/ioutil"
"log"
"os"
"path/filepath"
)
var filePath string
func init() {
if len(os.Args) > 1 {
filePath = os.Args[1]
} else {
os.Exit(0)
}
}
func main() {
switch filepath.Ext(filePath) {
case ".mp3":
speechToText()
case ".txt":
textToSpeech()
default:
log.Fatalf("Error: %s is not supported.\n", filePath)
}
}
func speechToText() {
ctx := context.Background()
// Creates a client.
client, err := speech.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Detects speech in the audio file.
resp, err := client.Recognize(ctx, &speechpb.RecognizeRequest{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: 16000,
LanguageCode: "en-US",
},
Audio: &speechpb.RecognitionAudio{
AudioSource: &speechpb.RecognitionAudio_Uri{Uri: filePath},
},
})
if err != nil {
log.Fatalf("failed to recognize: %v", err)
}
// Prints the results.
for _, result := range resp.Results {
for _, alt := range result.Alternatives {
fmt.Printf("\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence)
}
}
}
func textToSpeech() {
// Read the file
content, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
// Instantiates a client.
ctx := context.Background()
client, err := texttospeech.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
// Perform the text-to-speech request on the text input with the selected, voice parameters and audio file type.
req := texttospeechpb.SynthesizeSpeechRequest{
// Set the text input to be synthesized.
Input: &texttospeechpb.SynthesisInput{
InputSource: &texttospeechpb.SynthesisInput_Text{Text: string(content)},
},
// Build the voice request, select the language code ("en-US") and the SSML, voice gender ("neutral").
Voice: &texttospeechpb.VoiceSelectionParams{
LanguageCode: "en-US",
SsmlGender: texttospeechpb.SsmlVoiceGender_NEUTRAL,
},
// Select the type of audio file you want returned.
AudioConfig: &texttospeechpb.AudioConfig{
AudioEncoding: texttospeechpb.AudioEncoding_MP3,
},
}
resp, err := client.SynthesizeSpeech(ctx, &req)
if err != nil {
log.Fatal(err)
}
// The resp's AudioContent is binary.
filename := fmt.Sprintf(filePath, ".mp3")
err = ioutil.WriteFile(filename, resp.AudioContent, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Audio content written to file: %v\n", filename)
}