Download quasar/tokenizers/audio_aligned.py from AtlasAI-org/Quasar: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/AtlasAI-org/Quasar/resolve/main/quasar/tokenizers/audio_aligned.py
- Command line
-
hf download hf://AtlasAI-org/Quasar/quasar/tokenizers/audio_aligned.py
-
curl -L -o audio_aligned.py https://huggingface.co/AtlasAI-org/Quasar/resolve/main/quasar/tokenizers/audio_aligned.py
1.85 kB
| """Frozen DAC interface with explicit, training-calibrated boundary alignment. | |
| Source utterances remain whole. Silent boundary guards are codec padding, and | |
| the fixed decoder offset is recorded separately from the generative weights. | |
| """ | |
| import numpy as np | |
| from .audio import AudioTokenizer | |
| class AlignedAudioTokenizer(AudioTokenizer): | |
| def __init__(self, local_path, alignment_samples: int, codebooks=4, max_seconds=2.0, device='cpu'): | |
| super().__init__(local_path, codebooks, max_seconds, device) | |
| if self.sr != 24000 or self.hop != 320 or abs(alignment_samples) > 32: | |
| raise ValueError('alignment is only supported for the pinned 24 kHz/320-hop codec') | |
| self.source_max_seconds = max_seconds | |
| self.max_seconds = max_seconds + 2 * self.hop / self.sr | |
| self.alignment_samples = alignment_samples | |
| def encode(self, waveform, sample_rate): | |
| x = np.asarray(waveform) | |
| if x.ndim != 1 or not 0 < len(x) <= self.source_max_seconds * self.sr: | |
| raise ValueError('complete source utterance exceeds duration budget') | |
| packet = super().encode(np.pad(x, (self.hop, self.hop)), sample_rate) | |
| packet['samples'] = len(x) | |
| return packet | |
| def decode(self, packet): | |
| # Ask the base decoder for all samples, before applying boundary guards. | |
| expanded = dict(packet, samples=(len(packet['codes']) + 2) * self.hop) | |
| raw = super().decode(expanded) | |
| start = self.hop + self.alignment_samples | |
| n = packet.get('samples', min(int(self.source_max_seconds * self.sr), len(raw) - 2 * self.hop)) | |
| if not 0 < n <= self.source_max_seconds * self.sr or start + n > len(raw): | |
| raise ValueError('decoded audio cannot preserve the complete source duration') | |
| return raw[start:start + n] | |