File size: 2,065 Bytes
f0858f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""pYAAPT's f0 interpolation, with the fix the training pipeline applies.

Copied verbatim from `precompute_prosody._patched_interpolate` in the
prosody_embeddings_to_mel repo. Stock pYAAPT mishandles extrapolation before
the first and after the last voiced frame, which can flatten the entire
contour; the preprocessor the training pipeline replaced monkey-patches this
in, so f0_interp only matches the offline pipeline with the same patch.

Kept as a verbatim copy rather than an import because that repo's module
pulls in a submodule that is not checked out. If the original changes, this
must be re-copied.
"""

from __future__ import annotations


def patched_interpolate(self):
    """pYAAPT.PitchObj.interpolate with the MU-NLPC fix, copied verbatim.

    Stock pYAAPT mishandles extrapolation before the first and after the last
    voiced frame, which can flatten the entire f0 contour. The preprocessor we
    are replacing monkey-patches this in, so f0_interp only matches the offline
    pipeline's if the same patch is applied here.
    """
    import numpy as _np
    from scipy import interpolate as scipy_interp
    from scipy.signal import medfilt

    pitch = _np.zeros((self.nframes))
    pitch[:] = self.samp_values
    pitch2 = medfilt(self.samp_values, self.SMOOTH_FACTOR)

    edges = self.edges_finder(pitch)
    first_sample = pitch[0]
    last_sample = pitch[-1]

    if len(_np.nonzero(pitch2)[0]) < 2:
        pitch[pitch == 0] = self.PTCH_TYP
    else:
        nz_pitch = pitch2[pitch2 > 0]
        pitch2 = scipy_interp.pchip(_np.nonzero(pitch2)[0],
                                    nz_pitch)(range(self.nframes))
        pitch[pitch == 0] = pitch2[pitch == 0]
    if self.SMOOTH > 0:
        pitch = medfilt(pitch, self.SMOOTH_FACTOR)
    try:
        if first_sample == 0:
            if edges[0] == 0:
                edges[0] = 1
            pitch[:edges[0] - 1] = pitch[edges[0]]
        if last_sample == 0:
            pitch[edges[-1] + 1:] = pitch[edges[-1]]
    except Exception:
        pass
    self.samp_interp = pitch