Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:mrtianjin829/wavtobin into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtianjin829 committed Aug 29, 2023
2 parents 01501fb + f530baa commit 7cebf4a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
18 changes: 12 additions & 6 deletions argp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ function gen(args) {
let cf = {
file: "",
sampleRate: 32000,
output: "out.bin"
output: "out.bin",
useFFmpeg: false,
help: false
};
args.forEach((v,i)=>{
if(v == "-r"){
if(i <= 1) return

if(v == "--sampleRate" || v == "-r"){
cf.sampleRate = parseInt(args[i+1]);
} else if(v == "-o"){
} else if(v == "--help" || v == "-h") {
cf.help = true
} else if(v == "--output" || v == "-o"){
cf.output = args[i+1]
} else if(v.endsWith(".wav")){
} else if(v == "--ffmpeg"){
cf.useFFmpeg = true
} else {
cf.file = v
}
})
console.log("Received arguments as: ")
console.log(cf)
return cf;
}

Expand Down
30 changes: 30 additions & 0 deletions ffmpeg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
let cp = require("child_process") // sussy variable name o_O
const pth = require("path")
const os = require("os")
function makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
async function ffmpeg(input){
let ret = {
path: "",
exitcode: 0
};
let path = pth.resolve(os.tmpdir(),makeid(16)+".wav")
let proc = cp.spawn("ffmpeg",["-i",input,path])
proc.stdout.pipe(process.stdout)
let get_code = ()=>new Promise(res=>proc.on("close",res))
ret.exitcode = await get_code();
ret.path = path
console.log("[wtb.ffmpeg] exit code is "+ret.exitcode)
return ret
}

module.exports = ffmpeg
10 changes: 10 additions & 0 deletions help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
USAGE: (command) [OPTIONS] FILE
Options:

-o / --output : Specify output file (default is out.bin)
-r / --sampleRate : Sample rate of output file (default is 32000)
--ffmpeg : Use ffmpeg to enable wavtobin to process other audio types

Example:
(command) -r 32000 -o out.bin file.wav
(command) -r 48000 -o /home/myfile.bin --ffmpeg file.mp3
38 changes: 31 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
#!/usr/bin/node
console.log("WaveBin");
console.log("[wtb] WaveToBin");
const ffmpeg = require("./ffmpeg");
(async function(){
try {
// Import library for handling wav files.
const { WaveFile } = require("wavefile");
// Load filesystem API
const fs = require("fs");
// Initialize output file as an wave file
const wf = new WaveFile();
// Procure argyments
const cf = require("./argp")(process.argv)
const fd = fs.readFileSync(cf.file);
// Read the file.
if(cf.help){
console.log(fs.readFileSync("help.txt").toString("utf8"))
return
}
let inputFilePath = cf.file
if(cf.useFFmpeg){
console.log("[wtb] Waiting for FFmpeg")
inputFilePath = (await ffmpeg(inputFilePath)).path
}
const fd = fs.readFileSync(inputFilePath);
// Load input file into wf.
wf.fromBuffer(fd);
wf.toSampleRate(cf.sampleRate);
wf.toBitDepth("8");
/*do important conversions
*this will convert the sample-type into PCM u8
*changes the sample rate to the desired rate otherwise 32 Khz (good for html5bytebeat)
* */
if(wf.fmt.sampleRate != cf.sampleRate) wf.toSampleRate(cf.sampleRate);
if(wf.bitDepth !== "8") wf.toBitDepth("8");
// Obtain "samples" from audio
let smp = wf.getSamples();
let n;
let n; // contains the PCM buffer for output file
// properly interleave the channels
if (wf.fmt.numChannels == 2) {
let [a, b] = smp;
// Use average to mix stereo channels.
n = a.map((v, i) => (v + b[i]) / 2);
} else {
n = smp;
}
n = new Uint8Array(n);
fs.writeFileSync(cf.output, Buffer.from(n));
fs.writeFileSync(cf.output, Buffer.from(n)); // Write output.
} catch (err) {
// Definitely helpful error handling.
console.log("[wtb] an error occurred:");
throw err
process.exit(1);
}
}})()

0 comments on commit 7cebf4a

Please sign in to comment.