File size: 5,480 Bytes
97e5de8
3a0cbf1
 
 
 
 
 
 
 
 
 
 
 
97e5de8
3a0cbf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55908fe
3a0cbf1
4608d10
 
3a0cbf1
04d3069
3a0cbf1
4608d10
 
 
3a0cbf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04d3069
 
3a0cbf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
language: "en"
inference: false
tags:
- Vocoder
- DiffWave
- text-to-speech
- TTS
- speech-synthesis
- speechbrain
license: "apache-2.0"
datasets:
- LJSpeech
---

# Vocoder with DiffWave trained on LJSpeech

This repository provides all the necessary tools for using a [DiffWave](https://arxiv.org/pdf/2009.09761.pdf) vocoder trained with [LJSpeech](https://keithito.com/LJ-Speech-Dataset/). 

The pre-trained model takes as input a spectrogram and generates a waveform as output. Typically, a vocoder is used after a TTS model that converts an input text into a spectrogram.

The sampling frequency is 22050 Hz.


## Install SpeechBrain

```bash
pip install speechbrain
```

Please notice that we encourage you to read our tutorials and learn more about
[SpeechBrain](https://speechbrain.github.io).

### Using the Vocoder as reconstructor
```python
import torch
import torchaudio
import speechbrain as sb
from speechbrain.inference.vocoders import DiffWaveVocoder
from speechbrain.lobes.models.HifiGAN import mel_spectogram
from speechbrain.utils.fetching import fetch
from speechbrain.utils.data_utils import split_path

diffwave = DiffWaveVocoder.from_hparams(source="speechbrain/tts-diffwave-ljspeech", savedir="pretrained_models/tts-diffwave-ljspeech")

source, fl = split_path("speechbrain/tts-diffwave-ljspeech/LJ050-0075.wav")
path = fetch(fl, source=source, savedir="tmpdir")
audio, fs_file = torchaudio.load(path)
audio = torch.FloatTensor(audio)

mel = mel_spectogram(
    sample_rate=22050,
    hop_length=256,
    win_length=1024,
    n_fft=1024,
    n_mels=80,
    f_min=0,
    f_max=8000,
    power=1.0,
    normalized=False,
    norm="slaney",
    mel_scale="slaney",
    compression=True,
    audio=audio,
)

# Running Vocoder (spectrogram-to-waveform), a fast sampling can be realized by passing user-defined variance schedules. According to the paper, high-quality audios can be generated with only 6 steps (instead of a total of 50).
waveforms = diffwave.decode_batch(
    mel,
    hop_len=256,  # upsample factor, should be the same as "hop_len" during the extraction of mel-spectrogram
    fast_sampling=True,  # fast sampling is highly recommanded
    fast_sampling_noise_schedule=[0.0001, 0.001, 0.01, 0.05, 0.2, 0.5],  # customized noise schedule 
)

torchaudio.save('reconstructed.wav', waveforms.squeeze(1), 22050)
```

### Using the Vocoder with TTS
```python
import torchaudio
from speechbrain.pretrained import FastSpeech2
from speechbrain.pretrained import DiffWaveVocoder

# Intialize TTS (FastSpeech2) and Vocoder (DiffWave)
fastspeech2 = FastSpeech2.from_hparams(source="speechbrain/tts-fastspeech2-ljspeech", savedir="pretrained_models/tts-fastspeech2-ljspeech")
diffwave = DiffWaveVocoder.from_hparams(source="speechbrain/tts-diffwave-ljspeech", savedir="pretrained_models/tts-diffwave-ljspeech")

input_text = "This is a test run with FastSpeech and DiffWave."

# Running the TTS
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), a fast sampling can be realized by passing user-defined variance schedules. According to the paper, high-quality audios can be generated with only 6 steps (instead of a total of 50).
waveforms = diffwave.decode_batch(
    mel_output,
    hop_len=256,  # upsample factor, should be the same as "hop_len" during the extraction of mel-spectrogram
    fast_sampling=True,  # fast sampling is highly recommanded
    fast_sampling_noise_schedule=[0.0001, 0.001, 0.01, 0.05, 0.2, 0.5],  # customized noise schedule 
)

# Save the waverform
torchaudio.save('example_TTS.wav',waveforms.squeeze(1), 22050)
```

### Inference on GPU
To perform inference on the GPU, add  `run_opts={"device":"cuda"}`  when calling the `from_hparams` method.

### Training
The model was trained with SpeechBrain.
To train it from scratch follow these steps:
1. Clone SpeechBrain:
```bash
git clone https://github.com/speechbrain/speechbrain/
```
2. Install it:
```bash
cd speechbrain
pip install -r requirements.txt
pip install -e .
```
3. Run Training:
```bash
cd recipes/LJSpeech/TTS/vocoder/diffwave/
python train.py hparams/train.yaml --data_folder /path/to/LJspeech
```
You can find our training results (models, logs, etc) [here](https://www.dropbox.com/sh/tbhpn1xirtaix68/AACvYaVDiUGAKURf2o-fvgMoa?dl=0).


### Limitations
The SpeechBrain team does not provide any warranty on the performance achieved by this model when used on other datasets.

# **About SpeechBrain**
- Website: https://speechbrain.github.io/
- Code: https://github.com/speechbrain/speechbrain/
- HuggingFace: https://huggingface.co/speechbrain/


# **Citing SpeechBrain**
Please, cite SpeechBrain if you use it for your research or business.

```bibtex
@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}
}
```