-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
130 lines (105 loc) · 3.06 KB
/
index.ts
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
import dotenv from "dotenv";
import play from "./utils/play";
import wakeup from "./utils/wakeup";
import { EventEmitter } from "stream";
import recordWait from "./utils/recordWait";
import TTS from "./utils/TTS";
import STT from "./utils/STT";
import query from "./utils/query";
import { SerialPort } from "serialport";
import fs from "fs";
import DomoticClient from "./DomoticClient";
dotenv.config();
import conf = require("./config.json");
class VelcroClient {
private audioDevice: string;
private _domotic: DomoticClient;
private _os: "linux" | "windows" = "windows";
private _events = new EventEmitter();
constructor(config: typeof conf) {
this.audioDevice = config.audioInput;
this._domotic = new DomoticClient(config);
if (conf.os === "windows") this._os = "windows";
else if (conf.os === "linux") this._os = "linux";
else throw Error("OS target not found");
}
on(eventName: "wake", listener: () => void): this;
on(eventName: "wake" | string, listener: (...args: any[]) => void): any {
this._events.on(eventName, (args) => {
listener(args);
});
}
emit(eventName: "wake"): boolean;
emit(eventName: "wake" | string, ...args: any[]): any {
this._events.emit(eventName, args);
}
//@ts-ignore
communicate(callback: (code: boolean) => void | Promise<void>) {
console.log(`Waiting for speech...`);
let _this = this;
recordWait(this.audioDevice, async (err, filepath) => {
if (err) {
this.communicate(callback);
} else if (filepath) {
let { text } = await STT(filepath);
if (text === "") {
this.communicate(callback);
return;
}
console.log(`User:`, text);
let response = await query(text);
if (response) {
let res = response.response;
let code = response.code;
if (code) {
console.log(code.replace(/^(client)/g, "_this"));
eval(code.replace(/^(client)/g, "_this"));
}
console.log(`Velcro:`, res);
let { path } = await TTS(res);
play(path, async () => {
fs.rmSync(path);
fs.rmSync(filepath);
await callback(code ? true : false);
});
}
}
});
}
async wake() {
let activated = await wakeup("velcro", this.audioDevice);
if (activated) this.emit("wake");
return activated;
}
quit() {
process.kill(process.pid);
}
music(service: string) {
return {
search: (str: string) => {
return {
play: () => {
(async () => {
let s = this._domotic[service];
let url = await s.search(str);
if (url) {
await s.play(url);
}
})();
},
};
},
};
}
}
// Microphone (USB Audio Device)
// let client = new VelcroClient(require("./config.json"));
// (async () => {
// async function vel() {
// client.communicate(async (isThereCode) => {
// await vel();
// });
// }
// vel();
// })();
export default VelcroClient;