import type { AudioProcessor, ProcessorCategory } from './AudioProcessor';

export class SlowTapeStop implements AudioProcessor {
	getName(): string {
		return 'Slow Tape Stop';
	}

	getDescription(): string {
		return 'Simulates a tape machine gradually slowing to a stop';
	}

	getCategory(): ProcessorCategory {
		return 'Time';
	}

	process(leftIn: Float32Array, rightIn: Float32Array): [Float32Array, Float32Array] {
		const length = leftIn.length;
		const leftOut = new Float32Array(length);
		const rightOut = new Float32Array(length);

		const stopPoint = length * 0.4;
		const stopDuration = length * 0.6;

		let readPos = 0;

		for (let i = 0; i < length; i++) {
			if (i < stopPoint) {
				readPos = i;
			} else {
				const t = (i - stopPoint) / stopDuration;
				const curve = 1.0 - (t * t * t);
				const speed = Math.max(0, curve);
				readPos += speed;
			}

			const idx = Math.floor(readPos);
			const frac = readPos - idx;

			if (idx < length - 1) {
				leftOut[i] = leftIn[idx] * (1.0 - frac) + leftIn[idx + 1] * frac;
				rightOut[i] = rightIn[idx] * (1.0 - frac) + rightIn[idx + 1] * frac;
			} else if (idx < length) {
				leftOut[i] = leftIn[idx];
				rightOut[i] = rightIn[idx];
			}
		}

		return [leftOut, rightOut];
	}
}
