-
Notifications
You must be signed in to change notification settings - Fork 110
/
microphone.go
122 lines (109 loc) · 3.09 KB
/
microphone.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
//go:build !no_cgo
// Package microphone implements a microphone audio input. Really the microphone
// is any audio input device that can be found via gostream.
package microphone
import (
"context"
"errors"
"path/filepath"
"regexp"
"github.com/pion/mediadevices"
"go.viam.com/rdk/components/audioinput"
"go.viam.com/rdk/gostream"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
)
var model = resource.DefaultModelFamily.WithModel("microphone")
func init() {
resource.RegisterComponent(
audioinput.API,
model,
resource.Registration[audioinput.AudioInput, *Config]{
Constructor: func(
_ context.Context,
_ resource.Dependencies,
conf resource.Config,
logger logging.Logger,
) (audioinput.AudioInput, error) {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, err
}
src, err := newMicrophoneSource(newConf, logger)
if err != nil {
return nil, err
}
// This always rebuilds on reconfiguration right now. A better system
// would be to reuse the monitored webcam code.
return audioinput.FromAudioSource(conf.ResourceName(), src)
},
})
}
// Config is the attribute struct for microphones.
type Config struct {
resource.TriviallyValidateConfig
Path string `json:"audio_path"`
PathPattern string `json:"audio_path_pattern"`
Debug bool `json:"debug"`
}
// newMicrophoneSource returns a new source based on a microphone discovered from the given attributes.
func newMicrophoneSource(conf *Config, logger logging.Logger) (audioinput.AudioSource, error) {
var err error
debug := conf.Debug
if conf.Path != "" {
return tryMicrophoneOpen(conf.Path, gostream.DefaultConstraints, logger)
}
var pattern *regexp.Regexp
if conf.PathPattern != "" {
pattern, err = regexp.Compile(conf.PathPattern)
if err != nil {
return nil, err
}
}
all := gostream.QueryAudioDevices()
for _, info := range all {
logger.Debugf("%s", info.ID)
logger.Debugf("\t labels: %v", info.Labels)
for _, label := range info.Labels {
if pattern != nil && !pattern.MatchString(label) {
if debug {
logger.Debug("\t skipping because of pattern")
}
continue
}
for _, p := range info.Properties {
logger.Debugf("\t %+v", p.Audio)
if p.Audio.ChannelCount == 0 {
if debug {
logger.Debug("\t skipping because audio channels are empty")
}
continue
}
s, err := tryMicrophoneOpen(label, gostream.DefaultConstraints, logger)
if err == nil {
if debug {
logger.Debug("\t USING")
}
return s, nil
}
if debug {
logger.Debugw("cannot open driver with properties", "properties", p,
"error", err)
}
}
}
}
return nil, errors.New("found no microphones")
}
func tryMicrophoneOpen(
path string,
constraints mediadevices.MediaStreamConstraints,
logger logging.Logger,
) (audioinput.AudioSource, error) {
source, err := gostream.GetNamedAudioSource(filepath.Base(path), constraints, logger)
if err != nil {
return nil, err
}
// TODO(XXX): implement LivenessMonitor
return audioinput.NewAudioSourceFromGostreamSource(source)
}