from pydub import AudioSegment
from pydub.playback import play
sound = AudioSegment.from_file('in.wav', format="wav")
# shift the pitch up by half an octave (speed will increase proportionally)
octaves = 0.5
new_sample_rate = int(sound.frame_rate * (2.0 ** octaves))
# keep the same samples but tell the computer they ought to be played at the
# new, higher sample rate. This file sounds like a chipmunk but has a weird sample rate.
hipitch_sound = sound._spawn(sound.raw_data, overrides={'frame_rate': new_sample_rate})
# now we just convert it to a common sample rate (44.1k - standard audio CD) to
# make sure it works in regular audio players. Other than potentially losing audio quality (if
# you set it too low - 44.1k is plenty) this should now noticeable change how the audio sounds.
hipitch_sound = hipitch_sound.set_frame_rate(44100)
#Play pitch changed sound
play(hipitch_sound)
#export / save pitch changed sound
hipitch_sound.export("out.wav", format="wav")
wr = wave.open('input.wav', 'r')
# Set the parameters for the output file.
par = list(wr.getparams())
par[3] = 0 # The number of samples will be set by writeframes.
par = tuple(par)
ww = wave.open('pitch1.wav', 'w')
ww.setparams(par)
声音应该在一秒钟内被处理。这降低了混响。试着把fr设为1;你会听到烦人的回声。
fr = 20
sz = wr.getframerate()//fr # Read and process 1/fr second at a time.
# A larger number for fr means less reverb.
c = int(wr.getnframes()/sz) # count of the whole file
shift = 100//fr # shifting 100 Hz
for num in range(c):
读取数据,将其分成左声道和右声道(假设是立体声WAV文件)。
da = np.fromstring(wr.readframes(sz), dtype=np.int16)
left, right = da[0::2], da[1::2] # left and right channel
import librosa
y, sr = librosa.load('your_file.wav', sr=16000) # y is a numpy array of the wav file, sr = sample rate
y_shifted = librosa.effects.pitch_shift(y, sr, n_steps=4) # shifted by 4 half steps
您可以尝试pydub在整个音频文件和不同格式(wav、mp3等)中快速轻松地改变音调。
这是一个工作代码。来自here的灵感,有关音高变化的详细信息,请参阅here。
由于
wav
文件基本上是原始音频数据,因此如果没有“原始音频处理”,就无法更改音调。这是你能做的。 您将需要
wave
(标准库)和numpy
模块。打开文件。
声音应该在一秒钟内被处理。这降低了混响。试着把
fr
设为1;你会听到烦人的回声。读取数据,将其分成左声道和右声道(假设是立体声WAV文件)。
使用内置于numpy中的快速傅立叶变换提取频率。
滚动阵列以增加音高。
最高频率转为最低频率。那不是我们想要的,所以把它们归零。
现在使用逆傅里叶变换将信号转换回振幅。
合并这两个频道。
写入输出数据。
处理完所有帧后关闭文件。
我建议尝试Librosa的音高变换功能: https://librosa.github.io/librosa/generated/librosa.effects.pitch_shift.html
相关问题 更多 >
编程相关推荐