/
audio_input_stream.go
222 lines (196 loc) · 7.53 KB
/
audio_input_stream.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
package audio
import (
"sync"
"unsafe"
"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
)
// #include <stdlib.h>
// #include <stdint.h>
// #include <string.h>
// #include <speechapi_c_common.h>
// #include <speechapi_c_audio_stream.h>
//
// /* Proxy functions forward declarations */
// int cgo_audio_read_callback_wrapper(void *context, uint8_t *buffer, uint32_t size);
// void cgo_audio_get_property_callback_wrapper(void* context, int id, uint8_t* value, uint32_t size);
// void cgo_audio_close_callback_wrapper(void *context);
import "C"
// AudioInputStream represents audio input stream used for custom audio input configurations
type AudioInputStream interface {
Close()
getHandle() C.SPXHANDLE
}
type audioInputStreamBase struct {
handle C.SPXHANDLE
}
func (stream audioInputStreamBase) getHandle() C.SPXHANDLE {
return stream.handle
}
func (stream audioInputStreamBase) Close() {
C.audio_stream_release(stream.handle)
}
// PushAudioInputStream represents memory backed push audio input stream used for custom audio input configurations.
type PushAudioInputStream struct {
audioInputStreamBase
}
// CreatePushAudioInputStreamFromFormat creates a memory backed PushAudioInputStream with the specified audio format.
// Currently, only WAV / PCM with 16-bit samples, 16 kHz sample rate, and a single channel (Mono) is supported. When used
// with Conversation Transcription, eight channels are supported.
func CreatePushAudioInputStreamFromFormat(format *AudioStreamFormat) (*PushAudioInputStream, error) {
var handle C.SPXHANDLE
ret := uintptr(C.audio_stream_create_push_audio_input_stream(&handle, format.handle))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
stream := new(PushAudioInputStream)
stream.handle = handle
return stream, nil
}
// CreatePushAudioInputStream creates a memory backed PushAudioInputStream using the default format (16 kHz, 16 bit, mono PCM).
func CreatePushAudioInputStream() (*PushAudioInputStream, error) {
format, err := GetDefaultInputFormat()
if err != nil {
return nil, err
}
return CreatePushAudioInputStreamFromFormat(format)
}
// Write writes the audio data specified by making an internal copy of the data.
// Note: The dataBuffer should not contain any audio header.
func (stream PushAudioInputStream) Write(buffer []byte) error {
size := uint(len(buffer))
cBuffer := C.CBytes(buffer)
defer C.free(cBuffer)
ret := uintptr(C.push_audio_input_stream_write(stream.handle, (*C.uint8_t)(cBuffer), (C.uint32_t)(size)))
if ret != C.SPX_NOERROR {
return common.NewCarbonError(ret)
}
return nil
}
// SetProperty sets value of a property. The properties of the audio data should be set before writing the audio data.
func (stream PushAudioInputStream) SetProperty(id common.PropertyID, value string) error {
v := C.CString(value)
defer C.free(unsafe.Pointer(v))
ret := uintptr(C.push_audio_input_stream_set_property_by_id(stream.handle, (C.int)(id), v))
if ret != C.SPX_NOERROR {
return common.NewCarbonError(ret)
}
return nil
}
// SetPropertyByName sets value of a property. The properties of the audio data should be set before writing the audio data.
func (stream PushAudioInputStream) SetPropertyByName(name string, value string) error {
n := C.CString(name)
defer C.free(unsafe.Pointer(n))
v := C.CString(value)
defer C.free(unsafe.Pointer(v))
ret := uintptr(C.push_audio_input_stream_set_property_by_name(stream.handle, n, v))
if ret != C.SPX_NOERROR {
return common.NewCarbonError(ret)
}
return nil
}
// CloseStream closes the stream.
func (stream PushAudioInputStream) CloseStream() {
C.push_audio_input_stream_close(stream.handle)
}
// PullAudioInputStream represents audio input stream used for custom audio input configurations.
type PullAudioInputStream struct {
audioInputStreamBase
}
// PullAudioInputStreamCallback interface that defines callback methods (Read(), GetProperty() and CloseStream()) for custom
// audio input streams).
type PullAudioInputStreamCallback interface {
Read(maxSize uint32) ([]byte, int)
GetProperty(id common.PropertyID) string
CloseStream()
}
var mu sync.Mutex
var pullStreamCallbacks = make(map[C.SPXHANDLE]PullAudioInputStreamCallback)
func registerCallback(handle C.SPXHANDLE, callback PullAudioInputStreamCallback) {
mu.Lock()
defer mu.Unlock()
pullStreamCallbacks[handle] = callback
}
func getCallback(handle C.SPXHANDLE) *PullAudioInputStreamCallback {
mu.Lock()
defer mu.Unlock()
cb, ok := pullStreamCallbacks[handle]
if ok {
return &cb
}
return nil
}
func deregisterCallback(handle C.SPXHANDLE) {
mu.Lock()
defer mu.Unlock()
pullStreamCallbacks[handle] = nil
}
//export cgoAudioCallReadCallback
func cgoAudioCallReadCallback(handle C.SPXRECOHANDLE, dataBuffer *C.uint8_t, size C.uint32_t) int {
callback := getCallback(handle)
if callback != nil {
goBuffer, readSize := (*callback).Read(uint32(size))
buffer := C.CBytes(goBuffer)
defer C.free(buffer)
C.memcpy(unsafe.Pointer(dataBuffer), buffer, (C.size_t)(readSize))
return readSize
}
return 0
}
//export cgoAudioCallGetPropertyCallback
func cgoAudioCallGetPropertyCallback(handle C.SPXHANDLE, id int, value *C.uint8_t, size C.uint32_t) {
callback := getCallback(handle)
if callback != nil {
propValue := (*callback).GetProperty((common.PropertyID)(size))
buffer := C.CString(propValue)
defer C.free(unsafe.Pointer(buffer))
s := size
if uintptr(len(propValue)) < uintptr(size) {
s = (C.uint32_t)(len(propValue))
}
C.memcpy(unsafe.Pointer(value), unsafe.Pointer(buffer), (C.size_t)(s))
}
}
//export cgoAudioCallCloseCallback
func cgoAudioCallCloseCallback(handle C.SPXHANDLE) {
callback := getCallback(handle)
if callback != nil {
(*callback).CloseStream()
}
}
// CreatePullStreamFromFormat creates a PullAudioInputStream that delegates to the specified callback interface for Read()
// and CloseStream() methods and the specified format.
// Currently, only WAV / PCM with 16-bit samples, 16 kHz sample rate, and a single channel (Mono) is supported. When used with Conversation Transcription, eight channels are supported.
func CreatePullStreamFromFormat(callback PullAudioInputStreamCallback, format *AudioStreamFormat) (*PullAudioInputStream, error) {
var handle C.SPXHANDLE
ret := uintptr(C.audio_stream_create_pull_audio_input_stream(&handle, format.handle))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
ret = uintptr(C.pull_audio_input_stream_set_callbacks(
handle,
unsafe.Pointer(handle),
(C.CUSTOM_AUDIO_PULL_STREAM_READ_CALLBACK)(unsafe.Pointer(C.cgo_audio_read_callback_wrapper)),
(C.CUSTOM_AUDIO_PULL_STREAM_CLOSE_CALLBACK)(unsafe.Pointer(C.cgo_audio_close_callback_wrapper))))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
ret = uintptr(C.pull_audio_input_stream_set_getproperty_callback(
handle,
unsafe.Pointer(handle),
(C.CUSTOM_AUDIO_PULL_STREAM_GET_PROPERTY_CALLBACK)(unsafe.Pointer(C.cgo_audio_get_property_callback_wrapper))))
registerCallback(handle, callback)
stream := new(PullAudioInputStream)
stream.handle = handle
return stream, nil
}
// CreatePullStream creates a PullAudioInputStream that delegates to the specified callback interface for Read() and CloseStream()
// methods using the default format (16 kHz, 16 bit, mono PCM).
func CreatePullStream(callback PullAudioInputStreamCallback) (*PullAudioInputStream, error) {
format, err := GetDefaultInputFormat()
if err != nil {
return nil, err
}
return CreatePullStreamFromFormat(callback, format)
}