2017-05-14 329 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

我試過你的代碼。音調變化不錯,但播放速度也在變化。我只需要改變音調。 –