MCPcopy Create free account
hub / github.com/1038lab/ComfyUI-FireRedTTS / RedCodecInfer

Class RedCodecInfer

fireredtts2/codec/model.py:197–376  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

195
196
197class RedCodecInfer(RedCodec):
198 def __init__(self, codec: RedCodec):
199 super().__init__(
200 codec.ssl,
201 codec.ssl_adaptor,
202 codec.acoustic_encoder,
203 codec.downsample,
204 codec.rvq,
205 codec.upsample,
206 codec.semantic_decoder,
207 codec.acoustic_decoder,
208 )
209
210 @classmethod
211 def from_pretrained(cls, conf_path: str, ckpt_path: str) -> "RedCodecInfer":
212 with open(conf_path, "r") as f:
213 codec = RedCodec.from_config(conf_path)
214 ckpt = torch.load(ckpt_path)["generator"]
215 codec.load_state_dict(ckpt)
216 return cls(codec)
217
218 def _encode_one_batch(self, audio16k: torch.Tensor):
219 B, T = audio16k.shape
220 audio16k_length = torch.tensor(
221 [T] * B, dtype=torch.long, device=audio16k.device
222 )
223 # Semantic
224 ssl, ssl_length = self.ssl.forward(audio16k, audio16k_length)
225 ssl = ssl.clone() # For onnx export
226 sem_feats, sem_length = self.ssl_adaptor(ssl, ssl_length)
227 # Acoustic
228 aco_feats, aco_length = self.acoustic_encoder(audio16k, audio16k_length)
229 # VQ
230 vq_in_feats = torch.cat([sem_feats, aco_feats], dim=2)
231 vq_in_feats, vq_in_length = self.downsample(vq_in_feats, aco_length)
232 # RVQ,
233 indices = self.rvq.encode_codes(vq_in_feats.transpose(1, 2)) # (nq, B, L)
234 indices = indices.permute(1, 0, 2)
235 return indices # (B, nq, L)
236
237 @staticmethod
238 def _pad_and_chunk(audio: torch.Tensor, chunk_size: int) -> List[torch.Tensor]:
239 pad_len = math.ceil(audio.shape[1] / chunk_size) * chunk_size - audio.shape[1]
240 audio = F.pad(audio, (0, pad_len), mode="constant", value=0)
241 audio_chunks = audio.split(chunk_size, dim=1)
242 return audio_chunks
243
244 @torch.inference_mode()
245 def encode(
246 self,
247 audio16k: torch.Tensor,
248 audio16k_length: torch.Tensor = None,
249 batch_size: int = 96,
250 ):
251 """
252 Args:
253 audio16k: shape (b, t)
254 audio16k_length: (b,)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected