/
connect.go
228 lines (191 loc) · 4.26 KB
/
connect.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package bouyomichan
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"net"
)
type Command int16
const (
CommandStart Command = 1
CommandPause Command = iota * 1 * 16
CommandResume
CommandSkip
CommandClear
)
type Voices int16
const (
// Voice 1:女性1、2:女性2、3:男性1、4:男性2、5:中性、6:ロボット、7:機械1、8:機械2、10001~:SAPI5)
VoiceDefault Voices = iota
VoiceWoman01
VoiceWoman02
VoiceMan01
VoiceMan02
VoiceNeutral
VoiceRobot01
VoiceRobot02
VoiceRobot03
VoiceLocal
)
type Code int8
// Code... 文字コード
const (
CodeUTF8 Code = iota
CodeUnicode
CodeShiftJIS
)
type Client struct {
addr string
Speed int16
Tone int16
Volume int16
Voice Voices
Code Code
}
func New(addr string) *Client {
return &Client{
addr: addr,
// Default
Speed: 100,
Tone: -1,
Volume: 20,
Voice: VoiceDefault,
Code: CodeUTF8,
}
}
// Speaking ローカルなりに棒読みちゃんアプリが立ち上げられ、かつ、設定のアプリケーション連携がTrue担っていることが前提
// 棒読みちゃんからEOFが返ってくるため常々接続
func (p *Client) Speaking(msg string) error {
conn, err := net.Dial("tcp", p.addr)
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Write(
verbalizing(
[]byte(msg), p.Speed, p.Tone, p.Volume, p.Voice, p.Code,
),
)
if err != nil {
log.Println("[ERROR] ", err)
return err
}
// READ EOF
var buf = make([]byte, 16)
n, _ := conn.Read(buf)
if n != 0 {
log.Println("[INFO] READ: ", n, buf)
}
return nil
}
// verbalizing tcp websocket用byteを作る
// 各パラメータ引数未設定
func verbalizing(message []byte, speed, tone, volume int16, voice Voices, bcode Code) []byte {
msg := string(message)
msg_length := uint32(len(msg))
// var iCommand int16 = int16(1)
// var d = make([]byte, 2)
// binary.BigEndian.PutUint16(d, uint16(iCommand))
// log.Println("command: ", d)
d := []byte{1, 0}
var tmp = make([]byte, 2)
if speed != -1 && speed < 50 || speed > 300 {
speed = -1
}
binary.LittleEndian.PutUint16(tmp, uint16(speed))
d = append(d, tmp...)
if tone != -1 && tone < 50 || tone > 200 {
tone = -1
}
binary.LittleEndian.PutUint16(tmp, uint16(tone))
d = append(d, tmp...)
if volume != -1 && volume < -1 || volume > 100 {
volume = -1
}
binary.LittleEndian.PutUint16(tmp, uint16(volume))
d = append(d, tmp...)
binary.LittleEndian.PutUint16(tmp, uint16(voice))
d = append(d, tmp...)
binary.BigEndian.PutUint16(tmp, uint16(bcode))
d = append(d, tmp[1])
bMsglength := make([]byte, 4)
binary.LittleEndian.PutUint32(bMsglength, msg_length)
d = append(d, bMsglength...)
d = append(d, []byte(msg)...)
return d
}
func (p *Client) IsNowPlayng() bool {
conn, err := net.Dial("tcp", p.addr)
if err != nil {
return false
}
defer conn.Close()
var tmp = make([]byte, 2)
binary.LittleEndian.PutUint16(tmp, uint16(288))
_, err = conn.Write(
tmp,
)
if err != nil {
log.Println("[ERROR] ", err)
return false
}
// READ EOF
var buf = make([]byte, 16)
n, _ := conn.Read(buf)
temp := bytes.NewBuffer(buf[:n]) // b is []byte
result, _ := binary.ReadVarint(temp)
if result != 0 {
log.Println("[INFO] Now playng: ", n, result)
return true
}
return false
}
func (p *Client) Pause() error {
conn, err := net.Dial("tcp", p.addr)
if err != nil {
return nil
}
defer conn.Close()
var tmp = make([]byte, 2)
binary.LittleEndian.PutUint16(tmp, uint16(272))
_, err = conn.Write(
tmp,
)
if err != nil {
return fmt.Errorf("[ERROR] %v", err)
}
// READ EOF
var buf = make([]byte, 16)
n, _ := conn.Read(buf)
temp := bytes.NewBuffer(buf[:n]) // b is []byte
result, _ := binary.ReadVarint(temp)
if result != 0 {
return fmt.Errorf("[INFO] Pause: %d, %v", n, result)
}
return nil
}
func (p *Client) Stop() error {
conn, err := net.Dial("tcp", p.addr)
if err != nil {
return nil
}
defer conn.Close()
var tmp = make([]byte, 2)
binary.LittleEndian.PutUint16(tmp, uint16(64))
_, err = conn.Write(
tmp,
)
if err != nil {
return fmt.Errorf("[ERROR] %v", err)
}
// READ EOF
var buf = make([]byte, 16)
n, _ := conn.Read(buf)
temp := bytes.NewBuffer(buf[:n]) // b is []byte
result, _ := binary.ReadVarint(temp)
if result != 0 {
return fmt.Errorf("[INFO] Stop: %d, %v", n, result)
}
return nil
}