File size: 7,252 Bytes
56c74ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | <!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<title>Gemini Real-time TTS</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background-color: #f0f2f5; margin: 0; padding: 20px; display: flex; justify-content: center; align-items: center; min-height: 100vh; }
.container { background: white; padding: 30px; border-radius: 10px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); width: 100%; max-width: 600px; }
h1 { text-align: center; color: #333; }
textarea { width: 100%; padding: 10px; font-size: 16px; border-radius: 5px; border: 1px solid #ccc; margin-bottom: 15px; box-sizing: border-box; resize: vertical; }
.button-container { display: flex; gap: 10px; }
button { flex-grow: 1; padding: 12px; font-size: 18px; border: none; border-radius: 5px; color: white; cursor: pointer; transition: background-color 0.2s; }
#speak-button { background-color: #007bff; }
#stop-button { background-color: #dc3545; }
button:disabled { background-color: #a0cfff; cursor: not-allowed; }
#stop-button:disabled { background-color: #f5c6cb; }
#status { margin-top: 15px; text-align: center; color: #555; font-style: italic; }
#audio-player-container { margin-top: 20px; }
audio { width: 100%; }
</style>
</head>
<body>
<div class="container">
<h1>🎙️ پخش صدای آنی Gemini</h1>
<textarea id="text-input" rows="4" placeholder="متن خود را اینجا وارد کنید..."></textarea>
<div class="button-container">
<button id="speak-button">صحبت کن</button>
<button id="stop-button" disabled>توقف</button>
</div>
<div id="status">در حال اتصال به سرور...</div>
<div id="audio-player-container" style="display: none;">
<p>پخش مجدد:</p>
<audio id="audio-player" controls></audio>
</div>
</div>
<script>
const textInput = document.getElementById('text-input');
const speakButton = document.getElementById('speak-button');
const stopButton = document.getElementById('stop-button');
const statusDiv = document.getElementById('status');
const audioPlayerContainer = document.getElementById('audio-player-container');
const audioPlayer = document.getElementById('audio-player');
let audioContext;
let audioQueue = [];
let sourceNodes = [];
let isPlaying = false;
let isStopped = false;
let nextStartTime = 0;
let socket;
function initializeAudio() {
if (!audioContext || audioContext.state === 'suspended') {
audioContext = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 24000 });
}
nextStartTime = audioContext.currentTime;
}
function getWebSocketURL() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
return `${protocol}//${window.location.host}/ws`;
}
function connectWebSocket() {
const wsURL = getWebSocketURL();
socket = new WebSocket(wsURL);
socket.onopen = () => {
statusDiv.textContent = "آماده دریافت متن";
speakButton.disabled = false;
};
socket.onmessage = async (event) => {
if (typeof event.data === 'string') {
const message = JSON.parse(event.data);
if (message.event === "STREAM_ENDED") {
handleStreamEnd(message.url);
} else if (message.event === "ERROR") {
statusDiv.textContent = `خطا: ${message.message}`;
resetUI();
}
} else {
if (isStopped) return;
const arrayBuffer = await event.data.arrayBuffer();
const pcmData = new Int16Array(arrayBuffer);
audioQueue.push(pcmData);
if (!isPlaying) {
playFromQueue();
}
}
};
socket.onclose = () => {
statusDiv.textContent = "اتصال قطع شد. تلاش مجدد...";
resetUI(true);
setTimeout(connectWebSocket, 3000);
};
}
async function playFromQueue() {
if (audioQueue.length === 0 || isStopped) {
isPlaying = false;
return;
}
isPlaying = true;
while (audioQueue.length > 0) {
const pcmData = audioQueue.shift();
const float32Data = new Float32Array(pcmData.length);
for (let i = 0; i < pcmData.length; i++) {
float32Data[i] = pcmData[i] / 32768.0;
}
const audioBuffer = audioContext.createBuffer(1, float32Data.length, audioContext.sampleRate);
audioBuffer.getChannelData(0).set(float32Data);
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
const currentTime = audioContext.currentTime;
if (nextStartTime < currentTime) {
nextStartTime = currentTime;
}
source.start(nextStartTime);
sourceNodes.push(source);
nextStartTime += audioBuffer.duration;
}
isPlaying = false;
}
function handleStreamEnd(audioUrl) {
if (audioUrl) {
audioPlayer.src = audioUrl;
audioPlayerContainer.style.display = 'block';
}
const checkPlaybackEnd = setInterval(() => {
if (audioQueue.length === 0 && audioContext.currentTime > nextStartTime) {
if(!isStopped) {
statusDiv.textContent = "پخش تمام شد.";
resetUI();
}
clearInterval(checkPlaybackEnd);
}
}, 100);
}
function resetUI(isConnectionError = false) {
speakButton.disabled = isConnectionError;
stopButton.disabled = true;
isPlaying = false;
}
speakButton.addEventListener('click', () => {
const text = textInput.value.trim();
if (!text || !socket || socket.readyState !== WebSocket.OPEN) return;
initializeAudio();
isStopped = false;
audioQueue = [];
sourceNodes.forEach(source => source.stop());
sourceNodes = [];
socket.send(text);
speakButton.disabled = true;
stopButton.disabled = false;
statusDiv.textContent = "در حال دریافت و پخش صدا...";
audioPlayerContainer.style.display = 'none';
audioPlayer.src = "";
});
stopButton.addEventListener('click', () => {
isStopped = true;
audioQueue = [];
sourceNodes.forEach(source => source.stop());
sourceNodes = [];
statusDiv.textContent = "پخش متوقف شد.";
resetUI();
});
window.addEventListener('load', connectWebSocket);
</script>
</body>
</html> |