61 lines
1.1 KiB
JavaScript
61 lines
1.1 KiB
JavaScript
|
import wavefile from 'wavefile';
|
||
|
let WaveFile = wavefile.WaveFile;
|
||
|
|
||
|
import {
|
||
|
readFileSync,
|
||
|
writeFileSync
|
||
|
} from 'fs';
|
||
|
|
||
|
function byte2byte(arr) {
|
||
|
let lines = [];
|
||
|
for (let i=0; i < arr.length; i++) {
|
||
|
lines.push(`.byte ${arr[i]}`);
|
||
|
}
|
||
|
return lines.join('\n');
|
||
|
}
|
||
|
|
||
|
function to4bit(val8) {
|
||
|
let val = val8 + 7 >> 4;
|
||
|
if (val > 15) {
|
||
|
return 15;
|
||
|
}
|
||
|
return val;
|
||
|
}
|
||
|
|
||
|
function pack(audio) {
|
||
|
let packed = [];
|
||
|
for (let i = 0; i < audio.length; i += 2) {
|
||
|
let low = to4bit(audio[i]);
|
||
|
let high = to4bit(audio[i + 1]);
|
||
|
let byte = low | (high << 4);
|
||
|
packed.push(byte);
|
||
|
}
|
||
|
return packed;
|
||
|
}
|
||
|
|
||
|
function audio2assembly(audio) {
|
||
|
return `.data
|
||
|
.export audio_samples
|
||
|
.export audio_samples_end
|
||
|
|
||
|
audio_samples:
|
||
|
${byte2byte(pack(audio))}
|
||
|
audio_samples_end:
|
||
|
|
||
|
`;
|
||
|
}
|
||
|
|
||
|
function wav2assembly(buffer) {
|
||
|
let wav = new WaveFile(buffer);
|
||
|
let samples = wav.getSamples();
|
||
|
return audio2assembly(samples);
|
||
|
}
|
||
|
|
||
|
let infile = process.argv[2];
|
||
|
let outfile = process.argv[3];
|
||
|
|
||
|
let buffer = readFileSync(infile);
|
||
|
let asm = wav2assembly(buffer);
|
||
|
writeFileSync(outfile, asm, 'utf-8');
|
||
|
|