如何解决我如何从麦克风的其他样本中检索音高和响度?
这是现有的代码,我已经有了。虽然它只从麦克风中检索一个响度和音调值
import aubio
import numpy as num
import pyaudio
import sys
import requests
BUFFER_SIZE = 2048
CHANNELS = 1
FORMAT = pyaudio.paFloat32
METHOD = "default"
SAMPLE_RATE = 44100
HOP_SIZE = BUFFER_SIZE//2
PERIOD_SIZE_IN_FRAME = HOP_SIZE
def main(args):
# Initiating PyAudio object.
pA = pyaudio.PyAudio()
# Open the microphone stream.
mic = pA.open(format=FORMAT,channels=CHANNELS,rate=SAMPLE_RATE,input=True,frames_per_buffer=PERIOD_SIZE_IN_FRAME)
# Initiating Aubio's pitch detection object.
pDetection = aubio.pitch(METHOD,BUFFER_SIZE,HOP_SIZE,SAMPLE_RATE)
# Set unit.
pDetection.set_unit("Hz")
# Frequency under -40 dB will considered
# as a silence.
pDetection.set_silence(-40)
# Infinite loop!
while True:
# mic listening
data = mic.read(PERIOD_SIZE_IN_FRAME)
# Convert into number that Aubio understand.
samples = num.fromstring(data,dtype=aubio.float_type)
# Finally get the pitch.
pitch = pDetection(samples)[0]
# Compute the energy (volume)
# of the current frame.
volume = num.sum(samples**2)/len(samples)
# format output
# display 6 decimal digits like 0.000000.
volume = "{:6f}".format(volume)
# print loudness value and pitch value
print(str(pitch/300) + " " + str(volume))
url = "http://192.168.43.89:8000"
requests.post(url,"[" + str(pitch/300) + "," + str(volume) + "]")
if __name__ == "__main__": main(sys.argv)
目标:获取脚本以检索麦克风的多个样本,例如 16 个样本。
我的伪代码看起来像
pitch1 = pDetection(samples)[0]
pitch2 = pDetection(samples)[1]
pitch3 = pDetection(samples)[2]
pitch4 = pDetection(samples)[3]
volume1 = num.sum(samples[0]**2)/len(samples[0])
volume2 = num.sum(samples[1]**2)/len(samples[1])
volume3 = num.sum(samples[2]**2)/len(samples[2])
volume4 = num.sum(samples[3]**2)/len(samples[3])
我认为如果我打印所有值,输出将是:
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
这些值已被设为零,就好像麦克风处于静音状态且未被使用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。