From 45d95fc69544c8455b85d5935050ded6cbf61079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Sat, 18 May 2024 23:27:15 +0800 Subject: [PATCH] issues/752 --- .../LunaTranslator/myutils/utils.py | 100 ++++++++++++------ LunaTranslator/retrieval.py | 18 ---- plugins/CMakeLists.txt | 2 +- 3 files changed, 69 insertions(+), 51 deletions(-) diff --git a/LunaTranslator/LunaTranslator/myutils/utils.py b/LunaTranslator/LunaTranslator/myutils/utils.py index 29da5d9b..9a72980f 100644 --- a/LunaTranslator/LunaTranslator/myutils/utils.py +++ b/LunaTranslator/LunaTranslator/myutils/utils.py @@ -11,9 +11,6 @@ import time from PyQt5.QtWidgets import ( QApplication, ) -from PyQt5.QtCore import QIODevice, QBuffer -from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent - from traceback import print_exc from myutils.config import ( globalconfig, @@ -238,38 +235,77 @@ def argsort(l): class wavmp3player: - def changepos(self, pos): - if pos != 0 and pos == self.player.duration(): - if self.next: - content, volume = self.next - self.next = None - self.realplay(content, volume) - def __init__(self): - self.player = QMediaPlayer() - self.player.positionChanged.connect(self.changepos) - self.next = None + self.i = 0 + self.lastfile = None + self.tasks = None + self.lock = threading.Lock() + self.lock.acquire() + threading.Thread(target=self.dotasks).start() - def realplay(self, content, volume): - self.player.stop() - self.keepbuffer = [] - buffer = QBuffer() - buffer.open(QIODevice.ReadWrite) - self.player.setMedia(QMediaContent(), buffer) - self.player.setVolume(volume) - buffer.write(content) - self.keepbuffer = [buffer] - self.player.play() + def mp3playfunction(self, binary, volume, force): + try: + self.tasks = (binary, volume, force) + self.lock.release() + except: + pass - def mp3playfunction(self, content, volume, force): - if ( - globalconfig["ttsnointerrupt"] - and self.player.position() < self.player.duration() - and not force - ): - self.next = [content, volume] - else: - self.realplay(content, volume) + def dotasks(self): + durationms = 0 + try: + while True: + self.lock.acquire() + task = self.tasks + self.tasks = None + if task is None: + continue + binary, volume, force = task + os.makedirs("./cache/tts", exist_ok=True) + + tgt = os.path.abspath("./cache/tts/" + str(time.time()) + ".wav") + with open(tgt, "wb") as ff: + ff.write(binary) + durationms = self._playsoundWin(tgt, volume) + self.lastfile = tgt + if durationms and globalconfig["ttsnointerrupt"]: + while durationms > 0: + durationms -= 100 + time.sleep(0.1) + if self.tasks and self.tasks[-1]: + break + # time.sleep(durationms / 1000) + except: + print_exc() + + def _playsoundWin(self, sound, volume): + try: + + windows.mciSendString(("stop lunatranslator_mci_{}".format(self.i))) + windows.mciSendString(("close lunatranslator_mci_{}".format(self.i))) + self.i += 1 + if self.lastfile: + os.remove(self.lastfile) + self.lastfile = sound + windows.mciSendString( + 'open "{}" type mpegvideo alias lunatranslator_mci_{}'.format( + sound, self.i + ) + ) + durationms = int( + windows.mciSendString( + "status lunatranslator_mci_{} length".format(self.i) + ) + ) + windows.mciSendString( + "setaudio lunatranslator_mci_{} volume to {}".format( + self.i, volume * 10 + ) + ) + windows.mciSendString(("play lunatranslator_mci_{}".format(self.i))) + except: + durationms = 0 + + return durationms def selectdebugfile(path): diff --git a/LunaTranslator/retrieval.py b/LunaTranslator/retrieval.py index 0d6cbbaf..1e38de55 100644 --- a/LunaTranslator/retrieval.py +++ b/LunaTranslator/retrieval.py @@ -183,20 +183,6 @@ copycheck( os.path.join(runtime, "PyQt5/Qt5/bin"), ) -### -copycheck( - os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/Qt5Multimedia.dll"), - os.path.join(runtime, "PyQt5/Qt5/bin"), -) -copycheck( - os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/Qt5Network.dll"), - os.path.join(runtime, "PyQt5/Qt5/bin"), -) -copycheck( - os.path.join(py37Path, "Lib/site-packages/PyQt5/QtNetwork.pyd"), - os.path.join(runtime, "PyQt5"), -) -### copycheck( os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/Qt5Widgets.dll"), os.path.join(runtime, "PyQt5/Qt5/bin"), @@ -206,10 +192,6 @@ copycheck( os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/plugins/imageformats"), os.path.join(runtime, "PyQt5/Qt5/plugins"), ) -copycheck( - os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/plugins/mediaservice"), - os.path.join(runtime, "PyQt5/Qt5/plugins"), -) copycheck( os.path.join( py37Path, "Lib/site-packages/PyQt5/Qt5/plugins/platforms/qoffscreen.dll" diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index a22bb0fd..faf9f693 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -29,7 +29,7 @@ include(generate_product_version) set(VERSION_MAJOR 2) set(VERSION_MINOR 51) -set(VERSION_PATCH 6) +set(VERSION_PATCH 7) add_library(pch pch.cpp) target_precompile_headers(pch PUBLIC pch.h)