const graph_density = 1000;

const threshold_at_point = 1;

const inertia_s = 0.1;
const inertia_samples = inertia_s * graph_density;

let position = 0;
let last_swap_position = 0;

const s = (n: number) => Math.round(n / graph_density);

const minutes = (units: number) => Math.floor(s(units) / 60);

const formatTime = (units: number) =>
	`${minutes(units)}:${Math.floor(s(units) % 60)}`;

let keep_loud_until = 0;

let total_speaking = 0;

const results: [string, number][] = [];

process.stdin.on("readable", () => {
	let chunk: Buffer | null;
	let was_loud_last_time = false;
	while ((chunk = process.stdin.read()) !== null) {
		for (let i = 0; i < chunk.byteLength; i++) {
			position++;
			const byte = chunk[i];
			const volume = Math.abs(byte - 128);
			const is_loud: boolean =
				volume > threshold_at_point || position < keep_loud_until;
			if (is_loud) {
				total_speaking++;
			}
			if (is_loud != was_loud_last_time) {
				results.push([
					is_loud ? "silence" : "speaking",
					position - last_swap_position,
				]);
				last_swap_position = position;
				was_loud_last_time = is_loud;
			}
			if (volume > threshold_at_point) {
				keep_loud_until = position + inertia_samples;
			}
		}
	}
});

const mode_to_image = (mode: string) => {
	return mode === "silence" ? "pics/cisza.png" : "pics/kuba.png";
};

process.stdin.on("end", () => {
	results.forEach(([mode, duration_units]) => {
		console.log("file", `'${mode_to_image(mode)}'`);
		console.log("duration", (duration_units / graph_density).toFixed(4));
	});

	console.log("file", `'${mode_to_image(results[results.length - 1][0])}'`);

	// console.log(formatTime(total_speaking), formatTime(position));
});
