File size: 3,886 Bytes
fbb4cab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
"""
Standalone TTS Program untuk Bahasa Indonesia
Berdasarkan app.py - TTS Indonesiaku Gratis

Program ini menghasilkan file audio dari teks Bahasa Indonesia
dengan menggunakan model TTS yang sudah dilatih khusus untuk bahasa Indonesia.

Parameter:
- text: Teks yang akan diubah menjadi suara
- file_path: Path file output audio (.wav)
"""

import subprocess
import html
from pathlib import Path

try:
    from g2pid import G2P
    # Inisialisasi G2P (Grapheme to Phoneme)
    g2p = G2P()
    G2P_AVAILABLE = True
except ImportError:
    print("WARNING: G2P module tidak tersedia. Install dependencies dengan: pip install -r requirements.txt")
    G2P_AVAILABLE = False

# Parameter default untuk konfigurasi
params = {
    "model_path": "checkpoint_1260000-inference.pth",
    "config_path": "config.json",
    # "speaker": "ardi"  # Default speaker
    "speaker": "gadis"  # Default speaker
}
# params = {
#     "model_path": "kobov2.pth",
#     "config_path": "config.json",
#     # "speaker": "kobov2.index"  # Default speaker
#     "speaker": "gadis",
# }

def generate_tts(text, file_path, speaker="ardi"):
    """
    Generate TTS audio dari teks Bahasa Indonesia
    
    Args:
        text (str): Teks yang akan diubah menjadi suara
        file_path (str): Path file output audio (.wav)
        speaker (str): Nama speaker (default: "ardi")
    
    Returns:
        bool: True jika berhasil, False jika gagal
    """
    try:
        print(f"Memproses teks: {text}")
        
        # Konversi teks ke format TTS menggunakan G2P jika tersedia
        if G2P_AVAILABLE:
            print("Mengonversi teks ke fonem...")
            text_to_tts = g2p(text)
            print(f"Teks setelah konversi G2P: {text_to_tts}")
        else:
            print("WARNING: Menggunakan teks asli tanpa konversi G2P")
            text_to_tts = text
        
        # Pastikan direktori output ada
        output_path = Path(file_path)
        output_path.parent.mkdir(parents=True, exist_ok=True)
        
        # Perintah untuk menjalankan TTS
        command = [
            "tts",
            "--text", text_to_tts,
            "--model_path", params["model_path"],
            "--config_path", params["config_path"],
            "--speaker_idx", speaker,
            "--out_path", str(output_path)
        ]
        
        print("Menjalankan proses TTS...")
        print(f"Command: {' '.join(command)}")
        
        # Jalankan proses TTS
        result = subprocess.run(command, capture_output=True, text=True)
        
        if result.returncode != 0:
            print(f"Error: {result.stderr}")
            return False
        
        print(f"SUCCESS: Audio berhasil dibuat: {file_path}")
        return True
        
    except Exception as e:
        print(f"ERROR: {str(e)}")
        return False

def main():
    """
    Fungsi utama dengan parameter yang diminta
    """
    # Parameter sesuai permintaan
    text = "Selamat pagi, nama saya Aryo."
    file_path = "output_id_coqui.wav"
    
    print("=" * 60)
    print("TTS Standalone - Bahasa Indonesia")
    print("=" * 60)
    print(f"Teks: {text}")
    print(f"Output file: {file_path}")
    print(f"Speaker: {params['speaker']}")
    print("=" * 60)
    
    # Generate TTS
    success = generate_tts(text, file_path, params["speaker"])
    
    if success:
        print("\nSUCCESS: Proses TTS selesai!")
        print(f"File audio tersimpan di: {file_path}")
    else:
        print("\nERROR: Proses TTS gagal!")
        print("Pastikan:")
        print("1. Model checkpoint_1260000-inference.pth tersedia")
        print("2. File config.json tersedia")
        print("3. TTS library terinstall (pip install TTS)")
        print("4. Speaker tersedia dalam model")
        print("5. Dependencies terinstall: pip install -r requirements.txt")

if __name__ == "__main__":
    main()