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

export class TapeSpeedUp implements AudioProcessor {
	getName(): string {
		return 'Tape Speed Up';
	}

	getDescription(): string {
		return 'Simulates a tape machine accelerating from slow to normal speed';
	}

	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 accelDuration = length * 0.3;

		let readPos = 0;

		for (let i = 0; i < length; i++) {
			if (i < accelDuration) {
				const t = i / accelDuration;
				const curve = t * t * t;
				const speed = curve;
				readPos += speed;
			} else {
				readPos += 1.0;
			}

			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];
	}
}
