// AudioWorklet Processor für MotorSound
class MotorSoundProcessor extends AudioWorkletProcessor {
  constructor() {
    super();
    this.data = null;
    this.speed = 0.6;
    this.currentFrame = 0;
    
    this.port.onmessage = (e) => {
      const { type, data, speed } = e.data;
      switch (type) {
        case 'init':
          this.data = data;
          this.speed = speed;
          break;
        case 'updateData':
          this.data = data;
          break;
        case 'updateSpeed':
          this.speed = speed;
          break;
      }
    };
  }

  process(inputs, outputs, parameters) {
    const output = outputs[0];
    if (!this.data || this.data.length === 0) return true;

    for (let channel = 0; channel < output.length; channel++) {
      const outputChannel = output[channel];
      for (let i = 0; i < outputChannel.length; i++) {
        this.currentFrame += this.speed;
        const index = Math.floor(this.currentFrame) % this.data.length;
        outputChannel[i] = this.data[index];
      }
    }
    this.currentFrame %= this.data.length;
    return true;
  }
}

registerProcessor('motor-sound-processor', MotorSoundProcessor);
