"""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