-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of github.com:mrtianjin829/wavtobin into dev
- Loading branch information
Showing
4 changed files
with
83 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}})() |