smo-proximity-chat/src/lib/audio.svelte.ts
2024-12-20 04:33:50 -06:00

35 lines
1 KiB
TypeScript

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