import React, { useContext, useEffect, useMemo, useRef, useState } from 'react'; import io, { Socket } from 'socket.io-client'; import Avatar from './Avatar'; import { GameStateContext, SettingsContext } from './App'; import { AmongUsState, GameState, Player } from '../main/GameReader'; import Peer from 'simple-peer'; import { ipcRenderer, remote } from 'electron'; import VAD from './vad'; import { ISettings } from './Settings'; interface PeerConnections { [peer: string]: Peer.Instance; } interface InCall { [peer: string]: boolean; } interface AudioElements { [peer: string]: { element: HTMLAudioElement; gain: GainNode; pan: PannerNode; }; } interface AudioListeners { [peer: string]: any; } interface SocketIdMap { [socketId: string]: number; } interface ConnectionStuff { socket: typeof Socket; stream: MediaStream; pushToTalk: boolean; deafened: boolean; } interface OtherTalking { [playerId: number]: boolean; // isTalking } interface OtherDead { [playerId: number]: boolean; // isTalking } // function clamp(number: number, min: number, max: number): number { // if (min > max) { // let tmp = max; // max = min; // min = tmp; // } // return Math.max(min, Math.min(number, max)); // } // function mapNumber(n: number, oldLow: number, oldHigh: number, newLow: number, newHigh: number): number { // return clamp((n - oldLow) / (oldHigh - oldLow) * (newHigh - newLow) + newLow, newLow, newHigh); // } function calculateVoiceAudio(state: AmongUsState, settings: ISettings, me: Player, other: Player, gain: GainNode, pan: PannerNode): void { const audioContext = pan.context; pan.positionZ.setValueAtTime(-0.5, audioContext.currentTime); let panPos = [ (other.x - me.x), (other.y - me.y) ]; if (state.gameState === GameState.DISCUSSION || (state.gameState === GameState.LOBBY && !settings.stereoInLobby)) { panPos = [0, 0]; } if (isNaN(panPos[0])) panPos[0] = 999; if (isNaN(panPos[1])) panPos[1] = 999; panPos[0] = Math.min(999, Math.max(-999, panPos[0])); panPos[1] = Math.min(999, Math.max(-999, panPos[1])); if (other.inVent) { gain.gain.value = 0; return; } if (me.isDead && other.isDead) { gain.gain.value = 1; pan.positionX.setValueAtTime(panPos[0], audioContext.currentTime); pan.positionY.setValueAtTime(panPos[1], audioContext.currentTime); return; } if (!me.isDead && other.isDead) { gain.gain.value = 0; return; } if (state.gameState === GameState.LOBBY || state.gameState === GameState.DISCUSSION) { gain.gain.value = 1; pan.positionX.setValueAtTime(panPos[0], audioContext.currentTime); pan.positionY.setValueAtTime(panPos[1], audioContext.currentTime); } else if (state.gameState === GameState.TASKS) { // const distance = Math.sqrt(Math.pow(me.x - other.x, 2) + Math.pow(me.y - other.y, 2)); gain.gain.value = 1; // gain.gain.value = mapNumber(distance, 0, 2.66, 1, 0); pan.positionX.setValueAtTime(panPos[0], audioContext.currentTime); pan.positionY.setValueAtTime(panPos[1], audioContext.currentTime); } else { gain.gain.value = 0; } if (gain.gain.value === 1 && Math.sqrt(Math.pow(panPos[0], 2) + Math.pow(panPos[1], 2)) > 7) { gain.gain.value = 0; } } export default function Voice() { const [settings] = useContext(SettingsContext); const settingsRef = useRef(settings); const gameState = useContext(GameStateContext); let { lobbyCode: displayedLobbyCode } = gameState; if (displayedLobbyCode !== 'MENU' && settings.hideCode) displayedLobbyCode = 'LOBBY'; const [talking, setTalking] = useState(false); const [socketPlayerIds, setSocketPlayerIds] = useState({}); const [connect, setConnect] = useState<({ connect: (lobbyCode: string, playerId: number) => void }) | null>(null); const [otherTalking, setOtherTalking] = useState({}); const [otherDead, setOtherDead] = useState({}); const audioElements = useRef({}); const [deafenedState, setDeafened] = useState(false); const [connected, setConnected] = useState(false); useEffect(() => { if (!connectionStuff.current.stream) return; connectionStuff.current.stream.getAudioTracks()[0].enabled = !settings.pushToTalk; connectionStuff.current.pushToTalk = settings.pushToTalk; }, [settings.pushToTalk]); useEffect(() => { settingsRef.current = settings; }, [settings]); useEffect(() => { if (gameState.gameState === GameState.LOBBY) { setOtherDead({}); } else if (gameState.gameState !== GameState.TASKS) { if (!gameState.players) return; setOtherDead(old => { for (let player of gameState.players) { old[player.id] = player.isDead || player.disconnected; } return { ...old }; }); } }, [gameState.gameState]); // const [audioContext] = useState(() => new AudioContext()); const connectionStuff = useRef({ pushToTalk: settings.pushToTalk, deafened: false } as any); useEffect(() => { // Connect to voice relay server connectionStuff.current.socket = io(settings.serverURL, { transports: ['websocket'] }); const { socket } = connectionStuff.current; socket.on('connect', () => { setConnected(true); }); socket.on('disconnect', () => { setConnected(false); }); // Initialize variables let audioListener: any; let audio: boolean | MediaTrackConstraints = true; // Get microphone settings if (settings.microphone.toLowerCase() !== 'default') audio = { deviceId: settings.microphone }; navigator.getUserMedia({ video: false, audio }, async (stream) => { connectionStuff.current.stream = stream; stream.getAudioTracks()[0].enabled = !settings.pushToTalk; ipcRenderer.on('toggleDeafen', () => { connectionStuff.current.deafened = !connectionStuff.current.deafened; stream.getAudioTracks()[0].enabled = !connectionStuff.current.deafened; setDeafened(connectionStuff.current.deafened); }); ipcRenderer.on('pushToTalk', (_: any, pressing: boolean) => { if (!connectionStuff.current.pushToTalk) return; if (!connectionStuff.current.deafened) { stream.getAudioTracks()[0].enabled = pressing; } // console.log(stream.getAudioTracks()[0].enabled); }); const ac = new AudioContext(); ac.createMediaStreamSource(stream) audioListener = VAD(ac, ac.createMediaStreamSource(stream), undefined, { onVoiceStart: () => setTalking(true), onVoiceStop: () => setTalking(false), // onUpdate: console.log, noiseCaptureDuration: 1, }); // audioListener = audioActivity(stream, (level) => { // setTalking(level > 0.1); // }); const peerConnections: PeerConnections = {}; const inCall: InCall = {}; audioElements.current = {}; const audioListeners: AudioListeners = {}; const connect = (lobbyCode: string, playerId: number) => { console.log("Connect called", lobbyCode, playerId); socket.emit('leave'); Object.keys(peerConnections).forEach(k => { disconnectPeer(k); }); setSocketPlayerIds({}); if (lobbyCode === 'MENU') return; function disconnectPeer(peer: string) { const connection = peerConnections[peer]; if (!connection) return; delete inCall[peer]; connection.destroy(); delete peerConnections[peer]; if (audioElements.current[peer]) { document.body.removeChild(audioElements.current[peer].element); audioElements.current[peer].pan.disconnect(); audioElements.current[peer].gain.disconnect(); delete audioElements.current[peer]; } if (audioListeners[peer]) { audioListeners[peer].destroy(); } } socket.emit('join', lobbyCode, playerId); }; setConnect({ connect }); function createPeerConnection(peer: string, initiator: boolean) { // console.log("Opening connection to ", peer, "Initiator: ", initiator); const connection = new Peer({ stream, initiator, config: { iceServers: [ { 'urls': 'stun:stun.l.google.com:19302' } ] } }); peerConnections[peer] = connection; connection.on('stream', (stream: MediaStream) => { let audio = document.createElement('audio'); document.body.appendChild(audio); audio.srcObject = stream; if (settings.speaker.toLowerCase() !== 'default') (audio as any).setSinkId(settings.speaker); const context = new AudioContext(); var source = context.createMediaStreamSource(stream); let gain = context.createGain(); let pan = context.createPanner(); // let compressor = context.createDynamicsCompressor(); pan.refDistance = 0.1; pan.panningModel = 'equalpower'; pan.distanceModel = 'linear'; pan.maxDistance = 2.66 * 2; pan.rolloffFactor = 1; source.connect(pan); pan.connect(gain); // Source -> pan -> gain -> VAD -> destination VAD(context, gain, context.destination, { onVoiceStart: () => setTalking(true), onVoiceStop: () => setTalking(false), // onUpdate: console.log, }); const setTalking = (talking: boolean) => { setSocketPlayerIds(socketPlayerIds => { setOtherTalking(old => ({ ...old, [socketPlayerIds[peer]]: talking && gain.gain.value > 0 })); return socketPlayerIds; }); }; // gain.connect(compressor); // compressor.connect(); // console.log(pan, audio); // pan.pan.setValueAtTime(-1, audioContext.currentTime); // source.connect(pan); // pan.connect(audioContext.destination); audioElements.current[peer] = { element: audio, gain, pan }; // audioListeners[peer] = audioActivity(stream, (level) => { // setSocketPlayerIds(socketPlayerIds => { // setOtherTalking(old => ({ // ...old, // [socketPlayerIds[peer]]: level // })); // return socketPlayerIds; // }); // }); }); connection.on('signal', (data) => { socket.emit('signal', { data, to: peer }); }); return connection; } socket.on('join', async (peer: string, playerId: number) => { createPeerConnection(peer, true); setSocketPlayerIds(old => ({ ...old, [peer]: playerId })); }); socket.on('signal', ({ data, from }: any) => { let connection: Peer.Instance; if (peerConnections[from]) connection = peerConnections[from]; else connection = createPeerConnection(from, false); connection.signal(data); }); socket.on('setId', (socketId: string, id: number) => { setSocketPlayerIds(old => ({ ...old, [socketId]: id })); }) socket.on('setIds', (ids: SocketIdMap) => { setSocketPlayerIds(ids); }); }, (error) => { console.error(error); remote.dialog.showErrorBox('Error', 'Couldn\'t connect to your microphone:\n' + error); }); return () => { connectionStuff.current.socket.close(); audioListener.destroy(); } }, []); const myPlayer = useMemo(() => { if (!gameState || !gameState.players) return undefined; else return gameState.players.find(p => p.isLocal); }, [gameState]); const otherPlayers = useMemo(() => { let otherPlayers: Player[]; if (!gameState || !gameState.players || gameState.lobbyCode === 'MENU' || !myPlayer) otherPlayers = []; else otherPlayers = gameState.players.filter(p => !p.isLocal); let playerSocketIds = {} as any; for (let k of Object.keys(socketPlayerIds)) { playerSocketIds[socketPlayerIds[k]] = k; } for (let player of otherPlayers) { const audio = audioElements.current[playerSocketIds[player.id]]; if (audio) { calculateVoiceAudio(gameState, settingsRef.current, myPlayer!, player, audio.gain, audio.pan); if (connectionStuff.current.deafened) { audio.gain.gain.value = 0; } } } return otherPlayers; }, [gameState]); useEffect(() => { if (connect?.connect && gameState.lobbyCode && myPlayer?.id !== undefined) { connect.connect(gameState.lobbyCode, myPlayer.id); } }, [connect?.connect, gameState?.lobbyCode]); useEffect(() => { if (connect?.connect && gameState.lobbyCode && myPlayer?.id !== undefined && gameState.gameState === GameState.LOBBY && (gameState.oldGameState === GameState.DISCUSSION || gameState.oldGameState === GameState.TASKS)) { connect.connect(gameState.lobbyCode, myPlayer.id); } }, [gameState.gameState]); useEffect(() => { if (connectionStuff.current.socket && myPlayer && myPlayer.id !== undefined) { connectionStuff.current.socket.emit('id', myPlayer.id); } }, [myPlayer?.id]); return (
{myPlayer && //
// //
}
{myPlayer && gameState?.gameState !== GameState.MENU && {myPlayer.name} } {gameState.lobbyCode && {displayedLobbyCode} }

{ otherPlayers.map(player => { let connected = Object.values(socketPlayerIds).includes(player.id); return ( ); }) }
) }