Upload codec/audio_processing/dune_codec.py with huggingface_hub
Browse files
codec/audio_processing/dune_codec.py
CHANGED
|
@@ -275,6 +275,30 @@ class DuneAudioTokenizer(nn.Module):
|
|
| 275 |
|
| 276 |
return tokens[0]
|
| 277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
def decode(self, vq_code):
|
| 279 |
tokens = vq_code if vq_code.dim() == 3 else vq_code.unsqueeze(0)
|
| 280 |
tokens = tokens.to(self.device)
|
|
@@ -290,7 +314,7 @@ class DuneAudioTokenizer(nn.Module):
|
|
| 290 |
else:
|
| 291 |
audio, _ = self.codec.decode(tokens=tokens, tokens_len=tokens_len)
|
| 292 |
|
| 293 |
-
return audio.cpu().numpy()
|
| 294 |
|
| 295 |
|
| 296 |
def _state_dict_from(ckpt):
|
|
|
|
| 275 |
|
| 276 |
return tokens[0]
|
| 277 |
|
| 278 |
+
def _post_filter(self, audio):
|
| 279 |
+
"""Spectral post-filter over the reconstructed waveform.
|
| 280 |
+
|
| 281 |
+
Applied per item at the output rate. A failure here must not cost the
|
| 282 |
+
caller their audio, so it degrades to the unfiltered signal.
|
| 283 |
+
"""
|
| 284 |
+
try:
|
| 285 |
+
from ._postfilter import get_post_filter
|
| 286 |
+
|
| 287 |
+
pf = get_post_filter(device="cpu")
|
| 288 |
+
except Exception:
|
| 289 |
+
return audio
|
| 290 |
+
|
| 291 |
+
out = np.array(audio, dtype=np.float32, copy=True)
|
| 292 |
+
flat = out.reshape(-1, out.shape[-1]) if out.ndim > 1 else out[None]
|
| 293 |
+
for i in range(flat.shape[0]):
|
| 294 |
+
try:
|
| 295 |
+
filtered = pf(flat[i], self.output_sample_rate)
|
| 296 |
+
except Exception:
|
| 297 |
+
continue
|
| 298 |
+
n = min(filtered.size, flat.shape[1])
|
| 299 |
+
flat[i, :n] = filtered[:n]
|
| 300 |
+
return flat.reshape(out.shape) if out.ndim > 1 else flat[0]
|
| 301 |
+
|
| 302 |
def decode(self, vq_code):
|
| 303 |
tokens = vq_code if vq_code.dim() == 3 else vq_code.unsqueeze(0)
|
| 304 |
tokens = tokens.to(self.device)
|
|
|
|
| 314 |
else:
|
| 315 |
audio, _ = self.codec.decode(tokens=tokens, tokens_len=tokens_len)
|
| 316 |
|
| 317 |
+
return self._post_filter(audio.cpu().numpy())
|
| 318 |
|
| 319 |
|
| 320 |
def _state_dict_from(ckpt):
|