【发布时间】:2017-02-04 07:55:30
【问题描述】:
我正在尝试从 ALSA 缓冲区中提取数据,以便从麦克风中生成计数噪音。但是,当我尝试将数据转换为数组时,我得到了不正确的结果。
以下是我的部分代码:
#!/usr/bin/env python
from __future__ import print_function
import alsaaudio
import numpy
card = 'default'
buf = [64]
numpy.set_printoptions(threshold=numpy.inf)
stream = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, card)
stream.setchannels(1)
stream.setrate(44100)
stream.setformat(alsaaudio.PCM_FORMAT_S16_LE)
stream.setperiodsize(64)
def listen():
print("Listening")
while True:
try:
l, data = stream.read()
f = open('test.raw', 'wb')
if l:
f.write(data)
f.close()
except IOError, e:
error_count += 1
print(" (%d) Error recording: %s" % (error_count, e))
else:
decoded_block = numpy.frombuffer(data, dtype='i2' )
print('Array PCM: \n',decoded_block)
return 0
listen()
【问题讨论】: