Skip to content

Commit 3a92561

Browse files
committed
* [maixcam2] delete melotts of app_chat
1 parent b49bf58 commit 3a92561

File tree

1 file changed

+3
-70
lines changed

1 file changed

+3
-70
lines changed

projects/app_chat/main.py

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from maix import nn, audio, time, display, app, image, touchscreen
22
import threading
3-
from queue import Queue, Empty
43
import re
54

65
class PagedText:
@@ -110,16 +109,6 @@ def __init__(self):
110109
except:
111110
self.vad = None
112111

113-
self.__show_load_info('loading player..')
114-
self.player = audio.Player(sample_rate=44100, block=False)
115-
# max buffer size: (period_size * period_count) / sample_rate * 1000
116-
# (160 * 4 * 4200) / 44100 = 60.95s
117-
self.player.period_count(4200)
118-
self.player.volume(50)
119-
self.player_queue = Queue(100)
120-
self.player_thread = threading.Thread(target=self.player_thread_handle, daemon=True)
121-
self.player_thread.start()
122-
123112
self.__show_load_info('loading whisper..')
124113
ai_isp_on = bool(int(app.get_sys_config_kv("npu", "ai_isp", "1")))
125114
if ai_isp_on is True:
@@ -148,50 +137,8 @@ def __init__(self):
148137
self.llm.set_reply_callback(self.__llm_on_reply)
149138
self.llm_last_msg = ""
150139

151-
self.__show_load_info('loading melotts..')
152-
self.tts = nn.MeloTTS(model="/root/models/melotts/melotts-zh.mud", speed = 0.8, language='en')
153-
154-
self.tts_queue = Queue(100)
155-
self.tts_thread = threading.Thread(target=self.tts_thread_handle, daemon=True)
156-
self.tts_thread.start()
157-
158140
self.page_text = PagedText()
159-
160-
def player_thread_handle(self):
161-
bytes_per_frame = 2
162-
while not app.need_exit():
163-
try:
164-
pcm = self.player_queue.get(timeout=500)
165-
t = time.ticks_ms()
166-
print('self.player.remaining size1', self.player.get_remaining_frames())
167-
while not app.need_exit():
168-
idle_frames = self.player.get_remaining_frames()
169-
write_frames = len(pcm) / bytes_per_frame
170-
print('idle', idle_frames, 'write', write_frames)
171-
if idle_frames >= write_frames:
172-
break
173-
else:
174-
time.sleep_ms(10)
175-
print('wait remain cost', time.ticks_ms() - t)
176-
print('idle', self.player.get_remaining_frames(), 'write', len(pcm) / bytes_per_frame)
177-
t = time.ticks_ms()
178-
self.player.play(pcm)
179-
print('play cost', time.ticks_ms() - t)
180-
except Empty:
181-
continue
182-
183-
def tts_thread_handle(self):
184-
while not app.need_exit():
185-
try:
186-
msg = self.tts_queue.get(timeout=500)
187-
print('tts queue get:', msg)
188-
t = time.ticks_ms()
189-
pcm = self.tts.infer(msg, output_pcm=True)
190-
print('tts infer cost', time.ticks_ms() - t)
191-
self.player_queue.put(pcm)
192-
except Empty:
193-
continue
194-
141+
195142
def _whisper_thread_handle(self, path):
196143
self.whisper_results = self.whisper.transcribe(path)
197144

@@ -228,19 +175,7 @@ def __llm_on_reply(self, obj, resp):
228175
parts=re.split(r"[,.!?]", self.llm_last_msg)
229176
# print('parts', parts)
230177
if len(parts) > 1:
231-
if "!" in self.llm_last_msg:
232-
push_msg = parts[0] + "!"
233-
elif "," in self.llm_last_msg:
234-
push_msg = parts[0] + ","
235-
elif "." in self.llm_last_msg:
236-
push_msg = parts[0] + "."
237-
elif "?" in self.llm_last_msg:
238-
push_msg = parts[0] + "?"
239-
else:
240-
push_msg = parts[0]
241-
pass
242178
self.llm_last_msg = parts[-1]
243-
self.tts_queue.put(push_msg)
244179

245180
def __show_load_info(self, text: str, x:int = 0, y:int = 0, color:image.Color=image.COLOR_WHITE):
246181
if self.disp:
@@ -369,14 +304,12 @@ class Status:
369304
llm_result = llm_result0.msg
370305
self.llm.clear_context()
371306
print(llm_result)
372-
status = Status.TTS
373-
asr_result = None
374-
elif status == Status.TTS:
375-
if self.tts_queue.empty():
376307
status = Status.IDLE
308+
asr_result = None
377309
else:
378310
status = Status.IDLE
379311
time.sleep_ms(5)
312+
del self.llm
380313

381314
if __name__ == '__main__':
382315
appication = App()

0 commit comments

Comments
 (0)