【鼎革‧革鼎】︰ Raspbian Stretch 《六之 K.3-言語界面-6.5 》

話說從頭,年前友人偶訪談及 ReSpeaker Raspbian Jessie 相容性問題 ?意外引發

PyAudio

PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms, such as GNU/Linux, Microsoft Windows, and Apple Mac OS X / macOS.

PyAudio is inspired by:

錯誤訊息

pi@raspberrypi:~ python3 Python 3.5.3 (default, Jan 19 2017, 14:11:04)  [GCC 6.3.0 20170124] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pyaudio >>> p = pyaudio.PyAudio() ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device' ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such file or directory ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device' ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such file or directory ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa ALSA lib pcm_dmix.c:990:(snd_pcm_dmix_open) The dmix plugin supports only playback stream ALSA lib pcm_dmix.c:990:(snd_pcm_dmix_open) The dmix plugin supports only playback stream ALSA lib pcm_ac108.c:469:(_snd_pcm_ac108_open) a108 is only for capture Expression 'ioctl( devHandle, SNDCTL_DSP_CHANNELS, &numChannels )' failed in 'src/hostapi/oss/pa_unix_oss.c', line: 414 Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock >>></pre>    <span style="color: #808000;">誰該負責之論!</span>  <span style="color: #808000;">由於那是多種『語音辨識』平台使用的程式庫,網路上早已嘰嘰喳喳滿是 Q&A 矣!?</span>  <span style="color: #808080;">─── 《<a style="color: #808080;" href="http://www.freesandal.org/?p=80711">Raspbian Stretch 《六之 K 》</a>》</span>     <span style="color: #666699;">既然已花了<a style="color: #666699;" href="http://www.freesandal.org/?p=80763">相當的篇幅</a>介紹派生之 <a style="color: #666699;" href="http://www.freesandal.org/?p=80740">Sound Device</a> ,想必讀者自能應用吧☆</span>  <span style="color: #666699;">此處回返略補充 pyaudio 文件 </span> <h1><span style="color: #ff9900;"><a style="color: #ff9900;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.PyAudio.get_default_input_device_info">PyAudio Documentation</a></span></h1> <div id="contents" class="contents topic"> <span style="color: #808080;">Contents</span>  <ul class="simple">  	<li><span style="color: #808080;"><a id="id1" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio-documentation">PyAudio Documentation</a></span> <ul>  	<li><span style="color: #808080;"><a id="id2" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#module-pyaudio">Introduction</a></span> <ul>  	<li><span style="color: #808080;"><a id="id3" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#example-blocking-mode-audio-i-o">Example: Blocking Mode Audio I/O</a></span></li>  	<li><span style="color: #808080;"><a id="id4" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#example-callback-mode-audio-i-o">Example: Callback Mode Audio I/O</a></span></li>  	<li><span style="color: #808080;"><a id="id5" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#overview">Overview</a></span></li>  	<li><span style="color: #808080;"><a id="id6" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#details">Details</a></span></li> </ul> </li>  	<li><span style="color: #808080;"><a id="id7" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#class-pyaudio">Class PyAudio</a></span></li>  	<li><span style="color: #808080;"><a id="id8" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#class-stream">Class Stream</a></span></li>  	<li><span style="color: #808080;"><a id="id9" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#platform-specific">Platform Specific</a></span></li> </ul> </li>  	<li><span style="color: #808080;"><a id="id10" class="reference internal" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#indices-and-tables">Indices and tables</a></span></li> </ul> </div> <div id="module-pyaudio" class="section"> <h2><span style="color: #808080;"><a class="toc-backref" style="color: #808080;" href="http://people.csail.mit.edu/hubert/pyaudio/docs/#id2">Introduction</a></span></h2> <span style="color: #808080;">PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms. PyAudio is inspired by:</span> <ul class="simple">  	<li><span style="color: #808080;">pyPortAudio/fastaudio: Python bindings for PortAudio v18 API.</span></li>  	<li><span style="color: #808080;">tkSnack: cross-platform sound toolkit for Tcl/Tk and Python.</span></li> </ul> </div>    <span style="color: #666699;">及驗證乙事。</span> <pre class="lang:default decode:true">pi@raspberrypi:~ ipython3 
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import pyaudio

In [2]: pyaudio.get_portaudio_version_text()
Out[2]: 'PortAudio V19.6.0-devel, revision 396fe4b6699ae929d3a685b3ef8a7e97396139a4'

In [3]: pyaudio.get_portaudio_version()
Out[3]: 1246720

In [4]: p = pyaudio.PyAudio()
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
...

In [5]: p.get_device_count()
Out[5]: 10

In [6]: p. get_default_input_device_info()
Out[6]: 
{'defaultHighInputLatency': 0.034829931972789115,
 'defaultHighOutputLatency': -1.0,
 'defaultLowInputLatency': 0.005804988662131519,
 'defaultLowOutputLatency': -1.0,
 'defaultSampleRate': 44100.0,
 'hostApi': 0,
 'index': 2,
 'maxInputChannels': 2,
 'maxOutputChannels': 0,
 'name': 'seeed-4mic-voicecard: - (hw:1,0)',
 'structVersion': 2}

In [7]: p.get_default_output_device_info()
Out[7]: 
{'defaultHighInputLatency': -1.0,
 'defaultHighOutputLatency': 0.034829931972789115,
 'defaultLowInputLatency': -1.0,
 'defaultLowOutputLatency': 0.005804988662131519,
 'defaultSampleRate': 44100.0,
 'hostApi': 0,
 'index': 8,
 'maxInputChannels': 0,
 'maxOutputChannels': 2,
 'name': 'default',
 'structVersion': 2}

In [8]: p.get_device_info_by_index(2)
Out[8]: 
{'defaultHighInputLatency': 0.034829931972789115,
 'defaultHighOutputLatency': -1.0,
 'defaultLowInputLatency': 0.005804988662131519,
 'defaultLowOutputLatency': -1.0,
 'defaultSampleRate': 44100.0,
 'hostApi': 0,
 'index': 2,
 'maxInputChannels': 2,
 'maxOutputChannels': 0,
 'name': 'seeed-4mic-voicecard: - (hw:1,0)',
 'structVersion': 2}

In [9]:

 

測試還請參考︰

/sound_recorder.py

Created Jan 28, 2014

Simple script to record sound from the microphone, dependencies: easy_install pyaudio