Skip to content

Commit

Permalink
Update the execution order of the audio configuration sending logic
Browse files Browse the repository at this point in the history
We shall push user config to server before handling any audio data.
  • Loading branch information
jinmiaoluo committed Jun 2, 2024
1 parent b7e3ea7 commit 723e46f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 6 additions & 5 deletions client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ let websocket;
let context;
let processor;
let globalStream;
let language;
let isRecording = false;

const websocketAddress = document.querySelector('#websocketAddress');
Expand Down Expand Up @@ -48,8 +47,6 @@ function resetWebsocketHandler() {
}

function connectWebsocketHandler() {
language = selectedLanguage.value !== 'multilingual' ? selectedLanguage.value : null;

if (!websocketAddress.value) {
console.log("WebSocket address is required.");
return;
Expand Down Expand Up @@ -126,15 +123,19 @@ function startRecordingHandler() {
isRecording = true;

context = new AudioContext();

let onSuccess = async (stream) => {
// Push user config to server
let language = selectedLanguage.value !== 'multilingual' ? selectedLanguage.value : null;
sendAudioConfig(language);

globalStream = stream;
const input = context.createMediaStreamSource(stream);
const recordingNode = await setupRecordingWorkletNode();
recordingNode.port.onmessage = (event) => {
processAudio(event.data);
};
input.connect(recordingNode);
sendAudioConfig();
};
let onError = (error) => {
console.error(error);
Expand Down Expand Up @@ -182,7 +183,7 @@ function stopRecordingHandler() {
stopButton.disabled = true;
}

function sendAudioConfig() {
function sendAudioConfig(language) {
let processingArgs = {};

if (selectedStrategy.value === 'silence_at_end_of_chunk') {
Expand Down
2 changes: 2 additions & 0 deletions src/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import ssl
import uuid

Expand Down Expand Up @@ -57,6 +58,7 @@ async def handle_audio(self, client, websocket):
config = json.loads(message)
if config.get("type") == "config":
client.update_config(config["data"])
logging.debug(f"Updated config: {client.config}")
continue
else:
print(f"Unexpected message type from {client.client_id}")
Expand Down

0 comments on commit 723e46f

Please sign in to comment.