-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (138 loc) · 3.89 KB
/
index.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
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
require("dotenv").config();
const setTimeout = require("timers/promises").setTimeout;
const mqtt = require("mqtt");
const fs = require("fs");
const spawn = require("child_process").spawn;
const MPR121 = require("mpr121");
const mpr121 = new MPR121(0x5a, 1);
const WaveFile = require("wavefile").WaveFile;
const ws281x = require('rpi-ws281x-native');
let childRecord;
let childPlay;
const inLocalMode = false;
let isRecording = false;
let isPlaying = false;
const brightness = 120;
// colors are BGR!
const options = {
gpio: 10,
brightness: brightness,
stripType: ws281x.stripType.SK6812W,
};
const channel = ws281x(1, options);
const colors = channel.array;
ws281x.reset();
// update color-values
colors[0] = 0x00ffff;
ws281x.render();
console.log("starting...");
const client = mqtt.connect(process.env.MQTT_HOST, {
username: process.env.MQTT_USER,
password: process.env.MQTT_PASSWORD,
clientId: process.env.CLIENT_ID,
clean: false,
reconnectPeriod: 1,
});
// MQTT pub/sub
// prints a received message
client.on("message", function (topic, payload) {
if (topic === process.env.TOPIC_SUB) {
console.log(`${process.env.TOPIC_SUB} received`);
if (!inLocalMode) playFile(payload);
}
});
// reassurance that the connection worked
client.on("connect", () => {
console.log("Connected!");
ws281x.reset();
});
// prints an error message
client.on("error", (error) => {
console.log("Error:", error);
});
// subscribe and publish to the same topic
client.subscribe(process.env.TOPIC_SUB);
// listen for pin 5 events
mpr121.on(5, (state) => {
console.log(`pin 5: ${state}`);
if (state && !isRecording &&!isPlaying) {
startRecording();
} else if (!state && childRecord && isRecording && !isPlaying) {
stopRecording();
}
});
// arecord -D plughw:0 -c1 -r 48000 -f S32_LE -t wav -V mono -v file.wav
const startRecording = () => {
colors[0] = 0x00ff00;
ws281x.render();
isRecording = true;
const voiceFile = `${new Date().getTime()}.wav`; // generating the music file name
childRecord = spawn("arecord", [
"-D",
"plughw:1",
"-c1",
"-r",
"48000",
"-f",
"S32_LE",
"-t",
"wav",
"-V",
"mono",
"-v",
`${voiceFile}`,
]);
childRecord.on("exit", function (code, sig) {
if (code !== null && sig === null) {
console.log("done recording");
isRecording = false;
inLocalMode ? playFile(voiceFile) : sendFile(voiceFile);
}
});
childRecord.stderr.on("data", function (data) {
console.log("music record stderr :" + data);
});
childRecord.stdout.on("data", function (data) {
console.log("music record stdout data: " + data);
});
};
const playFile = async (payload) => {
isPlaying = true;
const file = inLocalMode ? payload : await createWavFile(payload);
childPlay = spawn("aplay", [`${file}`]);
childPlay.on("exit", function (code, sig) {
if (code !== null && sig === null) {
console.log("done playing");
deleteFile(file);
isPlaying = false;
}
});
childPlay.stderr.on("data", function (data) {
console.log("music record stderr :" + data);
});
};
const stopRecording = async () => {
colors[0] = 0x0088ff;
ws281x.render();
await setTimeout(1000)
ws281x.reset();
console.log("stopped recording");
childRecord.kill("SIGTERM");
};
const sendFile = (voiceFile) => {
const recordedBuffer = new WaveFile(fs.readFileSync(voiceFile)).toBuffer();
client.publish(process.env.TOPIC, recordedBuffer);
};
const createWavFile = async (bufferMsg) => {
let receivedFile = `receivedFile-${new Date().getTime()}.wav`; // generating the music file name
await fs.promises.writeFile(receivedFile, bufferMsg);
return receivedFile;
};
// To delete file:
const deleteFile = async (path) => {
await setTimeout(1000);
fs.unlink(path, (err) => {
if (err) throw err; //handle your error the way you want to;
console.log(`${path} was deleted`); //or else the file will be deleted
});
};