File size: 1,068 Bytes
d2a1a41 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | """DAC boundary padding for complete source utterances; no source cropping."""
import numpy as np
from .audio import AudioTokenizer
class CompleteAudioTokenizer(AudioTokenizer):
"""Guard the decoder's eight-sample edge shortfall with a full silent hop.
The original source must fit the two-second budget. Only codec boundary padding
extends beyond it; the packet retains the original sample count for decoding.
"""
def __init__(self, local_path, codebooks=4, max_seconds=2.0, device='cpu'):
super().__init__(local_path, codebooks, max_seconds, device)
self.source_max_seconds = max_seconds
self.max_seconds = max_seconds + self.hop / self.sr
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, (0, self.hop)), sample_rate)
packet['samples'] = len(x)
return packet
|