【问题标题】:Reading audio data from an ALSA buffer to a numpy array从 ALSA 缓冲区读取音频数据到 numpy 数组
【发布时间】: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()

【问题讨论】:

    标签: python arrays numpy alsa


    【解决方案1】:

    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]
    

    【讨论】:

    • ali_m 你说得对,但这不是问题。我尝试找到问题,为什么从 alsa 获取错误数据。
    • 你怎么知道你得到了“错误的数据”?您期望获得什么价值?
    • 我预计 int16 范围内的数据签名为 -32,768 到 32,767。我不知道做错了什么。可能是我的麦克风工作不正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多