-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.js
65 lines (53 loc) · 1.9 KB
/
example.js
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
const AlsaCapture = require("./index");
// default values for the options object
const captureInstance = new AlsaCapture({
channels: 2,
debug: false,
device: "default",
format: "S16_LE",
periodSize: 32,
periodTime: undefined,
rate: 44100,
});
// data is an Uint8Array
// Buffer size = numChannels * formatByteSize * periodSize
// Example: 2 Bytes (AlsaFormat.S16_LE) * 2 (numChannels) * 32 (periodSize) = 128 Bytes
captureInstance.on("audio", (data) => {
console.log(data);
});
// if the requested rate is not available for the capture device
// ALSA will select the nearest available
captureInstance.on("rateDeviating", (actualRate) => {
console.warn(`Sound card rate deviates from requested rate; Actual rate: ${actualRate}`);
});
// if the requested period size is not available for the capture device
// ALSA will select the nearest available
captureInstance.on("periodSizeDeviating", (actualPeriodSize) => {
console.warn(`Sound card period size deviates from requested period size; Actual period size: ${actualPeriodSize}`);
});
captureInstance.on("periodTime", (periodTime) => {
console.log(`Actual period time ${periodTime}`);
});
// if an error occurs, error will contain the message
captureInstance.on("error", (error) => {
console.error(error);
process.exit(1);
});
// if overruns occur frequently try increasing the period_size
captureInstance.on("overrun", () => {
console.warn("NodeAlsaCapture overrun");
});
captureInstance.on("shortRead", (framesRead) => {
console.warn(`NodeAlsaCapture shortRead: Only ${framesRead} read`);
});
captureInstance.on("readError", (error) => {
console.warn(`NodeAlsaCapture readError: ${error}`);
});
setTimeout(() => {
// close capture device
captureInstance.close();
}, 10000);
// event is sent after capture devices is closed completely
captureInstance.on("close", () => {
console.log("capture closed")
});