export class AudioManager {
  globalGain = $state(1.0);
  useMusic: boolean;
  ac: AudioContext;
  dest: MediaStreamAudioDestinationNode;
  gain: GainNode;
  source: AudioNode;
  public readonly output: MediaStream;

  constructor(media: HTMLAudioElement | MediaStream) {
    this.ac = new AudioContext();
    this.dest = this.ac.createMediaStreamDestination();
    this.gain = this.ac.createGain();
    globalThis.audioPlay = this;

    if (media instanceof HTMLAudioElement) {
      this.source = this.ac.createMediaElementSource(media);
      media.autoplay = true;
      media.controls = true;
      media.play();
      this.useMusic = true;
    } else {
      this.source = this.ac.createMediaStreamSource(media);
      this.useMusic = false;
    }
    // .connect(this.gain)
    // this.source.connect(this.dest);
    this.gain.gain.value /= 32;
    this.source.connect(this.gain).connect(this.dest)
    this.output = this.dest.stream;
    // this.output = media;
    console.log("epic", this.dest.stream, this.output);
  }
}
