test-card/av-sync/render-audio.js
Tomáš Mládek 33b6bbb216
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat: add a/v sync indicator scaffolding + PoC
2024-02-18 21:54:40 +01:00

33 lines
1.1 KiB
JavaScript

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);