Newer
Older
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
package googleapi
import (
"net/http"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
texttospeech "google.golang.org/api/texttospeech/v1beta1"
)
//TextConfig Google Text-to-Speech config
type TextConfig struct {
Text string
LanguageCode string
AudioEncoding string
}
//TextToSpeech get the text to speech
func TextToSpeech(client *http.Client, c TextConfig) (string, error) {
loggermdl.LogInfo("IN TextToSpeech")
texttospeechService, err := texttospeech.New(client)
if c.AudioEncoding == "" {
c.AudioEncoding = "MP3"
}
if c.LanguageCode == "" {
c.LanguageCode = "en-IN"
}
if err != nil {
loggermdl.LogError("texttospeechService Error: ", err)
loggermdl.LogInfo("OUT TextToSpeech")
return "", err
}
ttsConfig := texttospeech.AudioConfig{
AudioEncoding: c.AudioEncoding,
}
inputText := texttospeech.SynthesisInput{
Text: c.Text,
}
voiceParams := texttospeech.VoiceSelectionParams{
LanguageCode: c.LanguageCode,
}
ttsSpeechReq := texttospeech.SynthesizeSpeechRequest{
AudioConfig: &ttsConfig,
Input: &inputText,
Voice: &voiceParams,
}
ttsSpeechCall := texttospeechService.Text.Synthesize(&ttsSpeechReq)
syncResponse, err := ttsSpeechCall.Do()
if err != nil {
loggermdl.LogError("ttsSpeechCall Error", err)
loggermdl.LogInfo("OUT TextToSpeech")
return "", err
}
resp, err := syncResponse.MarshalJSON()
if err != nil {
loggermdl.LogError("syncResponse Error", err)
loggermdl.LogInfo("OUT TextToSpeech")
return "", err
}
loggermdl.LogInfo("OUT TextToSpeech")
return string(resp), nil
}