2021-12-10 05:59:56 +00:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
from data.audio.unsupervised_audio_dataset import load_audio
|
|
|
|
from data.util import find_files_of_type, is_audio_file
|
|
|
|
from models.diffusion.gaussian_diffusion import get_named_beta_schedule
|
|
|
|
from models.diffusion.respace import SpacedDiffusion, space_timesteps
|
2022-02-04 04:42:53 +00:00
|
|
|
from trainer.injectors.audio_injectors import TorchMelSpectrogramInjector
|
2021-12-10 05:59:56 +00:00
|
|
|
from utils.audio import plot_spectrogram
|
|
|
|
|
|
|
|
|
2021-12-13 02:52:08 +00:00
|
|
|
def wav_to_mel(wav, mel_norms_file='../experiments/clips_mel_norms.pth'):
|
2021-12-10 05:59:56 +00:00
|
|
|
"""
|
|
|
|
Converts an audio clip into a MEL tensor that the vocoder, DVAE and GptTts models use whenever a MEL is called for.
|
|
|
|
"""
|
2021-12-13 02:52:08 +00:00
|
|
|
return TorchMelSpectrogramInjector({'in': 'wav', 'out': 'mel', 'mel_norm_file': mel_norms_file},{})({'wav': wav})['mel']
|
2021-12-10 05:59:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def convert_mel_to_codes(dvae_model, mel):
|
|
|
|
"""
|
|
|
|
Converts an audio clip into discrete codes.
|
|
|
|
"""
|
|
|
|
dvae_model.eval()
|
|
|
|
with torch.no_grad():
|
|
|
|
return dvae_model.get_codebook_indices(mel)
|
|
|
|
|
|
|
|
|
|
|
|
def load_gpt_conditioning_inputs_from_directory(path, num_candidates=3, sample_rate=22050, max_samples=44100):
|
|
|
|
candidates = find_files_of_type('img', os.path.dirname(path), qualifier=is_audio_file)[0]
|
|
|
|
assert len(candidates) < 50000 # Sanity check to ensure we aren't loading "related files" that aren't actually related.
|
|
|
|
if len(candidates) == 0:
|
|
|
|
print(f"No conditioning candidates found for {path} (not even the clip itself??)")
|
|
|
|
raise NotImplementedError()
|
|
|
|
# Sample with replacement. This can get repeats, but more conveniently handles situations where there are not enough candidates.
|
|
|
|
related_mels = []
|
|
|
|
for k in range(num_candidates):
|
|
|
|
rel_clip = load_audio(random.choice(candidates), sample_rate)
|
|
|
|
gap = rel_clip.shape[-1] - max_samples
|
|
|
|
if gap > 0:
|
|
|
|
rand_start = random.randint(0, gap)
|
|
|
|
rel_clip = rel_clip[:, rand_start:rand_start+max_samples]
|
|
|
|
as_mel = wav_to_mel(rel_clip)
|
|
|
|
related_mels.append(as_mel)
|
|
|
|
return torch.stack(related_mels, dim=0)
|
|
|
|
|
|
|
|
|
2022-01-30 23:14:06 +00:00
|
|
|
def load_discrete_vocoder_diffuser(trained_diffusion_steps=4000, desired_diffusion_steps=200, schedule='linear'):
|
2021-12-10 05:59:56 +00:00
|
|
|
"""
|
|
|
|
Helper function to load a GaussianDiffusion instance configured for use as a vocoder.
|
|
|
|
"""
|
|
|
|
return SpacedDiffusion(use_timesteps=space_timesteps(trained_diffusion_steps, [desired_diffusion_steps]), model_mean_type='epsilon',
|
2022-01-30 23:14:06 +00:00
|
|
|
model_var_type='learned_range', loss_type='mse', betas=get_named_beta_schedule(schedule, trained_diffusion_steps))
|
2021-12-10 05:59:56 +00:00
|
|
|
|
|
|
|
|
2022-02-04 03:00:01 +00:00
|
|
|
def do_spectrogram_diffusion(diffusion_model, dvae_model, diffuser, mel_codes, conditioning_input, spectrogram_compression_factor=128, plt_spec=False, mean=False):
|
2021-12-10 05:59:56 +00:00
|
|
|
"""
|
|
|
|
Uses the specified diffusion model and DVAE model to convert the provided MEL & conditioning inputs into an audio clip.
|
|
|
|
"""
|
|
|
|
diffusion_model.eval()
|
|
|
|
dvae_model.eval()
|
|
|
|
with torch.no_grad():
|
|
|
|
mel = dvae_model.decode(mel_codes)[0]
|
|
|
|
if plt_spec:
|
|
|
|
plot_spectrogram(mel[0].cpu())
|
|
|
|
|
2022-01-20 18:27:49 +00:00
|
|
|
# Pad MEL to multiples of 2048//spectrogram_compression_factor
|
2021-12-10 05:59:56 +00:00
|
|
|
msl = mel.shape[-1]
|
2022-01-20 18:27:49 +00:00
|
|
|
dsl = 2048 // spectrogram_compression_factor
|
2021-12-10 05:59:56 +00:00
|
|
|
gap = dsl - (msl % dsl)
|
|
|
|
if gap > 0:
|
|
|
|
mel = torch.nn.functional.pad(mel, (0, gap))
|
|
|
|
|
|
|
|
output_shape = (mel.shape[0], 1, mel.shape[-1] * spectrogram_compression_factor)
|
2022-02-04 03:00:01 +00:00
|
|
|
if mean:
|
|
|
|
return diffuser.p_sample_loop(diffusion_model, output_shape, noise=torch.zeros(output_shape, device=mel_codes.device),
|
|
|
|
model_kwargs={'spectrogram': mel, 'conditioning_input': conditioning_input})
|
|
|
|
else:
|
|
|
|
return diffuser.p_sample_loop(diffusion_model, output_shape, model_kwargs={'spectrogram': mel, 'conditioning_input': conditioning_input})
|
2021-12-10 05:59:56 +00:00
|
|
|
|