This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
wakeup.go
78 lines (63 loc) · 1.53 KB
/
wakeup.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
package main
import (
"flag"
"fmt"
"io"
snowboy "github.com/brentnd/go-snowboy"
"github.com/icexin/dueros/audio"
)
var (
wakeupSensitivity = flag.Float64("sens", 0.46, "wakeup detector sensitivity")
)
const (
KeyboardListener = "keyboard"
KeywordListener = "keyword"
)
type WakeupListener interface {
ListenAndWakeup()
Close() error
}
type keyboardWakeupListener struct {
}
func newKeyboardWakeupListener() WakeupListener {
return new(keyboardWakeupListener)
}
func (k keyboardWakeupListener) ListenAndWakeup() {
fmt.Scanln()
}
func (k keyboardWakeupListener) Close() error {
return nil
}
type keywordWakeupListener struct {
detector snowboy.Detector
recordReader io.ReadCloser
}
func newKeywordWakeupListener() WakeupListener {
k := &keywordWakeupListener{
detector: snowboy.NewDetector("resource/common.res"),
}
k.detector.HandleFunc(snowboy.NewHotword("resource/wakeup.pmdl", float32(*wakeupSensitivity)), k.onWakeup)
return k
}
func (k *keywordWakeupListener) onWakeup(string) {
fmt.Println(">>> wakeup")
k.recordReader.Close()
}
func (k *keywordWakeupListener) ListenAndWakeup() {
k.recordReader = audio.NewRecordStream()
k.detector.ReadAndDetect(k.recordReader)
k.detector.Reset()
}
func (k *keywordWakeupListener) Close() error {
return k.detector.Close()
}
func NewWakeupListener(method string) WakeupListener {
switch method {
case KeyboardListener:
return newKeyboardWakeupListener()
case KeywordListener:
return newKeywordWakeupListener()
default:
panic("wakeup method not found: " + method)
}
}