From e32f597a42a3085830f2f9ff2254a28d4b15859e Mon Sep 17 00:00:00 2001 From: makaveli10 Date: Mon, 10 Jul 2023 22:14:04 +0530 Subject: [PATCH] wait for server to be ready with models --- Audio-Transcription-Firefox/content.js | 8 ++++++-- Audio-Transcription/options.js | 12 ++++++++++-- client.py | 20 ++++++++++++++++++-- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Audio-Transcription-Firefox/content.js b/Audio-Transcription-Firefox/content.js index f8b06da..51ef0cd 100644 --- a/Audio-Transcription-Firefox/content.js +++ b/Audio-Transcription-Firefox/content.js @@ -45,8 +45,12 @@ function startRecording() { socket.send("handshake"); }; + let isServerReady = false; socket.onmessage = (event) => { - // console.log(event.data); + if (!isServerReady){ + isServerReady = true; + return; + } const data = event.data; browser.runtime.sendMessage({ action: "transcript", data }) .catch(function(error) { @@ -64,7 +68,7 @@ function startRecording() { recorder = audioContext.createScriptProcessor(4096, 1, 1); recorder.onaudioprocess = async (event) => { - if (!audioContext || !isCapturing) return; + if (!audioContext || !isCapturing || !isServerReady) return; const inputData = event.inputBuffer.getChannelData(0); const audioData16kHz = resampleTo16kHZ(inputData, audioContext.sampleRate); diff --git a/Audio-Transcription/options.js b/Audio-Transcription/options.js index 4134394..8f2092d 100644 --- a/Audio-Transcription/options.js +++ b/Audio-Transcription/options.js @@ -81,25 +81,32 @@ async function startRecord(option) { }; const socket = new WebSocket("ws://localhost:9090/"); + let isServerReady = false; socket.onopen = function(e) { socket.send("handshake"); }; socket.onmessage = async (event) => { - // console.log(event.data); + console.log(event.data); + if (isServerReady === false){ + isServerReady = true; + return; + } + res = await sendMessageToTab(option.currentTabId, { type: "transcript", data: event.data, }); }; + const audioDataCache = []; const context = new AudioContext(); const mediaStream = context.createMediaStreamSource(stream); const recorder = context.createScriptProcessor(4096, 1, 1); recorder.onaudioprocess = async (event) => { - if (!context) return; + if (!context || !isServerReady) return; const inputData = event.inputBuffer.getChannelData(0); const audioData16kHz = resampleTo16kHZ(inputData, context.sampleRate); @@ -114,6 +121,7 @@ async function startRecord(option) { mediaStream.connect(recorder); recorder.connect(context.destination); mediaStream.connect(context.destination); + // } } else { window.close(); } diff --git a/client.py b/client.py index 4dbdeab..62dfd83 100644 --- a/client.py +++ b/client.py @@ -17,11 +17,15 @@ FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 RECORD_SECONDS = 60000 - +START_RECORDING = False def on_message(ws, message): + global START_RECORDING message = json.loads(message) + if message == "SERVER_READY": + START_RECORDING = True + return text = [] if len(message): for seg in message: @@ -35,7 +39,10 @@ def on_message(ws, message): wrapper = textwrap.TextWrapper(width=60) word_list = wrapper.wrap(text="".join(text)) # Print each line. - os.system('clear') + if os.name=='nt': + os.system('cls') + else: + os.system('clear') for element in word_list: print(element) @@ -222,6 +229,15 @@ if __name__=="__main__": opt = parser.parse_args() c = Client(host=opt.host, port=opt.port) + # while loop to wait for server to be ready + print("Waiting for server ready ...") + while not START_RECORDING: + pass + if os.name=='nt': + os.system('cls') + else: + os.system('clear') + if opt.audio is not None: resampled_file = resample(opt.audio) c.play_file(resampled_file)