Views
No views yet
pip install speechbrain1import torch
2from speechbrain.inference.vocoders import HIFIGAN
3hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-libritts-22050Hz", savedir="pretrained_models/tts-hifigan-libritts-22050Hz")
4mel_specs = torch.rand(2, 80,298)
5
6# Running Vocoder (spectrogram-to-waveform)
7waveforms = 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-libritts-22050Hz", savedir="pretrained_models/tts-hifigan-libritts-22050Hz")
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 16000 Hz Vocoder.
10#signal, rate = torchaudio.load('speechbrain/tts-hifigan-libritts-22050H/example_22kHz.wav')
11signal, rate = torchaudio.load('/home/mirco/Downloads/example_22kHz.wav')
12
13# Ensure the audio is sigle channel
14signal = signal[0].squeeze()
15
16torchaudio.save('waveform.wav', signal.unsqueeze(0), 22050)
17
18# Compute the mel spectrogram.
19# IMPORTANT: Use these specific parameters to match the Vocoder's training settings for optimal results.
20spectrogram, _ = mel_spectogram(
21 audio=signal.squeeze(),
22 sample_rate=22050,
23 hop_length=256,
24 win_length=1024,
25 n_mels=80,
26 n_fft=1024,
27 f_min=0.0,
28 f_max=8000.0,
29 power=1,
30 normalized=False,
31 min_max_energy_norm=True,
32 norm="slaney",
33 mel_scale="slaney",
34 compression=True
35)
36
37# Convert the spectrogram to waveform
38waveforms = hifi_gan.decode_batch(spectrogram)
39
40# Save the reconstructed audio as a waveform
41torchaudio.save('waveform_reconstructed.wav', waveforms.squeeze(1), 22050)
42
43# If everything is set up correctly, the original and reconstructed audio should be nearly indistinguishable.
441import 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-libritts-22050Hz", savedir="pretrained_models/tts-hifigan-libritts-22050Hz")
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/LibriTTS/vocoder/hifigan/
2python train.py hparams/train.yaml --data_folder=/path/to/LibriTTS_data_destination --sample_rate=22050"recipes/LibriTTS/vocoder/hifigan/hparams/train.yaml" file and change the value for sample_rate as required.
The training logs and checkpoints are available here.