class_docstr: A ready-to-use wrapper for Fastspeech2 (text -> mel_spec).
  Arguments
  ---------
  hparams
      Hyperparameters (from HyperPyYAML)
  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)
  >>> # Intialize 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
docstr: 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
docstr_tok: ["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"]
code: 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,
      )
  
imports: import re
  import logging
  import torch
  import torchaudio
  import random
  import speechbrain
  from speechbrain.utils.fetching import fetch
  from speechbrain.inference.interfaces import Pretrained
  from speechbrain.utils.text_to_sequence import text_to_sequence
  from speechbrain.inference.text import GraphemeToPhoneme
  from speechbrain.inference.encoders import MelSpectrogramEncoder
  from speechbrain.inference.classifiers import EncoderClassifier
label_desc: Use image node_id 2 for calling the FastSpeech2 obj's underlying member method code with example usage: obj.encode_text(texts, pace, pitch_rate, energy_rate) and returns: self