本文介绍了计算帧中的FFT并写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是python的新手,我正在尝试获取上载的wav文件的FFT值,并返回文本文件每行中每帧的FFT(使用GCP)
I'm new to python,I'm trying get a FFT value of a uploaded wav file and return the FFT of each frame in each line of a text file (using GCP)
使用scipy或librosa
using scipy or librosa
我需要的帧速率为30fps
Frame rate i require is 30fps
wave文件的采样率为48k
wave file will be of 48k sample rate
推荐答案
您可以将pyaudio与回调一起使用,以实现所执行的操作.
You can use pyaudio with callback to acheive whatever you are doing.
import pyaudio
import wave
import time
import struct
import sys
import numpy as np
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
def callback_test(data, frame_count, time_info, status):
frame_count =1024
elm = wf.readframes(frame_count) # read n frames
da_i = np.frombuffer(elm, dtype='<i2') # convert to little endian int pairs
da_fft = np.fft.rfft(da_i) # fast fourier transform for real values
da_ifft = np.fft.irfft(da_fft) # inverse fast fourier transform for real values
da_i = da_ifft.astype('<i2') # convert to little endian int pairs
da_m = da_i.tobytes() # convert to bytes
return (da_m, pyaudio.paContinue)
# open stream using callback (3)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),# sampling frequency
output=True,
stream_callback=callback_test)
# # start the stream (4)
stream.start_stream()
# # wait for stream to finish (5)
while stream.is_active():
time.sleep(0.1)
# # stop stream (6)
stream.stop_stream()
stream.close()
wf.close()
# close PyAudio (7)
p.terminate()
请参考以下链接进行进一步研究:
Please refer these links for further study:
https://people.csail.mit.edu/hubert/pyaudio/docs/#example-callback-mode-audio-io 和 Python更改wav文件的音高
这篇关于计算帧中的FFT并写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!