Text-to-Speech
Vietnamese
vietnamese
female
male
voice-cloning
File size: 7,444 Bytes
8bdc19f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a84125d0-ae92-4829-976d-456491ac7c0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] =  \"0\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3179777-b19d-46fb-8eb5-c5a4dd175f68",
   "metadata": {},
   "outputs": [],
   "source": [
    "from vinorm import TTSnorm\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9427640-6c1e-4337-9230-30838b508ea8",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "189956af-477b-4756-88e6-e3002a8ed039",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from f5tts_wrapper import F5TTSWrapper\n",
    "\n",
    "# Initialize the F5-TTS wrapper\n",
    "tts = F5TTSWrapper(\n",
    "    vocoder_name=\"vocos\",\n",
    "    # point to  EraX model checkpoint downloaded from HuggingFace\n",
    "    ckpt_path=\"path to model_last.pt\",\n",
    "    vocab_file=\"vocab.txt\",\n",
    "    use_ema=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ccf4419-a9b7-4dcb-9ce0-5ab3b948de61",
   "metadata": {},
   "outputs": [],
   "source": [
    "ref_audio_path = \"update_213000_ref.wav\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "240415b2-5e32-4caa-b833-ceae50675ca3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import IPython\n",
    "IPython.display.Audio(ref_audio_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bde9c39-d8fa-454a-9fcc-418175d8a989",
   "metadata": {},
   "outputs": [],
   "source": [
    "ref_text =  \"Thậm chí không ăn thì cũng có cảm giác rất là cứng bụng, chủ yếu là cái phần rốn...trở lên. Em có cảm giác khó thở, và ngủ cũng không ngon, thường bị ợ hơi rất là nhiều\"\n",
    "ref_text = TTSnorm(ref_text)\n",
    "ref_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1587e309-d118-40f1-91ee-a949a28eb135",
   "metadata": {},
   "outputs": [],
   "source": [
    "text =  \"Trong khi đó, tại một chung cư trên địa bàn P.Vĩnh Tuy (Q.Hoàng Mai), nhiều người sống trên tầng cao giật mình khi thấy rung lắc mạnh nên đã chạy xuống sảnh tầng 1. Cư dân tại đây cho biết, họ chưa bao giờ cảm thấy ảnh hưởng của động đất mạnh như hôm nay.\"\n",
    "text_norm =  TTSnorm(text)\n",
    "text_norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "918bf198-1ec7-4236-87bd-3be6210ea011",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Path to reference audio and output directory\n",
    "output_dir = \"output\"\n",
    "os.makedirs(output_dir, exist_ok=True)\n",
    "\n",
    "# Preprocess the reference audio just once\n",
    "# You can provide a reference text, or leave it blank for auto-transcription\n",
    "tts.preprocess_reference(\n",
    "    ref_audio_path=ref_audio_path,\n",
    "    ref_text=ref_text,  # Leave empty for auto-transcription\n",
    "    clip_short=True  # Clip long audio to ~12 seconds max\n",
    ")\n",
    "\n",
    "print(f\"Reference audio duration: {tts.get_current_audio_length():.2f} seconds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23aa55ad-612f-42f9-9742-ce0ce50a054a",
   "metadata": {},
   "outputs": [],
   "source": [
    "sentences =  [\n",
    "    gen_text\n",
    "]\n",
    "for i, sentence in enumerate(sentences):\n",
    "    output_path = os.path.join(output_dir, f\"generated_non_ema_{i+1}.wav\")\n",
    "\n",
    "    sentence_normed  = TTSnorm(sentence)\n",
    "    \n",
    "    # Generate the audio\n",
    "    tts.generate(\n",
    "        text=sentence_normed,\n",
    "        output_path=output_path,\n",
    "        nfe_step=20,               # Number of denoising steps (32)\n",
    "        cfg_strength=2.0,          # Classifier-free guidance strength (2)\n",
    "        speed=1.0,                 # Speed of generated speech (1.0)\n",
    "        cross_fade_duration=0.15,  # Cross-fade between chunks (0.15)\n",
    "    )\n",
    "    \n",
    "    print(f\"Generated: {output_path}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a3cb66e-1343-43cd-bed7-84254a74262f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import IPython\n",
    "IPython.display.Audio(f\"{output_path}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "832f3426-7a2a-4be9-aaaf-630866ecb31b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# For applications needing the raw audio data rather than saved files:\n",
    "audio_array, sample_rate = tts.generate(\n",
    "    text=gen_text,\n",
    "    return_numpy=True,\n",
    "    nfe_step=20,               # Number of denoising steps (32)\n",
    "    cfg_strength=2.0,          # Classifier-free guidance strength (2)\n",
    "    speed=1.0,                 # Speed of generated speech (1.0)\n",
    "    cross_fade_duration=0.15,  # Cross-fade between chunks (0.15)\n",
    ")\n",
    "print(f\"Generated audio array with shape {audio_array.shape} and sample rate {sample_rate}\")\n",
    "\n",
    "# Get audio and spectrogram (useful for visualization)\n",
    "audio_array, sample_rate, spectrogram = tts.generate(\n",
    "    text=\"This returns both audio and its spectrogram.\",\n",
    "    return_numpy=True,\n",
    "    return_spectrogram=True,\n",
    "    nfe_step=20,               # Number of denoising steps (32)\n",
    "    cfg_strength=2.0,          # Classifier-free guidance strength (2)\n",
    "    speed=1.0,                 # Speed of generated speech (1.0)\n",
    "    cross_fade_duration=0.15,  # Cross-fade between chunks (0.15)\n",
    ")\n",
    "print(f\"Generated spectrogram with shape {spectrogram.shape}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "862de193-96b3-4a37-9326-697838168437",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def save_spectrogram(spectrogram):\n",
    "    plt.figure(figsize=(12, 4))\n",
    "    plt.imshow(spectrogram, origin=\"lower\", aspect=\"auto\")\n",
    "    plt.colorbar()\n",
    "    plt.show()\n",
    "    plt.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb1979e7-5645-4cb0-94d8-aacc59109b03",
   "metadata": {},
   "outputs": [],
   "source": [
    "save_spectrogram(spectrogram)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87824cb4-7721-4d46-948a-a3457869b679",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}