def decode( self, acoustic_feature: AcousticFeature, ): length = len(acoustic_feature.f0) f0_buffer = utils.cast_1d_list_to_1d_pointer(acoustic_feature.f0.flatten().tolist()) sp_buffer = utils.cast_2d_list_to_2d_pointer(acoustic_feature.spectrogram.tolist()) ap_buffer = utils.cast_2d_list_to_2d_pointer(acoustic_feature.aperiodicity.tolist()) apidefinitions._AddParameters(f0_buffer, length, sp_buffer, ap_buffer, self._synthesizer) ys = [] while apidefinitions._Synthesis2(self._synthesizer) != 0: y = numpy.array([self._synthesizer.buffer[i] for i in range(self.buffer_size)]) ys.append(y) if len(ys) > 0: out_wave = Wave( wave=numpy.concatenate(ys), sampling_rate=self.out_sampling_rate, ) else: out_wave = Wave( wave=numpy.empty(0), sampling_rate=self.out_sampling_rate, ) self._before_buffer.append((f0_buffer, sp_buffer, ap_buffer)) # for holding memory if len(self._before_buffer) > 16: self._before_buffer.pop(0) return out_wave
def decode( self, acoustic_feature: AcousticFeature, ): from world4py.native import apidefinitions, utils length = len(acoustic_feature.f0) f0_buffer = utils.cast_1d_list_to_1d_pointer(acoustic_feature.f0.flatten().tolist()) sp_buffer = utils.cast_2d_list_to_2d_pointer(acoustic_feature.spectrogram.tolist()) ap_buffer = utils.cast_2d_list_to_2d_pointer(acoustic_feature.aperiodicity.tolist()) apidefinitions._AddParameters(f0_buffer, length, sp_buffer, ap_buffer, self._synthesizer) ys = [] while apidefinitions._Synthesis2(self._synthesizer) != 0: y = numpy.array([self._synthesizer.buffer[i] for i in range(self.buffer_size)]) ys.append(y) if len(ys) > 0: out_wave = Wave( wave=numpy.concatenate(ys), sampling_rate=self.out_sampling_rate, ) else: out_wave = Wave( wave=numpy.empty(0), sampling_rate=self.out_sampling_rate, ) self._before_buffer.append((f0_buffer, sp_buffer, ap_buffer)) # for holding memory if len(self._before_buffer) > 16: self._before_buffer.pop(0) return out_wave
def code_aperiodicity(aperiodicity, fs, f0_length, fft_size): '''Compress aperiodicity's dimension Args: aperiodicity (POINTER(POINTER(c_double))): Aperiodicity fs (int): Sampling frequency [Hz] f0_length (int): Rank of aperiodicity 1d fft_size (int): FFT size Returns: POINTER(POINTER(c_double)): compressed aperiodicity Notice: You can get compressed apeiodicity's rank via `get_codec_aperiodicity` method ''' tmp_2d_array = [[0 for i in range(get_codec_aperiodicity_num(fs))] for o in range(f0_length)] coded_aperiodicity = utils.cast_2d_list_to_2d_pointer( tmp_2d_array, ctypes.c_double) apidefinitions._CodeAperiodicity(aperiodicity, f0_length, fs, fft_size, coded_aperiodicity) return code_aperiodicity
def decode_spectral_envelope(coded_spectral_envelope, fs, f0_length, number_of_dimensions, fft_size): '''Restore compressed spectral envelope's dimension Args: coded_spectral_envelope (POINTER(POINTER(c_double))): Coded spectral envelope fs (int): Sampling frequency [Hz] f0_length (int): Rank of spectrogram 1d number_of_dimensions (int): Number of compressed spectral envelope's dimension fft_size (int): FFT size Returns: POINTER(POINTER(c_double)): Spectral envelope ''' tmp_2d_array = [[0 for i in range(number_of_dimensions)] for o in range(f0_length)] spectrogram = utils.cast_2d_list_to_2d_pointer(tmp_2d_array, ctypes.c_double) apidefinitions._DecodeSpectralEnvelope(coded_spectral_envelope, f0_length, fs, fft_size, number_of_dimensions, spectrogram) return spectrogram
def d4c(x, fs, temporal_positions, f0, threshold=0.85, f0_floor=DEFAULT_F0_FLOOR, fft_size=None, **dummy): '''Aperiodicity estimation by D4C Args: x (POINTER(c_double)): Input waveform fs (int): a temporal_positions (POINTER(c_double)): Temporal positions f0 (POINTER(c_double)): Extracted F0 threshold (double, optional): To determine frame is unvoice or not (high value is tending to regard as unvoiced) f0_floor (double, optional): To determine fft_size fft_size (double, optional): When fft_size is not set, determined by fs and default f0_floor Returns: POINTER(POINTER(c_double)): Aperiodicity Tuple(int, int): Aperiodicity's shape "(f0_length, fft_size // 2 + 1)" ''' option = structures.D4COption() apidefinitions._InitializeD4COption(option) option.threshold = threshold if fft_size is None: tmp_fft_size = get_fft_size(fs, f0_floor) else: tmp_fft_size = fft_size f0_length = get_f0_length(f0) tmp_2d_array = [[0 for i in range(tmp_fft_size // 2 + 1)] for o in range(f0_length)] aperiodicity = utils.cast_2d_list_to_2d_pointer(tmp_2d_array, ctypes.c_double) apidefinitions._D4C(x, len(list(x)), fs, temporal_positions, f0, f0_length, tmp_fft_size, option, aperiodicity) return aperiodicity, tmp_fft_size, (f0_length, tmp_fft_size // 2 + 1)
def cheap_trick(x, fs, temporal_positions, f0, q1=-0.15, f0_floor=DEFAULT_F0_FLOOR, fft_size=None, **dummy): '''Spectral envelope estimation by CheapTrick Args: x (POINTER(c_double)): Input waveform fs (int): Sampling frequency [Hz] (To determine fft_size) temporal_positions (POINTER(c_double)): Temporal positions f0 (POINTER(c_double)): Extracted F0 q1 (double, optional): Spectral recovery parameter (should not change) f0_floor (double, optional): To determine fft_size fft_size (int, optional): When fft_size is not set, determined by fs and default f0_floor Returns: POINTER(POINTER(c_double)): Spectral envelope int: FFT size used by CheapTrick Tuple(int, int): Spectrogram's shape "(f0_length, fft_size // 2 + 1)" ''' option = structures.CheapTrickOption() apidefinitions._InitializeCheapTrickOption(fs, option) option.q1 = q1 if fft_size is None: option.f0_floor = f0_floor option.fft_size = apidefinitions._GetFFTSizeForCheapTrick(fs, option) else: option.fft_size = fft_size f0_length = get_f0_length(f0) tmp_2d_array = [[0 for i in range(option.fft_size // 2 + 1)] for o in range(f0_length)] spectrogram = utils.cast_2d_list_to_2d_pointer(tmp_2d_array, ctypes.c_double) apidefinitions._CheapTrick(x, len(list(x)), fs, temporal_positions, f0, f0_length, option, spectrogram) return spectrogram, option.fft_size, (f0_length, option.fft_size // 2 + 1)
def decode_aperiodicity(coded_aperiodicity, fs, f0_length, fft_size): '''Restore compressed aperiodicity's dimension Args: coded_aperiodicity (POINTER(POINTER(c_double))): Coded aperiodicity fs (int): Sampling frequency [Hz] f0_length (int): Rank of aperiodicity 1d fft_size (int): FFT size Returns: POINTER(POINTER(c_double)): Aperiodicity ''' tmp_2d_array = [[0 for i in range(get_codec_aperiodicity_num(fs))] for o in range(f0_length)] aperiodicity = utils.cast_2d_list_to_2d_pointer(tmp_2d_array, ctypes.c_double) apidefinitions._DecodeAperiodicity(coded_aperiodicity, f0_length, fs, fft_size, aperiodicity) return aperiodicity