원시 오디오 데이터 처리없이 내 wav 파일의 피치를 변경하려면 파이썬 라이브러리가 필요합니다. 나는 그것을 찾기 위해 몇 시간을 보냈지 만 소스 코드없이 실시간 피치 시프트를 보여주는 이상한 원시 데이터 처리 코드 스 니펫과 비디오를 발견했습니다. 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 피치를 1 반음으로 바꾸고 싶습니다. 어떻게하면 전체 파일을 읽고 whle 파일에 대해 한 번 피치를 변경할 수 있습니까? 나는 각 초의 피치를 바꾸는 것만으로 가능하다고 믿을 수 없다. 'readframes (wr.getnframes())'와'np.roll (lf, 500)'피치가 바뀌지 않고 500보다 큰 다른 값을 사용해야합니다. –
@DanielReshetnikov 제가 다시 썼습니다. 내 대답. 불쾌한 에코를 방지하려면 한 번에 두 번째의 분수를 처리해야합니다. –
이제 전체 파일을 조 변경 할 수 있습니다. 조금 나아 졌어. 이제는 헤르츠의 음조를 변경할 수 있지만 불행히도 헤르츠를 반음으로 변환 할 수는 없습니다 (내 잘못 -이 문제에서 내가 설명하지 않았 음). –
당신은 오디오 파일 전체에서 빠르고 쉽게 피치 변경 및 다른 형식 (등 WAV, MP3)에 대한 pydub을 시도 할 수 있습니다.
다음은 작동 코드입니다. 피치 변경에 대한 자세한 내용은 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
사이트는 우리가 당신을 위해 도서관을 찾거나 그 문제에 대한 하나를 만들려면 여기를하지 않은 상태 규칙. 만약 당신이 수색했거나 하나의 확률을 찾을 수 없다면 아무 것도 없기 때문에 직접 작성해야합니다. 최소한 이것이 표준이며, 나는 당신이 어떤 대답을 얻지 못하거나 질문이 닫히는 경우에 대비하여이 사실을 단순히 알려주고 있습니다. – Torxed
'ffmpeg python'을 검색 엔진에 입력하고 거기에서 가져옵니다. – boardrider