test-card/av-sync/render-audio.js

34 lines
1.1 KiB
JavaScript
Raw Normal View History

import { Command } from 'commander';
import wav from 'node-wav';
import fs from 'fs';
const program = new Command();
program
.requiredOption('-i, --input <input>', 'Input file')
.requiredOption('-o, --output <output>', 'Output file')
.requiredOption('--repeats <repeats>', 'Number of repeats')
.parse(process.argv);
const options = program.opts();
let beep = wav.decode(fs.readFileSync(options.input));
let samples = beep.channelData[0];
const sampleRate = beep.sampleRate;
const silenceDuration = sampleRate - samples.length;
const silenceSamples = new Float32Array(silenceDuration).fill(0);
let oneSecondChunk = new Float32Array(sampleRate);
oneSecondChunk.set(samples, 0);
oneSecondChunk.set(silenceSamples, samples.length);
let numberOfRepeats = parseInt(options.repeats);
let finalSamples = new Float32Array(sampleRate * numberOfRepeats);
for (let i = 0; i < numberOfRepeats; i++) {
finalSamples.set(oneSecondChunk, i * sampleRate);
}
let finalBuffer = wav.encode([finalSamples], { sampleRate: sampleRate, float: true, bitDepth: 32 });
fs.writeFileSync(options.output, finalBuffer);