-
Notifications
You must be signed in to change notification settings - Fork 1
/
microphone.go
276 lines (239 loc) · 6.26 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package aio
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"regexp"
"runtime"
"strings"
"syscall"
)
type Microphone struct {
name string // Microphone device name.
samplerate int // Audio Sample Rate in Hz.
channels int // Number of audio channels.
format string // Format of audio samples.
bps int // Bits per sample.
buffer []byte // Raw audio data.
pipe io.ReadCloser // Stdout pipe for ffmpeg process streaming microphone audio.
cmd *exec.Cmd // ffmpeg command.
}
func (mic *Microphone) Name() string {
return mic.name
}
// Audio Sample Rate in Hz.
func (mic *Microphone) SampleRate() int {
return mic.samplerate
}
func (mic *Microphone) Channels() int {
return mic.channels
}
func (mic *Microphone) BitsPerSample() int {
return mic.bps
}
func (mic *Microphone) Format() string {
switch mic.format {
case "u8", "s8":
return mic.format
default:
return mic.format[:len(mic.format)-2]
}
}
func (mic *Microphone) Buffer() []byte {
return mic.buffer
}
func (mic *Microphone) Samples() interface{} {
return bytesToSamples(mic.buffer, len(mic.buffer)/(mic.bps/8), mic.format)
}
// Sets the buffer to the given byte array. The length of the buffer must be a multiple
// of (bytes per sample * audio channels).
func (mic *Microphone) SetBuffer(buffer []byte) error {
if len(buffer)%(mic.bps/8*mic.channels) != 0 {
return fmt.Errorf("buffer size must be multiple of %d", mic.bps/8*mic.channels)
}
mic.buffer = buffer
return nil
}
func NewMicrophone(stream int, options *Options) (*Microphone, error) {
// Check if ffmpeg is installed on the users machine.
if err := installed("ffmpeg"); err != nil {
return nil, err
}
var device string
switch runtime.GOOS {
case "linux":
device = fmt.Sprintf("%d", stream)
case "darwin":
device = fmt.Sprintf(`":%d"`, stream)
case "windows":
// If OS is windows, we need to parse the listed devices to find which corresponds to the
// given "stream" index.
devices, err := getDevicesWindows()
if err != nil {
return nil, err
}
if stream < 0 || stream >= len(devices) {
return nil, fmt.Errorf("could not find device with index: %d", stream)
}
device = fmt.Sprintf("audio=%s", devices[stream])
default:
return nil, fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
mic := &Microphone{name: device}
if err := mic.getMicrophoneData(device); err != nil {
return nil, err
}
if options == nil {
options = &Options{}
}
if options.Format == "" {
mic.format = createFormat("s16") // s16 default format.
} else {
mic.format = createFormat(options.Format)
}
if err := checkFormat(mic.format); err != nil {
return nil, err
}
if options.SampleRate != 0 {
mic.samplerate = options.SampleRate
}
if options.Channels != 0 {
mic.channels = options.Channels
}
mic.bps = int(parse(regexp.MustCompile(`\d{1,2}`).FindString(mic.format))) // Bits per sample.
return mic, nil
}
// Parses the microphone metadata from ffmpeg output.
func (mic *Microphone) parseMicrophoneData(buffer string) {
// Sample String: "Stream #0:0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s".
index := strings.Index(buffer, "Stream #")
if index == -1 {
index++
}
buffer = buffer[index:]
// Sample rate.
regex := regexp.MustCompile(`\d+ Hz`)
match := regex.FindString(buffer)
if len(match) > 0 {
mic.samplerate = int(parse(match[:len(match)-len(" Hz")]))
}
mic.channels = 2 // stereo by default.
if strings.Contains(buffer, "stereo") {
mic.channels = 2
} else if strings.Contains(buffer, "mono") {
mic.channels = 1
}
}
// Get microphone meta data such as width, height, fps and codec.
func (mic *Microphone) getMicrophoneData(device string) error {
// Run command to get microphone data.
micDeviceName, err := microphone()
if err != nil {
return err
}
cmd := exec.Command(
"ffmpeg",
"-hide_banner",
"-f", micDeviceName,
"-i", device,
)
// The command will fail since we do not give a file to write to, therefore
// it will write the meta data to Stderr.
pipe, err := cmd.StderrPipe()
if err != nil {
return err
}
// Start the command.
if err := cmd.Start(); err != nil {
return err
}
// Read ffmpeg output from Stdout.
builder := bytes.Buffer{}
buffer := make([]byte, 1024)
for {
n, err := pipe.Read(buffer)
builder.Write(buffer[:n])
if err == io.EOF {
break
}
}
// Wait for the command to finish.
cmd.Wait()
mic.parseMicrophoneData(builder.String())
return nil
}
// Once the user calls Read() for the first time on a Microphone struct,
// the ffmpeg command which is used to read the microphone device is started.
func (mic *Microphone) init() error {
// If user exits with Ctrl+C, stop ffmpeg process.
mic.cleanup()
micDeviceName, err := microphone()
if err != nil {
return err
}
// Use ffmpeg to pipe microphone to stdout.
cmd := exec.Command(
"ffmpeg",
"-hide_banner",
"-loglevel", "quiet",
"-f", micDeviceName,
"-i", mic.name,
"-f", mic.format,
"-ar", fmt.Sprintf("%d", mic.samplerate),
"-ac", fmt.Sprintf("%d", mic.channels),
"-",
)
mic.cmd = cmd
pipe, err := cmd.StdoutPipe()
if err != nil {
return err
}
mic.pipe = pipe
if err := cmd.Start(); err != nil {
return err
}
if mic.buffer == nil {
mic.buffer = make([]byte, mic.samplerate*mic.channels*mic.bps/8)
}
return nil
}
// Reads the next frame from of audio and stores it in the buffer.
// If the last frame has been read, returns false, otherwise true.
func (mic *Microphone) Read() bool {
// If cmd is nil, microphone reading has not been initialized.
if mic.cmd == nil {
if err := mic.init(); err != nil {
return false
}
}
io.ReadFull(mic.pipe, mic.buffer)
return true
}
// Closes the pipe and stops the ffmpeg process.
func (mic *Microphone) Close() {
if mic.pipe != nil {
mic.pipe.Close()
}
if mic.cmd != nil {
mic.cmd.Process.Kill()
}
}
// Stops the "cmd" process running when the user presses Ctrl+C.
// https://stackoverflow.com/questions/11268943/is-it-possible-to-capture-a-ctrlc-signal-and-run-a-cleanup-function-in-a-defe.
func (mic *Microphone) cleanup() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if mic.pipe != nil {
mic.pipe.Close()
}
if mic.cmd != nil {
mic.cmd.Process.Kill()
}
os.Exit(1)
}()
}