@lumiastream/wakeword 1.0.1-alpha.6 → 1.0.1-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/wakeword +1 -1
- package/lib/record.js +120 -0
- package/lib/recorders/arecord.js +27 -0
- package/lib/recorders/index.js +23 -0
- package/lib/recorders/rec.js +36 -0
- package/lib/recorders/sox.js +51 -0
- package/lib/{voice.mjs → voice.js} +3 -2
- package/package.json +3 -4
package/bin/wakeword
CHANGED
package/lib/record.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import assert from "assert";
|
|
4
|
+
import debug from "debug";
|
|
5
|
+
import { spawn } from "child_process";
|
|
6
|
+
import recorders from "./recorders/index.js";
|
|
7
|
+
|
|
8
|
+
class Recording {
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
const defaults = {
|
|
11
|
+
sampleRate: 16000,
|
|
12
|
+
channels: 1,
|
|
13
|
+
compress: false,
|
|
14
|
+
threshold: 0.5,
|
|
15
|
+
thresholdStart: null,
|
|
16
|
+
thresholdEnd: null,
|
|
17
|
+
silence: "1.0",
|
|
18
|
+
recorder: "sox",
|
|
19
|
+
endOnSilence: false,
|
|
20
|
+
audioType: "wav",
|
|
21
|
+
binPath: null,
|
|
22
|
+
bufferSize: null,
|
|
23
|
+
arguments: [],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
this.options = Object.assign(defaults, options);
|
|
27
|
+
|
|
28
|
+
const recorder = recorders.load(this.options.recorder);
|
|
29
|
+
console.log(recorder);
|
|
30
|
+
const { cmd, args, spawnOptions = {} } = recorder(this.options);
|
|
31
|
+
|
|
32
|
+
this.cmd = cmd;
|
|
33
|
+
this.args = args;
|
|
34
|
+
this.cmdOptions = Object.assign(
|
|
35
|
+
{ encoding: "binary", stdio: "pipe" },
|
|
36
|
+
spawnOptions
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
debug(`Started recording`);
|
|
40
|
+
debug(this.options);
|
|
41
|
+
|
|
42
|
+
const command = ` ${this.cmd} ${this.args.join(" ")}`;
|
|
43
|
+
debug(command);
|
|
44
|
+
|
|
45
|
+
return this.start();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
start() {
|
|
49
|
+
const { cmd, args, cmdOptions } = this;
|
|
50
|
+
|
|
51
|
+
const cp = spawn(cmd, args, cmdOptions);
|
|
52
|
+
const rec = cp.stdout;
|
|
53
|
+
const err = cp.stderr;
|
|
54
|
+
|
|
55
|
+
this.process = cp; // expose child process
|
|
56
|
+
this._stream = rec; // expose output stream
|
|
57
|
+
|
|
58
|
+
cp.on("close", (code) => {
|
|
59
|
+
if (code === 0) return;
|
|
60
|
+
rec.emit(
|
|
61
|
+
"error",
|
|
62
|
+
`${this.cmd} has exited with error code ${code}.
|
|
63
|
+
|
|
64
|
+
Enable debugging with the environment variable DEBUG=record.`
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
err.on("data", (chunk) => {
|
|
69
|
+
debug(`STDERR: ${chunk}`);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
rec.on("data", (chunk) => {
|
|
73
|
+
debug(`Recording ${chunk.length} bytes`);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
rec.on("end", () => {
|
|
77
|
+
debug("Recording ended");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
stop() {
|
|
84
|
+
assert(this.process, "Recording not yet started");
|
|
85
|
+
|
|
86
|
+
this.process.kill();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
pause() {
|
|
90
|
+
assert(this.process, "Recording not yet started");
|
|
91
|
+
|
|
92
|
+
this.process.kill("SIGSTOP");
|
|
93
|
+
this._stream.pause();
|
|
94
|
+
debug("Paused recording");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
resume() {
|
|
98
|
+
assert(this.process, "Recording not yet started");
|
|
99
|
+
|
|
100
|
+
this.process.kill("SIGCONT");
|
|
101
|
+
this._stream.resume();
|
|
102
|
+
debug("Resumed recording");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
isPaused() {
|
|
106
|
+
assert(this.process, "Recording not yet started");
|
|
107
|
+
|
|
108
|
+
return this._stream.isPaused();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
stream() {
|
|
112
|
+
assert(this._stream, "Recording not yet started");
|
|
113
|
+
|
|
114
|
+
return this._stream;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export default {
|
|
119
|
+
record: (...args) => new Recording(...args),
|
|
120
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// On some systems (RasPi), arecord is the prefered recording binary
|
|
2
|
+
export default (options) => {
|
|
3
|
+
let cmd = "arecord";
|
|
4
|
+
|
|
5
|
+
if (options.binPath) {
|
|
6
|
+
cmd = options.binPath;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const args = [
|
|
10
|
+
"-q", // show no progress
|
|
11
|
+
"-r",
|
|
12
|
+
options.sampleRate, // sample rate
|
|
13
|
+
"-c",
|
|
14
|
+
options.channels, // channels
|
|
15
|
+
"-t",
|
|
16
|
+
options.audioType, // audio type
|
|
17
|
+
"-f",
|
|
18
|
+
"S16_LE", // Sample format
|
|
19
|
+
"-", // pipe
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
if (options.device) {
|
|
23
|
+
args.unshift("-D", options.device);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return { cmd, args };
|
|
27
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// import { fileURLToPath } from "node:url";
|
|
2
|
+
// import path from "node:path";
|
|
3
|
+
// const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
4
|
+
import rec from "./sox.js";
|
|
5
|
+
|
|
6
|
+
function load(recorderName) {
|
|
7
|
+
try {
|
|
8
|
+
// const recoderPath = path.resolve(__dirname, recorderName);
|
|
9
|
+
// const module = await import(recoderPath);
|
|
10
|
+
// return module.default;
|
|
11
|
+
return rec;
|
|
12
|
+
} catch (err) {
|
|
13
|
+
if (err.code === "MODULE_NOT_FOUND") {
|
|
14
|
+
throw new Error(`No such recorder found: ${recorderName}`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
throw err;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default {
|
|
22
|
+
load,
|
|
23
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export default (options) => {
|
|
2
|
+
let cmd = "rec";
|
|
3
|
+
|
|
4
|
+
if (options.binPath) {
|
|
5
|
+
cmd = options.binPath;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let args = [
|
|
9
|
+
"-q", // show no progress
|
|
10
|
+
"-r",
|
|
11
|
+
options.sampleRate, // sample rate
|
|
12
|
+
"-c",
|
|
13
|
+
options.channels, // channels
|
|
14
|
+
"-e",
|
|
15
|
+
"signed-integer", // sample encoding
|
|
16
|
+
"-b",
|
|
17
|
+
"16", // precision (bits)
|
|
18
|
+
"-t",
|
|
19
|
+
options.audioType, // audio type
|
|
20
|
+
"-", // pipe
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
if (options.endOnSilence) {
|
|
24
|
+
args = args.concat([
|
|
25
|
+
"silence",
|
|
26
|
+
"1",
|
|
27
|
+
"0.1",
|
|
28
|
+
options.thresholdStart || options.threshold + "%",
|
|
29
|
+
"1",
|
|
30
|
+
options.silence,
|
|
31
|
+
options.thresholdEnd || options.threshold + "%",
|
|
32
|
+
]);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return { cmd, args };
|
|
36
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export default (options) => {
|
|
2
|
+
let cmd = "sox";
|
|
3
|
+
|
|
4
|
+
if (options.binPath) {
|
|
5
|
+
cmd = options.binPath;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let args = [
|
|
9
|
+
"--default-device",
|
|
10
|
+
"--no-show-progress", // show no progress
|
|
11
|
+
"--rate",
|
|
12
|
+
options.sampleRate, // sample rate
|
|
13
|
+
"--channels",
|
|
14
|
+
options.channels, // channels
|
|
15
|
+
"--encoding",
|
|
16
|
+
"signed-integer", // sample encoding
|
|
17
|
+
"--bits",
|
|
18
|
+
"16", // precision (bits)
|
|
19
|
+
"--type",
|
|
20
|
+
options.audioType, // audio type
|
|
21
|
+
"-", // pipe
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
if (options.bufferSize) {
|
|
25
|
+
args.push("--buffer", options.bufferSize);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (options.endOnSilence) {
|
|
29
|
+
args = args.concat([
|
|
30
|
+
"silence",
|
|
31
|
+
"1",
|
|
32
|
+
"0.1",
|
|
33
|
+
options.thresholdStart || options.threshold + "%",
|
|
34
|
+
"1",
|
|
35
|
+
options.silence,
|
|
36
|
+
options.thresholdEnd || options.threshold + "%",
|
|
37
|
+
]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (options.arguments) {
|
|
41
|
+
args = args.concat(options.arguments);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const spawnOptions = {};
|
|
45
|
+
|
|
46
|
+
if (options.device) {
|
|
47
|
+
spawnOptions.env = { ...process.env, AUDIODEV: options.device };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { cmd, args, spawnOptions };
|
|
51
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Model, Recognizer, setLogLevel } from "vosk";
|
|
2
|
-
import record from "
|
|
1
|
+
import { Model, Recognizer, setLogLevel } from "vosk-koffi";
|
|
2
|
+
import record from "./record.js";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
|
|
5
5
|
const binPath = join(
|
|
@@ -14,6 +14,7 @@ const binPath = join(
|
|
|
14
14
|
console.log(binPath);
|
|
15
15
|
|
|
16
16
|
let COMMANDS = [
|
|
17
|
+
"blue",
|
|
17
18
|
"[unk]", // always keep an [unk] fallback!
|
|
18
19
|
];
|
|
19
20
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lumiastream/wakeword",
|
|
3
|
-
"version": "1.0.1-alpha.
|
|
3
|
+
"version": "1.0.1-alpha.8",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "lib/voice.
|
|
5
|
+
"main": "lib/voice.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"wakeword": "bin/wakeword"
|
|
8
8
|
},
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
"postinstall": "chmod +x binaries/soxmac binaries/soxlinux binaries/sox.exe || true"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"
|
|
20
|
-
"vosk": "^0.3.39"
|
|
19
|
+
"vosk-koffi": "^1.1.1"
|
|
21
20
|
}
|
|
22
21
|
}
|