Views
No views yet
pip install speechbrain1import torch
2from speechbrain.inference.vocoders import HIFIGAN
3hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="pretrained_models/tts-hifigan-ljspeech")
4mel_specs = torch.rand(2, 80,298)
5waveforms = hifi_gan.decode_batch(mel_specs)1import torchaudio
2from speechbrain.inference.vocoders import HIFIGAN
3from speechbrain.lobes.models.FastSpeech2 import mel_spectogram
4
5# Load a pretrained HIFIGAN Vocoder
6hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="pretrained_models/tts-hifigan-ljspeech")
7
8# Load an audio file (an example file can be found in this repository)
9# Ensure that the audio signal is sampled at 22050 Hz; refer to the provided link for a 16 kHz Vocoder.
10signal, rate = torchaudio.load('speechbrain/tts-hifigan-ljspeech/example.wav')
11
12# Compute the mel spectrogram.
13# IMPORTANT: Use these specific parameters to match the Vocoder's training settings for optimal results.
14spectrogram, _ = mel_spectogram(
15 audio=signal.squeeze(),
16 sample_rate=22050,
17 hop_length=256,
18 win_length=None,
19 n_mels=80,
20 n_fft=1024,
21 f_min=0.0,
22 f_max=8000.0,
23 power=1,
24 normalized=False,
25 min_max_energy_norm=True,
26 norm="slaney",
27 mel_scale="slaney",
28 compression=True
29)
30
31# Convert the spectrogram to waveform
32waveforms = hifi_gan.decode_batch(spectrogram)
33
34# Save the reconstructed audio as a waveform
35torchaudio.save('waveform_reconstructed.wav', waveforms.squeeze(1), 22050)
36
37# If everything is set up correctly, the original and reconstructed audio should be nearly indistinguishable.
38# Keep in mind that this Vocoder is trained for a single speaker; for multi-speaker Vocoder options, refer to the provided links.
391import torchaudio
2from speechbrain.inference.TTS import Tacotron2
3from speechbrain.inference.vocoders import HIFIGAN
4
5# Intialize TTS (tacotron2) and Vocoder (HiFIGAN)
6tacotron2 = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="pretrained_models/tts-tacotron2-ljspeech")
7hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="pretrained_model/tts-hifigan-ljspeech")
8
9# Running the TTS
10mel_output, mel_length, alignment = tacotron2.encode_text("Mary had a little lamb")
11
12# Running Vocoder (spectrogram-to-waveform)
13waveforms = hifi_gan.decode_batch(mel_output)
14
15# Save the waverform
16torchaudio.save('example_TTS.wav',waveforms.squeeze(1), 22050)run_opts={"device":"cuda"} when calling the from_hparams method.git clone https://github.com/speechbrain/speechbrain/1cd speechbrain
2pip install -r requirements.txt
3pip install -e .1cd recipes/LJSpeech/TTS/vocoder/hifi_gan/
2python train.py hparams/train.yaml --data_folder /path/to/LJspeech