Daporte commited on
Commit
d2dc799
·
verified ·
1 Parent(s): 6020e82

Upload _yaapt_patch.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. _yaapt_patch.py +55 -0
_yaapt_patch.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """pYAAPT's f0 interpolation, with the fix the training pipeline applies.
2
+
3
+ Copied verbatim from `precompute_prosody._patched_interpolate` in the
4
+ prosody_embeddings_to_mel repo. Stock pYAAPT mishandles extrapolation before
5
+ the first and after the last voiced frame, which can flatten the entire
6
+ contour; the preprocessor the training pipeline replaced monkey-patches this
7
+ in, so f0_interp only matches the offline pipeline with the same patch.
8
+
9
+ Kept as a verbatim copy rather than an import because that repo's module
10
+ pulls in a submodule that is not checked out. If the original changes, this
11
+ must be re-copied.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+
17
+ def patched_interpolate(self):
18
+ """pYAAPT.PitchObj.interpolate with the MU-NLPC fix, copied verbatim.
19
+
20
+ Stock pYAAPT mishandles extrapolation before the first and after the last
21
+ voiced frame, which can flatten the entire f0 contour. The preprocessor we
22
+ are replacing monkey-patches this in, so f0_interp only matches the offline
23
+ pipeline's if the same patch is applied here.
24
+ """
25
+ import numpy as _np
26
+ from scipy import interpolate as scipy_interp
27
+ from scipy.signal import medfilt
28
+
29
+ pitch = _np.zeros((self.nframes))
30
+ pitch[:] = self.samp_values
31
+ pitch2 = medfilt(self.samp_values, self.SMOOTH_FACTOR)
32
+
33
+ edges = self.edges_finder(pitch)
34
+ first_sample = pitch[0]
35
+ last_sample = pitch[-1]
36
+
37
+ if len(_np.nonzero(pitch2)[0]) < 2:
38
+ pitch[pitch == 0] = self.PTCH_TYP
39
+ else:
40
+ nz_pitch = pitch2[pitch2 > 0]
41
+ pitch2 = scipy_interp.pchip(_np.nonzero(pitch2)[0],
42
+ nz_pitch)(range(self.nframes))
43
+ pitch[pitch == 0] = pitch2[pitch == 0]
44
+ if self.SMOOTH > 0:
45
+ pitch = medfilt(pitch, self.SMOOTH_FACTOR)
46
+ try:
47
+ if first_sample == 0:
48
+ if edges[0] == 0:
49
+ edges[0] = 1
50
+ pitch[:edges[0] - 1] = pitch[edges[0]]
51
+ if last_sample == 0:
52
+ pitch[edges[-1] + 1:] = pitch[edges[-1]]
53
+ except Exception:
54
+ pass
55
+ self.samp_interp = pitch