这是现有的代码,我已经有了。虽然它只从麦克风中检索一个响度和音高值
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
这些值已设为零,就好像麦克风静音且未使用