Fork of https://github.com/alokprasad/fastspeech_squeezewave to also fix denoising in squeezewave
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
4.3 KiB

  1. # We retain the copyright notice by NVIDIA from the original code. However, we
  2. # we reserve our rights on the modifications based on the original code.
  3. #
  4. # *****************************************************************************
  5. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are met:
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. # * Neither the name of the NVIDIA CORPORATION nor the
  15. # names of its contributors may be used to endorse or promote products
  16. # derived from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. # ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  22. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #
  29. # *****************************************************************************
  30. import os
  31. from scipy.io.wavfile import write
  32. import torch
  33. from mel2samp import files_to_list, MAX_WAV_VALUE
  34. from denoiser import Denoiser
  35. import time
  36. def main(mel_files, squeezewave_path, sigma, output_dir, sampling_rate, is_fp16,
  37. denoiser_strength):
  38. mel_files = files_to_list(mel_files)
  39. #device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  40. device = torch.device('cpu')
  41. squeezewave = torch.load(squeezewave_path,map_location=device) ['model']
  42. squeezewave = squeezewave.remove_weightnorm(squeezewave)
  43. squeezewave.eval()
  44. if is_fp16:
  45. from apex import amp
  46. squeezewave, _ = amp.initialize(squeezewave,[],opt_level="O3")
  47. if denoiser_strength > 0:
  48. denoiser = Denoiser(squeezewave)
  49. start = time.time()
  50. for i, file_path in enumerate(mel_files):
  51. file_name = os.path.splitext(os.path.basename(file_path))[0]
  52. mel = torch.load(file_path,map_location=device)
  53. mel = torch.autograd.Variable(mel)
  54. mel = mel.half()
  55. with torch.no_grad():
  56. audio = squeezewave.infer(mel, sigma=sigma).float()
  57. if denoiser_strength > 0:
  58. audio = denoiser(audio, denoiser_strength)
  59. audio = audio * MAX_WAV_VALUE
  60. audio = audio.squeeze()
  61. audio = audio.cpu().numpy()
  62. audio = audio.astype('int16')
  63. audio_path = os.path.join(
  64. output_dir, "{}_synthesis.wav".format(file_name))
  65. write(audio_path, sampling_rate, audio)
  66. print(audio_path)
  67. end = time.time()
  68. print("Squeezewave vocoder time")
  69. print(end-start)
  70. if __name__ == "__main__":
  71. import argparse
  72. parser = argparse.ArgumentParser()
  73. parser.add_argument('-f', "--filelist_path", required=True)
  74. parser.add_argument('-w', '--squeezewave_path',
  75. help='Path to squeezewave decoder checkpoint with model')
  76. parser.add_argument('-o', "--output_dir", required=True)
  77. parser.add_argument("-s", "--sigma", default=1.0, type=float)
  78. parser.add_argument("--sampling_rate", default=22050, type=int)
  79. parser.add_argument("--is_fp16", action="store_true")
  80. parser.add_argument("-d", "--denoiser_strength", default=0.0, type=float,
  81. help='Removes model bias. Start with 0.1 and adjust')
  82. args = parser.parse_args()
  83. main(args.filelist_path, args.squeezewave_path, args.sigma, args.output_dir,
  84. args.sampling_rate, args.is_fp16, args.denoiser_strength)