语音变音调和加速减速

大道之行,天下为公。这篇文章主要讲述语音变音调和加速减速相关的知识,希望能为你提供帮助。

使用代码

"""
@FileName: Test.py
@Description: Implement Test
@Author: Ryuk
@CreateDate: 2020/12/08
@LastEditTime: 2020/12/08
@LastEditors: Please set LastEditors
@Version: v0.1
"""

from td_psola import *
import librosa
import soundfile as sf
import matplotlib.pyplot as plt


x, fs = librosa.load("./test.wav", sr=8000)
pitch_scale = 1.5
time_scale = 1.5
y = Processing(x, fs, pitch_scale, time_scale, cutoff_freq=500)
sf.write("./out.wav", y, fs)

fig=plt.figure()
ax=fig.add_subplot(2, 1, 1)
ax.plot(x)
ax=fig.add_subplot(2, 1, 2)
ax.plot(y)
plt.savefig("q.png")

if __name__ == __main__:
pass


实际代码
【语音变音调和加速减速】
"""
@FileName: Algorithm.py
@Description: Implement Algorithm
@Author: Ryuk
@CreateDate: 2020/12/07
@LastEditTime: 2020/12/07
@LastEditors: Please set LastEditors
@Version: v0.1
"""

import numpy as np
from scipy import signal

def Processing(x, fs, pitch_scale, time_scale, cutoff_freq=500):
# normalize
x = x - np.mean(x)
x = x / np.max(np.abs(x))

#x = LowPassFilter(x, fs, cutoff_freq)
pitch = PitchEstimator(x, fs)
output = PitchMark(x, pitch, fs, pitch_scale, time_scale)
return output

def LowPassFilter(x, fs, cutoff_freq):
if cutoff_freq == 0:
return x
else:
factor = np.exp(-1 / (fs / cutoff_freq))
y = signal.filtfilt([1 - factor], [1, -factor], x)
return y


def PitchEstimator(x, fs):
frame_length = round(fs * 0.03)
frame_shift = round(fs * 0.01)
length = len(x)
frame_num = int(np.floor((length - frame_length)/ frame_shift)) + 2
frame_pitch = np.zeros(frame_num + 2)

frame_range = np.arange(0, frame_length)
for count in range(1, frame_num):
frame = x[frame_range]
frame_pitch[count] = PitchDetection(frame, fs)
frame_range += frame_shift

frame_pitch = signal.medfilt(frame_pitch, 5)

pitch = np.zeros(length)
for i in range(length):
index = int(np.floor((i + 1) / frame_shift))
pitch[i] = frame_pitch[index]
return pitch

def CenterClipping(x, clip_rate):
max_amplitude = np.max(np.abs(x))
clip_level = max_amplitude * clip_rate
positive_index = np.where(x > clip_level)
negative_index = np.where(x < -clip_level)
clipped_data = https://www.songbingjia.com/android/np.zeros(len(x))
clipped_data[positive_index] = x[positive_index] - clip_level
clipped_data[negative_index] = x[negative_index] + clip_level
return clipped_data

def AutoCorrelation(x, lags):
N = len(x)
auto_corr = np.correlate(x, x, mode = full)
assert N > = lags - 1
auto_corr = auto_corr[N - lags - 1 : N + lags]
auto_corr = auto_corr / np.max(auto_corr)
return auto_corr

def IsPeak(index, low, high, x):
if index == low or index == high:
return False
if x[index] < x[index-1] or x[index] < x[index+1]:
return False
return True

def PitchDetection(x, fs):
min_lag = round(fs / 500)
max_lag = round(fs / 70)
x = CenterClipping(x, 0.3)

auto_corr = AutoCorrelation(x, max_lag)
auto_corr = auto_corr[max_lag: 2 * max_lag]

search_range = auto_corr[min_lag - 1:max_lag]
max_corr = np.max(search_range)
max_corr_index

    推荐阅读