Skip to content
PyThaiNLPPublic

About

Open Source Thai Text-to-speech library in Python

Topics

Resources

Stars

117 stars

Watchers

2 watching

Forks

Repository files navigation

PyThaiTTS

Open Source Thai Text-to-speech library in Python

Google Colab | Docs | Notebooks Download

License: Apache-2.0 License

Install

Install by pip:

pip install pythaitts

Usage

Basic Usage

from pythaitts import TTS

tts = TTS()
file = tts.tts("ภาษาไทย ง่าย มาก มาก", filename="cat.wav") # It will get wav file path.
wave = tts.tts("ภาษาไทย ง่าย มาก มาก",return_type="waveform") # It will get waveform.

Using Different TTS Models

PyThaiTTS supports multiple TTS models. You can specify which model to use:

from pythaitts import TTS

# Use FastThaiG2P (default) (default voice: thai_som)
# FastThaiG2P Sample Rate is 24000 Hz
tts = TTS(pretrained="fastthaig2p")
file = tts.tts("สวัสดีครับ", speaker_idx="thai_som", filename="output.wav")

# Use MakHamTTS Paradee (standalone, single-voice 8MB quantized INT8 ONNX model, offline without download)
# MakHamTTS Paradee Sample Rate is 24000 Hz
tts = TTS(pretrained="makham_paradee")
file = tts.tts("สวัสดีค่ะ", filename="output.wav")

# Use MakHamTTS (Kokoro-82M Thai base model, voices: newvoice_female, newvoice_male)
# MakHamTTS Sample Rate is 24000 Hz
tts = TTS(pretrained="makham")
file = tts.tts("สวัสดีค่ะ", speaker_idx="newvoice_female", filename="output.wav")

# Use Lunarlist ONNX
# Sample Rate is 22050 Hz
tts = TTS(pretrained="lunarlist_onnx")
file = tts.tts("ภาษาไทย ง่าย มาก", filename="output.wav")

# Use VachanaTTS (default voices: th_f_1, th_m_1, th_f_2, th_m_2)
# Sample Rate is 22050 Hz
tts = TTS(pretrained="vachana")
file = tts.tts("สวัสดีครับ", speaker_idx="th_f_1", filename="output.wav")

# Use KhanomTan
# Sample Rate is 16000 Hz
tts = TTS(pretrained="khanomtan")
file = tts.tts("ภาษาไทย", speaker_idx="Linda", filename="output.wav")

Real-time / Streaming TTS (FastThaiG2P)

PyThaiTTS supports low-latency, real-time streaming speech synthesis with FastThaiG2P, making it ideal for conversational voice agents and LLM streaming:

1. Streaming Audio from Text

Synthesize chunk-by-chunk in real time:

from pythaitts import TTS

tts = TTS(pretrained="fastthaig2p")

# Stream audio chunks as 24kHz float32 NumPy arrays
for audio_chunk in tts.stream("สวัสดีครับ ยินดีต้อนรับสู่ระบบเรียลไทม์ทีทีเอส"):
    print(f"Audio chunk shape: {audio_chunk.shape}")

# Stream raw 16-bit PCM bytes (for WebSockets or PyAudio)
for pcm_bytes in tts.stream("สวัสดีครับ", return_type="bytes"):
    # send over websocket or write to audio stream
    pass

2. Streaming from an LLM Token Stream

Feed tokens directly from an LLM or generator into tts.stream():

from pythaitts import TTS

tts = TTS()

def token_stream():
    tokens = ["สวัสดี", "ครับ", " ", "นี่", "คือ", "การ", "สตรีม", "มิ่ง"]
    for tok in tokens:
        yield tok

for audio_chunk in tts.stream(token_stream()):
    # Process or play chunk with low latency
    pass

3. Integration with the RealtimeTTS Library

You can use FastThaiG2P as an engine with KoljaB/RealtimeTTS:

pip install pythaitts[realtime]
from RealtimeTTS import TextToAudioStream
from pythaitts.realtime import FastThaiG2PEngine

engine = FastThaiG2PEngine()
stream = TextToAudioStream(engine)
stream.feed("สวัสดีครับ วันนี้อากาศดีมาก")
stream.play()

Thai-English Code-Switching (FastThaiG2P)

