Views
No views yet
pip install speechbrain1import torch
2from speechbrain.inference.vocoders import HIFIGAN
3hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-libritts-16kHz", savedir="pretrained_models/tts-hifigan-libritts-16kHz")
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-16kHz", savedir="pretrained_models/tts-hifigan-libritts-16kHz")
7
8# Load an audio file (an example file can be found in this repository)
9# Ensure that the audio signal is sampled at 16000 Hz; refer to the provided link for a 22050 Hz Vocoder.
10signal, rate = torchaudio.load('tests/samples/ASR/spk1_snt1.wav')
11
12# Ensure the audio is sigle channel
13signal = signal[0].squeeze()
14
15torchaudio.save('waveform.wav', signal.unsqueeze(0), 16000)
16
17# Compute the mel spectrogram.
18# IMPORTANT: Use these specific parameters to match the Vocoder's training settings for optimal results.
19spectrogram, _ = mel_spectogram(
20 audio=signal.squeeze(),
21 sample_rate=16000,
22 hop_length=256,
23 win_length=1024,
24 n_mels=80,
25 n_fft=1024,
26 f_min=0.0,
27 f_max=8000.0,
28 power=1,
29 normalized=False,
30 min_max_energy_norm=True,
31 norm="slaney",
32 mel_scale="slaney",
33 compression=True
34)
35
36# Convert the spectrogram to waveform
37waveforms = hifi_gan.decode_batch(spectrogram)
38
39# Save the reconstructed audio as a waveform
40torchaudio.save('waveform_reconstructed.wav', waveforms.squeeze(1), 16000)
41
42# If everything is set up correctly, the original and reconstructed audio should be nearly indistinguishable
43run_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=16000"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.