Skip to content

Commit 686252e

Browse files
authoredOct 30, 2024··
fix: make mp3 replayable (#557)
Signed-off-by: Frost Ming <[email protected]>
1 parent 9e4a4b5 commit 686252e

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed
 

‎xiaogpt/cli.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ def main():
111111
)
112112
parser.add_argument(
113113
"--verbose",
114+
"-v",
114115
dest="verbose",
115-
action="store_true",
116-
default=None,
116+
action="count",
117+
default=0,
117118
help="show info",
118119
)
119120
parser.add_argument(

‎xiaogpt/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Config:
7777
api_base: str | None = None
7878
deployment_id: str | None = None
7979
use_command: bool = False
80-
verbose: bool = False
80+
verbose: int = 0
8181
start_conversation: str = "开始持续对话"
8282
end_conversation: str = "结束持续对话"
8383
stream: bool = False

‎xiaogpt/tts/live.py

+5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ def do_GET(self):
2727
self.end_headers()
2828
key = self.path.split("/")[-1]
2929
queue = get_queue(key)
30+
chunks: list[bytes] = []
3031
while True:
3132
chunk = queue.get()
33+
chunks.append(chunk)
3234
if chunk == b"":
3335
break
3436
self.wfile.write(chunk)
37+
for chunk in chunks:
38+
queue.put_nowait(chunk)
3539

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

7781
while True:
7882
if await self.get_if_xiaoai_is_playing():
83+
logger.debug("Xiaoai is playing, waiting")
7984
await asyncio.sleep(1)
8085
else:
8186
break

‎xiaogpt/xiaogpt.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,20 @@ async def close(self):
5757
async def poll_latest_ask(self):
5858
async with ClientSession() as session:
5959
session._cookie_jar = self.cookie_jar
60+
log_polling = int(self.config.verbose) > 1
6061
while True:
61-
self.log.debug(
62-
"Listening new message, timestamp: %s", self.last_timestamp
63-
)
62+
if log_polling:
63+
self.log.debug(
64+
"Listening new message, timestamp: %s", self.last_timestamp
65+
)
6466
new_record = await self.get_latest_ask_from_xiaoai(session)
6567
start = time.perf_counter()
66-
self.log.debug(
67-
"Polling_event, timestamp: %s %s", self.last_timestamp, new_record
68-
)
68+
if log_polling:
69+
self.log.debug(
70+
"Polling_event, timestamp: %s %s",
71+
self.last_timestamp,
72+
new_record,
73+
)
6974
await self.polling_event.wait()
7075
if (
7176
self.config.mute_xiaoai
@@ -75,7 +80,10 @@ async def poll_latest_ask(self):
7580
await self.stop_if_xiaoai_is_playing()
7681
if (d := time.perf_counter() - start) < 1:
7782
# sleep to avoid too many request
78-
self.log.debug("Sleep %f, timestamp: %s", d, self.last_timestamp)
83+
if log_polling:
84+
self.log.debug(
85+
"Sleep %f, timestamp: %s", d, self.last_timestamp
86+
)
7987
# if you want force mute xiaoai, comment this line below.
8088
await asyncio.sleep(1 - d)
8189

@@ -334,6 +342,7 @@ async def get_if_xiaoai_is_playing(self):
334342
async def stop_if_xiaoai_is_playing(self):
335343
is_playing = await self.get_if_xiaoai_is_playing()
336344
if is_playing:
345+
self.log.debug("Muting xiaoai")
337346
# stop it
338347
await self.mina_service.player_pause(self.device_id)
339348

0 commit comments

Comments
 (0)
Please sign in to comment.