-
Notifications
You must be signed in to change notification settings - Fork 109
/
microphone.go
118 lines (101 loc) · 2.74 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
// 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/edaniels/golog"
"github.com/edaniels/gostream"
"github.com/pion/mediadevices"
"go.viam.com/rdk/components/audioinput"
"go.viam.com/rdk/config"
"go.viam.com/rdk/registry"
"go.viam.com/rdk/utils"
)
const model = "microphone"
func init() {
registry.RegisterComponent(
audioinput.Subtype,
model,
registry.Component{Constructor: func(
_ context.Context,
_ registry.Dependencies,
config config.Component,
logger golog.Logger,
) (interface{}, error) {
attrs, ok := config.ConvertedAttributes.(*Attrs)
if !ok {
return nil, utils.NewUnexpectedTypeError(attrs, config.ConvertedAttributes)
}
return newMicrophoneSource(attrs, logger)
}})
config.RegisterComponentAttributeMapConverter(audioinput.SubtypeName, model,
func(attributes config.AttributeMap) (interface{}, error) {
var conf Attrs
attrs, err := config.TransformAttributeMapToStruct(&conf, attributes)
if err != nil {
return nil, err
}
result, ok := attrs.(*Attrs)
if !ok {
return nil, utils.NewUnexpectedTypeError(result, attrs)
}
return result, nil
}, &Attrs{})
}
// Attrs is the attribute struct for microphones.
type Attrs struct {
Path string `json:"path"`
PathPattern string `json:"path_pattern"`
Debug bool `json:"debug"`
}
// newMicrophoneSource returns a new source based on a microphone discovered from the given attributes.
func newMicrophoneSource(attrs *Attrs, logger golog.Logger) (audioinput.AudioInput, error) {
var err error
debug := attrs.Debug
if attrs.Path != "" {
return tryMicrophoneOpen(attrs.Path, gostream.DefaultConstraints)
}
var pattern *regexp.Regexp
if attrs.PathPattern != "" {
pattern, err = regexp.Compile(attrs.PathPattern)
if err != nil {
return nil, err
}
}
labels := gostream.QueryAudioDeviceLabels()
for _, label := range labels {
if debug {
logger.Debugf("%s", label)
}
if pattern != nil && !pattern.MatchString(label) {
if debug {
logger.Debug("\t skipping because of pattern")
}
continue
}
s, err := tryMicrophoneOpen(label, gostream.DefaultConstraints)
if err == nil {
if debug {
logger.Debug("\t USING")
}
return s, nil
}
if debug {
logger.Debugf("\t %w", err)
}
}
return nil, errors.New("found no microphones")
}
func tryMicrophoneOpen(
path string,
constraints mediadevices.MediaStreamConstraints,
) (audioinput.AudioInput, error) {
source, err := gostream.GetNamedAudioSource(filepath.Base(path), constraints)
if err != nil {
return nil, err
}
return audioinput.NewFromSource(source)
}