Python更改wav文件的音调

Dan*_*kov 10 python audio wav pitch

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

Nic*_*aro 11

我建议尝试 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
Run Code Online (Sandbox Code Playgroud)


Rol*_*ith 10

由于wav文件基本上原始音频数据,因此如果没有 “原始音频处理” ,您将无法更改音调。

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

import wave
import numpy as np
Run Code Online (Sandbox Code Playgroud)

打开文件。

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)
Run Code Online (Sandbox Code Playgroud)

声音应在几分之一秒内进行处理。这样可以减少混响。尝试设置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):
Run Code Online (Sandbox Code Playgroud)

读取数据,将其拆分为左右声道(假设为立体声WAV文件)。

    da = np.fromstring(wr.readframes(sz), dtype=np.int16)
    left, right = da[0::2], da[1::2]  # left and right channel
Run Code Online (Sandbox Code Playgroud)

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

    lf, rf = np.fft.rfft(left), np.fft.rfft(right)
Run Code Online (Sandbox Code Playgroud)

滚动阵列以增加音高。

    lf, rf = np.roll(lf, shift), np.roll(rf, shift)
Run Code Online (Sandbox Code Playgroud)

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

    lf[0:shift], rf[0:shift] = 0, 0
Run Code Online (Sandbox Code Playgroud)

现在,使用傅立叶逆变换将信号转换回振幅。

    nl, nr = np.fft.irfft(lf), np.fft.irfft(rf)
Run Code Online (Sandbox Code Playgroud)

结合两个渠道。

    ns = np.column_stack((nl, nr)).ravel().astype(np.int16)
Run Code Online (Sandbox Code Playgroud)

写入输出数据。

    ww.writeframes(ns.tostring())
Run Code Online (Sandbox Code Playgroud)

处理完所有帧后,关闭文件。

wr.close()
ww.close()
Run Code Online (Sandbox Code Playgroud)


Ani*_*l_M 5

您可以尝试使用pydub,以便在整个音频文件中快速便捷地更改音高,并更改不同格式(wav,mp3等)。

这是一个工作代码。从这里得到启发,并在此处获得有关音高变化的更多详细信息。

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")
Run Code Online (Sandbox Code Playgroud)

  • 我尝试了您的代码。音高变化很好,但是演奏速度也在变化。我只需要改变音调。 (2认同)