-
Notifications
You must be signed in to change notification settings - Fork 0
/
xf_ui_test.go
170 lines (144 loc) · 3.33 KB
/
xf_ui_test.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
package xf
import (
"bytes"
"encoding/json"
"io"
"log"
"os"
"testing"
"time"
)
func receiveEvent(evt *Event) {
// log.Printf("evt: %#v", evt)
switch evt.EventType() {
case EventState:
state := (StateType)(evt.Arg1())
switch state {
case StateIdle:
log.Printf("EventState: %s\n", "IDLE")
case StateReady:
log.Printf("EventState: %s\n", "READY")
case StateWorking:
log.Printf("EventState: %s\n", "WORKING")
default:
log.Printf("InvalidState %d", state)
}
case EventVad:
vad := (VadType)(evt.Arg1())
switch vad {
case VadBos:
log.Printf("VadState: %s\n", "检测到开始")
case VadEos:
log.Printf("VadState: %s\n", "检测到结束")
case VadVol:
//
}
case EventResult:
info := evt.Info()
var (
result AIUIResult
data AIUIResultData
)
if err := json.Unmarshal([]byte(info), &result); err != nil {
log.Printf("解析 Result 错误: %s", err)
}
data = result.Data[0]
if data.Params.Sub == "nlp" {
contentId := data.Content[0].CNT_ID
if len(contentId) == 0 {
log.Printf("missing contentId")
}
buffer := evt.Data().GetBinary(contentId)
var out bytes.Buffer
var buff = buffer.Data()
buff = buff[:len(buff)-1]
if err := json.Indent(&out, buff, "", " "); err != nil {
log.Printf("格式化内容错误: %s", err)
}
log.Printf("output: %s", out.String())
}
case EventError:
log.Printf("Error Code : %d", evt.Arg1())
default:
log.Printf("消息 EventType: %d", evt.EventType())
}
}
func TestXFUI(t *testing.T) {
file, err := os.Open("aiui.cfg") // For read access.
if err != nil {
log.Fatal(err)
}
dec := json.NewDecoder(file)
var cfg map[string]interface{}
for {
if err := dec.Decode(&cfg); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
}
cfg["login"] = map[string]interface{}{
"appid": "58c51121",
}
buf, err := json.Marshal(cfg)
if err != nil {
log.Fatal(err)
}
listener := NewListener(receiveEvent)
log.Printf("Listener %#v", listener)
agent := NewAgent(string(buf), listener)
log.Printf("Agent %#v", agent)
// agent.Start()
time.Sleep(time.Second)
agent.Weakup()
msg := NewMessageText(CmdWrite, "你好")
agent.SendMessage(msg)
msg.Destroy()
time.Sleep(3 * time.Second)
}
func TestXFUI_Wav(t *testing.T) {
li := NewListener(receiveEvent)
cfile, err := os.Open("aiui.cfg") // For read access.
if err != nil {
log.Fatal(err)
}
dec := json.NewDecoder(cfile)
var cfg map[string]interface{}
for {
if err := dec.Decode(&cfg); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
}
cfg["login"] = map[string]interface{}{
"appid": appId,
}
buf, err := json.Marshal(cfg)
if err != nil {
log.Fatal(err)
}
agent := NewAgent(string(buf), li)
time.Sleep(time.Second)
agent.Weakup()
file, err := os.Open("test/test.pcm")
if err != nil {
log.Fatalf("打开测试文件失败 %s", err)
}
var pcm = make([]byte, 1279)
for _, err := file.Read(pcm); err != io.EOF; _, err = file.Read(pcm) {
msg := NewMessageBytes(CmdWrite, "data_type=audio,sample_rate=16000", pcm)
agent.SendMessage(msg)
msg.Destroy()
time.Sleep(10 * time.Millisecond)
}
msg := NewMessageBytes(CmdStopWrite, "data_type=audio,sample_rate=16000", nil)
agent.SendMessage(msg)
msg.Destroy()
time.Sleep(3 * time.Second)
}
func init() {
if a := os.Getenv("APPID"); len(a) > 0 {
appId = a
}
}