{"record":{"id":"d223093b02b6fb3b","repo":"huggingface/transformers","slug":"mel-scale-should-be-one-of-htk-slaney-or-kal","errorCode":null,"errorMessage":"mel_scale should be one of \"htk\", \"slaney\" or \"kaldi\".","messagePattern":"mel_scale should be one of \"htk\", \"slaney\" or \"kaldi\"\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/audio_utils.py","lineNumber":463,"sourceCode":"    return make_list_of_audio(audio)\n\n\ndef hertz_to_mel(freq: float | np.ndarray, mel_scale: str = \"htk\") -> float | np.ndarray:\n    \"\"\"\n    Convert frequency from hertz to mels.\n\n    Args:\n        freq (`float` or `np.ndarray`):\n            The frequency, or multiple frequencies, in hertz (Hz).\n        mel_scale (`str`, *optional*, defaults to `\"htk\"`):\n            The mel frequency scale to use, `\"htk\"`, `\"kaldi\"` or `\"slaney\"`.\n\n    Returns:\n        `float` or `np.ndarray`: The frequencies on the mel scale.\n    \"\"\"\n\n    if mel_scale not in [\"slaney\", \"htk\", \"kaldi\"]:\n        raise ValueError('mel_scale should be one of \"htk\", \"slaney\" or \"kaldi\".')\n\n    if mel_scale == \"htk\":\n        return 2595.0 * np.log10(1.0 + (freq / 700.0))\n    elif mel_scale == \"kaldi\":\n        return 1127.0 * np.log(1.0 + (freq / 700.0))\n\n    min_log_hertz = 1000.0\n    min_log_mel = 15.0\n    logstep = 27.0 / np.log(6.4)\n    mels = 3.0 * freq / 200.0\n\n    if isinstance(freq, np.ndarray):\n        log_region = freq >= min_log_hertz\n        mels[log_region] = min_log_mel + np.log(freq[log_region] / min_log_hertz) * logstep\n    elif freq >= min_log_hertz:\n        mels = min_log_mel + np.log(freq / min_log_hertz) * logstep\n\n    return mels","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/audio_utils.py#L445-L481","documentation":"Thrown by `hertz_to_mel` when the `mel_scale` argument is not one of the three supported scales: \"htk\", \"slaney\", or \"kaldi\". The function converts frequencies in Hz to mel units and the conversion formula differs per scale, so an unknown string has no defined behavior and is rejected before any math runs.","triggerScenarios":"Calling `hertz_to_mel(freq, mel_scale=...)` with a typo (\"htkk\", \"Slaney\" with capital letter), an unsupported scale (\"librosa\", \"mfcc\"), or a non-string value. Indirectly hit through `mel_filter_bank(..., mel_scale=...)` which forwards the argument.","commonSituations":"Copy-pasting mel_scale from another library's config (torchaudio, librosa use different vocabularies); case-sensitivity mistakes; custom feature-extractor configs overriding `mel_scale` with a wrong value.","solutions":["Set mel_scale to exactly one of \"htk\", \"slaney\", or \"kaldi\" (lowercase)","If the value comes from a config/JSON, check for typos or casing issues in that file","If you intended torchaudio/librosa-style 'slaney' behavior, use mel_scale=\"slaney\""],"exampleFix":"// before\nmels = hertz_to_mel(440.0, mel_scale=\"HTK\")  # ValueError\n\n// after\nmels = hertz_to_mel(440.0, mel_scale=\"htk\")","handlingStrategy":"validation","validationCode":"VALID_MEL_SCALES = {\"htk\", \"slaney\", \"kaldi\"}\nassert mel_scale in VALID_MEL_SCALES, f\"mel_scale must be one of {sorted(VALID_MEL_SCALES)}, got {mel_scale!r}\"","typeGuard":"def is_valid_mel_scale(s: str) -> bool:\n    return isinstance(s, str) and s in {\"htk\", \"slaney\", \"kaldi\"}","tryCatchPattern":"try:\n    mels = hertz_to_mel(freq, mel_scale=mel_scale)\nexcept ValueError as e:\n    if \"mel_scale\" in str(e):\n        mel_scale = \"htk\"  # or log and re-raise\n        mels = hertz_to_mel(freq, mel_scale=mel_scale)\n    else:\n        raise","preventionTips":["Define mel_scale once as a module constant, lowercase","Validate config-loaded strings against the allowed set before use","Watch for casing: only lowercase values are accepted"],"tags":["audio","mel-spectrogram","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}