def f0_to_coarse(f0, pitch_bin, f0_min, f0_max):
    """
    Convert f0 (Hz) to pitch (mel scale), and then quantize the mel-scale pitch to the
    range from [1, 2, 3, ..., pitch_bin-1]

    Reference: https://en.wikipedia.org/wiki/Mel_scale

    Args:
        f0 (array or Tensor): Hz
        pitch_bin (int): the vocabulary size
        f0_min (int): the minimum f0 (Hz)
        f0_max (int): the maximum f0 (Hz)

    Returns:
        quantized f0 (array or Tensor)
    """
    f0_mel_min = 1127 * np.log(1 + f0_min / 700)
    f0_mel_max = 1127 * np.log(1 + f0_max / 700)

    is_torch = isinstance(f0, torch.Tensor)
    f0_mel = 1127 * (1 + f0 / 700).log() if is_torch else 1127 * np.log(1 + f0 / 700)
    f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * (pitch_bin - 2) / (
        f0_mel_max - f0_mel_min
    ) + 1

    f0_mel[f0_mel <= 1] = 1
    f0_mel[f0_mel > pitch_bin - 1] = pitch_bin - 1
    f0_coarse = (f0_mel + 0.5).long() if is_torch else np.rint(f0_mel).astype(np.int32)
    assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, (
        f0_coarse.max(),
        f0_coarse.min(),
    )
    return f0_coarse
Convert f0 (Hz) to pitch (mel scale), and then quantize the mel-scale pitch to the
range from [1, 2, 3, ..., pitch_bin-1]

Reference: https://en.wikipedia.org/wiki/Mel_scale

Args:
f0 (array or Tensor): Hz
pitch_bin (int): the vocabulary size
f0_min (int): the minimum f0 (Hz)
f0_max (int): the maximum f0 (Hz)

Returns:
quantized f0 (array or Tensor)
utils/f0.py