- 读取音频
import librosa
import pyworld
sound, _ = librosa.load(wav_path, sr=16000)
print(f'sound.shape = {sound.shape}') #sound.shape = (80000,)
- 提取基频F0
sr = 16000
#输入sound 需要为 double类型 librosa load 的waveform 是 float32
print(f'sound.dtype = {sound.dtype}') # sound.dtype = float32
sound = sound.astype(np.double)
#第一种
_f0, t = pw.dio(sound, sr) # raw pitch extractor
f0 = pw.stonemask(sound, _f0, t, sr) # pitch refinement
#第二种
f0, timeaxis = pyworld.harvest(sound, sr)
print(f'f0.shape = {f0.shape}') # f0.shape = (1001,)
基频维度计算
源码
#python
f0_length = GetSamplesForHarvest(fs, x_length, option.frame_period)
#c++
int GetSamplesForHarvest(int fs, int x_length, double frame_period) {
return static_cast<int>(1000.0 * x_length / fs / frame_period) + 1;
}
3.提取非周期特征AP
fft_size = 1024
ap = pyworld.d4c(sound, f0, timeaxis, sr, fft_size=fft_size)
print(f'ap.shape = {ap.shape}') # ap.shape = (1001, 513)
维度计算
dim = fft_size//2 + 1
- 提取 频谱包络 SP
sp = pyworld.cheaptrick(wav, f0, timeaxis, sr, fft_size=fft_size)
print(f'sp.shape = {sp.shape}')#ap.shape = (1001, 513)
# 降维
coded_sp = pyworld.code_spectral_envelope(sp, sr, dim=128)
print(f'coded_sp.shape = {coded_sp.shape}') #coded_sp.shape = (1001, 128)