wait for server to be ready with models
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user