615 lines
26 KiB
HTML
615 lines
26 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
|
|
<head>
|
|
<title>Real-time Voice Chat</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css">
|
|
</head>
|
|
|
|
<body class="bg-gray-100">
|
|
<div class="container mx-auto px-4 py-8">
|
|
<h1 class="text-2xl font-bold mb-4 text-center">Real-time Voice Chat</h1>
|
|
|
|
<div class="flex justify-center items-center mb-4">
|
|
<!-- Username Input -->
|
|
<input type="text" id="username" class="border rounded p-2 mr-4" placeholder="Enter your username">
|
|
<div id="join-container" class="hidden">
|
|
<button id="btn-join" onclick="logInAndStoreSession()"
|
|
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Join Chat</button>
|
|
<select id="language-select">
|
|
<option value="auto">Auto</option>
|
|
<option value="en">English</option>
|
|
<option value="bg">Български</option>
|
|
<option value="fr">Français</option>
|
|
</select>
|
|
</div>
|
|
|
|
|
|
<!-- Clear Session Option -->
|
|
<button id="btn-disconnect" onclick="clearSession()"
|
|
class="hidden bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded">Clear Session</button>
|
|
</div>
|
|
|
|
<!-- Active Users List -->
|
|
<div id="active-users-container" class="hidden flex justify-center items-center mb-4">
|
|
<div class="w-1/3">
|
|
<h2 class="text-xl font-bold mb-2">Active Users</h2>
|
|
<select id="users-list" class="w-full bg-white p-4 rounded shadow" multiple size="10">
|
|
<!-- Dynamic list of users -->
|
|
</select>
|
|
<button onclick="startChat()"
|
|
class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded mt-4">Start
|
|
Chat</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="previous-chats-container" class="hidden w-2/3 mx-auto">
|
|
<h2 class="text-xl font-bold mb-2">Previous Chats</h2>
|
|
<div id="previous-chats" class="bg-white p-4 rounded shadow">
|
|
<!-- Previous chats content -->
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Chat Room -->
|
|
<div id="chat-room-container" class="hidden w-2/3 mx-auto">
|
|
<h2 class="text-xl font-bold mb-2">Chat Room</h2>
|
|
<div id="chat-room" class="bg-white p-4 rounded shadow mb-4">
|
|
<!-- Chat room content -->
|
|
<div>
|
|
<div id="chat-room-users" class="flex flex-wrap mb-4">
|
|
<!-- Participants list -->
|
|
</div>
|
|
</div>
|
|
<div class="mb-4">
|
|
<label class="flex items-center space-x-2">
|
|
<input type="checkbox" id="autosend" class="mr-2">
|
|
<span>Continuous</span>
|
|
</label>
|
|
</div>
|
|
<div class="mb-4">
|
|
<button id="record-button" disabled
|
|
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mr-4">Push to
|
|
Talk</button>
|
|
</div>
|
|
<div id="status-recording" class="flex justify-center items-center mb-4">
|
|
</div>
|
|
|
|
<div id="transcription" class="border rounded p-4 h-48 overflow-y-scroll mb-4">
|
|
<!-- Transcription content -->
|
|
</div>
|
|
<canvas id="canvas" class="w-full mb-4"></canvas>
|
|
<div class="flex justify-between items-center">
|
|
<button id="copyButton"
|
|
class="bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium py-2 px-4 rounded focus:outline-none"
|
|
onclick="copyToClipboard('transcription')">Copy</button>
|
|
<button id="clearButton"
|
|
class="bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium py-2 px-4 rounded focus:outline-none"
|
|
onclick="clearTranscription()">Clear</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Connection Status and Info -->
|
|
<div class="flex justify-center items-center mb-4">
|
|
<div id="connection-status" class="mr-4"></div>
|
|
</div>
|
|
<div class="flex justify-center items-center mb-4">
|
|
<div id="info"></div>
|
|
</div>
|
|
<div id="status-recording" class="flex justify-center items-center mb-4"> status</div>
|
|
</div>
|
|
|
|
<script type="module">
|
|
// import * as audio from './audio.js';
|
|
|
|
let socket;
|
|
let sessionId;
|
|
let username;
|
|
let users = [];
|
|
let selectedUsers = [];
|
|
let chats = [];
|
|
let recordButton;
|
|
let connectionStatus;
|
|
let statusRecording;
|
|
let connected = false;
|
|
|
|
document.getElementById('autosend').addEventListener('change', (event) => {
|
|
const autosend = event.target.checked;
|
|
fetch('/settings', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ autosend, sessionId }),
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'same-origin'
|
|
});
|
|
});
|
|
|
|
function connect() {
|
|
return new Promise((resolve, reject) => {
|
|
connectionStatus.innerHTML = "Connecting to WS...";
|
|
let wsurl = "ws://localhost:8080";
|
|
fetch("/wsurl")
|
|
.then((response) => response.text())
|
|
.then((data) => {
|
|
wsurl = data;
|
|
console.log("Got ws url: '" + wsurl + "'");
|
|
})
|
|
.then(() => {
|
|
socket = new WebSocket(wsurl);
|
|
// audio.setSocket(socket); // Set the socket in the audio module
|
|
socket.onopen = () => {
|
|
connectionStatus.innerHTML = "Connected to " + wsurl;
|
|
recordButton.disabled = false;
|
|
connected = true;
|
|
//if we stored a session id in a cookie, reconnect
|
|
const sessionId = getCookie("sessionId");
|
|
if (sessionId) {
|
|
socket.send(JSON.stringify({ type: 'reconnect', sessionId }));
|
|
}
|
|
else {
|
|
socket.send(JSON.stringify({ type: 'sessionId' }));
|
|
}
|
|
resolve(socket);
|
|
};
|
|
socket.onmessage = onmessage;
|
|
socket.onclose = () => {
|
|
connectionStatus.innerHTML = "Disconnected";
|
|
recordButton.disabled = true;
|
|
connected = false;
|
|
setTimeout(() => {
|
|
connect().then(resolve).catch(reject);
|
|
}, 5000);
|
|
};
|
|
})
|
|
.catch((error) => {
|
|
connectionStatus.innerHTML = "Error getting ws url: " + error;
|
|
reject(error);
|
|
});
|
|
});
|
|
};
|
|
|
|
function onmessage(event) {
|
|
try {
|
|
let json = JSON.parse(event.data);
|
|
switch (json.type) {
|
|
case "sessionId":
|
|
sessionId = json.sessionId;
|
|
//set the session id in the cookie
|
|
document.cookie = `sessionId=${sessionId}; path=/;`;
|
|
console.log("Got session id: " + sessionId);
|
|
break;
|
|
case "languageDetected":
|
|
statusRecording.innerHTML = "Detected language: " + json.languageDetected;
|
|
break;
|
|
case "text":
|
|
case "transcriptionResult":
|
|
transcription.innerHTML += "<br />" + json.text;
|
|
let latency = Date.now() - serverTime;
|
|
if (autosend.checked) {
|
|
// const arr = event.data.split(/[(\)]/);
|
|
// let queue = arr[1];
|
|
// let text = arr[2].trim();
|
|
// info.innerHTML = "latency: " + latency + "ms; server queue: " + queue + " requests";
|
|
//transcription.value += event.data + " ";
|
|
statusRecording.innerHTML = "Listening...";
|
|
statusRecording.style.color = "black";
|
|
} else {
|
|
//transcription.innerHTML = event.data;
|
|
}
|
|
break;
|
|
case 'audio':
|
|
const audioBuffer = Uint8Array.from(atob(json.audio), char => char.charCodeAt(0));
|
|
const audioBlob = new Blob([audioBuffer], { type: 'audio/mp3' });
|
|
const audioUrl = URL.createObjectURL(audioBlob);
|
|
const audio = new Audio(audioUrl);
|
|
audio.play();
|
|
break;
|
|
|
|
case "userList":
|
|
users = json.users;
|
|
updateUserList();
|
|
break;
|
|
case "chats":
|
|
chats = json.chats;
|
|
updateChatList();
|
|
break;
|
|
case "chat":
|
|
displayChatParticipants(json.chat.id, json.chat.participants);
|
|
break;
|
|
default:
|
|
console.log("Unknown message type:", json.type);
|
|
}
|
|
} catch (e) {
|
|
console.error("Failed to parse message", e);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
function logInAndStoreSession() {
|
|
username = document.getElementById('username').value;
|
|
if (username.trim() === "") {
|
|
alert("Please enter a username");
|
|
return;
|
|
}
|
|
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
|
connect().then(() => {
|
|
userJoin(sessionId, username, document.getElementById('language-select').value);
|
|
});
|
|
} else {
|
|
userJoin(sessionId, username, document.getElementById('language-select').value);
|
|
}
|
|
}
|
|
|
|
function userJoin(sessionId, username, language) {
|
|
socket.send(JSON.stringify({ type: 'join', username, language }));
|
|
document.cookie = `sessionId=${sessionId}; path=/;`;
|
|
document.cookie = `username=${username}; path=/;`;
|
|
|
|
showClearSessionOption();
|
|
}
|
|
|
|
|
|
function clearSession() {
|
|
document.cookie = "sessionId=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
location.reload();
|
|
}
|
|
|
|
function showClearSessionOption() {
|
|
const sessionId = getCookie("sessionId");
|
|
if (sessionId) {
|
|
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
|
connect().then((s) => {
|
|
s.send(JSON.stringify({ type: 'reconnect', sessionId }));
|
|
});
|
|
}
|
|
document.getElementById('btn-disconnect').classList.remove('hidden');
|
|
document.getElementById('join-container').classList.add('hidden');
|
|
|
|
document.getElementById('active-users-container').classList.remove('hidden');
|
|
document.getElementById('previous-chats-container').classList.remove('hidden');
|
|
|
|
} else {
|
|
document.getElementById('btn-disconnect').classList.add('hidden');
|
|
document.getElementById('join-container').classList.remove('hidden');
|
|
}
|
|
}
|
|
|
|
function getCookie(name) {
|
|
const value = `; ${document.cookie}`;
|
|
const parts = value.split(`; ${name}=`);
|
|
if (parts.length === 2) return parts.pop().split(';').shift();
|
|
}
|
|
|
|
window.onload = () => {
|
|
recordButton = document.getElementById("record-button");
|
|
setRecordButton(recordButton);
|
|
connectionStatus = document.getElementById("connection-status");
|
|
statusRecording = document.getElementById("status-recording");
|
|
|
|
showClearSessionOption();
|
|
connect().then(() => {
|
|
initializeVolumeChecker();
|
|
});
|
|
};
|
|
|
|
function copyToClipboard(id) {
|
|
var textarea = document.getElementById(id);
|
|
textarea.select();
|
|
document.execCommand('copy');
|
|
}
|
|
|
|
function clearTranscription() {
|
|
document.getElementById('transcription').innerText = '';
|
|
}
|
|
|
|
function updateUserList() {
|
|
const usersList = document.getElementById('users-list');
|
|
usersList.innerHTML = '';
|
|
users.forEach(user => {
|
|
const option = document.createElement('option');
|
|
option.value = user.sessionId;
|
|
option.innerText = "[" + user.language + "] " + user.username;
|
|
if (user.username === username) {
|
|
option.innerText += " (me)";
|
|
}
|
|
usersList.appendChild(option);
|
|
});
|
|
}
|
|
|
|
function startChat() {
|
|
const selectedOptions = Array.from(document.querySelectorAll('#users-list option:checked'));
|
|
selectedUsers = selectedOptions.map(option => option.value);
|
|
if (selectedUsers.length === 0) {
|
|
alert("Please select at least one user to start a chat.");
|
|
return;
|
|
}
|
|
selectedUsers.push(sessionId); // Add self to the selected users list for self-chat
|
|
socket.send(JSON.stringify({ type: 'startChat', users: selectedUsers }));
|
|
document.getElementById('chat-room-container').classList.remove('hidden');
|
|
}
|
|
|
|
|
|
|
|
function fetchPreviousChats(username) {
|
|
fetch(`/chats?username=${username}`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
chats = data.chats;
|
|
updateChatList();
|
|
});
|
|
}
|
|
|
|
function updateChatList() {
|
|
const previousChats = document.getElementById('previous-chats');
|
|
previousChats.innerHTML = '';
|
|
chats.forEach(chat => {
|
|
const chatDiv = document.createElement('div');
|
|
chatDiv.classList.add('border', 'rounded', 'p-2', 'mb-2', 'cursor-pointer');
|
|
chatDiv.setAttribute('data-chat-id', chat.id); // Store chat ID in data attribute
|
|
|
|
const participants = chat.participants.join(', ');
|
|
const status = chat.participants.map(participant => {
|
|
const user = users.find(u => u.username === participant);
|
|
return user ? `${participant} (online)` : `${participant} (offline)`;
|
|
}).join(', ');
|
|
|
|
chatDiv.innerHTML = `${status}`;
|
|
|
|
chatDiv.addEventListener('click', () => {
|
|
// Remove highlight from all chat divs
|
|
document.querySelectorAll('#previous-chats > div').forEach(div => {
|
|
div.classList.remove('bg-blue-100');
|
|
});
|
|
// Highlight selected chat div
|
|
chatDiv.classList.add('bg-blue-100');
|
|
selectChatRoom(chat.id);
|
|
});
|
|
|
|
previousChats.appendChild(chatDiv);
|
|
});
|
|
}
|
|
|
|
function selectChatRoom(chatId) {
|
|
const chat = chats.find(c => c.id === chatId);
|
|
if (!chat) return;
|
|
|
|
const chatRoomUsers = document.getElementById('chat-room-users');
|
|
chatRoomUsers.innerHTML = ''; // Clear existing content
|
|
|
|
socket.send(JSON.stringify({ type: 'enterChat', chatId }));
|
|
document.getElementById('chat-room-container').classList.remove('hidden');
|
|
// displayChatParticipants(chatId, chat.participants);
|
|
}
|
|
|
|
function displayChatParticipants(chatId, participants) {
|
|
const chatRoomUsers = document.getElementById('chat-room-users');
|
|
let participantsHtml = '<div class="flex flex-wrap mb-4">';
|
|
participants.forEach(participantId => {
|
|
const user = users.find(u => u.sessionId === participantId);
|
|
const status = user ? "online" : "offline";
|
|
const username = user ? user.username : "Unknown User";
|
|
participantsHtml += `<span class="inline-flex items-center px-3 py-0.5 rounded-full text-sm font-medium bg-${status === 'online' ? 'green' : 'gray'}-100 text-${status === 'online' ? 'green' : 'gray'}-800 mr-2 mb-2">
|
|
${username} (${status})
|
|
</span>`;
|
|
});
|
|
participantsHtml += '</div>';
|
|
chatRoomUsers.innerHTML = participantsHtml;
|
|
}
|
|
|
|
// REGION AUDIO RECORDING
|
|
let selectedDeviceId = "default";
|
|
export let serverTime;
|
|
// export let recordButton;
|
|
// export let socket;
|
|
// let connectionStatus;
|
|
// let statusRecording;
|
|
let audioRecorder;
|
|
let audioStream;
|
|
let recording = false;
|
|
let audioContext;
|
|
let volumeChecker;
|
|
let lastVolumes = new Array(5);
|
|
let averageVolume;
|
|
let silenceCount = 0;
|
|
let isSpeaking = false;
|
|
let soundDetected = false;
|
|
let speakingCount = 0;
|
|
let analyser = null;
|
|
|
|
let SILENCE_DELAY_MS = 50;
|
|
let preDetect_IncludedAudio = 400; //ms
|
|
let soundCount_Threshold = 10;
|
|
let silenceCount_Threshold = 10;
|
|
|
|
const volumeHistory = [];
|
|
|
|
function setSocket(newSocket) {
|
|
socket = newSocket;
|
|
}
|
|
export function setRecordButton(newRecordButton) {
|
|
recordButton = newRecordButton;
|
|
recordButton.addEventListener("click", toggleListening);
|
|
}
|
|
|
|
export function InitAudioAnalyser(stream) {
|
|
audioContext = new AudioContext();
|
|
const source = audioContext.createMediaStreamSource(stream);
|
|
analyser = audioContext.createAnalyser();
|
|
analyser.fftSize = 2048;
|
|
analyser.smoothingTimeConstant = 0.8;
|
|
source.connect(analyser);
|
|
}
|
|
|
|
export function startListening() {
|
|
recording = true;
|
|
navigator.mediaDevices.getUserMedia({ audio: { sampleRate: 16000 } })
|
|
.then((stream) => {
|
|
audioStream = stream;
|
|
|
|
const audioContext = new AudioContext();
|
|
const sourceNode = audioContext.createMediaStreamSource(audioStream);
|
|
const audioSampleRate = sourceNode.context.sampleRate;
|
|
|
|
info.innerHTML = "Sample rate: " + audioSampleRate + " Hz";
|
|
var preBuffer = [];
|
|
|
|
const channelSplitter = audioContext.createChannelSplitter(2);
|
|
const channelMerger = audioContext.createChannelMerger(1);
|
|
sourceNode.connect(channelSplitter);
|
|
channelSplitter.connect(channelMerger, 0, 0);
|
|
const outputNode = channelMerger;
|
|
|
|
const mediaStreamDestination = audioContext.createMediaStreamDestination();
|
|
outputNode.connect(mediaStreamDestination);
|
|
const singleChannelStream = mediaStreamDestination.stream;
|
|
|
|
audioRecorder = new MediaRecorder(singleChannelStream);
|
|
audioRecorder.start();
|
|
audioRecorder.addEventListener("dataavailable", (event) => {
|
|
if (!soundDetected && autosend.checked) {
|
|
preBuffer = [];
|
|
preBuffer.push(event.data);
|
|
return;
|
|
}
|
|
if (event.data.size > 0) {
|
|
let data = event.data;
|
|
if (preBuffer.length > 0) {
|
|
sendAudioToServerPost(preBuffer);
|
|
}
|
|
sendAudioToServer(data);
|
|
soundDetected = false;
|
|
}
|
|
});
|
|
|
|
InitAudioAnalyser(stream);
|
|
});
|
|
|
|
recordButton.innerHTML = "Stop Talking";
|
|
recordButton.classList.toggle('bg-red-500');
|
|
recordButton.classList.toggle('bg-blue-500');
|
|
recordButton.classList.toggle('hover:bg-blue-700');
|
|
}
|
|
|
|
export function stopListening() {
|
|
recording = false;
|
|
audioRecorder.stop();
|
|
recordButton.innerHTML = "Push to Talk";
|
|
recordButton.classList.toggle('bg-blue-500');
|
|
recordButton.classList.toggle('bg-red-500');
|
|
recordButton.classList.toggle('hover:bg-blue-700');
|
|
clearInterval(volumeChecker);
|
|
if (audioStream) {
|
|
audioStream.getTracks().forEach(track => track.stop());
|
|
audioStream = null;
|
|
}
|
|
}
|
|
|
|
export function sendAudioToServerPost(data) {
|
|
const blob = new Blob(data, { type: "audio/ogg; codecs=opus" });
|
|
var formData = new FormData();
|
|
formData.append('file', data);
|
|
fetch('/upload', {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
}
|
|
|
|
export function sendAudioToServerJson(data) {
|
|
if (socket && socket.readyState === WebSocket.OPEN) {
|
|
|
|
const binaryData = Buffer.from(base64AudioData, 'base64');
|
|
socket.send(JSON.stringify({ type: 'audio', audiobase64: binaryData }));
|
|
serverTime = Date.now();
|
|
if (!autosend.checked) {
|
|
transcription.placeholder = "Processing audio...";
|
|
}
|
|
}
|
|
}
|
|
export function sendAudioToServer(data) {
|
|
if (socket && socket.readyState === WebSocket.OPEN) {
|
|
socket.send(data);
|
|
serverTime = Date.now();
|
|
if (!autosend.checked) {
|
|
transcription.placeholder = "Processing audio...";
|
|
}
|
|
}
|
|
}
|
|
|
|
export function toggleListening() {
|
|
if (socket.readyState === WebSocket.OPEN) {
|
|
if (recording) {
|
|
stopListening();
|
|
} else {
|
|
startListening();
|
|
}
|
|
}
|
|
}
|
|
|
|
export function initializeVolumeChecker() {
|
|
volumeChecker = setInterval(() => {
|
|
if (!audioContext) {
|
|
console.log("No audio context");
|
|
return;
|
|
}
|
|
const frequencyData = new Uint8Array(analyser.frequencyBinCount);
|
|
analyser.getByteFrequencyData(frequencyData);
|
|
|
|
let totalVolume = 0;
|
|
for (let i = 0; i < frequencyData.length; i++) {
|
|
totalVolume += frequencyData[i];
|
|
}
|
|
averageVolume = totalVolume / frequencyData.length;
|
|
|
|
volumeHistory.push(averageVolume);
|
|
if (volumeHistory.length > 100) {
|
|
volumeHistory.shift();
|
|
}
|
|
|
|
const threshold = volumeHistory.reduce((acc, curr) => acc + curr) / volumeHistory.length + 5;
|
|
const isSilent = averageVolume < threshold;
|
|
|
|
if (averageVolume > threshold) {
|
|
if (autosend.checked && speakingCount == 0 && audioRecorder) {
|
|
soundDetected = false;
|
|
audioRecorder.stop();
|
|
audioRecorder.start();
|
|
}
|
|
speakingCount++;
|
|
if (speakingCount > soundCount_Threshold) {
|
|
statusRecording.innerHTML = "Listening...";
|
|
statusRecording.style.color = "green";
|
|
isSpeaking = true;
|
|
}
|
|
} else if (averageVolume - 5 < threshold) {
|
|
speakingCount = 0;
|
|
if (isSpeaking) {
|
|
silenceCount++;
|
|
if (silenceCount > silenceCount_Threshold) {
|
|
if (autosend.checked) {
|
|
soundDetected = true;
|
|
audioRecorder.stop();
|
|
audioRecorder.start();
|
|
}
|
|
isSpeaking = false;
|
|
statusRecording.innerHTML = "Silence detected...";
|
|
statusRecording.style.color = "orange";
|
|
}
|
|
}
|
|
}
|
|
}, SILENCE_DELAY_MS);
|
|
}
|
|
|
|
// Expose functions to global scope
|
|
window.logInAndStoreSession = logInAndStoreSession;
|
|
window.clearSession = clearSession;
|
|
window.copyToClipboard = copyToClipboard;
|
|
window.clearTranscription = clearTranscription;
|
|
window.startChat = startChat;
|
|
</script>
|
|
|
|
<script src="https://cdn.webrtc-experiment.com/MediaStreamRecorder.js"></script>
|
|
</body>
|
|
|
|
</html> |