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.

39 lines
1.5 KiB

  1. import sys
  2. import torch
  3. from stft import STFT
  4. class Denoiser(torch.nn.Module):
  5. """ Removes model bias from audio produced with squeezewave"""
  6. def __init__(self, squeezewave, filter_length=1024, n_overlap=4,
  7. win_length=1024, mode='zeros'):
  8. super(Denoiser, self).__init__()
  9. self.stft = STFT(filter_length=filter_length,
  10. hop_length=int(filter_length/n_overlap),
  11. win_length=win_length)
  12. if mode == 'zeros':
  13. mel_input = torch.zeros(
  14. (1, 80, 88),
  15. dtype=squeezewave.upsample.weight.dtype,
  16. device=squeezewave.upsample.weight.device)
  17. elif mode == 'normal':
  18. mel_input = torch.randn(
  19. (1, 80, 88),
  20. dtype=squeezewave.upsample.weight.dtype,
  21. device=squeezewave.upsample.weight.device)
  22. else:
  23. raise Exception("Mode {} if not supported".format(mode))
  24. with torch.no_grad():
  25. bias_audio = squeezewave.infer(mel_input, sigma=0.0).float()
  26. bias_spec, _ = self.stft.transform(bias_audio)
  27. self.register_buffer('bias_spec', bias_spec[:, :, 0][:, :, None])
  28. def forward(self, audio, strength=0.1):
  29. audio_spec, audio_angles = self.stft.transform(audio.float())
  30. audio_spec_denoised = audio_spec - self.bias_spec * strength
  31. audio_spec_denoised = torch.clamp(audio_spec_denoised, 0.0)
  32. audio_denoised = self.stft.inverse(audio_spec_denoised, audio_angles)
  33. return audio_denoised