import { Command } from 'commander';
import wav from 'node-wav';
import fs from 'fs';
const program = new Command();
program
.requiredOption('-i, --input ', 'Input file')
.requiredOption('-o, --output ', 'Output file')
.requiredOption('--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 halfSecondSilence = new Float32Array(sampleRate / 2).fill(0);
finalSamples = Float32Array.from([...halfSecondSilence, ...finalSamples]);
let finalBuffer = wav.encode([finalSamples], { sampleRate: sampleRate, float: true, bitDepth: 32 });
fs.writeFileSync(options.output, finalBuffer);