podcast-visualizer/generate-demuxer.ts

79 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-06-06 22:37:48 +02:00
import findLoudness, { SwapPoint } from "./find-loudness";
const graph_density = 8000;
const threshold_at_point = parseInt(process.env.demuxer_volume_threshold) || 1;
2020-06-06 22:37:48 +02:00
const inertia_s = 0.3;
const inertia_samples = inertia_s * graph_density;
2020-06-06 22:37:48 +02:00
const s = (n: number) => n / graph_density;
const minutes = (units: number) => Math.floor(s(units) / 60);
2020-06-06 22:37:48 +02:00
const hours = (units: number) => Math.floor(units / graph_density / 60 / 60);
const formatTime = (units: number) =>
`${hours(units)}:${minutes(units)}:${Math.floor(s(units) % 60)}`;
2020-06-06 22:37:48 +02:00
type Mode = { left: boolean; right: boolean };
2020-06-06 22:37:48 +02:00
async function run() {
const [left_breaks, right_breaks] = await Promise.all([
findLoudness("/tmp/leftraw", threshold_at_point, inertia_samples, "left"),
findLoudness("/tmp/rightraw", threshold_at_point, inertia_samples, "right"),
]);
const merged = [...left_breaks, ...right_breaks].sort((a, b) =>
a.position_start < b.position_start
? -1
: a.position_start > b.position_start
? 1
: 0
);
// console.log("left breaks:", left_breaks);
// console.log(`right_breaks`, right_breaks);
// console.log(`merged`, merged);
2020-06-06 22:37:48 +02:00
function new_mode(m: Mode, s: SwapPoint): Mode {
return { ...m, [s.label]: s.loud };
}
2020-06-06 22:37:48 +02:00
function mode_to_string(mode: Mode) {
if (mode.left && mode.right) {
return "both";
}
for (const side of ["left", "right"]) {
if (mode[side as keyof Mode]) {
return side;
}
}
return "none";
}
2020-05-15 21:48:24 +02:00
console.log("file", `${process.cwd()}/pics/none.png`);
let last_point = 0;
let mode: Mode = { left: false, right: false };
let last_file;
let total = 0;
for (let i = 2; i < merged.length; i++) {
const point = merged[i];
mode = new_mode(mode, point);
const file = `${process.cwd()}/pics/${mode_to_string(mode)}.png`;
const duration = (point.position_start - last_point) / graph_density;
console.log(
"duration",
(point.position_start - last_point) / graph_density
);
console.log("file", file);
last_point = point.position_start;
last_file = file;
total += duration * graph_density;
}
console.log("duration", merged[merged.length - 1].duration / graph_density);
console.log("file", last_file);
console.error(total, formatTime(total));
2020-06-06 22:37:48 +02:00
}
run();