import time
import numpy as np
import resampy
import requests
from io import BytesIO
from typing import Iterator

from utils.logger import logger
from .base_tts import BaseTTS, State
from registry import register


@register("tts", "cosyvoice")
class CosyVoiceTTS(BaseTTS):
    """适配本机既有 CosyVoice3 流式 TTS (POST /tts -> 24kHz int16 PCM 流)."""

    def txt_to_audio(self, msg: tuple[str, dict]):
        text, textevent = msg
        voice = textevent.get('tts', {}).get('voice', self.opt.REF_FILE or 'default')
        self.stream_tts(self.cosyvoice(text, voice), msg)

    def cosyvoice(self, text: str, voice: str) -> Iterator[bytes]:
        start = time.perf_counter()
        try:
            res = requests.post(
                f"{self.opt.TTS_SERVER}/tts",
                json={"text": text, "voice": voice},
                stream=True,
                timeout=(5, 300),
            )
            if res.status_code != 200:
                logger.error("cosyvoice http %s: %s", res.status_code, res.text[:200])
                return
            first = True
            for chunk in res.iter_content(chunk_size=4800):  # 100ms @24k int16
                if first:
                    logger.info("cosyvoice time to first chunk: %.3fs", time.perf_counter() - start)
                    first = False
                if chunk and self.state == State.RUNNING:
                    yield chunk
                elif self.state != State.RUNNING:
                    logger.info("cosyvoice interrupted, stop consuming")
                    res.close()
                    return
        except Exception:
            logger.exception('cosyvoice tts error')

    def stream_tts(self, audio_stream: Iterator[bytes], msg: tuple[str, dict]):
        text, textevent = msg
        first = True
        for chunk in audio_stream:
            if not chunk:
                continue
            pcm24 = np.frombuffer(chunk, dtype=np.int16).astype(np.float32) / 32767.0
            stream = resampy.resample(pcm24, sr_orig=24000, sr_new=self.sample_rate)
            streamlen = stream.shape[0]
            idx = 0
            while streamlen >= self.chunk:
                eventpoint = {}
                if first:
                    eventpoint = {'status': 'start', 'text': text}
                    first = False
                eventpoint.update(**textevent)
                self.parent.put_audio_frame(stream[idx:idx + self.chunk], eventpoint)
                streamlen -= self.chunk
                idx += self.chunk
        eventpoint = {'status': 'end', 'text': text}
        eventpoint.update(**textevent)
        self.parent.put_audio_frame(np.zeros(self.chunk, np.float32), eventpoint)
diff --git a/avatars/base_avatar.py b/avatars/base_avatar.py
index 28c826a..a9744e6 100644
--- a/avatars/base_avatar.py
+++ b/avatars/base_avatar.py
@@ -90,7 +90,7 @@ class BaseAvatar:
             'edgetts': 'tts.edge',
             'gpt-sovits': 'tts.sovits',
             'xtts': 'tts.xtts',
-            # 'cosyvoice': 'tts.cosyvoice',
+            'cosyvoice': 'tts.cosyvoice',
             # 'fishtts': 'tts.fish',
             'tencent': 'tts.tencent',
             'doubao': 'tts.doubao',
