Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // bytebeat.js (v2)
- // by funute - public domain
- /**
- * Now in ES6!
- * Run using node like this:
- *
- * $ node bytebeat.js < input.js | pacat --format=u8 --channels=1 --rate=8000 (PulseAudio)
- * $ node bytebeat.js < input.js | aplay --rate=8000 (ALSA)
- *
- * You can also "render" to a raw file like this:
- *
- * $ node bytebeat.js < input.js > output.raw
- *
- * Or you can just run it, enter stuff in, and listen like this:
- *
- * $ node bytebeat.js | aplay --rate=8000
- * ((t >> 10) & 42) * t
- * ^D
- */
- if (process.stdout.isTTY) {
- console.log('stdout is tty, exiting');
- process.exit(1);
- }
- const CHUNK = 1024;
- const LIM = Number(process.argv[2]) || -1;
- let expr = 'return ';
- process.stdin.on('data', (buf) => {
- expr += buf;
- });
- process.stdin.on('end', () => {
- // hacky "globalization" of Math (!)
- const mathNames = Object.getOwnPropertyNames(Math);
- const mathProps = mathNames.map((prop) => {
- return Math[prop];
- });
- const mfun = new Function(...mathNames, 't', expr);
- const fun = mfun.bind(null, ...mathProps)
- let t = 0;
- function write() {
- let ok = true;
- while (ok && (t < LIM || LIM == -1)) {
- const data = new Buffer(CHUNK);
- for (let k = 0; k < CHUNK; ++k) {
- data[k] = fun(t+k);
- }
- ok = process.stdout.write(data);
- t += CHUNK;
- }
- if (t < LIM || LIM == -1) {
- process.stdout.once('drain', write);
- }
- };
- write();
- });
- process.stdout.on('error', (err) => {
- if (err.code === 'EPIPE') {
- process.exit(0);
- } else {
- throw(err);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement