Skip to content

Commit

Permalink
fix: make mp3 replayable
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Oct 30, 2024
1 parent 9e4a4b5 commit 2665955
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
5 changes: 3 additions & 2 deletions xiaogpt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ def main():
)
parser.add_argument(
"--verbose",
"-v",
dest="verbose",
action="store_true",
default=None,
action="count",
default=0,
help="show info",
)
parser.add_argument(
Expand Down
2 changes: 1 addition & 1 deletion xiaogpt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Config:
api_base: str | None = None
deployment_id: str | None = None
use_command: bool = False
verbose: bool = False
verbose: int = 0
start_conversation: str = "开始持续对话"
end_conversation: str = "结束持续对话"
stream: bool = False
Expand Down
5 changes: 5 additions & 0 deletions xiaogpt/tts/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ def do_GET(self):
self.end_headers()
key = self.path.split("/")[-1]
queue = get_queue(key)
chunks: list[bytes] = []
while True:
chunk = queue.get()
chunks.append(chunk)
if chunk == b"":
break
self.wfile.write(chunk)
for chunk in chunks:
queue.put_nowait(chunk)

def log_message(self, format, *args):
logger.debug(f"{self.address_string()} - {format}", *args)
Expand Down Expand Up @@ -76,6 +80,7 @@ async def worker():

while True:
if await self.get_if_xiaoai_is_playing():
logger.debug("Xiaoai is playing, waiting")
await asyncio.sleep(1)
else:
break
Expand Down
23 changes: 16 additions & 7 deletions xiaogpt/xiaogpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,20 @@ async def close(self):
async def poll_latest_ask(self):
async with ClientSession() as session:
session._cookie_jar = self.cookie_jar
log_polling = int(self.config.verbose) > 1
while True:
self.log.debug(
"Listening new message, timestamp: %s", self.last_timestamp
)
if log_polling:
self.log.debug(
"Listening new message, timestamp: %s", self.last_timestamp
)
new_record = await self.get_latest_ask_from_xiaoai(session)
start = time.perf_counter()
self.log.debug(
"Polling_event, timestamp: %s %s", self.last_timestamp, new_record
)
if log_polling:
self.log.debug(
"Polling_event, timestamp: %s %s",
self.last_timestamp,
new_record,
)
await self.polling_event.wait()
if (
self.config.mute_xiaoai
Expand All @@ -75,7 +80,10 @@ async def poll_latest_ask(self):
await self.stop_if_xiaoai_is_playing()
if (d := time.perf_counter() - start) < 1:
# sleep to avoid too many request
self.log.debug("Sleep %f, timestamp: %s", d, self.last_timestamp)
if log_polling:
self.log.debug(
"Sleep %f, timestamp: %s", d, self.last_timestamp
)
# if you want force mute xiaoai, comment this line below.
await asyncio.sleep(1 - d)

Expand Down Expand Up @@ -334,6 +342,7 @@ async def get_if_xiaoai_is_playing(self):
async def stop_if_xiaoai_is_playing(self):
is_playing = await self.get_if_xiaoai_is_playing()
if is_playing:
self.log.debug("Muting xiaoai")
# stop it
await self.mina_service.player_pause(self.device_id)

Expand Down

0 comments on commit 2665955

Please sign in to comment.