This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
sphinx.go
258 lines (233 loc) · 7.1 KB
/
sphinx.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
//
// Copyright (c) 2019-present Codist <countstarlight@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Written by Codist <countstarlight@gmail.com>, March 2019
//
package sphinx
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/countstarlight/homo/cmd/webview/config"
"github.com/countstarlight/homo/module/audio"
"github.com/countstarlight/homo/module/baidu"
"github.com/countstarlight/homo/module/nlu"
"github.com/countstarlight/homo/module/view"
"github.com/sirupsen/logrus"
"github.com/xlab/pocketsphinx-go/sphinx"
"github.com/xlab/portaudio-go/portaudio"
"io/ioutil"
"unicode"
"unsafe"
)
const (
samplesPerChannel = 512
sampleRate = 16000
channels = 1
sampleFormat = portaudio.PaInt16
)
type Listener struct {
inSpeech bool
uttStarted bool
dec *sphinx.Decoder
}
func LoadCMUSphinx() {
config.SphinxLoop.Add(1)
// Init CMUSphinx
cfg := sphinx.NewConfig(
sphinx.HMMDirOption(config.HMMDirEn),
sphinx.DictFileOption(config.DictFileEn),
sphinx.LMFileOption(config.LMFileEn),
sphinx.SampleRateOption(sampleRate),
)
//Specify output dir for RAW recorded sound files (s16le). Directory must exist.
sphinx.RawLogDirOption(config.RawDir)(cfg)
sphinx.LogFileOption(config.SphinxLogFile)(cfg)
logrus.Info("开始加载 CMU PhocketSphinx...")
logrus.Info("开始加载唤醒模型...")
dec, err := sphinx.NewDecoder(cfg)
if err != nil {
logrus.Fatalf("sphinx.NewDecoder failed: %s", err.Error())
}
defer dec.Destroy()
dec.SetRawDataSize(300000)
l := &Listener{
dec: dec,
}
var stream *portaudio.Stream
errStr := portaudio.OpenDefaultStream(&stream, channels, 0, sampleFormat, sampleRate, samplesPerChannel, l.paCallback, nil)
if audio.PaError(errStr) {
logrus.Fatalf("PortAudio failed: %s", audio.PaErrorText(errStr))
}
defer func() {
if errStr := portaudio.CloseStream(stream); audio.PaError(errStr) {
logrus.Warnf("PortAudio error:", audio.PaErrorText(errStr))
}
}()
if errStr := portaudio.StartStream(stream); audio.PaError(errStr) {
logrus.Fatalf("PortAudio error: %s", audio.PaErrorText(errStr))
}
defer func() {
if errStr := portaudio.StopStream(stream); audio.PaError(errStr) {
logrus.Fatalf("PortAudio error:", audio.PaErrorText(errStr))
}
}()
if !dec.StartUtt() {
logrus.Fatalln("Sphinx failed to start utterance")
}
logrus.Infof("开始从麦克风检测唤醒词:采样率[%dHz] 通道数[%d]", sampleRate, channels)
config.SphinxLoop.Wait()
}
// paCallback: for simplicity reasons we process raw audio with sphinx in the this stream callback,
// never do that for any serious applications, use a buffered channel instead.
func (l *Listener) paCallback(input unsafe.Pointer, _ unsafe.Pointer, sampleCount uint,
_ *portaudio.StreamCallbackTimeInfo, _ portaudio.StreamCallbackFlags, _ unsafe.Pointer) int32 {
const (
statusContinue = int32(portaudio.PaContinue)
statusAbort = int32(portaudio.PaAbort)
)
// Do not record if is playing voice
if config.IsPlayingVoice && !config.InterruptMode {
return statusContinue
}
in := (*(*[1 << 24]int16)(input))[:int(sampleCount)*channels]
// ProcessRaw with disabled search because callback needs to be relatime
_, ok := l.dec.ProcessRaw(in, true, false)
// log.Printf("processed: %d frames, ok: %v", frames, ok)
if !ok {
return statusAbort
}
if l.dec.IsInSpeech() {
l.inSpeech = true
if !l.uttStarted {
l.uttStarted = true
if config.WakeUpd {
logrus.Info("发现音频波动,开始录制音频...")
} else {
logrus.Info("发现音频波动,开始检测唤醒词...")
}
}
} else if l.uttStarted {
// speech -> silence transition, time to start new utterance
l.dec.EndUtt()
l.uttStarted = false
l.report() // report results
if !l.dec.StartUtt() {
logrus.Fatalln("Sphinx failed to start utterance")
}
}
return statusContinue
}
func (l *Listener) report() {
if config.WakeUpd {
//Display typing animate during asr
if !config.SilenceMode {
view.TypingAnimate()
}
// Save raw data to file
rData := l.dec.RawData()
buf := new(bytes.Buffer)
for _, v := range rData {
err := binary.Write(buf, binary.LittleEndian, v)
if err != nil {
logrus.Errorf("binary.Write failed: %s", err.Error())
}
}
//Reduce sensitivity
if len(buf.Bytes()) < config.RecordThreshold {
logrus.Infof("音频长度小于阈值 %d,取消录制", config.RecordThreshold)
view.TypingAnimateStop()
return
}
logrus.Infof("保存原始音频文件到: %s, 音频流长度: %d Byte\n", config.InputRaw, len(buf.Bytes()))
if err := ioutil.WriteFile(config.InputRaw, buf.Bytes(), 0644); err != nil {
logrus.Warnf("binary.Write failed: %s", err.Error())
}
// Speech to text
success := false
var errorMsg string
result, err := baidu.SpeechToText(config.InputRaw, "pcm", sampleRate)
if err != nil {
if baidu.IsErrSpeechQuality(err) {
view.TypingAnimateStop()
// errorMsg = "没有听清在说什么"
return
//result = []string{"没有听清在说什么"}
//logrus.Warnf("没有听清在说什么")
} else {
errorMsg = fmt.Sprintf("语音在线识别出错:%s", err.Error())
//logrus.Warnf("语音在线识别出错:%s", err.Error())
}
} else {
if len(result) == 0 {
view.TypingAnimateStop()
// errorMsg = "没有听清在说什么"
return
//result = []string{"没有听清在说什么"}
//logrus.Warnf("没有听清在说什么")
} else {
success = true
//logrus.Infof("语音在线识别结果: %v", result)
}
}
if !success {
//Output message to log if in silence mode
if config.SilenceMode {
logrus.Warnf("%s", errorMsg)
} else {
/*if !config.OfflineMode {
view.SendReplyWithVoice([]string{errorMsg})
} else {
view.SendReply([]string{errorMsg})
}*/
//error message not to speech
view.SendReply([]string{errorMsg})
}
} else {
// Send input text to webview
var message string
s := []rune(result[0])
// Remove latest symbol of Chinese string, eg. '。' of '你好。'
if !unicode.Is(unicode.Han, s[len(s)-1]) {
message = string(s[:len(s)-1])
}
// If in silence mode, post message to nlu
if config.SilenceMode {
replyMessage, err := nlu.ActionLocal(message)
if err != nil {
logrus.Warnf("连接到nlu出错: %s", err.Error())
}
if !config.SilenceMode {
view.SendOnlyInputText(message)
if config.OfflineMode {
view.SendReply(replyMessage)
} else {
view.SendReplyWithVoice(replyMessage)
}
}
} else {
view.SendInputText(message)
}
}
if config.RawToWav {
err := Pcm2Wav(config.InputRaw)
if err != nil {
logrus.Warnf("Convert raw input %s to wav failed: %s", config.InputRaw, err.Error())
} else {
logrus.Infof("原始音频文件编码为wav,保存到: %s", config.InputWav)
}
}
} else {
hyp, _ := l.dec.Hypothesis()
if len(hyp) > 0 {
if hyp == "homo" || hyp == "como" {
logrus.Info("命中唤醒词,开始唤醒...")
config.WakeUpWait.Done()
config.WakeUpd = true
}
return
}
logrus.Println("没有检测到唤醒词")
}
}