#33 Implement Recording #34
@@ -10,6 +10,7 @@ const { closeProcessWrapper } = require("../utilities/utilities");
|
|||||||
|
|
||||||
// Global vars
|
// Global vars
|
||||||
let pythonProcess;
|
let pythonProcess;
|
||||||
|
let recordingProcess;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Status of the discord process
|
* Get Status of the discord process
|
||||||
@@ -82,3 +83,77 @@ exports.leaveServer = async (req, res) => {
|
|||||||
|
|
||||||
return res.sendStatus(202);
|
return res.sendStatus(202);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start a recording of what the bot is listening to, if it's currently connected
|
||||||
|
*
|
||||||
|
* @param {*} req
|
||||||
|
* @param {*} res
|
||||||
|
*/
|
||||||
|
exports.startRecording = async (req, res) => {
|
||||||
|
log.INFO("Starting recording")
|
||||||
|
//if (pythonProcess === undefined) return res.sendStatus(204);
|
||||||
|
if (!recordingProcess === undefined) return res.sendStatus(202);
|
||||||
|
const deviceId = process.env.AUDIO_DEVICE_ID;
|
||||||
|
const filename = "./recordings/" + new Date().toJSON().slice(0,10) + ".wav";
|
||||||
|
|
||||||
|
// Joining the server to record
|
||||||
|
log.INFO("Start recording: ", deviceId, filename);
|
||||||
|
if (process.platform === "win32") {
|
||||||
|
log.DEBUG("Starting Windows Python");
|
||||||
|
recordingProcess = await spawn('python', [resolve(__dirname, "../pdab/recorder.py"), deviceId, filename ], { cwd: resolve(__dirname, "../pdab/").toString() });
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
log.DEBUG("Starting Linux Python");
|
||||||
|
recordingProcess = await spawn('python3', [resolve(__dirname, "../pdab/recorder.py"), deviceId, filename ], { cwd: resolve(__dirname, "../pdab/") });
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.getProcessOutput(recordingProcess);
|
||||||
|
|
||||||
|
return res.sendStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop the recording if the bot is currently recording
|
||||||
|
*
|
||||||
|
* @param {*} req
|
||||||
|
* @param {*} res
|
||||||
|
*/
|
||||||
|
exports.stopRecording = async (req, res) => {
|
||||||
|
log.INFO("Stopping recording the server");
|
||||||
|
if (!recordingProcess) return res.sendStatus(202)
|
||||||
|
|
||||||
|
recordingProcess = await closeProcessWrapper(recordingProcess);
|
||||||
|
|
||||||
|
return res.sendStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the output of a running process
|
||||||
|
*
|
||||||
|
* @param {*} runningProcess
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
exports.getProcessOutput = async (runningProcess) => {
|
||||||
|
let fullOutput;
|
||||||
|
runningProcess.stdout.setEncoding('utf8');
|
||||||
|
runningProcess.stdout.on("data", (data) => {
|
||||||
|
botLog.VERBOSE("From Process: ", data);
|
||||||
|
fullOutput += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
runningProcess.stderr.on('data', (data) => {
|
||||||
|
botLog.VERBOSE(`stderr: ${data}`);
|
||||||
|
fullOutput += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
runningProcess.on('close', (code) => {
|
||||||
|
log.DEBUG(`child process exited with code ${code}`);
|
||||||
|
log.VERBOSE("Full output from bot: ", fullOutput);
|
||||||
|
});
|
||||||
|
|
||||||
|
runningProcess.on("error", (code, signal) => {
|
||||||
|
log.ERROR("Error from the process: ", code, signal);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -27,4 +27,20 @@ router.post('/join', botController.joinServer);
|
|||||||
*/
|
*/
|
||||||
router.post('/leave', botController.leaveServer);
|
router.post('/leave', botController.leaveServer);
|
||||||
|
|
||||||
|
/** POST bot start recording
|
||||||
|
* If the bot is in a channel, it will start to record what it hears
|
||||||
|
*
|
||||||
|
* The status of the bot: 200 = starting to record, 202 = already recording, 204 = not in a server, 500 + JSON = encountered error
|
||||||
|
* @returns status
|
||||||
|
*/
|
||||||
|
router.post('/startRecording', botController.startRecording);
|
||||||
|
|
||||||
|
/** POST bot stop recording
|
||||||
|
* If the bot is recording, it will stop recording
|
||||||
|
*
|
||||||
|
* The status of the bot: 200 = will stop the recording, 204 = not currently recording, 500 + JSON = encountered error
|
||||||
|
* @returns status
|
||||||
|
*/
|
||||||
|
router.post('/stopRecording', botController.stopRecording);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user