Skip to content

Commit

Permalink
Add channels and bitDepth options
Browse files Browse the repository at this point in the history
  • Loading branch information
qualitymanifest committed Jun 27, 2020
1 parent db76056 commit 400a7b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ node main.js [options]
--minDuration | After timeout, if recording was less than <minDuration> seconds, file is deleted | DEFAULT: 5
--maxFiles | Number of files to save before exiting program | DEFAULT: 5
--dateFmt | Date formatting: "datetime", "unix", or custom moment.js format | DEFAULT: "datetime"
--sampleRate | Input sample rate in Hz | DEFAULT: 48000
--sampleRate | Sample rate in Hz | DEFAULT: 48000
--channels | 1 for mono, 2 for stereo, etc | DEFAULT: 1
--bitDepth | Bits per sample | DEFAULT: 16
--host | Local IP address serving data | DEFAULT: "127.0.0.1"
--port | Local UDP port serving data | DEFAULT: 7355
-p | Print options used and source they were chosen from (CLI, config file, default) |
Expand Down
8 changes: 2 additions & 6 deletions src/create_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ const { FileWriter } = require("wav");
const moment = require("moment");

const createFile = (options, currFileNum) => {
const { dateFmt, maxFiles, sampleRate } = options;
const { dateFmt, maxFiles, sampleRate, channels, bitDepth } = options;
const fileName = join("recordings", `${moment().format(dateFmt)}.wav.temp`);
console.log(`* Starting recording #${currFileNum}/${maxFiles}, filename: ${fileName}`);
return {
timeoutRunning: false,
millisElapsed: 0,
recordStartTime: Date.now(),
name: fileName,
writer: new FileWriter(fileName, {
channels: 1,
sampleRate,
bitDepth: 16
})
writer: new FileWriter(fileName, { channels, sampleRate, bitDepth })
}
}

Expand Down
31 changes: 17 additions & 14 deletions src/handle_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const defaultOptions = {
maxFiles: {val: 5, src: "DEFAULT"},
dateFmt: {val: "datetime", src: "DEFAULT"},
sampleRate: {val: 48000, src: "DEFAULT"},
channels: {val: 1, src: "DEFAULT"},
bitDepth: {val: 16, src: "DEFAULT"},
host: {val: "127.0.0.1", src: "DEFAULT"},
port: {val: 7355, src: "DEFAULT"},
};
Expand Down Expand Up @@ -49,8 +51,14 @@ const isOptionInvalid = (option, key) =>{
if (key === "port" && isNumInvalid({ num: val, min: 1, max: 65535, isInt: true })) {
return wasInvalid(option, key, "Must be an integer between 1 and 65535");
}
if (key === "sampleRate" && isNumInvalid({ num: val, min: 0, isInt: true, })) { // Not sure what all the valid wav sample rates are
return wasInvalid(option, key, "Must be a positive integer");
if (key === "sampleRate" && isNumInvalid({ num: val, min: 1, max: 4_300_000_000, isInt: true, })) {
return wasInvalid(option, key, "Must be an integer between 1 and 4300000000");
}
if (key === "channels" && isNumInvalid({ num: val, min: 1, max: 65535, isInt: true })) {
return wasInvalid(option, key, "Must be an integer between 1 and 65535");
}
if (key === "bitDepth" && isNumInvalid({ num: val, min: 4, max: 32, isInt: true })) {
return wasInvalid(option, key, "Must be an integer between 4 and 32");
}
return false;
}
Expand Down Expand Up @@ -88,18 +96,13 @@ const handleOptions = () => {
if (args.p) {
printOptions(finalOptions);
}
const { timeout, minDuration, maxFiles, dateFmt, sampleRate, host, port } = finalOptions;
return {
// timeout specified in seconds, convert to milliseconds
// minDuration should be kept as seconds for printing when file is too short
timeout: timeout.val * 1000,
minDuration: minDuration.val,
maxFiles: maxFiles.val,
dateFmt: dateFmtOptions(dateFmt.val),
sampleRate: sampleRate.val,
host: host.val,
port: port.val
}
// Only needed option.src for printing purposes, so each option should just contain it's value now
Object.keys(finalOptions).map(key => finalOptions[key] = finalOptions[key].val);
// timeout specified in seconds, convert to milliseconds
// minDuration should be kept as seconds for printing when file is too short
finalOptions.timeout *= 1000;
finalOptions.dateFmt = dateFmtOptions(finalOptions.dateFmt)
return finalOptions;
}

module.exports = handleOptions;

0 comments on commit 400a7b8

Please sign in to comment.