From 9c8503701013972d4a6d78547414bbc4fb475d28 Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Fri, 5 Jul 2024 15:02:09 +0700 Subject: [PATCH 1/2] send eof msg before close ws connection --- client-samples/asterisk/eagi.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client-samples/asterisk/eagi.py b/client-samples/asterisk/eagi.py index 8b30695..44c476e 100755 --- a/client-samples/asterisk/eagi.py +++ b/client-samples/asterisk/eagi.py @@ -42,6 +42,8 @@ def startAGI(): except Exception as err: agi.verbose(''.join(traceback.format_exception(type(err), err, err.__traceback__)).replace('\n', ' ')) finally: + ws.send('{"eof" : 1}') + ws.recv() ws.close() startAGI() From fb70a7f2e991a532c368842ca6ec6ddbc4b03223 Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Fri, 5 Jul 2024 15:21:56 +0700 Subject: [PATCH 2/2] extract process_result function and process the last response from vosk server --- client-samples/asterisk/eagi.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/client-samples/asterisk/eagi.py b/client-samples/asterisk/eagi.py index 44c476e..03e058f 100755 --- a/client-samples/asterisk/eagi.py +++ b/client-samples/asterisk/eagi.py @@ -10,17 +10,20 @@ CONTENT_TYPE = 'audio/l16; rate=8000; channels=1' ACCEPT = 'audio/pcm' +def process_result(agi, text): + os.system("espeak -w /tmp/response22.wav \"" + text.encode('utf-8') + "\"") + os.system("sox /tmp/response22.wav -r 8000 /tmp/response.wav") + agi.stream_file("/tmp/response") + os.remove("/tmp/response.wav") + def process_chunk(agi, ws, buf): agi.verbose("Processing chunk") ws.send_binary(buf) res = json.loads(ws.recv()) agi.verbose("Result: " + str(res)) - if 'result' in res: - text = " ".join([w['word'] for w in res['result']]) - os.system("espeak -w /tmp/response22.wav \"" + text.encode('utf-8') + "\"") - os.system("sox /tmp/response22.wav -r 8000 /tmp/response.wav") - agi.stream_file("/tmp/response") - os.remove("/tmp/response.wav") + if "text" in res: + text = res.get("text") + process_result(agi, text) def startAGI(): agi = AGI() @@ -43,7 +46,10 @@ def startAGI(): agi.verbose(''.join(traceback.format_exception(type(err), err, err.__traceback__)).replace('\n', ' ')) finally: ws.send('{"eof" : 1}') - ws.recv() + res = json.loads(ws.recv()) + if "text" in res: + text = res.get("text") + process_result(agi, text) ws.close() startAGI()