File size: 1,541 Bytes
a1cd84e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""

Murli V2.0 Neural Vocoder Wrapper.

Supports Vocos 24kHz (100 HTK mel bins) by default and optional BigVGAN-v2 24kHz 100-band backend.

"""

import torch
import torch.nn as nn


class MurliVocoder(nn.Module):
    def __init__(self, backend: str = "vocos"):
        super().__init__()
        self.backend = backend
        self.model = None
        self.sample_rate = 24000

    def load(self, device: torch.device):
        if self.model is not None:
            return self.model

        if self.backend == "bigvgan_v2":
            import bigvgan
            model = bigvgan.BigVGAN.from_pretrained(
                "nvidia/bigvgan_v2_24khz_100band_256x", use_cuda_kernel=False
            ).to(device).eval()
            model.remove_weight_norm()
            self.model = model
            return model
        if self.backend != "vocos":
            raise ValueError(f"Unknown vocoder: {self.backend}")

        from vocos import Vocos
        print("Loading pretrained Vocos 24kHz (charactr/vocos-mel-24khz)...")
        self.model = Vocos.from_pretrained("charactr/vocos-mel-24khz").to(device).eval()
        return self.model

    @torch.no_grad()
    def decode(self, mel: torch.Tensor) -> torch.Tensor:
        """Convert log mel [B, 100, T] -> waveform [B, samples]."""
        voc = self.load(mel.device)
        if self.backend == "bigvgan_v2":
            wav = voc(mel.float())
            return wav.squeeze(1) if wav.dim() == 3 else wav
        return voc.decode(mel.float())