/
voice_profile_client.go
245 lines (224 loc) · 9.38 KB
/
voice_profile_client.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
package speaker
import (
"strings"
"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_speaker_recognition.h>
import "C"
// VoiceProfileClient connects to a speaker recognition backend.
type VoiceProfileClient struct {
Properties *common.PropertyCollection
handle C.SPXHANDLE
}
func newVoiceProfileClientFromHandle(handle C.SPXHANDLE) (*VoiceProfileClient, error) {
var propBagHandle C.SPXHANDLE
ret := uintptr(C.voice_profile_client_get_property_bag(handle, &propBagHandle))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
client := new(VoiceProfileClient)
client.handle = handle
client.Properties = common.NewPropertyCollectionFromHandle(handle2uintptr(propBagHandle))
return client, nil
}
// NewVoiceProfileClientFromConfig creates a voice profile service client from a speech config.
// Users should use this function to create a voice profile client.
func NewVoiceProfileClientFromConfig(config *speech.SpeechConfig) (*VoiceProfileClient, error) {
var handle C.SPXHANDLE
if config == nil {
return nil, common.NewCarbonError(uintptr(C.SPXERR_INVALID_ARG))
}
configHandle := config.GetHandle()
ret := uintptr(C.create_voice_profile_client_from_config(&handle, (uintptr2handle(configHandle))))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
return newVoiceProfileClientFromHandle(handle)
}
// Close performs cleanup of resources.
func (client VoiceProfileClient) Close() {
client.Properties.Close()
C.voice_profile_client_release_handle(client.handle)
}
type CreateProfileOutcome struct {
common.OperationOutcome
Profile *VoiceProfile
}
// Close releases the underlying resources
func (outcome CreateProfileOutcome) Close() {
if outcome.Profile != nil {
outcome.Profile.Close()
}
}
// CreateProfileAsync attempts to create a new voice profile on the service.
func (client VoiceProfileClient) CreateProfileAsync(profileType common.VoiceProfileType, locale string) chan CreateProfileOutcome {
outcome := make(chan CreateProfileOutcome)
go func() {
var profileHandle C.SPXHANDLE
loc := C.CString(locale)
defer C.free(unsafe.Pointer(loc))
ret := uintptr(C.create_voice_profile(client.handle, (C.int)(profileType), loc, &profileHandle))
if ret != C.SPX_NOERROR {
outcome <- CreateProfileOutcome{Profile: nil, OperationOutcome: common.OperationOutcome{common.NewCarbonError(ret)}}
} else {
newProfile, err := newVoiceProfileFromHandle(handle2uintptr(profileHandle))
if err != nil {
outcome <- CreateProfileOutcome{Profile: nil, OperationOutcome: common.OperationOutcome{err}}
} else {
outcome <- CreateProfileOutcome{Profile: newProfile, OperationOutcome: common.OperationOutcome{nil}}
}
}
}()
return outcome
}
// DeleteProfileAsync sends a profile delete request to the service.
func (client VoiceProfileClient) DeleteProfileAsync(profile *VoiceProfile) <-chan VoiceProfileOutcome {
outcome := make(chan VoiceProfileOutcome)
go func() {
var handle C.SPXRESULTHANDLE
profileHandle := profile.GetHandle()
ret := uintptr(C.delete_voice_profile(client.handle, uintptr2handle(profileHandle), &handle))
if ret != C.SPX_NOERROR {
outcome <- VoiceProfileOutcome{Result: nil, OperationOutcome: common.OperationOutcome{common.NewCarbonError(ret)}}
} else {
result, err := newVoiceProfileResultFromHandle(handle2uintptr(handle))
outcome <- VoiceProfileOutcome{Result: result, OperationOutcome: common.OperationOutcome{err}}
}
}()
return outcome
}
// ResetProfileAsync sends a profile reset request to the service.
func (client VoiceProfileClient) ResetProfileAsync(profile *VoiceProfile) <-chan VoiceProfileOutcome {
outcome := make(chan VoiceProfileOutcome)
go func() {
var handle C.SPXRESULTHANDLE
profileHandle := profile.GetHandle()
ret := uintptr(C.reset_voice_profile(client.handle, uintptr2handle(profileHandle), &handle))
if ret != C.SPX_NOERROR {
outcome <- VoiceProfileOutcome{Result: nil, OperationOutcome: common.OperationOutcome{common.NewCarbonError(ret)}}
} else {
result, err := newVoiceProfileResultFromHandle(handle2uintptr(handle))
outcome <- VoiceProfileOutcome{Result: result, OperationOutcome: common.OperationOutcome{err}}
}
}()
return outcome
}
// GetActivationPhrasesAsync returns a result containing a list of activation phrases required for voice profile enrollment.
func (client VoiceProfileClient) GetActivationPhrasesAsync(profileType common.VoiceProfileType, locale string) chan VoiceProfilePhraseOutcome {
outcome := make(chan VoiceProfilePhraseOutcome)
go func() {
var handle C.SPXRESULTHANDLE
loc := C.CString(locale)
defer C.free(unsafe.Pointer(loc))
ret := uintptr(C.get_activation_phrases(client.handle, loc, (C.int)(profileType), &handle))
if ret != C.SPX_NOERROR {
outcome <- VoiceProfilePhraseOutcome{Result: nil, OperationOutcome: common.OperationOutcome{common.NewCarbonError(ret)}}
} else {
newResult, err := newVoiceProfilePhraseResultFromHandle(handle2uintptr(handle))
if err != nil {
outcome <- VoiceProfilePhraseOutcome{Result: nil, OperationOutcome: common.OperationOutcome{err}}
} else {
outcome <- VoiceProfilePhraseOutcome{Result: newResult, OperationOutcome: common.OperationOutcome{nil}}
}
}
}()
return outcome
}
// EnrollProfileAsync sends audio for voice profile enrollment returns a result detailing enrollment status for the given profile
func (client VoiceProfileClient) EnrollProfileAsync(profile *VoiceProfile, audioConfig *audio.AudioConfig) chan VoiceProfileEnrollmentOutcome {
outcome := make(chan VoiceProfileEnrollmentOutcome)
go func() {
var handle C.SPXRESULTHANDLE
profileHandle := profile.GetHandle()
var audioHandle C.SPXHANDLE
if audioConfig == nil {
audioHandle = nil
} else {
audioHandle = uintptr2handle(audioConfig.GetHandle())
}
ret := uintptr(C.enroll_voice_profile(client.handle, uintptr2handle(profileHandle), audioHandle, &handle))
if ret != C.SPX_NOERROR {
outcome <- VoiceProfileEnrollmentOutcome{Result: nil, OperationOutcome: common.OperationOutcome{common.NewCarbonError(ret)}}
} else {
newResult, err := newVoiceProfileEnrollmentResultFromHandle(handle2uintptr(handle))
if err != nil {
outcome <- VoiceProfileEnrollmentOutcome{Result: nil, OperationOutcome: common.OperationOutcome{err}}
} else {
outcome <- VoiceProfileEnrollmentOutcome{Result: newResult, OperationOutcome: common.OperationOutcome{nil}}
}
}
}()
return outcome
}
// RetrieveEnrollmentResultAsync returns a result detailing enrollment status for the given profile
func (client VoiceProfileClient) RetrieveEnrollmentResultAsync(profile *VoiceProfile) chan VoiceProfileEnrollmentOutcome {
outcome := make(chan VoiceProfileEnrollmentOutcome)
go func() {
var handle C.SPXRESULTHANDLE
id, err := profile.Id()
if err != nil {
outcome <- VoiceProfileEnrollmentOutcome{Result: nil, OperationOutcome: common.OperationOutcome{err}}
}
cId := C.CString(id)
defer C.free(unsafe.Pointer(cId))
profileType, err := profile.Type()
if err != nil {
outcome <- VoiceProfileEnrollmentOutcome{Result: nil, OperationOutcome: common.OperationOutcome{err}}
}
ret := uintptr(C.retrieve_enrollment_result(client.handle, cId, (C.int)(profileType), &handle))
if ret != C.SPX_NOERROR {
outcome <- VoiceProfileEnrollmentOutcome{Result: nil, OperationOutcome: common.OperationOutcome{common.NewCarbonError(ret)}}
} else {
newResult, err := newVoiceProfileEnrollmentResultFromHandle(handle2uintptr(handle))
if err != nil {
outcome <- VoiceProfileEnrollmentOutcome{Result: nil, OperationOutcome: common.OperationOutcome{err}}
} else {
outcome <- VoiceProfileEnrollmentOutcome{Result: newResult, OperationOutcome: common.OperationOutcome{nil}}
}
}
}()
return outcome
}
type GetAllProfilesOutcome struct {
common.OperationOutcome
Profiles []*VoiceProfile
}
// Close releases the underlying resources
func (outcome GetAllProfilesOutcome) Close() {
for _, profile := range outcome.Profiles {
if profile != nil {
profile.Close()
}
}
}
// GetAllProfilesAsync attempts to create a new voice profile on the service.
func (client VoiceProfileClient) GetAllProfilesAsync(profileType common.VoiceProfileType) chan GetAllProfilesOutcome {
outcome := make(chan GetAllProfilesOutcome)
go func() {
rawProfileJson := C.get_profiles_json(client.handle, (C.int)(profileType))
if rawProfileJson == nil {
outcome <- GetAllProfilesOutcome{Profiles: nil, OperationOutcome: common.OperationOutcome{common.NewCarbonError(uintptr(C.SPXERR_INVALID_ARG))}}
} else {
goProfilesJson := C.GoString(rawProfileJson)
splitProfileIds := strings.Split(goProfilesJson, "|")
profileList := make([]*VoiceProfile, len(splitProfileIds))
for index, id := range splitProfileIds {
profile, err := NewVoiceProfileFromIdAndType(id, profileType)
if err != nil {
outcome <- GetAllProfilesOutcome{Profiles: nil, OperationOutcome: common.OperationOutcome{err}}
}
profileList[index] = profile
}
outcome <- GetAllProfilesOutcome{Profiles: profileList, OperationOutcome: common.OperationOutcome{nil}}
}
}()
return outcome
}