From a7b160eec179884d98289fd095d3562912485bf8 Mon Sep 17 00:00:00 2001 From: Ben Xu Date: Thu, 2 May 2024 14:14:34 -0400 Subject: [PATCH] add accumulate mobile --- software/source/server/server.py | 6 ++-- software/source/utils/accumulator.py | 52 ++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/software/source/server/server.py b/software/source/server/server.py index b2f8936..6483160 100644 --- a/software/source/server/server.py +++ b/software/source/server/server.py @@ -227,15 +227,15 @@ async def listener(mobile: bool): await asyncio.sleep(1) if mobile: - message = accumulator_mobile.accumulate(chunk, mobile) + message = accumulator_mobile.accumulate_mobile(chunk) else: - message = accumulator_global.accumulate(chunk, mobile) + message = accumulator_global.accumulate(chunk) if message == None: # Will be None until we have a full message ready continue - # print(str(message)[:1000]) + print(str(message)[:1000]) # At this point, we have our message diff --git a/software/source/utils/accumulator.py b/software/source/utils/accumulator.py index 13ed953..d4715e1 100644 --- a/software/source/utils/accumulator.py +++ b/software/source/utils/accumulator.py @@ -3,7 +3,7 @@ class Accumulator: self.template = {"role": None, "type": None, "format": None, "content": None} self.message = self.template - def accumulate(self, chunk, mobile): + def accumulate(self, chunk): # print(str(chunk)[:100]) if type(chunk) == dict: if "format" in chunk and chunk["format"] == "active_line": @@ -44,10 +44,50 @@ class Accumulator: if "content" not in self.message or type(self.message["content"]) != bytes: self.message["content"] = b"" self.message["content"] += chunk + return None - if mobile: - self.message["type"] = "audio" - self.message["format"] = "bytes.wav" - return self.message - else: + def accumulate_mobile(self, chunk): + # print(str(chunk)[:100]) + if type(chunk) == dict: + if "format" in chunk and chunk["format"] == "active_line": + # We don't do anything with these + return None + + if "start" in chunk: + self.message = chunk + self.message.pop("start") + return None + + if "content" in chunk: + if any( + self.message[key] != chunk[key] + for key in self.message + if key != "content" + ): + self.message = chunk + if "content" not in self.message: + self.message["content"] = chunk["content"] + else: + if type(chunk["content"]) == dict: + # dict concatenation cannot happen, so we see if chunk is a dict + self.message["content"]["content"] += chunk["content"][ + "content" + ] + else: + self.message["content"] += chunk["content"] return None + + if "end" in chunk: + # We will proceed + message = self.message + self.message = self.template + return message + + if type(chunk) == bytes: + if "content" not in self.message or type(self.message["content"]) != bytes: + self.message["content"] = b"" + self.message["content"] += chunk + + self.message["type"] = "audio" + self.message["format"] = "bytes.wav" + return self.message