Python更改wav文件的间距

Python更改wav文件的间距,python,audio,wav,pitch,Python,Audio,Wav,Pitch,我需要任何python库来改变我的wav文件的音高,而无需任何原始音频数据处理。 我花了几个小时才找到它,但只找到了一些奇怪的原始数据处理代码片段和视频,它们显示了实时音高变化,但没有源代码。由于wav文件基本上是原始音频数据,如果没有“原始音频处理”,您将无法更改音高 这是你能做的。 您将需要wave(标准库)和numpy模块 import wave import numpy as np 打开文件 wr = wave.open('input.wav', 'r') # Set the para

我需要任何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文件)

使用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()
您可以尝试在整个音频文件和不同格式(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")

我建议尝试Librosa的音高转换功能:


网站规则规定,我们不是来为您查找库或为此创建库的。如果你已经搜索过了,但找不到一个,很可能没有,你必须自己写。至少这是正常的,我只是简单地告诉你这一点,以防你没有得到任何答案或你的问题被关闭。在你的搜索引擎中输入
ffmpeg-python
,然后从那里开始。好的。我可以读第一秒,改变音高500(什么?),例如,我想改变音高1个半音。如何读取整个文件并为whle文件更改一次其音高。我不敢相信仅仅改变每一秒的音高是可能的。当我尝试
readframes(wr.getnframes())
np.roll(lf,500)
音高不变,我需要使用另一个更大的值,而不是500。@DanielReshetnikov我已经写下了我的答案。事实证明,你需要一次处理几秒钟来防止恶劣的回声。现在我可以转置整个文件了。好一点。现在我可以改变赫兹的音高,但不幸的是,无法将赫兹转换为半音(我的错——我没有在本期中概述它)。如果有人想稍微移动,比如3.4赫兹的钢琴低频会稍微移动,比如我试过你的代码。音高变化很好,但比赛速度也在变化。我只需要改变音高。这有点遥不可及,但这种方法有没有保持速度的方法?你的两个URL都重定向到GitHub上的同一个问题,音质好,速度快。
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")
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