模型:
Sunbird/sunbird-lug-tts-commonvoice-female
该存储库提供了使用SpeechBrain进行文字转语音(TTS)所需的所有工具。
预训练模型接收一个短文本作为输入,并输出一个声谱图。可以通过在生成的声谱图之上应用声码器(例如HiFIGAN)来获取最终的波形。
pip install speechbrain
请注意,我们鼓励您阅读我们的教程并了解更多 SpeechBrain 的内容。
import torchaudio from speechbrain.pretrained import Tacotron2 from speechbrain.pretrained import HIFIGAN # Intialize TTS (tacotron2) and Vocoder (HiFIGAN) tacotron2 = Tacotron2.from_hparams(source="sunbird/sunbird-lug-tts-commonvoice-female/", savedir="tmpdir_tts") hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmpdir_vocoder") # Running the TTS mel_output, mel_length, alignment = tacotron2.encode_text("Mbagaliza Christmass Enungi Nomwaka Omugya Gubaberere Gwamirembe") # Running Vocoder (spectrogram-to-waveform) waveforms = hifi_gan.decode_batch(mel_output) # Save the waverform torchaudio.save('example_TTS.wav',waveforms.squeeze(1), 22050)
如果要一次生成多个句子,可以按照以下方式操作:
from speechbrain.pretrained import Tacotron2 tacotron2 = Tacotron2.from_hparams(source="speechbrain/TTS_Tacotron2", savedir="tmpdir") items = [ "Nsanyuse okukulaba", "Erinnya lyo ggwe ani?", "Mbagaliza Christmass Enungi Nomwaka Omugya Gubaberere Gwamirembe" ] mel_outputs, mel_lengths, alignments = tacotron2.encode_batch(items)
要在GPU上执行推理,调用 from_hparams 方法时添加 run_opts={"device":"cuda"}。