demo_cli.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import argparse
  2. import os
  3. from pathlib import Path
  4. import librosa
  5. import numpy as np
  6. import soundfile as sf
  7. import torch
  8. from encoder import inference as encoder
  9. from encoder.params_model import model_embedding_size as speaker_embedding_size
  10. from synthesizer.inference import Synthesizer
  11. from utils.argutils import print_args
  12. from utils.default_models import ensure_default_models
  13. from vocoder import inference as vocoder
  14. if __name__ == '__main__':
  15. parser = argparse.ArgumentParser(
  16. formatter_class=argparse.ArgumentDefaultsHelpFormatter
  17. )
  18. parser.add_argument("-e", "--enc_model_fpath", type=Path,
  19. default="saved_models/default/encoder.pt",
  20. help="Path to a saved encoder")
  21. parser.add_argument("-s", "--syn_model_fpath", type=Path,
  22. default="saved_models/default/synthesizer.pt",
  23. help="Path to a saved synthesizer")
  24. parser.add_argument("-v", "--voc_model_fpath", type=Path,
  25. default="saved_models/default/vocoder.pt",
  26. help="Path to a saved vocoder")
  27. parser.add_argument("--cpu", action="store_true", help=\
  28. "If True, processing is done on CPU, even when a GPU is available.")
  29. parser.add_argument("--no_sound", action="store_true", help=\
  30. "If True, audio won't be played.")
  31. parser.add_argument("--seed", type=int, default=None, help=\
  32. "Optional random number seed value to make toolbox deterministic.")
  33. args = parser.parse_args()
  34. arg_dict = vars(args)
  35. print_args(args, parser)
  36. # Hide GPUs from Pytorch to force CPU processing
  37. if arg_dict.pop("cpu"):
  38. os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
  39. print("Running a test of your configuration...\n")
  40. if torch.cuda.is_available():
  41. device_id = torch.cuda.current_device()
  42. gpu_properties = torch.cuda.get_device_properties(device_id)
  43. ## Print some environment information (for debugging purposes)
  44. print("Found %d GPUs available. Using GPU %d (%s) of compute capability %d.%d with "
  45. "%.1fGb total memory.\n" %
  46. (torch.cuda.device_count(),
  47. device_id,
  48. gpu_properties.name,
  49. gpu_properties.major,
  50. gpu_properties.minor,
  51. gpu_properties.total_memory / 1e9))
  52. else:
  53. print("Using CPU for inference.\n")
  54. ## Load the models one by one.
  55. print("Preparing the encoder, the synthesizer and the vocoder...")
  56. ensure_default_models(Path("saved_models"))
  57. encoder.load_model(args.enc_model_fpath)
  58. synthesizer = Synthesizer(args.syn_model_fpath)
  59. vocoder.load_model(args.voc_model_fpath)
  60. ## Run a test
  61. print("Testing your configuration with small inputs.")
  62. # Forward an audio waveform of zeroes that lasts 1 second. Notice how we can get the encoder's
  63. # sampling rate, which may differ.
  64. # If you're unfamiliar with digital audio, know that it is encoded as an array of floats
  65. # (or sometimes integers, but mostly floats in this projects) ranging from -1 to 1.
  66. # The sampling rate is the number of values (samples) recorded per second, it is set to
  67. # 16000 for the encoder. Creating an array of length <sampling_rate> will always correspond
  68. # to an audio of 1 second.
  69. print("\tTesting the encoder...")
  70. encoder.embed_utterance(np.zeros(encoder.sampling_rate))
  71. # Create a dummy embedding. You would normally use the embedding that encoder.embed_utterance
  72. # returns, but here we're going to make one ourselves just for the sake of showing that it's
  73. # possible.
  74. embed = np.random.rand(speaker_embedding_size)
  75. # Embeddings are L2-normalized (this isn't important here, but if you want to make your own
  76. # embeddings it will be).
  77. embed /= np.linalg.norm(embed)
  78. # The synthesizer can handle multiple inputs with batching. Let's create another embedding to
  79. # illustrate that
  80. embeds = [embed, np.zeros(speaker_embedding_size)]
  81. texts = ["test 1", "test 2"]
  82. print("\tTesting the synthesizer... (loading the model will output a lot of text)")
  83. mels = synthesizer.synthesize_spectrograms(texts, embeds)
  84. # The vocoder synthesizes one waveform at a time, but it's more efficient for long ones. We
  85. # can concatenate the mel spectrograms to a single one.
  86. mel = np.concatenate(mels, axis=1)
  87. # The vocoder can take a callback function to display the generation. More on that later. For
  88. # now we'll simply hide it like this:
  89. no_action = lambda *args: None
  90. print("\tTesting the vocoder...")
  91. # For the sake of making this test short, we'll pass a short target length. The target length
  92. # is the length of the wav segments that are processed in parallel. E.g. for audio sampled
  93. # at 16000 Hertz, a target length of 8000 means that the target audio will be cut in chunks of
  94. # 0.5 seconds which will all be generated together. The parameters here are absurdly short, and
  95. # that has a detrimental effect on the quality of the audio. The default parameters are
  96. # recommended in general.
  97. vocoder.infer_waveform(mel, target=200, overlap=50, progress_callback=no_action)
  98. print("All test passed! You can now synthesize speech.\n\n")
  99. ## Interactive speech generation
  100. print("This is a GUI-less example of interface to SV2TTS. The purpose of this script is to "
  101. "show how you can interface this project easily with your own. See the source code for "
  102. "an explanation of what is happening.\n")
  103. print("Interactive generation loop")
  104. num_generated = 0
  105. while True:
  106. try:
  107. # Get the reference audio filepath
  108. message = "Reference voice: enter an audio filepath of a voice to be cloned (mp3, " \
  109. "wav, m4a, flac, ...):\n"
  110. in_fpath = Path(input(message).replace("\"", "").replace("\'", ""))
  111. ## Computing the embedding
  112. # First, we load the wav using the function that the speaker encoder provides. This is
  113. # important: there is preprocessing that must be applied.
  114. # The following two methods are equivalent:
  115. # - Directly load from the filepath:
  116. preprocessed_wav = encoder.preprocess_wav(in_fpath)
  117. # - If the wav is already loaded:
  118. original_wav, sampling_rate = librosa.load(str(in_fpath))
  119. preprocessed_wav = encoder.preprocess_wav(original_wav, sampling_rate)
  120. print("Loaded file succesfully")
  121. # Then we derive the embedding. There are many functions and parameters that the
  122. # speaker encoder interfaces. These are mostly for in-depth research. You will typically
  123. # only use this function (with its default parameters):
  124. embed = encoder.embed_utterance(preprocessed_wav)
  125. print("Created the embedding")
  126. ## Generating the spectrogram
  127. text = input("Write a sentence (+-20 words) to be synthesized:\n")
  128. # If seed is specified, reset torch seed and force synthesizer reload
  129. if args.seed is not None:
  130. torch.manual_seed(args.seed)
  131. synthesizer = Synthesizer(args.syn_model_fpath)
  132. # The synthesizer works in batch, so you need to put your data in a list or numpy array
  133. texts = [text]
  134. embeds = [embed]
  135. # If you know what the attention layer alignments are, you can retrieve them here by
  136. # passing return_alignments=True
  137. specs = synthesizer.synthesize_spectrograms(texts, embeds)
  138. spec = specs[0]
  139. print("Created the mel spectrogram")
  140. ## Generating the waveform
  141. print("Synthesizing the waveform:")
  142. # If seed is specified, reset torch seed and reload vocoder
  143. if args.seed is not None:
  144. torch.manual_seed(args.seed)
  145. vocoder.load_model(args.voc_model_fpath)
  146. # Synthesizing the waveform is fairly straightforward. Remember that the longer the
  147. # spectrogram, the more time-efficient the vocoder.
  148. generated_wav = vocoder.infer_waveform(spec)
  149. ## Post-generation
  150. # There's a bug with sounddevice that makes the audio cut one second earlier, so we
  151. # pad it.
  152. generated_wav = np.pad(generated_wav, (0, synthesizer.sample_rate), mode="constant")
  153. # Trim excess silences to compensate for gaps in spectrograms (issue #53)
  154. generated_wav = encoder.preprocess_wav(generated_wav)
  155. # Play the audio (non-blocking)
  156. if not args.no_sound:
  157. import sounddevice as sd
  158. try:
  159. sd.stop()
  160. sd.play(generated_wav, synthesizer.sample_rate)
  161. except sd.PortAudioError as e:
  162. print("\nCaught exception: %s" % repr(e))
  163. print("Continuing without audio playback. Suppress this message with the \"--no_sound\" flag.\n")
  164. except:
  165. raise
  166. # Save it on the disk
  167. filename = "demo_output_%02d.wav" % num_generated
  168. print(generated_wav.dtype)
  169. sf.write(filename, generated_wav.astype(np.float32), synthesizer.sample_rate)
  170. num_generated += 1
  171. print("\nSaved output as %s\n\n" % filename)
  172. except Exception as e:
  173. print("Caught exception: %s" % repr(e))
  174. print("Restarting\n")