将音频数据从ALSA缓冲区读取到numpy数组

时间:2016-09-26 20:31:38

标签: python arrays numpy alsa

我试图从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()

Image with result

1 个答案:

答案 0 :(得分:0)

hexdump -d将内容显示为 unsigned 16位整数,而您将其转换为 signed int16 numpy数组。

尝试转换为'u2'(或等效np.uint16)的dtype,您会看到输出与hexdump的输出相匹配:

print(np.array([-7, -9, -10, -6, -2], dtype=np.uint16))
# [65529 65527 65526 65530 65534]