File size: 1,183 Bytes
adabee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Shared Whisper waveform preprocessing for reproducible Danish ASR scores.

Short audio must be padded in waveform space before log-mel extraction. Padding
an already computed spectrogram with zero does not represent acoustic silence.
``truncation=False`` keeps all samples of inputs longer than the 30 s window.

Reference: Transformers 4.57.1 WhisperFeatureExtractor and short-form example:
https://huggingface.co/docs/transformers/v4.57.1/en/model_doc/whisper
"""

PREPROCESSING_VERSION = "whisper-waveform-pad-v2"
PREPROCESSING_SETTINGS = {
    "version": PREPROCESSING_VERSION,
    "sample_rate": 16000,
    "padding": "max_length",
    "truncation": False,
    "return_attention_mask": True,
    "description": "Pad short raw waveforms to the model window before log-mel extraction; retain full long audio.",
}


def prepare_whisper_input(processor, waveform):
    """Prepare one mono 16 kHz waveform; accepts a processor or feature extractor."""
    return processor(
        waveform,
        sampling_rate=PREPROCESSING_SETTINGS["sample_rate"],
        return_tensors="pt",
        truncation=False,
        padding="max_length",
        return_attention_mask=True,
    )