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.

87 lines
4.1 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. def main(mel_files, squeezewave_path, sigma, output_dir, sampling_rate, is_fp16,
  36. denoiser_strength):
  37. mel_files = files_to_list(mel_files)
  38. squeezewave = torch.load(squeezewave_path)['model']
  39. squeezewave = squeezewave.remove_weightnorm(squeezewave)
  40. squeezewave.cuda().eval()
  41. if is_fp16:
  42. from apex import amp
  43. squeezewave, _ = amp.initialize(squeezewave, [], opt_level="O3")
  44. if denoiser_strength > 0:
  45. denoiser = Denoiser(squeezewave).cuda()
  46. for i, file_path in enumerate(mel_files):
  47. file_name = os.path.splitext(os.path.basename(file_path))[0]
  48. mel = torch.load(file_path)
  49. mel = torch.autograd.Variable(mel.cuda())
  50. mel = torch.unsqueeze(mel, 0)
  51. mel = mel.half() if is_fp16 else mel
  52. with torch.no_grad():
  53. audio = squeezewave.infer(mel, sigma=sigma).float()
  54. if denoiser_strength > 0:
  55. audio = denoiser(audio, denoiser_strength)
  56. audio = audio * MAX_WAV_VALUE
  57. audio = audio.squeeze()
  58. audio = audio.cpu().numpy()
  59. audio = audio.astype('int16')
  60. audio_path = os.path.join(
  61. output_dir, "{}_synthesis.wav".format(file_name))
  62. write(audio_path, sampling_rate, audio)
  63. print(audio_path)
  64. if __name__ == "__main__":
  65. import argparse
  66. parser = argparse.ArgumentParser()
  67. parser.add_argument('-f', "--filelist_path", required=True)
  68. parser.add_argument('-w', '--squeezewave_path',
  69. help='Path to squeezewave decoder checkpoint with model')
  70. parser.add_argument('-o', "--output_dir", required=True)
  71. parser.add_argument("-s", "--sigma", default=1.0, type=float)
  72. parser.add_argument("--sampling_rate", default=22050, type=int)
  73. parser.add_argument("--is_fp16", action="store_true")
  74. parser.add_argument("-d", "--denoiser_strength", default=0.0, type=float,
  75. help='Removes model bias. Start with 0.1 and adjust')
  76. args = parser.parse_args()
  77. main(args.filelist_path, args.squeezewave_path, args.sigma, args.output_dir,
  78. args.sampling_rate, args.is_fp16, args.denoiser_strength)