Spaces:
Sleeping
Sleeping
feat: add EnCodec and Mimi codec implementations with self-registration
Browse files- compare_codec/__init__.py +2 -0
- compare_codec/encodec_codec.py +79 -0
- compare_codec/mimi_codec.py +72 -0
compare_codec/__init__.py
CHANGED
|
@@ -47,4 +47,6 @@ def get_all() -> dict[str, AudioCodec]:
|
|
| 47 |
|
| 48 |
# Import codec modules so they self-register on startup.
|
| 49 |
from compare_codec import dac as _dac # noqa: E402, F401
|
|
|
|
|
|
|
| 50 |
from compare_codec import snac_codec as _snac # noqa: E402, F401
|
|
|
|
| 47 |
|
| 48 |
# Import codec modules so they self-register on startup.
|
| 49 |
from compare_codec import dac as _dac # noqa: E402, F401
|
| 50 |
+
from compare_codec import encodec_codec as _encodec # noqa: E402, F401
|
| 51 |
+
from compare_codec import mimi_codec as _mimi # noqa: E402, F401
|
| 52 |
from compare_codec import snac_codec as _snac # noqa: E402, F401
|
compare_codec/encodec_codec.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""EnCodec (Meta) — wraps the HuggingFace transformers implementation."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch
|
| 9 |
+
import torchaudio
|
| 10 |
+
|
| 11 |
+
from compare_codec import CodecConfig, register
|
| 12 |
+
|
| 13 |
+
_BANDWIDTHS = [1.5, 3.0, 6.0, 12.0, 24.0]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class EnCodecCodec:
|
| 17 |
+
"""EnCodec 24kHz codec with lazy model loading."""
|
| 18 |
+
|
| 19 |
+
def __init__(self) -> None:
|
| 20 |
+
self._model = None
|
| 21 |
+
self._processor = None
|
| 22 |
+
|
| 23 |
+
@property
|
| 24 |
+
def name(self) -> str:
|
| 25 |
+
return "EnCodec"
|
| 26 |
+
|
| 27 |
+
@property
|
| 28 |
+
def sample_rate(self) -> int:
|
| 29 |
+
return 24_000
|
| 30 |
+
|
| 31 |
+
def configs(self) -> list[CodecConfig]:
|
| 32 |
+
return [
|
| 33 |
+
CodecConfig(
|
| 34 |
+
name=f"{bw:g} kbps",
|
| 35 |
+
params={"bandwidth": bw, "sample_rate": 24_000},
|
| 36 |
+
)
|
| 37 |
+
for bw in _BANDWIDTHS
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
def _load(self):
|
| 41 |
+
if self._model is None:
|
| 42 |
+
from transformers import AutoProcessor, EncodecModel
|
| 43 |
+
|
| 44 |
+
self._model = EncodecModel.from_pretrained("facebook/encodec_24khz")
|
| 45 |
+
self._model.eval()
|
| 46 |
+
self._processor = AutoProcessor.from_pretrained("facebook/encodec_24khz")
|
| 47 |
+
|
| 48 |
+
@torch.no_grad()
|
| 49 |
+
def encode_decode(self, audio_path: Path, config: CodecConfig) -> np.ndarray:
|
| 50 |
+
self._load()
|
| 51 |
+
bandwidth: float = config.params["bandwidth"]
|
| 52 |
+
target_sr: int = config.params["sample_rate"]
|
| 53 |
+
|
| 54 |
+
wav, sr = torchaudio.load(str(audio_path))
|
| 55 |
+
if wav.shape[0] > 1:
|
| 56 |
+
wav = wav.mean(dim=0, keepdim=True)
|
| 57 |
+
if sr != target_sr:
|
| 58 |
+
wav = torchaudio.functional.resample(wav, sr, target_sr)
|
| 59 |
+
|
| 60 |
+
inputs = self._processor(
|
| 61 |
+
raw_audio=wav.squeeze(0).numpy(),
|
| 62 |
+
sampling_rate=target_sr,
|
| 63 |
+
return_tensors="pt",
|
| 64 |
+
)
|
| 65 |
+
enc = self._model.encode(
|
| 66 |
+
inputs["input_values"],
|
| 67 |
+
inputs["padding_mask"],
|
| 68 |
+
bandwidth=bandwidth,
|
| 69 |
+
)
|
| 70 |
+
audio_out = self._model.decode(
|
| 71 |
+
enc.audio_codes,
|
| 72 |
+
enc.audio_scales,
|
| 73 |
+
padding_mask=inputs["padding_mask"],
|
| 74 |
+
)[0]
|
| 75 |
+
|
| 76 |
+
return audio_out.squeeze(0).squeeze(0).cpu().numpy()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
register(EnCodecCodec())
|
compare_codec/mimi_codec.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Mimi (Kyutai) — wraps the HuggingFace transformers implementation."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch
|
| 9 |
+
import torchaudio
|
| 10 |
+
|
| 11 |
+
from compare_codec import CodecConfig, register
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class MimiCodec:
|
| 15 |
+
"""Mimi codec with lazy model loading."""
|
| 16 |
+
|
| 17 |
+
def __init__(self) -> None:
|
| 18 |
+
self._model = None
|
| 19 |
+
self._fe = None
|
| 20 |
+
|
| 21 |
+
@property
|
| 22 |
+
def name(self) -> str:
|
| 23 |
+
return "Mimi"
|
| 24 |
+
|
| 25 |
+
@property
|
| 26 |
+
def sample_rate(self) -> int:
|
| 27 |
+
return 24_000
|
| 28 |
+
|
| 29 |
+
def configs(self) -> list[CodecConfig]:
|
| 30 |
+
return [
|
| 31 |
+
CodecConfig(
|
| 32 |
+
name="1.1 kbps",
|
| 33 |
+
params={"sample_rate": 24_000},
|
| 34 |
+
)
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
def _load(self):
|
| 38 |
+
if self._model is None:
|
| 39 |
+
from transformers import AutoFeatureExtractor, MimiModel
|
| 40 |
+
|
| 41 |
+
self._model = MimiModel.from_pretrained("kyutai/mimi")
|
| 42 |
+
self._model.eval()
|
| 43 |
+
self._fe = AutoFeatureExtractor.from_pretrained("kyutai/mimi")
|
| 44 |
+
|
| 45 |
+
@torch.no_grad()
|
| 46 |
+
def encode_decode(self, audio_path: Path, config: CodecConfig) -> np.ndarray:
|
| 47 |
+
self._load()
|
| 48 |
+
target_sr: int = config.params["sample_rate"]
|
| 49 |
+
|
| 50 |
+
wav, sr = torchaudio.load(str(audio_path))
|
| 51 |
+
if wav.shape[0] > 1:
|
| 52 |
+
wav = wav.mean(dim=0, keepdim=True)
|
| 53 |
+
if sr != target_sr:
|
| 54 |
+
wav = torchaudio.functional.resample(wav, sr, target_sr)
|
| 55 |
+
|
| 56 |
+
original_len = wav.shape[-1]
|
| 57 |
+
inputs = self._fe(
|
| 58 |
+
raw_audio=wav.squeeze(0).numpy(),
|
| 59 |
+
sampling_rate=target_sr,
|
| 60 |
+
return_tensors="pt",
|
| 61 |
+
)
|
| 62 |
+
enc = self._model.encode(inputs["input_values"], inputs["padding_mask"])
|
| 63 |
+
audio_out = self._model.decode(
|
| 64 |
+
enc.audio_codes, inputs["padding_mask"]
|
| 65 |
+
)[0]
|
| 66 |
+
|
| 67 |
+
# Trim to original length (Mimi may pad).
|
| 68 |
+
audio_out = audio_out.squeeze(0).squeeze(0).cpu().numpy()[:original_len]
|
| 69 |
+
return audio_out
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
register(MimiCodec())
|