|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>WebRTC TTS Demo</title> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | + <style> |
| 7 | + body { |
| 8 | + font-family: Arial, sans-serif; |
| 9 | + max-width: 800px; |
| 10 | + margin: 0 auto; |
| 11 | + padding: 20px; |
| 12 | + } |
| 13 | + .form-group { |
| 14 | + margin-bottom: 15px; |
| 15 | + } |
| 16 | + textarea { |
| 17 | + width: 100%; |
| 18 | + padding: 8px; |
| 19 | + } |
| 20 | + button { |
| 21 | + padding: 10px 20px; |
| 22 | + background-color: #4CAF50; |
| 23 | + color: white; |
| 24 | + border: none; |
| 25 | + border-radius: 4px; |
| 26 | + cursor: pointer; |
| 27 | + } |
| 28 | + button:hover { |
| 29 | + background-color: #45a049; |
| 30 | + } |
| 31 | + button.connected { |
| 32 | + background-color: #dc3545; |
| 33 | + } |
| 34 | + button.connected:hover { |
| 35 | + background-color: #c82333; |
| 36 | + } |
| 37 | + </style> |
| 38 | +</head> |
| 39 | +<body> |
| 40 | + <h1>Text to Speech with WebRTC</h1> |
| 41 | + <div id="connectionStatus" style="margin-bottom: 20px; padding: 10px; background-color: #f0f0f0; border-radius: 4px;"> |
| 42 | + <div>Connection State: <span id="connectionState">new</span></div> |
| 43 | + <div>ICE Connection: <span id="iceConnectionState">new</span></div> |
| 44 | + <div>Signaling State: <span id="signalingState">new</span></div> |
| 45 | + </div> |
| 46 | + <div class="form-group"> |
| 47 | + <button id="connectButton" onclick="toggleConnection()">Connect</button> |
| 48 | + </div> |
| 49 | + <div class="form-group"> |
| 50 | + <textarea id="textInput" rows="4" placeholder="Enter text to convert to speech"></textarea> |
| 51 | + </div> |
| 52 | + <div class="form-group"> |
| 53 | + <button id="convertButton" onclick="submitText()" disabled>Convert to Speech</button> |
| 54 | + </div> |
| 55 | + |
| 56 | + <script> |
| 57 | + let pc; |
| 58 | + let isConnected = false; |
| 59 | + |
| 60 | + async function toggleConnection() { |
| 61 | + const connectButton = document.getElementById('connectButton'); |
| 62 | + if (!isConnected) { |
| 63 | + connectButton.disabled = true; |
| 64 | + await initWebRTC(); |
| 65 | + } else { |
| 66 | + if (pc) { |
| 67 | + await pc.close(); |
| 68 | + pc = null; |
| 69 | + } |
| 70 | + document.getElementById('convertButton').disabled = true; |
| 71 | + document.getElementById('connectionState').textContent = 'new'; |
| 72 | + document.getElementById('iceConnectionState').textContent = 'new'; |
| 73 | + document.getElementById('signalingState').textContent = 'new'; |
| 74 | + connectButton.textContent = 'Connect'; |
| 75 | + connectButton.classList.remove('connected'); |
| 76 | + isConnected = false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + async function initWebRTC() { |
| 81 | + pc = new RTCPeerConnection({ |
| 82 | + iceServers: [{ |
| 83 | + urls: 'stun:stun.l.google.com:19302' |
| 84 | + }] |
| 85 | + }); |
| 86 | + |
| 87 | + // Add connection state monitoring |
| 88 | + pc.onconnectionstatechange = () => { |
| 89 | + const state = pc.connectionState; |
| 90 | + const connectButton = document.getElementById('connectButton'); |
| 91 | + document.getElementById('connectionState').textContent = state; |
| 92 | + if (state === 'connected') { |
| 93 | + document.getElementById('convertButton').disabled = false; |
| 94 | + connectButton.disabled = false; |
| 95 | + connectButton.textContent = 'Disconnect'; |
| 96 | + connectButton.classList.add('connected'); |
| 97 | + isConnected = true; |
| 98 | + } else { |
| 99 | + document.getElementById('convertButton').disabled = true; |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + pc.oniceconnectionstatechange = () => { |
| 104 | + document.getElementById('iceConnectionState').textContent = pc.iceConnectionState; |
| 105 | + }; |
| 106 | + |
| 107 | + pc.onsignalingstatechange = () => { |
| 108 | + document.getElementById('signalingState').textContent = pc.signalingState; |
| 109 | + }; |
| 110 | + |
| 111 | + pc.ontrack = function(event) { |
| 112 | + const audio = new Audio(); |
| 113 | + audio.srcObject = event.streams[0]; |
| 114 | + audio.play(); |
| 115 | + }; |
| 116 | + |
| 117 | + // Create promise to wait for ICE gathering |
| 118 | + const iceCandidatesComplete = new Promise((resolve) => { |
| 119 | + pc.onicegatheringstatechange = () => { |
| 120 | + if (pc.iceGatheringState === 'complete') { |
| 121 | + resolve(); |
| 122 | + } |
| 123 | + }; |
| 124 | + }); |
| 125 | + |
| 126 | + pc.addTransceiver('audio', { direction: 'recvonly' }); |
| 127 | + |
| 128 | + const offer = await pc.createOffer({ |
| 129 | + offerToReceiveAudio: true |
| 130 | + }); |
| 131 | + await pc.setLocalDescription(offer); |
| 132 | + |
| 133 | + // Wait for ICE gathering to complete |
| 134 | + await iceCandidatesComplete; |
| 135 | + |
| 136 | + const response = await fetch('/webrtc', { |
| 137 | + method: 'POST', |
| 138 | + headers: { |
| 139 | + 'Content-Type': 'application/json' |
| 140 | + }, |
| 141 | + body: JSON.stringify({ |
| 142 | + sdp: JSON.stringify(pc.localDescription) |
| 143 | + }) |
| 144 | + }); |
| 145 | + |
| 146 | + const answer = await response.json(); |
| 147 | + await pc.setRemoteDescription(new RTCSessionDescription(answer)); |
| 148 | + } |
| 149 | + |
| 150 | + async function submitText() { |
| 151 | + const text = document.getElementById('textInput').value; |
| 152 | + if (!text) return; |
| 153 | + |
| 154 | + const response = await fetch('/tts', { |
| 155 | + method: 'POST', |
| 156 | + headers: { |
| 157 | + 'Content-Type': 'application/json' |
| 158 | + }, |
| 159 | + body: JSON.stringify({ |
| 160 | + text: text |
| 161 | + }) |
| 162 | + }); |
| 163 | + |
| 164 | + const audioBlob = await response.blob(); |
| 165 | + const audio = new Audio(URL.createObjectURL(audioBlob)); |
| 166 | + audio.play(); |
| 167 | + } |
| 168 | + |
| 169 | + // Remove the automatic initWebRTC() call at the end |
| 170 | + </script> |
| 171 | +</body> |
| 172 | +</html> |
0 commit comments