当前位置:   article > 正文

python改变音频_Python的音频帧音调变化

python 音频 音乐 卡点

I'm attempting to use pyaudio to make a voice masker. With the way I have it set up right now, the only thing I have to do is input the sound, change the pitch on the fly, and chunk it right back out. The first and last part are working, and I think I'm getting close to changing pitch... emphasis on the "think".

Unfortunately, I'm not too familiar with the type of data I'm working with and how exactly to manipulate it the way I want. I've gone through the audioop documentation and havn't found what I needed (thought there are some things I could definately use in there). I guess what I'm asking is...

How is the data formatted in these audio frames.

How can I change the pitch of a frame (if I can), or is it even close to working like that?

import pyaudio

import sys

import numpy as np

import wave

import audioop

import struct

chunk = 1024

FORMAT = pyaudio.paInt16

CHANNELS = 1

RATE = 41000

RECORD_SECONDS = 5

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,

channels = CHANNELS,

rate = RATE,

input = True,

output = True,

frames_per_buffer = chunk)

swidth = 2

print "* recording"

while(True):

data = stream.read(chunk)

data = np.array(wave.struct.unpack("%dh"%(len(data)/swidth), data))*2

data = np.fft.rfft(data)

#MANipulation

data = np.fft.irfft(data)

stream.write(data3, chunk)

print "* done"

stream.stop_stream()

stream.close()

p.terminate()

解决方案

To change the pitch, you'll have to perform an FFT on a number of frames and then shift the data in frequency (move the data to different frequency bins) and perform an inverse FFT.

If you don't mind the sound fragment getting longer while lowering the pitch (or higher when increasing the pitch), you could resample the frames. For instance, you could double each frame (insert a copy of each frame in the stream) thereby lowering the playback speed and the pitch. You can then improve the audio quality by improving the resampling algorithm to use some sort of interpolation and/or filtering.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/354916
推荐阅读
相关标签
  

闽ICP备14008679号