/
dialog_service_connector.go
272 lines (246 loc) · 10.5 KB
/
dialog_service_connector.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
package dialog
import (
"unsafe"
"github.com/Microsoft/cognitive-services-speech-sdk-go/audio"
"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
"github.com/Microsoft/cognitive-services-speech-sdk-go/speech"
)
// #include <stdlib.h>
// #include <speechapi_c_common.h>
// #include <speechapi_c_factory.h>
// #include <speechapi_c_dialog_service_connector.h>
//
// /* Proxy functions forward declarations */
// void cgo_dialog_session_started(SPXRECOHANDLE handle, SPXEVENTHANDLE event, void* context);
// void cgo_dialog_session_stopped(SPXRECOHANDLE handle, SPXEVENTHANDLE event, void* context);
// void cgo_dialog_recognized(SPXRECOHANDLE handle, SPXEVENTHANDLE event, void* context);
// void cgo_dialog_recognizing(SPXRECOHANDLE handle, SPXEVENTHANDLE event, void* context);
// void cgo_dialog_canceled(SPXRECOHANDLE handle, SPXEVENTHANDLE event, void* context);
// void cgo_dialog_activity_received(SPXRECOHANDLE handle, SPXEVENTHANDLE event, void* context);
import "C"
// DialogServiceConnector connects to a speech enabled dialog backend.
type DialogServiceConnector struct {
Properties common.PropertyCollection
handle C.SPXHANDLE
}
func newDialogServiceConnectorFromHandle(handle C.SPXHANDLE) (*DialogServiceConnector, error) {
var propBagHandle C.SPXHANDLE
ret := uintptr(C.dialog_service_connector_get_property_bag(handle, &propBagHandle))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
connector := new(DialogServiceConnector)
connector.handle = handle
connector.Properties = common.NewPropertyCollectionFromHandle(handle2uintptr(propBagHandle))
return connector, nil
}
// NewDialogServiceConnectorFromConfig creates a dialog service connector from a dialog service config and an audio config.
// Users should use this function to create a dialog service connector.
func NewDialogServiceConnectorFromConfig(config DialogServiceConfig, audioConfig *audio.AudioConfig) (*DialogServiceConnector, error) {
var handle C.SPXHANDLE
if config == nil {
return nil, common.NewCarbonError(uintptr(C.SPXERR_INVALID_ARG))
}
configHandle := config.getHandle()
var audioHandle C.SPXHANDLE
if audioConfig == nil {
audioHandle = nil
} else {
audioHandle = uintptr2handle(audioConfig.GetHandle())
}
ret := uintptr(C.dialog_service_connector_create_dialog_service_connector_from_config(&handle, configHandle, audioHandle))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
return newDialogServiceConnectorFromHandle(handle)
}
// Close performs cleanup of resources.
func (connector DialogServiceConnector) Close() {
connector.Properties.Close()
C.dialog_service_connector_handle_release(connector.handle)
}
// ConnectAsync connects with the back end.
func (connector DialogServiceConnector) ConnectAsync() chan error {
outcome := make(chan error)
go func() {
ret := uintptr(C.dialog_service_connector_connect(connector.handle))
if ret != C.SPX_NOERROR {
outcome <- common.NewCarbonError(ret)
} else {
outcome <- nil
}
}()
return outcome
}
// DisconnectAsync disconnects from the back end.
func (connector DialogServiceConnector) DisconnectAsync() chan error {
outcome := make(chan error)
go func() {
ret := uintptr(C.dialog_service_connector_disconnect(connector.handle))
if ret != C.SPX_NOERROR {
outcome <- common.NewCarbonError(ret)
} else {
outcome <- nil
}
}()
return outcome
}
type SendActivityOutcome struct {
common.OperationOutcome
// InteractionID is the identifier associated with the interaction
InteractionID string
}
// SendActivityAsync sends an activity to the backing dialog.
func (connector DialogServiceConnector) SendActivityAsync(message string) chan SendActivityOutcome {
outcome := make(chan SendActivityOutcome)
go func() {
msg := C.CString(message)
defer C.free(unsafe.Pointer(msg))
buffer := C.malloc(C.sizeof_char * 37)
defer C.free(unsafe.Pointer(buffer))
ret := uintptr(C.dialog_service_connector_send_activity(connector.handle, msg, (*C.char)(buffer)))
if ret != C.SPX_NOERROR {
outcome <- SendActivityOutcome{InteractionID: "", OperationOutcome: common.OperationOutcome{common.NewCarbonError(ret)}}
} else {
interactionID := C.GoString((*C.char)(buffer))
outcome <- SendActivityOutcome{InteractionID: interactionID, OperationOutcome: common.OperationOutcome{nil}}
}
}()
return outcome
}
// ListenOnceAsync starts a listening session that will terminate after the first utterance.
func (connector DialogServiceConnector) ListenOnceAsync() <-chan speech.SpeechRecognitionOutcome {
outcome := make(chan speech.SpeechRecognitionOutcome)
go func() {
var handle C.SPXRESULTHANDLE
ret := uintptr(C.dialog_service_connector_listen_once(connector.handle, &handle))
if ret != C.SPX_NOERROR {
outcome <- speech.SpeechRecognitionOutcome{Result: nil, OperationOutcome: common.OperationOutcome{common.NewCarbonError(ret)}}
} else {
result, err := speech.NewSpeechRecognitionResultFromHandle(handle2uintptr(handle))
outcome <- speech.SpeechRecognitionOutcome{Result: result, OperationOutcome: common.OperationOutcome{err}}
}
}()
return outcome
}
// StartKeywordRecognitionAsync initiates keyword recognition.
func (connector DialogServiceConnector) StartKeywordRecognitionAsync(model *speech.KeywordRecognitionModel) chan error {
modelHandle := uintptr2handle(model.GetHandle())
outcome := make(chan error)
go func() {
ret := uintptr(C.dialog_service_connector_start_keyword_recognition(connector.handle, modelHandle))
if ret != C.SPX_NOERROR {
outcome <- common.NewCarbonError(ret)
} else {
outcome <- nil
}
}()
return outcome
}
// StopKeywordRecognitionAsync stops keyword recognition.
func (connector DialogServiceConnector) StopKeywordRecognitionAsync() chan error {
outcome := make(chan error)
go func() {
ret := uintptr(C.dialog_service_connector_stop_keyword_recognition(connector.handle))
if ret != C.SPX_NOERROR {
outcome <- common.NewCarbonError(ret)
} else {
outcome <- nil
}
}()
return outcome
}
// SetAuthorizationToken sets the authorization token that will be used for connecting to the service.
// Note: The caller needs to ensure that the authorization token is valid. Before the authorization token
// expires, the caller needs to refresh it by calling this setter with a new valid token.
// Otherwise, the connector will encounter errors during its operation.
func (connector DialogServiceConnector) SetAuthorizationToken(token string) error {
return connector.Properties.SetProperty(common.SpeechServiceAuthorizationToken, token)
}
// AuthorizationToken is the authorization token.
func (connector DialogServiceConnector) AuthorizationToken() string {
return connector.Properties.GetProperty(common.SpeechServiceAuthorizationToken, "")
}
// SetSpeechActivityTemplate sets the speech activity template. It is used to stamp properties from the template on the service generated
// activty for speech.
func (connector DialogServiceConnector) SetSpeechActivityTemplate(activityTemplate string) error {
return connector.Properties.SetProperty(common.ConversationSpeechActivityTemplate, activityTemplate)
}
// SpeechActivityTemplate is the used to stamp properties from the template on the service generated activities.
func (connector DialogServiceConnector) SpeechActivityTemplate() string {
return connector.Properties.GetProperty(common.ConversationSpeechActivityTemplate, "")
}
// Recognized signals events containing speech recognition results.
func (connector DialogServiceConnector) Recognized(handler speech.SpeechRecognitionEventHandler) {
registerRecognizedCallback(handler, connector.handle)
if handler != nil {
C.dialog_service_connector_recognized_set_callback(
connector.handle,
(C.PRECOGNITION_CALLBACK_FUNC)(unsafe.Pointer(C.cgo_dialog_recognized)),
nil)
} else {
C.dialog_service_connector_recognized_set_callback(connector.handle, nil, nil)
}
}
// Recognizing signals events containing intermediate recognition results.
func (connector DialogServiceConnector) Recognizing(handler speech.SpeechRecognitionEventHandler) {
registerRecognizingCallback(handler, connector.handle)
if handler != nil {
C.dialog_service_connector_recognizing_set_callback(
connector.handle,
(C.PRECOGNITION_CALLBACK_FUNC)(unsafe.Pointer(C.cgo_dialog_recognizing)),
nil)
} else {
C.dialog_service_connector_recognizing_set_callback(connector.handle, nil, nil)
}
}
// SessionStarted signals the start of a listening session.
func (connector DialogServiceConnector) SessionStarted(handler speech.SessionEventHandler) {
registerSessionStartedCallback(handler, connector.handle)
if handler != nil {
C.dialog_service_connector_session_started_set_callback(
connector.handle,
(C.PSESSION_CALLBACK_FUNC)(unsafe.Pointer(C.cgo_dialog_session_started)),
nil)
} else {
C.dialog_service_connector_session_started_set_callback(connector.handle, nil, nil)
}
}
// SessionStopped signals the end of a listening session.
func (connector DialogServiceConnector) SessionStopped(handler speech.SessionEventHandler) {
registerSessionStoppedCallback(handler, connector.handle)
if handler != nil {
C.dialog_service_connector_session_stopped_set_callback(
connector.handle,
(C.PSESSION_CALLBACK_FUNC)(unsafe.Pointer(C.cgo_dialog_session_stopped)),
nil)
} else {
C.dialog_service_connector_session_stopped_set_callback(connector.handle, nil, nil)
}
}
// Canceled signals events relating to the cancellation of an interaction. The event indicates if the reason is a direct cancellation or an error.
func (connector DialogServiceConnector) Canceled(handler speech.SpeechRecognitionCanceledEventHandler) {
registerCanceledCallback(handler, connector.handle)
if handler != nil {
C.dialog_service_connector_canceled_set_callback(
connector.handle,
(C.PRECOGNITION_CALLBACK_FUNC)(unsafe.Pointer(C.cgo_dialog_canceled)),
nil)
} else {
C.dialog_service_connector_canceled_set_callback(connector.handle, nil, nil)
}
}
// ActivityReceived signals that an activity was received from the backend.
func (connector DialogServiceConnector) ActivityReceived(handler ActivityReceivedEventHandler) {
registerActivityReceivedCallback(handler, connector.handle)
if handler != nil {
C.dialog_service_connector_activity_received_set_callback(
connector.handle,
(C.PRECOGNITION_CALLBACK_FUNC)(unsafe.Pointer(C.cgo_dialog_activity_received)),
nil)
} else {
C.dialog_service_connector_activity_received_set_callback(connector.handle, nil, nil)
}
}