簡體   English   中英

Python:改變音頻文件的音高

[英]Python: Change Pitch of Audio File

這是我在堆棧上的第一篇文章。 到目前為止,這個網站非常有用,但我是一個新手,需要清楚解釋我的問題,這與Python中的音調轉換音頻有關。 我安裝了當前的模塊:numpy,scipy,pygame和scikits“samplerate”api。

我的目標是采用立體聲文件,並以盡可能少的步驟以不同的音高播放。 目前,我使用pygame.sndarray將文件加載到數組中,然后使用scikits.samplerate.resample應用samplerate轉換,然后將輸出轉換回聲音對象以使用pygame進行回放。 問題是垃圾音頻來自我的揚聲器。 當然,我錯過了幾個步驟(除了對數學和音頻一無所知)。

謝謝。

import time, numpy, pygame.mixer, pygame.sndarray
from scikits.samplerate import resample

pygame.mixer.init(44100,-16,2,4096)

# choose a file and make a sound object
sound_file = "tone.wav"
sound = pygame.mixer.Sound(sound_file)

# load the sound into an array
snd_array = pygame.sndarray.array(sound)

# resample. args: (target array, ratio, mode), outputs ratio * target array.
# this outputs a bunch of garbage and I don't know why.
snd_resample = resample(snd_array, 1.5, "sinc_fastest")

# take the resampled array, make it an object and stop playing after 2 seconds.
snd_out = pygame.sndarray.make_sound(snd_resample)
snd_out.play()
time.sleep(2)

你的問題是,pygame的工作與numpy.int16陣列但將呼叫resample返回numpy.float32陣列:

>>> snd_array.dtype
dtype('int16')
>>> snd_resample.dtype
dtype('float32')

您可以將resample結果numpy.int16使用astype

>>> snd_resample = resample(snd_array, 1.5, "sinc_fastest").astype(snd_array.dtype)

通過這個修改,你的python腳本很好地播放tone.wav文件,以較低的音高和較低的速度播放。

你最好的選擇可能是使用python audiere。

這是一個鏈接,我用它來做同樣的事情,它很簡單,只需閱讀所有文檔。

http://audiere.sourceforge.net/home.php

很可能scikits.samplerate.resample“思考”你的音頻是另一種格式而不是16位立體聲。 檢查scikits.samplerate上關於在陣列中選擇正確音頻格式的位置的文檔 - 如果重新采樣16位音頻,將其視為8位垃圾就會出現。

scikits.samplerate.resample文檔:

如果輸入具有等級1,則使用所有數據,並且假設它們來自單聲道信號。 如果rank為2,則數字列將被假定為通道數。

所以我認為你需要做的是將立體聲數據傳遞給它所期望的格式resample

snd_array = snd_array.reshape((-1,2))

snd_resample = resample(snd_array, 1.5, "sinc_fastest")

snd_resample = snd_resample.reshape(-1) # Flatten it out again

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM