-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSpeechStylis.py
More file actions
68 lines (53 loc) · 3.14 KB
/
Copy pathSpeechStylis.py
File metadata and controls
68 lines (53 loc) · 3.14 KB
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
# -*- coding: utf-8 -*-
"""text-to-voice.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Xdzm-Cu1ofbyFv0xp7An-BNiXYYpTchV
"""
!pip install TTS
import torch
from TTS.api import TTS
# Get device
device = "cuda" if torch.cuda.is_available() else "cpu"
# Initialize TTS
# =================================================================================================
# CHOOSE YOUR MODEL:
# To generate speech, you need to select a model. Below are some recommended models.
# You can uncomment the model you want to use.
# =================================================================================================
# Option 1: High-quality multilingual voice cloning (recommended for quality)
# This model is great for cloning voices in different languages. It's a bit slower but offers excellent results.
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=True).to(device)
# Option 2: Faster English-only model
# If you only need English and want faster generation, this model is a good choice.
# tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=True).to(device)
# Option 3: Fairseq model for a wide range of languages
# These models support over 1100 languages but may be slower.
# Replace <lang-iso_code> with the desired language code (e.g., "deu" for German).
# tts = TTS(model_name="tts_models/<lang-iso_code>/fairseq/vits", progress_bar=True).to(device)
# =================================================================================================
# INPUT YOUR TEXT:
# Enter the text you want to convert to speech in the `text` variable.
# =================================================================================================
text = "Hello, world! This is a test of the new and improved SpeechStylis AI."
# =================================================================================================
# SPECIFY SPEAKER'S VOICE:
# To clone a voice, provide a path to a high-quality audio file of the speaker.
# The audio should be clear, without background noise.
# =================================================================================================
speaker_wav_path = "my/cloning/audio.wav"
# =================================================================================================
# CHOOSE LANGUAGE:
# Specify the language of the text. For multilingual models, this is crucial.
# =================================================================================================
language = "en"
# =================================================================================================
# GENERATE AND SAVE SPEECH:
# The following code will generate the speech and save it to a file.
# =================================================================================================
# Run TTS
wav = tts.tts(text=text, speaker_wav=speaker_wav_path, language=language)
# Save the waveform to an audio file
output_file_path = "output.wav"
tts.tts_to_file(text=text, speaker_wav=speaker_wav_path, language=language, file_path=output_file_path)
print(f"Text-to-speech completed. Audio saved to: {output_file_path}")