Paolify/RVC_4
0
1# File under the MIT license, see https://github.com/adefossez/julius/LICENSE for details.2# Author: adefossez, 20203"""4Signal processing or PyTorch related utilities.5"""6import math7import typing as tp8 9import torch10from torch.nn import functional as F11 12 13def sinc(x: torch.Tensor):14 """15 Implementation of sinc, i.e. sin(x) / x16 17 __Warning__: the input is not multiplied by `pi`!18 """19 return torch.where(x == 0, torch.tensor(1., device=x.device, dtype=x.dtype), torch.sin(x) / x)20 21 22def pad_to(tensor: torch.Tensor, target_length: int, mode: str = 'constant', value: float = 0):23 """24 Pad the given tensor to the given length, with 0s on the right.25 """26 return F.pad(tensor, (0, target_length - tensor.shape[-1]), mode=mode, value=value)27 28 29def hz_to_mel(freqs: torch.Tensor):30 """31 Converts a Tensor of frequencies in hertz to the mel scale.32 Uses the simple formula by O'Shaughnessy (1987).33 34 Args:35 freqs (torch.Tensor): frequencies to convert.36 37 """38 return 2595 * torch.log10(1 + freqs / 700)39 40 41def mel_to_hz(mels: torch.Tensor):42 """43 Converts a Tensor of mel scaled frequencies to Hertz.44 Uses the simple formula by O'Shaughnessy (1987).45 46 Args:47 mels (torch.Tensor): mel frequencies to convert.48 """49 return 700 * (10**(mels / 2595) - 1)50 51 52def mel_frequencies(n_mels: int, fmin: float, fmax: float):53 """54 Return frequencies that are evenly spaced in mel scale.55 56 Args:57 n_mels (int): number of frequencies to return.58 fmin (float): start from this frequency (in Hz).59 fmax (float): finish at this frequency (in Hz).60 61 62 """63 low = hz_to_mel(torch.tensor(float(fmin))).item()64 high = hz_to_mel(torch.tensor(float(fmax))).item()65 mels = torch.linspace(low, high, n_mels)66 return mel_to_hz(mels)67 68 69def volume(x: torch.Tensor, floor=1e-8):70 """71 Return the volume in dBFS.72 """73 return torch.log10(floor + (x**2).mean(-1)) * 1074 75 76def pure_tone(freq: float, sr: float = 128, dur: float = 4, device=None):77 """78 Return a pure tone, i.e. cosine.79 80 Args:81 freq (float): frequency (in Hz)82 sr (float): sample rate (in Hz)83 dur (float): duration (in seconds)84 """85 time = torch.arange(int(sr * dur), device=device).float() / sr86 return torch.cos(2 * math.pi * freq * time)87 88 89def unfold(input, kernel_size: int, stride: int):90 """1D only unfolding similar to the one from PyTorch.91 However PyTorch unfold is extremely slow.92 93 Given an input tensor of size `[*, T]` this will return94 a tensor `[*, F, K]` with `K` the kernel size, and `F` the number95 of frames. The i-th frame is a view onto `i * stride: i * stride + kernel_size`.96 This will automatically pad the input to cover at least once all entries in `input`.97 98 Args:99 input (Tensor): tensor for which to return the frames.100 kernel_size (int): size of each frame.101 stride (int): stride between each frame.102 103 Shape:104 105 - Inputs: `input` is `[*, T]`106 - Output: `[*, F, kernel_size]` with `F = 1 + ceil((T - kernel_size) / stride)`107 108 109 ..Warning:: unlike PyTorch unfold, this will pad the input110 so that any position in `input` is covered by at least one frame.111 """112 shape = list(input.shape)113 length = shape.pop(-1)114 n_frames = math.ceil((max(length, kernel_size) - kernel_size) / stride) + 1115 tgt_length = (n_frames - 1) * stride + kernel_size116 padded = F.pad(input, (0, tgt_length - length)).contiguous()117 strides: tp.List[int] = []118 for dim in range(padded.dim()):119 strides.append(padded.stride(dim))120 assert strides.pop(-1) == 1, 'data should be contiguous'121 strides = strides + [stride, 1]122 return padded.as_strided(shape + [n_frames, kernel_size], strides)123 