Luigi commited on
Commit
5f6cff2
·
1 Parent(s): 09eda27

fix websocket config timing issue

Browse files
Files changed (1) hide show
  1. app/static/index.html +26 -25
app/static/index.html CHANGED
@@ -145,7 +145,7 @@
145
  };
146
 
147
  let orig_sample_rate;
148
- const ws = new WebSocket("wss://" + location.host + "/ws");
149
 
150
  const vol = document.getElementById("vol");
151
  const partial = document.getElementById("partial");
@@ -157,7 +157,7 @@
157
  function updateModelInfo() {
158
  const meta = MODEL_METADATA[modelSelect.value];
159
  if (Array.isArray(meta.language)) {
160
- modelLangs.textContent = meta.language.join(', ');
161
  } else {
162
  modelLangs.textContent = meta.language;
163
  }
@@ -165,21 +165,36 @@
165
  }
166
 
167
  function sendConfig() {
168
- ws.send(JSON.stringify({
169
- type: "config",
170
- sampleRate: orig_sample_rate,
171
- model: modelSelect.value,
172
- precision: precisionSelect.value
173
- }));
 
 
 
 
174
  }
175
 
176
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
177
  const context = new AudioContext();
178
  orig_sample_rate = context.sampleRate;
 
179
 
180
- ws.onopen = () => {
181
- updateModelInfo();
182
- sendConfig();
 
 
 
 
 
 
 
 
 
 
183
  };
184
 
185
  modelSelect.addEventListener("change", () => {
@@ -188,28 +203,14 @@
188
  });
189
  precisionSelect.addEventListener("change", sendConfig);
190
 
191
- ws.onerror = err => console.error("WebSocket error:", err);
192
- ws.onclose = () => console.log("WebSocket closed");
193
-
194
  const source = context.createMediaStreamSource(stream);
195
  const processor = context.createScriptProcessor(4096, 1, 1);
196
  source.connect(processor);
197
  processor.connect(context.destination);
198
-
199
  processor.onaudioprocess = e => {
200
  const input = e.inputBuffer.getChannelData(0);
201
  ws.send(new Float32Array(input).buffer);
202
  };
203
-
204
- ws.onmessage = e => {
205
- const msg = JSON.parse(e.data);
206
- if (msg.volume !== undefined) {
207
- vol.value = Math.min(msg.volume * 20.0, 1.0);
208
- }
209
- if (msg.partial) {
210
- partial.textContent = msg.partial;
211
- }
212
- };
213
  });
214
  </script>
215
  </body>
 
145
  };
146
 
147
  let orig_sample_rate;
148
+ let ws;
149
 
150
  const vol = document.getElementById("vol");
151
  const partial = document.getElementById("partial");
 
157
  function updateModelInfo() {
158
  const meta = MODEL_METADATA[modelSelect.value];
159
  if (Array.isArray(meta.language)) {
160
+ modelLangs.textContent = meta.language.join(", ");
161
  } else {
162
  modelLangs.textContent = meta.language;
163
  }
 
165
  }
166
 
167
  function sendConfig() {
168
+ if (ws && ws.readyState === WebSocket.OPEN) {
169
+ ws.send(JSON.stringify({
170
+ type: "config",
171
+ sampleRate: orig_sample_rate,
172
+ model: modelSelect.value,
173
+ precision: precisionSelect.value
174
+ }));
175
+ } else {
176
+ console.warn("WebSocket not open yet. Cannot send config.");
177
+ }
178
  }
179
 
180
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
181
  const context = new AudioContext();
182
  orig_sample_rate = context.sampleRate;
183
+ updateModelInfo();
184
 
185
+ // Now that we know the sample rate, open the WS
186
+ ws = new WebSocket(`wss://${location.host}/ws`);
187
+ ws.onopen = () => sendConfig();
188
+ ws.onerror = err => console.error("WebSocket error:", err);
189
+ ws.onclose = () => console.log("WebSocket closed");
190
+ ws.onmessage = e => {
191
+ const msg = JSON.parse(e.data);
192
+ if (msg.volume !== undefined) {
193
+ vol.value = Math.min(msg.volume * 20.0, 1.0);
194
+ }
195
+ if (msg.partial) {
196
+ partial.textContent = msg.partial;
197
+ }
198
  };
199
 
200
  modelSelect.addEventListener("change", () => {
 
203
  });
204
  precisionSelect.addEventListener("change", sendConfig);
205
 
 
 
 
206
  const source = context.createMediaStreamSource(stream);
207
  const processor = context.createScriptProcessor(4096, 1, 1);
208
  source.connect(processor);
209
  processor.connect(context.destination);
 
210
  processor.onaudioprocess = e => {
211
  const input = e.inputBuffer.getChannelData(0);
212
  ws.send(new Float32Array(input).buffer);
213
  };
 
 
 
 
 
 
 
 
 
 
214
  });
215
  </script>
216
  </body>