Skip to content

Instantly share code, notes, and snippets.

@evert
Last active April 11, 2019 00:31
Show Gist options
  • Save evert/d7fdee2843c5267a5161f07594b4ca3a to your computer and use it in GitHub Desktop.
Save evert/d7fdee2843c5267a5161f07594b4ca3a to your computer and use it in GitHub Desktop.
async () => {
const response = await fetch('large.bin');
const buffer = await response.arrayBuffer();
};
const fs = require('fs');
const input = 'large.json';
const output = 'large.bin';
console.log('Reading %s', input);
const buffer = fs.readFileSync(input);
const json = JSON.parse(buffer);
console.log('Writing %s', output);
const outBuffer = new Int8Array(json.data.length);
let index = 0;
for(const item of json.data) {
// Input is between -1 and 1. This converts that float into a number between
// 1 and 255 inclusive.
const newValue = Math.floor((item * 127) + 128);
outBuffer[index] = newValue;
index++;
}
fs.writeFileSync(output, outBuffer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment