本仓库提供了使用 SpeechBrain 进行文本转语音(TTS)所需的全部工具,采用在 LJSpeech 上预训练的 FastSpeech2 模型。
预训练模型以文本或音素作为输入,输出频谱图。可以通过在生成的频谱图上叠加一个声码器(如 HiFIGAN)来获得最终的波形。需要注意的是,如果输入为文本,我们将使用最先进的 grapheme-to-phoneme 模块将其转换为音素,然后将音素传入 fastspeech2 模型。
git clone https://github.com/speechbrain/speechbrain.git
cd speechbrain
pip install -r requirements.txt
pip install --editable . 请注意,我们鼓励你阅读我们的教程,以了解更多关于 SpeechBrain 的内容。
import torchaudio
from speechbrain.inference.TTS import FastSpeech2
from speechbrain.inference.vocoders import HIFIGAN
# Intialize TTS (tacotron2) and Vocoder (HiFIGAN)
fastspeech2 = FastSpeech2.from_hparams(source="speechbrain/tts-fastspeech2-ljspeech", savedir="pretrained_models/tts-fastspeech2-ljspeech")
hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="pretrained_models/tts-hifigan-ljspeech")
# Run TTS with text input
input_text = "were the leaders in this luckless change; though our own Baskerville; who was at work some years before them; went much on the same lines;"
mel_output, durations, pitch, energy = fastspeech2.encode_text(
[input_text],
pace=1.0, # scale up/down the speed
pitch_rate=1.0, # scale up/down the pitch
energy_rate=1.0, # scale up/down the energy
)
# Running Vocoder (spectrogram-to-waveform)
waveforms = hifi_gan.decode_batch(mel_output)
# Save the waverform
torchaudio.save('example_TTS_input_text.wav', waveforms.squeeze(1), 22050)
# Run TTS with phoneme input
input_phonemes = ['W', 'ER', 'DH', 'AH', 'L', 'IY', 'D', 'ER', 'Z', 'IH', 'N', 'DH', 'IH', 'S', 'L', 'AH', 'K', 'L', 'AH', 'S', 'CH', 'EY', 'N', 'JH', 'spn', 'DH', 'OW', 'AW', 'ER', 'OW', 'N', 'B', 'AE', 'S', 'K', 'ER', 'V', 'IH', 'L', 'spn', 'HH', 'UW', 'W', 'AA', 'Z', 'AE', 'T', 'W', 'ER', 'K', 'S', 'AH', 'M', 'Y', 'IH', 'R', 'Z', 'B', 'IH', 'F', 'AO', 'R', 'DH', 'EH', 'M', 'spn', 'W', 'EH', 'N', 'T', 'M', 'AH', 'CH', 'AA', 'N', 'DH', 'AH', 'S', 'EY', 'M', 'L', 'AY', 'N', 'Z', 'spn']
mel_output, durations, pitch, energy = fastspeech2.encode_phoneme(
[input_phonemes],
pace=1.0, # scale up/down the speed
pitch_rate=1.0, # scale up/down the pitch
energy_rate=1.0, # scale up/down the energy
)
# Running Vocoder (spectrogram-to-waveform)
waveforms = hifi_gan.decode_batch(mel_output)
# Save the waverform
torchaudio.save('example_TTS_input_phoneme.wav', waveforms.squeeze(1), 22050)如果你想一次性生成多个句子,可以这样做:
from speechbrain.inference.TTS import FastSpeech2
fastspeech2 = FastSpeech2.from_hparams(source="speechbrain/tts-fastspeech2-ljspeech", savedir="pretrained_models/tts-fastspeech2-ljspeech")
items = [
"A quick brown fox jumped over the lazy dog",
"How much wood would a woodchuck chuck?",
"Never odd or even"
]
mel_outputs, durations, pitch, energy = fastspeech2.encode_text(
items,
pace=1.0, # scale up/down the speed
pitch_rate=1.0, # scale up/down the pitch
energy_rate=1.0, # scale up/down the energy
)在 GPU 上执行推理时,调用 from_hparams 方法需添加 run_opts={"device":"cuda"}。
该模型使用 SpeechBrain 进行训练。 要从头开始训练,请遵循以下步骤:
git clone https://github.com/speechbrain/speechbrain/cd speechbrain
pip install -r requirements.txt
pip install -e .cd recipes/LJSpeech/TTS/fastspeech2/
python train.py --device=cuda:0 --max_grad_norm=1.0 --data_folder=/your_folder/LJSpeech-1.1 hparams/train.yaml你可以在这里找到我们的训练结果(模型、日志等)。
SpeechBrain 团队不对该模型在其他数据集上的表现提供任何保证。
如果你在研究或商业项目中使用了 SpeechBrain,请引用相关文献。
@misc{speechbrain,
title={{SpeechBrain}: A General-Purpose Speech Toolkit},
author={Mirco Ravanelli and Titouan Parcollet and Peter Plantinga and Aku Rouhe and Samuele Cornell and Loren Lugosch and Cem Subakan and Nauman Dawalatabad and Abdelwahab Heba and Jianyuan Zhong and Ju-Chieh Chou and Sung-Lin Yeh and Szu-Wei Fu and Chien-Feng Liao and Elena Rastorgueva and François Grondin and William Aris and Hwidong Na and Yan Gao and Renato De Mori and Yoshua Bengio},
year={2021},
eprint={2106.04624},
archivePrefix={arXiv},
primaryClass={eess.AS},
note={arXiv:2106.04624}
}