Some checks failed
Windows / verify (push) Has been cancelled
게이지 위젯은 있었지만 엔진 상태 이벤트(시작·정지 때만 나감)로만 값을 받아서 번역 중에는 바닥에 멈춰 있었다. 소리가 들어오는지 확인할 방법이 없었다. - 엔진이 최근 입력 RMS를 level 로 내주고, 메인 창이 번역 중에만 약 30Hz로 읽어 그린다 - 프레임이 끊기면(프로그램이 소리를 안 냄) 레벨을 0으로 내린다 - 게이지를 dB 눈금(-60~0 dBFS)으로 그린다. 평범한 대사(-30 dBFS)가 절반을 채운다 - 빠르게 오르고 천천히 내려가며, 최근 최고점을 세로선으로 잠깐 남긴다 - '입력 소리' 라벨과 '소리 받는 중 / 소리 없음' 문구를 옆에 붙였다 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
5.0 KiB
Python
157 lines
5.0 KiB
Python
"""홈 화면 입력 소리 게이지 — 엔진이 레벨을 내주고, UI가 번역 중 계속 그리는지."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import time
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from livesub.audio.base import CaptureBackend
|
|
from livesub.config import AppConfig
|
|
from livesub.constants import SAMPLE_RATE
|
|
from livesub.core.engine import TranslationEngine
|
|
|
|
pytest.importorskip("PySide6")
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
|
|
from PySide6.QtWidgets import QApplication # noqa: E402
|
|
|
|
from livesub.core.events import EngineState, EngineStatus # noqa: E402
|
|
from livesub.ui.main_window import MainWindow # noqa: E402
|
|
from livesub.ui.widgets.common import LevelMeter, level_to_fraction # noqa: E402
|
|
|
|
|
|
class BurstCapture(CaptureBackend):
|
|
"""0.6초 동안 소리를 내고 그 뒤로는 프레임을 아예 보내지 않는다.
|
|
|
|
프로그램이 소리를 멈추면 프로그램별 캡처는 프레임이 끊길 수 있다.
|
|
"""
|
|
|
|
name = "burst"
|
|
|
|
@staticmethod
|
|
def available() -> bool:
|
|
return True
|
|
|
|
@staticmethod
|
|
def list_sources():
|
|
return []
|
|
|
|
def _run(self) -> None:
|
|
n = SAMPLE_RATE // 50 # 20ms
|
|
t = np.arange(n, dtype=np.float32) / SAMPLE_RATE
|
|
frame = (np.sin(2 * np.pi * 300 * t) * 0.3).astype(np.float32)
|
|
for _ in range(30):
|
|
if self._stop.is_set():
|
|
return
|
|
self._emit(frame.copy())
|
|
time.sleep(0.02)
|
|
self._stop.wait()
|
|
|
|
|
|
@pytest.fixture
|
|
def engine(tmp_path, monkeypatch):
|
|
config = AppConfig()
|
|
config.models.preload_on_start = False
|
|
config.glossary.path = str(tmp_path / "glossary.json")
|
|
eng = TranslationEngine(config)
|
|
monkeypatch.setattr("livesub.core.engine.create_capture", lambda cfg: BurstCapture())
|
|
yield eng
|
|
eng.shutdown()
|
|
|
|
|
|
def _wait_for(cond, timeout=3.0) -> bool:
|
|
deadline = time.time() + timeout
|
|
while time.time() < deadline:
|
|
if cond():
|
|
return True
|
|
time.sleep(0.02)
|
|
return False
|
|
|
|
|
|
def test_engine_level_follows_audio_and_drops_when_frames_stop(engine):
|
|
assert engine.level == 0.0
|
|
engine.start()
|
|
# 진폭 0.3 사인파의 RMS ≈ 0.21
|
|
assert _wait_for(lambda: engine.level > 0.15), "소리가 나는데 레벨이 오르지 않았습니다"
|
|
assert _wait_for(lambda: engine.level == 0.0), "프레임이 끊겼는데 레벨이 남아 있습니다"
|
|
engine.stop()
|
|
assert engine.level == 0.0
|
|
|
|
|
|
def test_engine_level_resets_on_stop(engine):
|
|
engine.start()
|
|
assert _wait_for(lambda: engine.level > 0.15)
|
|
engine.stop()
|
|
assert engine.level == 0.0
|
|
|
|
|
|
def test_level_to_fraction_uses_db_scale():
|
|
assert level_to_fraction(0.0) == 0.0
|
|
assert level_to_fraction(1.0) == pytest.approx(1.0)
|
|
# 평범한 게임 대사 크기(-30 dBFS)가 게이지 절반을 채워야 눈에 띈다.
|
|
assert level_to_fraction(10 ** (-30 / 20)) == pytest.approx(0.5)
|
|
assert level_to_fraction(1e-6) == 0.0 # 바닥 아래는 0
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def qapp():
|
|
return QApplication.instance() or QApplication([])
|
|
|
|
|
|
def test_meter_jumps_up_and_falls_back_slowly(qapp):
|
|
meter = LevelMeter()
|
|
meter.set_level(0.1)
|
|
high = meter.level
|
|
assert high == pytest.approx(level_to_fraction(0.1))
|
|
meter.set_level(0.0)
|
|
assert 0.0 < meter.level < high # 바로 떨어지지 않고 서서히
|
|
for _ in range(40):
|
|
meter.set_level(0.0)
|
|
assert meter.level == 0.0
|
|
|
|
|
|
@pytest.fixture
|
|
def window(qapp, tmp_path, monkeypatch):
|
|
monkeypatch.setattr("livesub.constants.user_data_dir", lambda: tmp_path)
|
|
config = AppConfig()
|
|
config.models.preload_on_start = False
|
|
config.glossary.path = str(tmp_path / "glossary.json")
|
|
win = MainWindow(config)
|
|
yield win
|
|
win.engine.shutdown()
|
|
win.overlay.close()
|
|
win.close()
|
|
|
|
|
|
def test_gauge_is_polled_only_while_running(window):
|
|
assert not window._level_timer.isActive()
|
|
window._on_status(EngineStatus(EngineState.RUNNING, "듣는 중"))
|
|
assert window._level_timer.isActive()
|
|
window._on_status(EngineStatus(EngineState.IDLE, "대기 중"))
|
|
assert not window._level_timer.isActive()
|
|
|
|
|
|
def test_timer_tick_moves_gauge_and_label(window, monkeypatch):
|
|
home = window.home_page
|
|
window._on_status(EngineStatus(EngineState.RUNNING, "듣는 중"))
|
|
monkeypatch.setattr(window.engine, "_level", 0.05)
|
|
window._level_timer.timeout.emit()
|
|
assert home.level_meter.level > 0.5
|
|
assert home.level_label.text() == "소리 받는 중"
|
|
|
|
# 말 사이 짧은 숨에는 문구가 바뀌지 않는다.
|
|
monkeypatch.setattr(window.engine, "_level", 0.0)
|
|
for _ in range(5):
|
|
window._level_timer.timeout.emit()
|
|
assert home.level_label.text() == "소리 받는 중"
|
|
for _ in range(home.QUIET_TICKS):
|
|
window._level_timer.timeout.emit()
|
|
assert home.level_label.text() == "소리 없음"
|
|
|
|
window._on_status(EngineStatus(EngineState.IDLE, "대기 중"))
|
|
assert home.level_meter.level == 0.0
|
|
assert home.level_label.text() == "번역을 시작하면 움직입니다"
|