// import * as Juce from "juce-framework-frontend";
// import reportWebVitals from "../reportWebVitals";

const AutoTuneDataReceiver_eventId = "autoTuneData";
export default class AutoTuneDataReceiver {
  constructor() {
    this.input_pitch = 0;
    this.output_pitch = 0;
    let self = this;
    this.autoTuneDataRegistrationId = window.__JUCE__.backend.addEventListener(
      AutoTuneDataReceiver_eventId,
      (event) => {
        self.input_pitch = event.input_pitch;
        self.output_pitch = event.output_pitch;
      }
    );
  }

  frequencytoMidi(frequency) {
    return 69 + 12 * Math.log2(frequency / 440);
  }

  getAutotuneNote() {
    if (this.output_pitch <= 0) return -1;
    const midi = this.frequencytoMidi(this.output_pitch);
    return Math.round(midi % 12);
  }
  getInputCents() {
    const midi = this.frequencytoMidi(this.input_pitch);
    return Math.round((midi - Math.round(midi)) * 100);
  }
  getOutputCents() {
    const midi = this.frequencytoMidi(this.output_pitch);
    return Math.round((midi - Math.round(midi)) * 100);
  }

  unregister() {
    window.__JUCE__.backend.removeEventListener(
      this.autoTuneDataRegistrationId
    );
  }
}
