MCPcopy Create free account
hub / github.com/FunAudioLLM/FunMusic / EncodecModel

Class EncodecModel

inspiremusic/wavtokenizer/encoder/model.py:68–301  ·  view source on GitHub ↗

EnCodec model operating on the raw waveform. Args: target_bandwidths (list of float): Target bandwidths. encoder (nn.Module): Encoder network. decoder (nn.Module): Decoder network. sample_rate (int): Audio sample rate. channels (int): Number of audio chann

Source from the content-addressed store, hash-verified

66
67
68class EncodecModel(nn.Module):
69 """EnCodec model operating on the raw waveform.
70 Args:
71 target_bandwidths (list of float): Target bandwidths.
72 encoder (nn.Module): Encoder network.
73 decoder (nn.Module): Decoder network.
74 sample_rate (int): Audio sample rate.
75 channels (int): Number of audio channels.
76 normalize (bool): Whether to apply audio normalization.
77 segment (float or None): segment duration in sec. when doing overlap-add.
78 overlap (float): overlap between segment, given as a fraction of the segment duration.
79 name (str): name of the model, used as metadata when compressing audio.
80 """
81 def __init__(self,
82 encoder: m.SEANetEncoder,
83 decoder: m.SEANetDecoder,
84 quantizer: qt.ResidualVectorQuantizer,
85 target_bandwidths: tp.List[float],
86 sample_rate: int,
87 channels: int,
88 normalize: bool = False,
89 segment: tp.Optional[float] = None,
90 overlap: float = 0.01,
91 name: str = 'unset'):
92 super().__init__()
93 self.bandwidth: tp.Optional[float] = None
94 self.target_bandwidths = target_bandwidths
95 self.encoder = encoder
96 self.quantizer = quantizer
97 self.decoder = decoder
98 self.sample_rate = sample_rate
99 self.channels = channels
100 self.normalize = normalize
101 self.segment = segment
102 self.overlap = overlap
103 self.frame_rate = math.ceil(self.sample_rate / np.prod(self.encoder.ratios))
104 self.name = name
105 self.bits_per_codebook = int(math.log2(self.quantizer.bins))
106 assert 2 ** self.bits_per_codebook == self.quantizer.bins, \
107 "quantizer bins must be a power of 2."
108
109 @property
110 def segment_length(self) -> tp.Optional[int]:
111 if self.segment is None:
112 return None
113 return int(self.segment * self.sample_rate)
114
115 @property
116 def segment_stride(self) -> tp.Optional[int]:
117 segment_length = self.segment_length
118 if segment_length is None:
119 return None
120 return max(1, int((1 - self.overlap) * segment_length))
121
122 def encode(self, x: torch.Tensor) -> tp.List[EncodedFrame]:
123 """Given a tensor `x`, returns a list of frames containing
124 the discrete encoded codes for `x`, along with rescaling factors
125 for each segment, when `self.normalize` is True.

Callers 2

__init__Method · 0.90
_get_modelMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected