# Các Phương Thức Suy Luận

Source: https://docs.vieneu.io/vi/docs/api/infer

## `infer()`

```python
audio = tts.infer(
    text: str,
    ref_audio: str = None,
    ref_codes: Tensor = None,
    ref_text: str = None,
    voice: dict = None,
    max_chars: int = 256,
    silence_p: float = 0.15,
    crossfade_p: float = 0.0,
    temperature: float = 1.0,
    top_k: int = 50,
    skip_normalize: bool = False,
)
```

### Tham Số

| Tham Số | Kiểu | Mô Tả |
|---------|------|-------|
| `text` | `str` | Văn bản cần tổng hợp |
| `ref_audio` | `str` | Đường dẫn âm thanh mẫu để nhân bản giọng nói |
| `ref_codes` | `Tensor` | Mã mẫu đã được mã hoá trước |
| `ref_text` | `str` | Bản ghi nội dung âm thanh mẫu |
| `voice` | `dict` | Dict giọng đặt sẵn từ `get_preset_voice()` |
| `max_chars` | `int` | Số ký tự tối đa mỗi chunk (mặc định 256) |
| `silence_p` | `float` | Thời gian im lặng giữa các chunk (giây) |
| `crossfade_p` | `float` | Thời gian crossfade giữa các chunk |
| `temperature` | `float` | Nhiệt độ lấy mẫu |
| `top_k` | `int` | Lấy mẫu top-k |
| `skip_normalize` | `bool` | Bỏ qua chuẩn hoá văn bản |

### Trả Về

`numpy.ndarray` — Sóng âm tại 24 kHz.

### Thứ Tự Ưu Tiên Giọng Nói

1. Dict `voice` (từ preset)
2. `ref_audio` + `ref_text`
3. `ref_codes` + `ref_text`
4. Giọng preset mặc định

---

## `infer_batch()`

```python
audios = tts.infer_batch(texts: List[str], ...)
```

Trả về `List[numpy.ndarray]`. Chế độ PyTorch dùng batch generation thực sự; GGUF xử lý tuần tự.

---

## `infer_stream()`

```python
for chunk in tts.infer_stream(text: str, ...):
    phat_am_thanh(chunk)
```

Yield các chunk `numpy.ndarray` (chỉ GGUF).

---

## `save()`

```python
tts.save(audio: numpy.ndarray, duong_dan_output: str)
```

---

## `encode_reference()`

```python
codes = tts.encode_reference(duong_dan_am_thanh_mau: str)
# Trả về: torch.Tensor
```

---

## `close()`

```python
tts.close()
# Hoặc dùng context manager:
with Vieneu() as tts:
    audio = tts.infer(text="...")
```
