disable auto gain control

This commit is contained in:
Aubrey 2025-01-08 16:11:53 -06:00
parent 3c54a5aafd
commit 9f4fafccdf
No known key found for this signature in database
3 changed files with 51 additions and 15 deletions

View file

@ -51,15 +51,16 @@ export class AudioManager {
audio.play(); audio.play();
outputDiv.appendChild(audio); outputDiv.appendChild(audio);
return new PlayerAudio(this.ac, dest, gain, localPlayer, peerPlayer, globalSpeak); return new PlayerAudio(this.ac, audio, dest, gain, localPlayer, peerPlayer, globalSpeak);
} }
} }
export class PlayerAudio { export class PlayerAudio {
private source: MediaStreamAudioSourceNode | undefined; private source: MediaStreamAudioSourceNode | undefined;
// private playerWatcher: Readable<[number[], boolean]>; private ender: Unsubscriber;
constructor( constructor(
private context: AudioContext, private context: AudioContext,
private audioElement: HTMLAudioElement,
private dest: MediaStreamAudioDestinationNode, private dest: MediaStreamAudioDestinationNode,
private gain: GainNode, private gain: GainNode,
localPlayer: Readable<Player | undefined>, localPlayer: Readable<Player | undefined>,
@ -68,7 +69,7 @@ export class PlayerAudio {
) { ) {
console.log("created player audio"); console.log("created player audio");
subscribe([localPlayer, peerPlayer], ([localPlayer, peerPlayer]) => { this.ender = subscribe([localPlayer, peerPlayer], ([localPlayer, peerPlayer]) => {
if (!localPlayer || !peerPlayer) { if (!localPlayer || !peerPlayer) {
this.gain.gain.value = 0; this.gain.gain.value = 0;
// console.warn("missing a player..."); // console.warn("missing a player...");
@ -81,7 +82,7 @@ export class PlayerAudio {
const [x, y, z] = localInfo.position.map((localPosition, i) => peerInfo.position[i] - localPosition); const [x, y, z] = localInfo.position.map((localPosition, i) => peerInfo.position[i] - localPosition);
const sameStage = localInfo.stage === peerInfo.stage; const sameStage = localInfo.stage === peerInfo.stage;
let magnitude = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)); let magnitude = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
const max = 3500, min = 750; const max = 3500, min = 1000;
let scaledGain = 1 - (magnitude - min) / max; let scaledGain = 1 - (magnitude - min) / max;
let t = Math.max(Math.min(scaledGain * +sameStage, 1), 0); let t = Math.max(Math.min(scaledGain * +sameStage, 1), 0);
function easeInExpo(x: number): number { function easeInExpo(x: number): number {
@ -105,4 +106,9 @@ export class PlayerAudio {
gainOut.connect(this.dest); gainOut.connect(this.dest);
// gainOut.connect(this.context.destination); // gainOut.connect(this.context.destination);
} }
delete() {
this.audioElement.parentElement?.removeChild(this.audioElement);
this.ender();
}
} }

View file

@ -49,10 +49,20 @@ export class Client {
return client; return client;
} }
public close() {
this.transport.close({ reason: "reconnecting prolly" });
for (const peer of Object.values(this.peers)) {
peer.close();
}
}
private async closeHandler() { private async closeHandler() {
await this.transport.closed; try {
await this.transport.closed;
} catch (_) { }
console.error("closed"); console.error("closed");
this.connected.set(false); this.connected.set(false);
this.close();
} }
private getPeer(id: string) { private getPeer(id: string) {
@ -227,6 +237,11 @@ export class Peer {
}); });
} }
public close() {
this.connection.close();
this.playerAudio.delete();
}
public async offer() { public async offer() {
const offer = await this.connection.createOffer({ offerToReceiveAudio: true }); const offer = await this.connection.createOffer({ offerToReceiveAudio: true });
await this.connection.setLocalDescription(offer); await this.connection.setLocalDescription(offer);

View file

@ -4,6 +4,7 @@
import { AudioManager } from "$lib/audio"; import { AudioManager } from "$lib/audio";
import { identity, localStore } from "$lib/stores"; import { identity, localStore } from "$lib/stores";
import { import {
get,
readable, readable,
readonly, readonly,
type Readable, type Readable,
@ -37,20 +38,32 @@
); );
let client: Client = $state(undefined as any); let client: Client = $state(undefined as any);
let players: Writable<Record<number, Player>> = undefined as any; let players: Writable<Record<number, Player>> = undefined as any;
let connected: Readable<boolean> = undefined as any; let connected = $state(false);
let ac: AudioManager; let ac: AudioManager;
let wyrm: HTMLAudioElement; let wyrm: HTMLAudioElement;
let outputAudio: HTMLDivElement; let outputAudio: HTMLDivElement;
async function audioPrep() { async function audioPrep() {
let media = $useMusic let media;
? wyrm if ($useMusic) {
: await navigator.mediaDevices.getUserMedia({ media = wyrm;
audio: true, } else {
video: false, media = await navigator.mediaDevices.getUserMedia({
}); audio: {
autoGainControl: false,
noiseSuppression: true,
},
video: false,
});
}
ac = new AudioManager(media, micVolume, muted); ac = new AudioManager(media, micVolume, muted);
} }
async function connect() { async function connect() {
if (client) {
if (get(client.connected)) {
return;
}
client.close();
}
await audioPrep(); await audioPrep();
client = await Client.connect( client = await Client.connect(
readonly(name), readonly(name),
@ -63,7 +76,9 @@
console.log("players", $players); console.log("players", $players);
players = client.players; players = client.players;
}); });
connected = readonly(client.connected); client.connected.subscribe(() => {
connected = true;
});
console.log(client); console.log(client);
} }
</script> </script>
@ -115,8 +130,8 @@
</div> </div>
{/if} {/if}
</div> </div>
{#if client !== undefined} {#if connected}
<input type="checkbox" disabled bind:checked={$connected} /> <input type="checkbox" disabled bind:checked={connected} />
<datalist> <datalist>
{#each Object.values($players) as player} {#each Object.values($players) as player}
<option>{player.name}</option> <option>{player.name}</option>