Pyaudio record issue on apalis toradex image (IOError: [Errno -9996] Invalid input device (no default output device))

Hello,

I am able to install the pyaudio packages as mentioned in the below link.

https://www.toradex.com/community/questions/17174/want-to-install-the-pyaudio-in-toradex-apalis-boar.html
Now I would like to run some python example to play & record audio.

https://people.csail.mit.edu/hubert/pyaudio/

I have connected the USB audio card to record the voice.

"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

Tried the above example and got the below error.

root@apalis-imx6:/test_pyaudio# python test_arecord.py
Traceback (most recent call last):
  File "test_arecord.py", line 20, in <module>
    frames_per_buffer=CHUNK)
  File "/usr/lib/python2.7/site-packages/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
IOError: [Errno -9996] Invalid input device (no default output device)
root@apalis-imx6:/test_pyaudio#

BTW, I am able to record using ‘arecord’ command.

root@apalis-imx6:/test_pyaudio#
root@apalis-imx6:/test_pyaudio# arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: U0x46d0x81b [USB Device 0x46d:0x81b], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
root@apalis-imx6:/test_pyaudio#
root@apalis-imx6:/test_pyaudio#
root@apalis-imx6:/test_pyaudio# arecord -f CD test2.wav
Recording WAVE 'test2.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
^CAborted by signal Interrupt...
arecord: pcm_read:2096: read error: Interrupted system call
root@apalis-imx6:/test_pyaudio#
root@apalis-imx6:/test_pyaudio#

get_device_count() returns 0.

(env) root@apalis-imx6:/# 
(env) root@apalis-imx6:/# python  
Python 2.7.12 (default, Jul 13 2017, 04:51:34) 
[GCC 6.2.1 20161016] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import pyaudio
>>> pa = pyaudio.PyAudio()
>>> pa.get_device_count()
0L
>>> pa.get_default_input_device_info()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/env/lib/python2.7/site-packages/pyaudio.py", line 949, in get_default_input_device_in
fo
    device_index = pa.get_default_input_device()
IOError: No Default Input Device Available
>>> 

Can you please help me on this issue ?

Thanks for the help.

Hi

Sorry, but we here at Toradex have no experience with pyaudio and/or portaudio.

You seem to have modified the kernel/dtb so that you no longer have the on module codec nor the spdif as a soundcard.

Did you modify /etc/asound.conf with the configuration needed for your USB soundcard?
E.g. as this post suggests?

Did you try the portaudio demo applications?
e.g. pa_devs patest_many?

Otherwise I have no idea.

Max

Thanks max.tx for your reply. Let me check the demo apps.
Have you tried with any external USB audio device ?

Hello max.tx,

I have tried portaudio examples too, it doesn’t work for me.

As you can see below, here I have tried with default image.

Can you please tell me what configuration I am missing here ?
And I am using the default /etc/asound.conf file.

root@apalis-imx6:/portaudio# 
root@apalis-imx6:/portaudio# 
root@apalis-imx6:/portaudio# python
Python 2.7.12 (default, Jul 13 2017, 04:51:34) 
[GCC 6.2.1 20161016] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyaudio
>>> 
>>> p = pyaudio.PyAudio()
>>> info = p.get_host_api_info_by_index(0)
>>> print(info.items())
[('index', 0), ('name', u'OSS'), ('defaultOutputDevice', -1L), ('type', 7L), ('deviceCount'
, 0L), ('defaultInputDevice', -1L), ('structVersion', 1L)]
>>> 
root@apalis-imx6:/portaudio#
root@apalis-imx6:/portaudio#
root@apalis-imx6:/portaudio# arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: imx6qapalissgtl [imx6q-apalis-sgtl5000], device 0: HiFi sgtl5000-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: imxspdif [imx-spdif], device 0: S/PDIF PCM snd-soc-dummy-dai-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 3: CX20924 [CX20924], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
root@apalis-imx6:/portaudio# 
root@apalis-imx6:/portaudio# 
root@apalis-imx6:/portaudio# 
root@apalis-imx6:/portaudio# aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: imx6qapalissgtl [imx6q-apalis-sgtl5000], device 0: HiFi sgtl5000-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: imxspdif [imx-spdif], device 0: S/PDIF PCM snd-soc-dummy-dai-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: imxhdmisoc [imx-hdmi-soc], device 0: i.MX HDMI Audio Tx hdmi-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
root@apalis-imx6:/portaudio# 
root@apalis-imx6:/portaudio# cd /
root@apalis-imx6:/# 
root@apalis-imx6:/# python pyaudio_test_playback.py test.wav
Traceback (most recent call last):
  File "pyaudio_test_playback.py", line 20, in <module>
    output=True)
  File "/usr/lib/python2.7/site-packages/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
IOError: [Errno -9996] Invalid output device (no default output device)
root@apalis-imx6:/# 
root@apalis-imx6:/# 
root@apalis-imx6:/# 
root@apalis-imx6:/# 
root@apalis-imx6:/# python pyaudio_test_record.py           
Traceback (most recent call last):
  File "pyaudio_test_record.py", line 19, in <module>
    frames_per_buffer=CHUNK)
  File "/usr/lib/python2.7/site-packages/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
IOError: [Errno -9996] Invalid input device (no default output device)
root@apalis-imx6:/# 
root@apalis-imx6:/# 
root@apalis-imx6:/# cd /portaudio/bin
root@apalis-imx6:/portaudio/bin# ./patest_many 
Loop 1 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Loop 2 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Loop 3 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Loop 4 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Loop 5 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Loop 6 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Loop 7 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Loop 8 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Loop 9 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Loop 10 out of 10.
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success
Test complete.
root@apalis-imx6:/portaudio/bin# 
root@apalis-imx6:/portaudio/bin# 
root@apalis-imx6:/portaudio/bin# ./pa_devs 
PortAudio version: 0x00130600
Version text: 'PortAudio V19.6.0-devel, revision 396fe4b6699ae929d3a685b3ef8a7e97396139a4'
Number of devices = 0
----------------------------------------------
root@apalis-imx6:/portaudio/bin# 
root@apalis-imx6:/portaudio/bin#

This is how I have installed the portaudio & pyaudio in toradex board.

	Download the portaudio (dependency of pyaudio)
	http://www.portaudio.com/archives/pa_stable_v190600_20161030.tgz
	tar -zxvf pa_stable_v190600_20161030.tgz
	cd portaudio
	./configure
	Edit the Makefile and add '-fPIC' compiler flag.
	make
	make install
	Copy the portaudio static library (.a & .la) from /usr/local/lib to /lib
	pip install pyaudio

We are sorry, but we really cannot support arbitrary standard software on top of the linux software.