FastThaiG2P automatically supports Thai-English code-switching text by converting English words into English IPA and adapting them into Thai IPA phonemes (Thai-style English pronunciation), while spelling out English acronyms and single letters in Thai (e.g., SMS -> เอสเอ็มเอส, แผน A -> แผน เอ):

from pythaitts import TTS

tts = TTS(pretrained="fastthaig2p")
file = tts.tts("วันนี้เรามี meeting เรื่อง project ใหม่", filename="code_switch.wav")

You can also use the English-to-Thai-IPA G2P utilities directly:

from pythaitts import EnglishG2P, en_g2p, en_to_ipa, en_ipa_to_thai_ipa

# Convert English word -> English IPA -> Thai IPA
print(en_to_ipa("computer"))                    # Output: /kəmˈpjuː.tɚ/
print(en_ipa_to_thai_ipa("/kəmˈpjuː.tɚ/"))      # Output: /kʰɔm˧.pʰiw˧.tɤː˥˩/
print(en_g2p("computer"))                       # Output: /kʰɔm˧.pʰiw˧.tɤː˥˩/

# Or use the EnglishG2P class
eng = EnglishG2P()
print(eng("project"))                           # Output: /proː˧.t͡ɕek̚˨˩/

Custom Word Pronunciation Dictionary

PyThaiTTS supports custom word dictionaries to define custom Thai pronunciation (as Thai text reading or IPA phonemes) for specific words, acronyms, or names.

Dictionaries can be passed as Python dicts, JSON files, or delimited text files (TSV, CSV, or colon-separated):

from pythaitts import TTS

# 1. Provide custom Thai pronunciation (hyphenated or plain text)
custom_dict = {
    "เพชรบุรี": "เพ็ด-ชะ-บุ-รี",
    "สถิติ": "สะ-ถิ-ติ",
    "ChatGPT": "แชตจีพีที",
}

tts = TTS(custom_dict=custom_dict)
tts.tts("ยินดีต้อนรับสู่เพชรบุรี", filename="custom.wav")

# 2. Or pass per-call or with IPA phonemes directly
tts.tts(
    "ผมไปเพชรบุรี",
    custom_dict={"เพชรบุรี": "/pʰet̚˨˩.t͡ɕʰa˦˥.bu˨˩.riː˧/"},
    filename="ipa.wav",
)

# 3. Add or update custom words dynamically
tts.add_custom_dict({"ก.พ.": "กอ-พอ"})

# 4. Load from a JSON or TSV file
tts.set_custom_dict("my_words.json")

You can also use custom dictionaries with G2P directly:

from pythaitts.pretrained.fastthaig2p import G2P

g2p = G2P(custom_dict={"เพชรบุรี": "เพ็ด-ชะ-บุ-รี"})
print(g2p.convert("ผมไปเพชรบุรี"))

Text Preprocessing

PyThaiTTS includes automatic text preprocessing to improve TTS quality:

  • Number to Thai text conversion: Converts digits (e.g., "123") to Thai text (e.g., "หนึ่งร้อยยี่สิบสาม")
  • Mai yamok (ๆ) expansion: Expands the Thai repetition character (e.g., "ดีๆ" becomes "ดีดี")

Preprocessing is enabled by default:

from pythaitts import TTS

tts = TTS()
# Automatic preprocessing: "มี 5 คนๆ" becomes "มี ห้า คนคน"
file = tts.tts("มี 5 คนๆ", filename="output.wav")

You can disable preprocessing if needed:

file = tts.tts("มี 5 คนๆ", preprocess=False, filename="output.wav")

You can also use preprocessing functions directly:

from pythaitts import num_to_thai, expand_maiyamok, preprocess_text

# Convert numbers to Thai text
print(num_to_thai("123"))  # Output: หนึ่งร้อยยี่สิบสาม

# Expand mai yamok
print(expand_maiyamok("ดีๆ"))  # Output: ดีดี

# Full preprocessing
print(preprocess_text("มี 5 คนๆ"))  # Output: มี ห้า คนคน

You can see more at https://pythainlp.github.io/PyThaiTTS/.

About

Open Source Thai Text-to-speech library in Python

Topics

Resources

Stars

117 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages