35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
import struct
|
|
import logging
|
|
from typing import Any
|
|
|
|
from app.services.asr_providers import create_asr_provider, ASRError, _to_traditional # noqa: F401
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def float32_to_s16le(float32_bytes: bytes) -> bytes:
|
|
num_samples = len(float32_bytes) // 4
|
|
floats = struct.unpack(f"<{num_samples}f", float32_bytes)
|
|
int16_samples = [max(-32768, min(32767, int(f * 32767.0))) for f in floats]
|
|
return struct.pack(f"<{num_samples}h", *int16_samples)
|
|
|
|
|
|
def build_display_text(accumulated: str, current: str) -> str:
|
|
parts = [p for p in (accumulated, current) if p and p.strip()]
|
|
return " ".join(parts)
|
|
|
|
|
|
class ASRClient:
|
|
def __init__(self, settings: Any):
|
|
self._settings = settings
|
|
self._provider = create_asr_provider(settings)
|
|
|
|
async def transcribe_full(self, audio_bytes: bytes, language: str = "yue") -> str:
|
|
try:
|
|
return await self._provider.transcribe(audio_bytes, language)
|
|
except ASRError:
|
|
raise
|
|
except Exception as e:
|
|
logger.error("transcribe_full failed: %s", e)
|
|
raise ASRError(f"Transcription failed: {e}") from e
|