如何使用 Pyaudio python 模块在 Raspberry Pi 中捕获音频而不会溢出? [英] How to capture audio in Raspberry Pi using Pyaudio python module without overflow?

查看:93
本文介绍了如何使用 Pyaudio python 模块在 Raspberry Pi 中捕获音频而不会溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码我试图在 Raspberry Pi 3 Model B 上运行它,它的内存容量有点大,我面临的代码问题是它有时会运行:

The following code I tried to run it on Raspberry Pi 3 Model B which has a little big of capacity on it's memory, the problem that I'm facing with the code is that it runs sometimes:

from os import environ, path
import pyaudio
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *

MODELDIR = "../../../model"
DATADIR = "../../../test/data"

config = Decoder.default_config()
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-lm', path.join(MODELDIR, '3199.lm'))
config.set_string('-dict', path.join(MODELDIR, '3199.dic'))
config.set_string('-logfn', '/dev/null')
decoder = Decoder(config)

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
stream.start_stream()

in_speech_bf = False
decoder.start_utt()
while True:
    buf = stream.read(1024)
    if buf:
        decoder.process_raw(buf, False, False)
        if decoder.get_in_speech() != in_speech_bf:
            in_speech_bf = decoder.get_in_speech()
            if not in_speech_bf:
                decoder.end_utt()
                result = decoder.hyp().hypstr

                print 'Result:', result
                if result == 'yes':
                      print 'Do whatever you want'

                decoder.start_utt()
    else:
        break
decoder.end_utt()

程序不断崩溃并抛出以下异常:OSError: [-9985] Errno 设备不可用

The program keep crashing and throws the following exception: OSError: [-9985] Errno Device unavailable

推荐答案

首先尝试打开和关闭流.

First try opening and closing stream.

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
# stream.start_stream()
in_speech_bf = False
decoder.start_utt()
while True:
    if stream.is_stopped():
        stream.start_stream() 
    buf = stream.read(1024)
    if buf:
        stream.stop_stream()
        decoder.process_raw(buf, False, False)

如果您仍然遇到问题,请尝试 ~/.asoundrc 中的 Alsa Plug 插件

If you still face issue then try Alsa Plug plugin in ~/.asoundrc

pcm.record {
    type plug;
    slave {
        pcm "hw:<CARD>,<DEVICE>"
    }
}

找出CAPTURE Device(用于录音的声卡)并记下CARD号和DEVICE号.在下面的例子中,两者都是 0.替换上面插件中的 CARD 和 DEVICE 值.

Find out CAPTURE Device (Soundcard used for audio recording) and note down CARD number and DEVICE number. In below example both are 0. Replace CARD and DEVICE value in plugin above.

> arecord -l

**** List of CAPTURE Hardware Devices ****
card 0: Devices [USB Device 2345:3x55], device 0: USB Audio [USB Audio]

现在插件看起来像

pcm.record {
    type plug;
    slave {
        pcm "hw:0,0"
    }
}

保存 ~/.asoundrc 文件并重启 RPi.现在使用以下python脚本找出新创建的设备(pcm.record)的index.

Save ~/.asoundrc file and reboot RPi. Now find out index of newly created device (pcm.record) using following python script.

import pyaudio
po = pyaudio.PyAudio()
for index in range(po.get_device_count()): 
    desc = po.get_device_info_by_index(index)
    if desc["name"] == "record":
        print "DEVICE: %s  INDEX:  %s  RATE:  %s " %  (desc["name"], index,  int(desc["defaultSampleRate"]))

它将输出 INDEX(此处为 9,但在您的情况下可能会有所不同)

It will output INDEX (9 here but in your case it might be different)

DEVICE: record  INDEX:  9  RATE:  48000 

现在稍微改变你的主脚本(在 p.open() 中插入 input_device_index=9 )

Now change your main script little bit (insert input_device_index=9 in p.open() )

stream = p.open(format=pyaudio.paInt16, 
                channels=1, 
                rate=16000, 
                input=True, 
                input_device_index=9,
                frames_per_buffer=1024)

就是这样,再次运行您的脚本,看看问题是否已解决.

Thats all, run your script again, see if issue reolve.

这篇关于如何使用 Pyaudio python 模块在 Raspberry Pi 中捕获音频而不会溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