2017-05-14 330 views
1

我需要任何Python库来改变我的WAV文件的音调,而无需任何原始音频数据处理。我花了几个小时找到它,但只发现了一些奇怪的原始数据处理代码片段和视频,它显示了实时音高转换,但没有源代码。Python的变化音调的WAV文件

+0

网站规则的状态,我们不是在这里找到一个库,你可以创建一个对这一问题。如果你已经搜索并找不到一个 - 赔率是没有的,你必须自己写。至少这是常态,如果你没有得到任何答案或者你的问题被关闭,我只是通知你这件事。 – Torxed

+0

在您的搜索引擎中输入'ffmpeg python',然后从中取出。 – boardrider

回答

1

由于wav文件基本上原始音频数据,您将无法改变音高没有“原始音频处理”。

这是你可以做的。 您将需要wave(标准库)和numpy模块。

import wave 
import numpy as np 

打开文件。

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 

使用内置于numpy中的快速傅立叶变换来提取频率。

lf, rf = np.fft.rfft(left), np.fft.rfft(right) 

滚动数组以增加音调。

lf, rf = np.roll(lf, shift), np.roll(rf, shift) 

最高频率翻到最低频率。这不是我们想要的,所以将它们归零。

lf[0:shift], rf[0:shift] = 0, 0 

现在使用傅立叶逆变换的信号转换回幅度。

nl, nr = np.fft.irfft(lf), np.fft.irfft(rf) 

组合这两个通道。

ns = np.column_stack((nl, nr)).ravel().astype(np.int16) 

写出输出数据。

ww.writeframes(ns.tostring()) 

处理所有帧时关闭文件。

wr.close() 
ww.close() 
+0

好的。我可以读第一秒,并将音高改变500(什么?),我想要例如改变音高1个半音。我如何读取整个文件并为whle文件改变一次音高。我不相信只有改变每秒的音调才有可能。当我尝试'readframes(wr.getnframes())'和'np.roll(lf,500)'音调不变,我需要使用另一个更大的值而不是500. –

+0

@DanielReshetnikov我已经重写我的答案。事实证明,您需要一次处理几分之一的时间以防止令人讨厌的回响。 –

+0

现在我可以转置整个文件。这有点好一点。现在我可以改变hertzes的音调,但不幸的是,不可能将hertzes转换为半音(我的错 - 我没有在这个问题中概述它)。 –

0

您可以尝试pydub跨越整个音频文件快速和容易的音调变化和不同的格式(WAV,MP3等)。

这里是一个工作代码。来自here的灵感,并参考here了解更多音高变化细节。

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") 
+1

我试过你的代码。音调变化不错,但播放速度也在变化。我只需要改变音调。 –