-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathTTS.py
More file actions
928 lines (787 loc) · 30.9 KB
/
TTS.py
File metadata and controls
928 lines (787 loc) · 30.9 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
"""Specifies the inference interfaces for Text-To-Speech (TTS) modules.
Authors:
* Aku Rouhe 2021
* Peter Plantinga 2021
* Loren Lugosch 2020
* Mirco Ravanelli 2020
* Titouan Parcollet 2021
* Abdel Heba 2021
* Andreas Nautsch 2022, 2023
* Pooneh Mousavi 2023
* Sylvain de Langen 2023
* Adel Moumen 2023
* Pradnya Kandarkar 2023
"""
import random
import re
import torch
import torchaudio
import speechbrain
from speechbrain.dataio import audio_io
from speechbrain.inference.classifiers import EncoderClassifier
from speechbrain.inference.encoders import MelSpectrogramEncoder
from speechbrain.inference.interfaces import Pretrained
from speechbrain.inference.text import GraphemeToPhoneme
from speechbrain.utils.fetching import fetch
from speechbrain.utils.logger import get_logger
from speechbrain.utils.text_to_sequence import text_to_sequence
logger = get_logger(__name__)
class Tacotron2(Pretrained):
"""
A ready-to-use wrapper for Tacotron2 (text -> mel_spec).
Arguments
---------
*args : tuple
**kwargs : dict
Arguments are forwarded to ``Pretrained`` parent class.
Example
-------
>>> tmpdir_tts = getfixture("tmpdir") / "tts"
>>> tacotron2 = Tacotron2.from_hparams(
... source="speechbrain/tts-tacotron2-ljspeech", savedir=tmpdir_tts
... )
>>> mel_output, mel_length, alignment = tacotron2.encode_text(
... "Mary had a little lamb"
... )
>>> items = [
... "A quick brown fox jumped over the lazy dog",
... "How much wood would a woodchuck chuck?",
... "Never odd or even",
... ]
>>> mel_outputs, mel_lengths, alignments = tacotron2.encode_batch(items)
>>> # One can combine the TTS model with a vocoder (that generates the final waveform)
>>> # Initialize the Vocoder (HiFIGAN)
>>> tmpdir_vocoder = getfixture("tmpdir") / "vocoder"
>>> from speechbrain.inference.vocoders import HIFIGAN
>>> hifi_gan = HIFIGAN.from_hparams(
... source="speechbrain/tts-hifigan-ljspeech", savedir=tmpdir_vocoder
... )
>>> # Running the TTS
>>> mel_output, mel_length, alignment = tacotron2.encode_text(
... "Mary had a little lamb"
... )
>>> # Running Vocoder (spectrogram-to-waveform)
>>> waveforms = hifi_gan.decode_batch(mel_output)
"""
HPARAMS_NEEDED = ["model", "text_to_sequence"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.text_cleaners = getattr(
self.hparams, "text_cleaners", ["english_cleaners"]
)
self.infer = self.hparams.model.infer
def text_to_seq(self, txt):
"""Encodes raw text into a tensor with a customer text-to-sequence function"""
sequence = self.hparams.text_to_sequence(txt, self.text_cleaners)
return sequence, len(sequence)
def encode_batch(self, texts):
"""Computes mel-spectrogram for a list of texts
Texts must be sorted in decreasing order on their lengths
Arguments
---------
texts: List[str]
texts to be encoded into spectrogram
Returns
-------
tensors of output spectrograms, output lengths and alignments
"""
with torch.no_grad():
inputs = [
{
"text_sequences": torch.tensor(
self.text_to_seq(item)[0], device=self.device
)
}
for item in texts
]
inputs = speechbrain.dataio.batch.PaddedBatch(inputs)
lens = [self.text_to_seq(item)[1] for item in texts]
assert lens == sorted(lens, reverse=True), (
"input lengths must be sorted in decreasing order"
)
input_lengths = torch.tensor(lens, device=self.device)
mel_outputs_postnet, mel_lengths, alignments = self.infer(
inputs.text_sequences.data, input_lengths
)
return mel_outputs_postnet, mel_lengths, alignments
def encode_text(self, text):
"""Runs inference for a single text str"""
return self.encode_batch([text])
def forward(self, texts):
"Encodes the input texts."
return self.encode_batch(texts)
class MSTacotron2(Pretrained):
"""
A ready-to-use wrapper for Zero-Shot Multi-Speaker Tacotron2.
For voice cloning: (text, reference_audio) -> (mel_spec).
For generating a random speaker voice: (text) -> (mel_spec).
Arguments
---------
*args : tuple
**kwargs : dict
Arguments are forwarded to ``Pretrained`` parent class.
Example
-------
>>> tmpdir_tts = getfixture("tmpdir") / "tts"
>>> mstacotron2 = MSTacotron2.from_hparams(
... source="speechbrain/tts-mstacotron2-libritts", savedir=tmpdir_tts
... ) # doctest: +SKIP
>>> # Sample rate of the reference audio must be greater or equal to the sample rate of the speaker embedding model
>>> reference_audio_path = "tests/samples/single-mic/example1.wav"
>>> input_text = "Mary had a little lamb."
>>> mel_output, mel_length, alignment = mstacotron2.clone_voice(
... input_text, reference_audio_path
... ) # doctest: +SKIP
>>> # One can combine the TTS model with a vocoder (that generates the final waveform)
>>> # Initialize the Vocoder (HiFIGAN)
>>> tmpdir_vocoder = getfixture("tmpdir") / "vocoder"
>>> from speechbrain.inference.vocoders import HIFIGAN
>>> hifi_gan = HIFIGAN.from_hparams(
... source="speechbrain/tts-hifigan-libritts-22050Hz",
... savedir=tmpdir_vocoder,
... ) # doctest: +SKIP
>>> # Running the TTS
>>> mel_output, mel_length, alignment = mstacotron2.clone_voice(
... input_text, reference_audio_path
... ) # doctest: +SKIP
>>> # Running Vocoder (spectrogram-to-waveform)
>>> waveforms = hifi_gan.decode_batch(mel_output) # doctest: +SKIP
>>> # For generating a random speaker voice, use the following
>>> mel_output, mel_length, alignment = mstacotron2.generate_random_voice(
... input_text
... ) # doctest: +SKIP
"""
HPARAMS_NEEDED = ["model"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.text_cleaners = ["english_cleaners"]
self.infer = self.hparams.model.infer
self.custom_mel_spec_encoder = self.hparams.custom_mel_spec_encoder
self.g2p = GraphemeToPhoneme.from_hparams(
self.hparams.g2p, run_opts={"device": self.device}
)
self.spk_emb_encoder = None
if self.custom_mel_spec_encoder:
self.spk_emb_encoder = MelSpectrogramEncoder.from_hparams(
source=self.hparams.spk_emb_encoder,
run_opts={"device": self.device},
)
else:
self.spk_emb_encoder = EncoderClassifier.from_hparams(
source=self.hparams.spk_emb_encoder,
run_opts={"device": self.device},
)
def __text_to_seq(self, txt):
"""Encodes raw text into a tensor with a customer text-to-sequence function"""
sequence = text_to_sequence(txt, self.text_cleaners)
return sequence, len(sequence)
def clone_voice(self, texts, audio_path):
"""
Generates mel-spectrogram using input text and reference audio
Arguments
---------
texts : str or list
Input text
audio_path : str
Reference audio
Returns
-------
tensors of output spectrograms, output lengths and alignments
"""
# Loads audio
ref_signal, signal_sr = audio_io.load(audio_path)
# Resamples the audio if required
if signal_sr != self.hparams.spk_emb_sample_rate:
ref_signal = torchaudio.functional.resample(
ref_signal, signal_sr, self.hparams.spk_emb_sample_rate
)
ref_signal = ref_signal.to(self.device)
# Computes speaker embedding
if self.custom_mel_spec_encoder:
spk_emb = self.spk_emb_encoder.encode_waveform(ref_signal)
else:
spk_emb = self.spk_emb_encoder.encode_batch(ref_signal)
spk_emb = spk_emb.squeeze(0)
# Converts input texts into the corresponding phoneme sequences
if isinstance(texts, str):
texts = [texts]
phoneme_seqs = self.g2p(texts)
for i in range(len(phoneme_seqs)):
phoneme_seqs[i] = " ".join(phoneme_seqs[i])
phoneme_seqs[i] = "{" + phoneme_seqs[i] + "}"
# Repeats the speaker embedding to match the number of input texts
spk_embs = spk_emb.repeat(len(texts), 1)
# Calls __encode_batch to generate the mel-spectrograms
return self.__encode_batch(phoneme_seqs, spk_embs)
def generate_random_voice(self, texts):
"""
Generates mel-spectrogram using input text and a random speaker voice
Arguments
---------
texts : str or list
Input text
Returns
-------
tensors of output spectrograms, output lengths and alignments
"""
spk_emb = self.__sample_random_speaker().float()
spk_emb = spk_emb.to(self.device)
# Converts input texts into the corresponding phoneme sequences
if isinstance(texts, str):
texts = [texts]
phoneme_seqs = self.g2p(texts)
for i in range(len(phoneme_seqs)):
phoneme_seqs[i] = " ".join(phoneme_seqs[i])
phoneme_seqs[i] = "{" + phoneme_seqs[i] + "}"
# Repeats the speaker embedding to match the number of input texts
spk_embs = spk_emb.repeat(len(texts), 1)
# Calls __encode_batch to generate the mel-spectrograms
return self.__encode_batch(phoneme_seqs, spk_embs)
def __encode_batch(self, texts, spk_embs):
"""Computes mel-spectrograms for a list of texts
Texts are sorted in decreasing order on their lengths
Arguments
---------
texts: List[str]
texts to be encoded into spectrogram
spk_embs: torch.Tensor
speaker embeddings
Returns
-------
tensors of output spectrograms, output lengths and alignments
"""
with torch.no_grad():
inputs = [
{
"text_sequences": torch.tensor(
self.__text_to_seq(item)[0], device=self.device
)
}
for item in texts
]
inputs = sorted(
inputs,
key=lambda x: x["text_sequences"].size()[0],
reverse=True,
)
lens = [entry["text_sequences"].size()[0] for entry in inputs]
inputs = speechbrain.dataio.batch.PaddedBatch(inputs)
assert lens == sorted(lens, reverse=True), (
"input lengths must be sorted in decreasing order"
)
input_lengths = torch.tensor(lens, device=self.device)
mel_outputs_postnet, mel_lengths, alignments = self.infer(
inputs.text_sequences.data, spk_embs, input_lengths
)
return mel_outputs_postnet, mel_lengths, alignments
def __sample_random_speaker(self):
"""Samples a random speaker embedding from a pretrained GMM
Returns
-------
x: torch.Tensor
A randomly sampled speaker embedding
"""
# Fetches and Loads GMM trained on speaker embeddings
speaker_gmm_local_path = fetch(
filename=self.hparams.random_speaker_sampler,
source=self.hparams.random_speaker_sampler_source,
savedir=self.hparams.pretrainer.collect_in,
)
random_speaker_gmm = torch.load(speaker_gmm_local_path)
gmm_n_components = random_speaker_gmm["gmm_n_components"]
gmm_means = random_speaker_gmm["gmm_means"]
gmm_covariances = random_speaker_gmm["gmm_covariances"]
# Randomly selects a speaker
counts = torch.zeros(gmm_n_components)
counts[random.randint(0, gmm_n_components - 1)] = 1
x = torch.empty(0, device=counts.device)
# Samples an embedding for the speaker
for k in torch.arange(gmm_n_components)[counts > 0]:
# Considers full covariance type
d_k = torch.distributions.multivariate_normal.MultivariateNormal(
gmm_means[k], gmm_covariances[k]
)
x_k = torch.stack([d_k.sample() for _ in range(int(counts[k]))])
x = torch.cat((x, x_k), dim=0)
return x
class FastSpeech2(Pretrained):
"""
A ready-to-use wrapper for Fastspeech2 (text -> mel_spec).
Arguments
---------
*args : tuple
**kwargs : dict
Arguments are forwarded to ``Pretrained`` parent class.
Example
-------
>>> tmpdir_tts = getfixture("tmpdir") / "tts"
>>> fastspeech2 = FastSpeech2.from_hparams(
... source="speechbrain/tts-fastspeech2-ljspeech", savedir=tmpdir_tts
... ) # doctest: +SKIP
>>> mel_outputs, durations, pitch, energy = fastspeech2.encode_text(
... ["Mary had a little lamb."]
... ) # doctest: +SKIP
>>> 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
... ) # doctest: +SKIP
>>>
>>> # One can combine the TTS model with a vocoder (that generates the final waveform)
>>> # Initialize the Vocoder (HiFIGAN)
>>> tmpdir_vocoder = getfixture("tmpdir") / "vocoder"
>>> from speechbrain.inference.vocoders import HIFIGAN
>>> hifi_gan = HIFIGAN.from_hparams(
... source="speechbrain/tts-hifigan-ljspeech", savedir=tmpdir_vocoder
... ) # doctest: +SKIP
>>> # Running the TTS
>>> mel_outputs, durations, pitch, energy = fastspeech2.encode_text(
... ["Mary had a little lamb."]
... ) # doctest: +SKIP
>>> # Running Vocoder (spectrogram-to-waveform)
>>> waveforms = hifi_gan.decode_batch(mel_outputs) # doctest: +SKIP
"""
HPARAMS_NEEDED = ["spn_predictor", "model", "input_encoder"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
lexicon = self.hparams.lexicon
lexicon = ["@@"] + lexicon
self.input_encoder = self.hparams.input_encoder
self.input_encoder.update_from_iterable(lexicon, sequence_input=False)
self.input_encoder.add_unk()
self.g2p = GraphemeToPhoneme.from_hparams("speechbrain/soundchoice-g2p")
self.spn_token_encoded = (
self.input_encoder.encode_sequence_torch(["spn"]).int().item()
)
def encode_text(self, texts, pace=1.0, pitch_rate=1.0, energy_rate=1.0):
"""Computes mel-spectrogram for a list of texts
Arguments
---------
texts: List[str]
texts to be converted to spectrogram
pace: float
pace for the speech synthesis
pitch_rate : float
scaling factor for phoneme pitches
energy_rate : float
scaling factor for phoneme energies
Returns
-------
tensors of output spectrograms, output lengths and alignments
"""
# Preprocessing required at the inference time for the input text
# "label" below contains input text
# "phoneme_labels" contain the phoneme sequences corresponding to input text labels
# "last_phonemes_combined" is used to indicate whether the index position is for a last phoneme of a word
# "punc_positions" is used to add back the silence for punctuations
phoneme_labels = list()
last_phonemes_combined = list()
punc_positions = list()
for label in texts:
phoneme_label = list()
last_phonemes = list()
punc_position = list()
words = label.split()
words = [word.strip() for word in words]
words_phonemes = self.g2p(words)
for i in range(len(words_phonemes)):
words_phonemes_seq = words_phonemes[i]
for phoneme in words_phonemes_seq:
if not phoneme.isspace():
phoneme_label.append(phoneme)
last_phonemes.append(0)
punc_position.append(0)
last_phonemes[-1] = 1
if words[i][-1] in ":;-,.!?":
punc_position[-1] = 1
phoneme_labels.append(phoneme_label)
last_phonemes_combined.append(last_phonemes)
punc_positions.append(punc_position)
# Inserts silent phonemes in the input phoneme sequence
all_tokens_with_spn = list()
max_seq_len = -1
for i in range(len(phoneme_labels)):
phoneme_label = phoneme_labels[i]
token_seq = (
self.input_encoder.encode_sequence_torch(phoneme_label)
.int()
.to(self.device)
)
last_phonemes = torch.LongTensor(last_phonemes_combined[i]).to(
self.device
)
# Runs the silent phoneme predictor
spn_preds = (
self.hparams.modules["spn_predictor"]
.infer(token_seq.unsqueeze(0), last_phonemes.unsqueeze(0))
.int()
)
spn_to_add = torch.nonzero(spn_preds).reshape(-1).tolist()
for j in range(len(punc_positions[i])):
if punc_positions[i][j] == 1:
spn_to_add.append(j)
tokens_with_spn = list()
for token_idx in range(token_seq.shape[0]):
tokens_with_spn.append(token_seq[token_idx].item())
if token_idx in spn_to_add:
tokens_with_spn.append(self.spn_token_encoded)
tokens_with_spn = torch.LongTensor(tokens_with_spn).to(self.device)
all_tokens_with_spn.append(tokens_with_spn)
if max_seq_len < tokens_with_spn.shape[-1]:
max_seq_len = tokens_with_spn.shape[-1]
# "tokens_with_spn_tensor" holds the input phoneme sequence with silent phonemes
tokens_with_spn_tensor_padded = torch.LongTensor(
len(texts), max_seq_len
).to(self.device)
tokens_with_spn_tensor_padded.zero_()
for seq_idx, seq in enumerate(all_tokens_with_spn):
tokens_with_spn_tensor_padded[seq_idx, : len(seq)] = seq
return self.encode_batch(
tokens_with_spn_tensor_padded,
pace=pace,
pitch_rate=pitch_rate,
energy_rate=energy_rate,
)
def encode_phoneme(
self, phonemes, pace=1.0, pitch_rate=1.0, energy_rate=1.0
):
"""Computes mel-spectrogram for a list of phoneme sequences
Arguments
---------
phonemes: List[List[str]]
phonemes to be converted to spectrogram
pace: float
pace for the speech synthesis
pitch_rate : float
scaling factor for phoneme pitches
energy_rate : float
scaling factor for phoneme energies
Returns
-------
tensors of output spectrograms, output lengths and alignments
"""
all_tokens = []
max_seq_len = -1
for phoneme in phonemes:
token_seq = (
self.input_encoder.encode_sequence_torch(phoneme)
.int()
.to(self.device)
)
if max_seq_len < token_seq.shape[-1]:
max_seq_len = token_seq.shape[-1]
all_tokens.append(token_seq)
tokens_padded = torch.LongTensor(len(phonemes), max_seq_len).to(
self.device
)
tokens_padded.zero_()
for seq_idx, seq in enumerate(all_tokens):
tokens_padded[seq_idx, : len(seq)] = seq
return self.encode_batch(
tokens_padded,
pace=pace,
pitch_rate=pitch_rate,
energy_rate=energy_rate,
)
def encode_batch(
self, tokens_padded, pace=1.0, pitch_rate=1.0, energy_rate=1.0
):
"""Batch inference for a tensor of phoneme sequences
Arguments
---------
tokens_padded : torch.Tensor
A sequence of encoded phonemes to be converted to spectrogram
pace : float
pace for the speech synthesis
pitch_rate : float
scaling factor for phoneme pitches
energy_rate : float
scaling factor for phoneme energies
Returns
-------
post_mel_outputs : torch.Tensor
durations : torch.Tensor
pitch : torch.Tensor
energy : torch.Tensor
"""
with torch.no_grad():
(
_,
post_mel_outputs,
durations,
pitch,
_,
energy,
_,
_,
) = self.hparams.model(
tokens_padded,
pace=pace,
pitch_rate=pitch_rate,
energy_rate=energy_rate,
)
# Transposes to make in compliant with HiFI GAN expected format
post_mel_outputs = post_mel_outputs.transpose(-1, 1)
return post_mel_outputs, durations, pitch, energy
def forward(self, text, pace=1.0, pitch_rate=1.0, energy_rate=1.0):
"""Batch inference for a tensor of phoneme sequences
Arguments
---------
text : str
A text to be converted to spectrogram
pace : float
pace for the speech synthesis
pitch_rate : float
scaling factor for phoneme pitches
energy_rate : float
scaling factor for phoneme energies
Returns
-------
Encoded text
"""
return self.encode_text(
[text], pace=pace, pitch_rate=pitch_rate, energy_rate=energy_rate
)
class FastSpeech2InternalAlignment(Pretrained):
"""
A ready-to-use wrapper for Fastspeech2 with internal alignment(text -> mel_spec).
Arguments
---------
*args : tuple
**kwargs : dict
Arguments are forwarded to ``Pretrained`` parent class.
Example
-------
>>> tmpdir_tts = getfixture("tmpdir") / "tts"
>>> fastspeech2 = FastSpeech2InternalAlignment.from_hparams(
... source="speechbrain/tts-fastspeech2-internal-alignment-ljspeech",
... savedir=tmpdir_tts,
... ) # doctest: +SKIP
>>> mel_outputs, durations, pitch, energy = fastspeech2.encode_text(
... ["Mary had a little lamb."]
... ) # doctest: +SKIP
>>> 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
... ) # doctest: +SKIP
>>> # One can combine the TTS model with a vocoder (that generates the final waveform)
>>> # Initialize the Vocoder (HiFIGAN)
>>> tmpdir_vocoder = getfixture("tmpdir") / "vocoder"
>>> from speechbrain.inference.vocoders import HIFIGAN
>>> hifi_gan = HIFIGAN.from_hparams(
... source="speechbrain/tts-hifigan-ljspeech", savedir=tmpdir_vocoder
... ) # doctest: +SKIP
>>> # Running the TTS
>>> mel_outputs, durations, pitch, energy = fastspeech2.encode_text(
... ["Mary had a little lamb."]
... ) # doctest: +SKIP
>>> # Running Vocoder (spectrogram-to-waveform)
>>> waveforms = hifi_gan.decode_batch(mel_outputs) # doctest: +SKIP
"""
HPARAMS_NEEDED = ["model", "input_encoder"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
lexicon = self.hparams.lexicon
lexicon = ["@@"] + lexicon
self.input_encoder = self.hparams.input_encoder
self.input_encoder.update_from_iterable(lexicon, sequence_input=False)
self.input_encoder.add_unk()
self.g2p = GraphemeToPhoneme.from_hparams("speechbrain/soundchoice-g2p")
def encode_text(self, texts, pace=1.0, pitch_rate=1.0, energy_rate=1.0):
"""Computes mel-spectrogram for a list of texts
Arguments
---------
texts: List[str]
texts to be converted to spectrogram
pace: float
pace for the speech synthesis
pitch_rate : float
scaling factor for phoneme pitches
energy_rate : float
scaling factor for phoneme energies
Returns
-------
tensors of output spectrograms, output lengths and alignments
"""
# Preprocessing required at the inference time for the input text
# "label" below contains input text
# "phoneme_labels" contain the phoneme sequences corresponding to input text labels
phoneme_labels = list()
max_seq_len = -1
for label in texts:
phonemes_with_punc = self._g2p_keep_punctuations(self.g2p, label)
if max_seq_len < len(phonemes_with_punc):
max_seq_len = len(phonemes_with_punc)
token_seq = (
self.input_encoder.encode_sequence_torch(phonemes_with_punc)
.int()
.to(self.device)
)
phoneme_labels.append(token_seq)
tokens_padded = torch.LongTensor(len(texts), max_seq_len).to(
self.device
)
tokens_padded.zero_()
for seq_idx, seq in enumerate(phoneme_labels):
tokens_padded[seq_idx, : len(seq)] = seq
return self.encode_batch(
tokens_padded,
pace=pace,
pitch_rate=pitch_rate,
energy_rate=energy_rate,
)
def _g2p_keep_punctuations(self, g2p_model, text):
"""do grapheme to phoneme and keep the punctuations between the words"""
# find the words where a "-" or "'" or "." or ":" appears in the middle
special_words = re.findall(r"\w+[-':\.][-':\.\w]*\w+", text)
# remove intra-word punctuations ("-':."), this does not change the output of speechbrain g2p
for special_word in special_words:
rmp = special_word.replace("-", "")
rmp = rmp.replace("'", "")
rmp = rmp.replace(":", "")
rmp = rmp.replace(".", "")
text = text.replace(special_word, rmp)
# keep inter-word punctuations
all_ = re.findall(r"[\w]+|[-!'(),.:;? ]", text)
try:
phonemes = g2p_model(text)
except RuntimeError:
logger.info(f"error with text: {text}")
quit()
word_phonemes = "-".join(phonemes).split(" ")
phonemes_with_punc = []
count = 0
try:
# if the g2p model splits the words correctly
for i in all_:
if i not in "-!'(),.:;? ":
phonemes_with_punc.extend(word_phonemes[count].split("-"))
count += 1
else:
phonemes_with_punc.append(i)
except IndexError:
# sometimes the g2p model cannot split the words correctly
logger.warning(
f"Do g2p word by word because of unexpected outputs from g2p for text: {text}"
)
for i in all_:
if i not in "-!'(),.:;? ":
p = g2p_model.g2p(i)
p_without_space = [i for i in p if i != " "]
phonemes_with_punc.extend(p_without_space)
else:
phonemes_with_punc.append(i)
while "" in phonemes_with_punc:
phonemes_with_punc.remove("")
return phonemes_with_punc
def encode_phoneme(
self, phonemes, pace=1.0, pitch_rate=1.0, energy_rate=1.0
):
"""Computes mel-spectrogram for a list of phoneme sequences
Arguments
---------
phonemes: List[List[str]]
phonemes to be converted to spectrogram
pace: float
pace for the speech synthesis
pitch_rate : float
scaling factor for phoneme pitches
energy_rate : float
scaling factor for phoneme energies
Returns
-------
tensors of output spectrograms, output lengths and alignments
"""
all_tokens = []
max_seq_len = -1
for phoneme in phonemes:
token_seq = (
self.input_encoder.encode_sequence_torch(phoneme)
.int()
.to(self.device)
)
if max_seq_len < token_seq.shape[-1]:
max_seq_len = token_seq.shape[-1]
all_tokens.append(token_seq)
tokens_padded = torch.LongTensor(len(phonemes), max_seq_len).to(
self.device
)
tokens_padded.zero_()
for seq_idx, seq in enumerate(all_tokens):
tokens_padded[seq_idx, : len(seq)] = seq
return self.encode_batch(
tokens_padded,
pace=pace,
pitch_rate=pitch_rate,
energy_rate=energy_rate,
)
def encode_batch(
self, tokens_padded, pace=1.0, pitch_rate=1.0, energy_rate=1.0
):
"""Batch inference for a tensor of phoneme sequences
Arguments
---------
tokens_padded : torch.Tensor
A sequence of encoded phonemes to be converted to spectrogram
pace : float
pace for the speech synthesis
pitch_rate : float
scaling factor for phoneme pitches
energy_rate : float
scaling factor for phoneme energies
Returns
-------
post_mel_outputs : torch.Tensor
durations : torch.Tensor
pitch : torch.Tensor
energy : torch.Tensor
"""
with torch.no_grad():
(
_,
post_mel_outputs,
durations,
pitch,
_,
energy,
_,
_,
_,
_,
_,
_,
) = self.hparams.model(
tokens_padded,
pace=pace,
pitch_rate=pitch_rate,
energy_rate=energy_rate,
)
# Transposes to make in compliant with HiFI GAN expected format
post_mel_outputs = post_mel_outputs.transpose(-1, 1)
return post_mel_outputs, durations, pitch, energy
def forward(self, text, pace=1.0, pitch_rate=1.0, energy_rate=1.0):
"""Batch inference for a tensor of phoneme sequences
Arguments
---------
text : str
A text to be converted to spectrogram
pace : float
pace for the speech synthesis
pitch_rate : float
scaling factor for phoneme pitches
energy_rate : float
scaling factor for phoneme energies
Returns
-------
Encoded text
"""
return self.encode_text(
[text], pace=pace, pitch_rate=pitch_rate, energy_rate=energy_rate
)