我需要任何Python庫來改變我的WAV文件的音調,而無需任何原始音頻數據處理。我花了幾個小時找到它,但只發現了一些奇怪的原始數據處理代碼片段和視頻,它顯示了實時音高轉換,但沒有源代碼。Python的變化音調的WAV文件
回答
由於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()
好的。我可以讀第一秒,並將音高改變500(什麼?),我想要例如改變音高1個半音。我如何讀取整個文件併爲whle文件改變一次音高。我不相信只有改變每秒的音調纔有可能。當我嘗試'readframes(wr.getnframes())'和'np.roll(lf,500)'音調不變,我需要使用另一個更大的值而不是500. –
@DanielReshetnikov我已經重寫我的答案。事實證明,您需要一次處理幾分之一的時間以防止令人討厭的迴響。 –
現在我可以轉置整個文件。這有點好一點。現在我可以改變hertzes的音調,但不幸的是,不可能將hertzes轉換爲半音(我的錯 - 我沒有在這個問題中概述它)。 –
您可以嘗試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")
我試過你的代碼。音調變化不錯,但播放速度也在變化。我只需要改變音調。 –
我建議嘗試Librosa的音高移功能: https://librosa.github.io/librosa/generated/librosa.effects.pitch_shift.html
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
- 1. Python播放mp3/wav文件音頻(Pygame調音臺)
- 2. Python - 向下採樣wav音頻文件
- 3. Python算法去噪音wav文件
- 4. 語音音調的變化
- 5. 在matlab中改變音頻wav文件的音高?
- 6. Wav文件語音文本
- 7. 分割音頻文件(WAV)
- 8. Python的讀取和寫入的聲音頻率,以WAV文件
- 9. 更改python中的wav文件的音量
- 10. WAV文件缺少音頻和錄音
- 11. 將具體音量和音調設置爲.wav文件
- 12. 去除wav文件上的噪音
- 13. 抑制wav文件中的噪音
- 14. 在wav文件中的聲音/沉默
- 15. C++ - 正常化wav文件的音量級別
- 16. 使用python在音頻文件中的某個點插入WAV
- 17. 我想通過語音調制解調器播放Wav文件
- 18. 計算wav文件和錄製聲音的聲譜圖(正常化音量)
- 19. 轉換音樂wav文件的文本,我想給一個音頻wav文件的符號
- 20. 音頻文件的每個幀的音頻頻率,如.mp3 .wav
- 21. 更改.wav文件的播放頻率(人聲音調爲動物聲音)?
- 22. Python上的麥克風的Wav文件
- 23. 將FLAC音頻文件隱藏到wav音頻文件在android
- 24. 解釋.WAV文件[Python]
- 25. Python Scipy FFT WAV文件
- 26. 如何更改節點中音頻WAV文件的音高?
- 27. 如何使用Python從.wav文件中提取聲音信封?
- 28. Android - 獲取來自wav文件的幅度變化
- 29. VB播放隨機聲音wav文件
- 30. Java - 下采樣wav音頻文件
網站規則的狀態,我們不是在這裏找到一個庫,你可以創建一個對這一問題。如果你已經搜索並找不到一個 - 賠率是沒有的,你必須自己寫。至少這是常態,如果你沒有得到任何答案或者你的問題被關閉,我只是通知你這件事。 – Torxed
在您的搜索引擎中輸入'ffmpeg python',然後從中取出。 – boardrider