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

该博客探讨了如何使用Python的pyaudio库进行实时音频处理,目标是创建一个声音掩码器。作者尝试在读取音频帧后改变音调,但遇到了困难。文章提到了已实现的输入和输出部分,以及对音频帧数据格式和如何改变音调的疑问。解决方案包括使用FFT进行频率域的音调调整或通过插值和过滤的重采样来改变播放速度和音调。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值