| 20 | |
| 21 | |
| 22 | def f0_to_coarse(f0): |
| 23 | is_torch = isinstance(f0, torch.Tensor) |
| 24 | f0_mel = 1127 * (1 + f0 / 700).log() if is_torch else 1127 * np.log(1 + f0 / 700) |
| 25 | f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * (f0_bin - 2) / (f0_mel_max - f0_mel_min) + 1 |
| 26 | |
| 27 | f0_mel[f0_mel <= 1] = 1 |
| 28 | f0_mel[f0_mel > f0_bin - 1] = f0_bin - 1 |
| 29 | f0_coarse = (f0_mel + 0.5).long() if is_torch else np.rint(f0_mel).astype(np.int) |
| 30 | assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, (f0_coarse.max(), f0_coarse.min()) |
| 31 | return f0_coarse |
| 32 | |
| 33 | |
| 34 | def norm_f0(f0, uv, hparams): |