2016-09-03 23 views
1

下面的代碼我試圖在樹莓派3運行該模型B具有容量大了一點關於它的記憶,是我面臨的代碼的問題是,它有時運行:如何使用Pyaudio python模塊在沒有溢出的情況下捕獲Raspberry Pi中的音頻?

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設備不可用

+0

您鏈接的代碼不會從麥克風讀取。您需要提供您實際使用的代碼和完整的日誌輸出,而不僅僅是錯誤。在RaspberryPI上使用pocketsphinx不可能使用語言模型進行大量的詞彙連續語音識別,但速度太慢。 –

+0

對不起,我已更新問題帖子。 – 0x01Brain

+0

如果你的音頻配置支持那麼你可以嘗試在44.1khz錄製。然後你需要添加選項'-samprate 44100 -nfft 2048'到解碼器配置。或者,您可以在系統上正確配置pulseaudio/alsa-dmix以爲您進行重採樣。 –

回答

1

首先嚐試打開和關閉流。

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

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

找出捕捉設備(聲卡用於音頻記錄),並記下CARD號和DEVICE號。在下面的例子中,兩者都是0.替換上面插件中的CARD和DEVICE值。

> 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

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"])) 

它將產出指數(9在這裏,但在你的情況可能會有所不同)

DEVICE: record INDEX: 9 RATE: 48000 

現在改變主腳本點點(在p.open()插入input_device_index=9

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

這就是所有,再次運行您的腳本,看問題是否解決。

+0

這並沒有解決問題。 – 0x01Brain

+0

感謝它現在的工作,我確實做了什麼,我不得不終止音頻引擎,並在識別出用戶講話時結束話語。 – 0x01Brain

相關問題