-
Notifications
You must be signed in to change notification settings - Fork 1
/
gonsspeech.go
152 lines (129 loc) · 3.04 KB
/
gonsspeech.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package gonsspeech
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework AppKit
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
NSSpeechSynthesizer *speechSynth = NULL;
int NsSpeechInit(){
if(speechSynth) return -1;
speechSynth=[[NSSpeechSynthesizer alloc] init];
[speechSynth setRate:240];
[speechSynth setVolume:1.0];
return 1;
}
int NsSpeechFree(){
if(!speechSynth) return -1;
speechSynth=NULL;
return 1;
}
int NsSpeechSpeak(char *str){
if(!speechSynth) return -1;
NSString *nsstr = [NSString stringWithCString: str encoding:NSUTF8StringEncoding];
[speechSynth startSpeakingString:nsstr];
usleep(5000);
return 1;
}
int NsSpeechStop(){
if(!speechSynth) return -1;
[speechSynth stopSpeaking];
return 1;
}
int NsSpeechSetRate(float rate){
if(!speechSynth) return -1;
if(rate<=0) return -2;
[speechSynth setRate:rate];
return 1;
}
float NsSpeechGetRate(){
if(!speechSynth) return -1;
return [speechSynth rate];
}
int NsSpeechSetVolume(float volume){
if(!speechSynth) return -1;
if(volume<0 || volume>1) return -2;
[speechSynth setVolume:volume];
return 1;
}
float NsSpeechGetVolume(){
if(!speechSynth) return -1;
return [speechSynth volume];
}
int NsSpeechIsSpeaking(){
if(!speechSynth) return -1;
return [speechSynth isSpeaking] == YES ? 1 : 0;
}
*/
import "C"
import (
"errors"
)
func NsSpeechInit() error {
ret := C.NsSpeechInit()
if ret == -1 {
return errors.New("Cannot initialize NsSpeechSynthesizer interface.")
}
return nil
}
func NsSpeechSpeak(text string) error {
ret := C.NsSpeechSpeak(C.CString(text))
if ret == -1 {
return errors.New("NsSpeechSynthesizer interface has not been initialized.")
}
return nil
}
func NsSpeechStop() error {
ret := C.NsSpeechStop()
if ret == -1 {
return errors.New("NsSpeechSynthesizer interface has not been initialized.")
}
return nil
}
func NsSpeechSetRate(rate float64) error {
ret := C.NsSpeechSetRate(C.float(rate))
if ret == -1 {
return errors.New("NsSpeechSynthesizer interface has not been initialized.")
}
if ret == -2 {
return errors.New("rate value is out of range")
}
return nil
}
func NsSpeechGetRate() (float64, error) {
ret := C.NsSpeechGetRate()
if ret == -1 {
return 0.0, errors.New("NsSpeechSynthesizer interface has not been initialized.")
}
return float64(ret), nil
}
func NsSpeechSetVolume(volume float64) error {
ret := C.NsSpeechSetVolume(C.float(volume))
if ret == -1 {
return errors.New("NsSpeechSynthesizer interface has not been initialized.")
}
if ret == -2 {
return errors.New("volume value is out of range")
}
return nil
}
func NsSpeechGetVolume() (float64, error) {
ret := C.NsSpeechGetVolume()
if ret == -1 {
return 0.0, errors.New("NsSpeechSynthesizer interface has not been initialized.")
}
return float64(ret), nil
}
func NsSpeechIsSpeaking() (bool, error) {
switch C.NsSpeechIsSpeaking() {
case -1:
return false, errors.New("NsSpeechSynthesizer interface has not been initialized.")
case 0:
return false, nil
case 1:
return true, nil
}
return false, errors.New("Unrecognized error.")
}
func NsSpeechFree() {
C.NsSpeechFree()
}