berry150
Posts: 8
Joined: Wed Feb 26, 2014 2:39 am

[Pyaudio] Error: invalid number of channels -9998

Mon Mar 03, 2014 2:41 am

Hello,
I'd like to run this code that I got from this site and measure the loudness of a sound input from a mic.

Code: Select all

import numpy
import pyaudio
import analyse

# Initialize PyAudio
pyaud = pyaudio.PyAudio()

# Open input stream, 16-bit mono at 48000 Hz
# On my system, device 4 is a USB camera
stream = pyaud.open(
    format = pyaudio.paInt16,
    channels = 1, 
    rate = 48000,
    input_device_index = 4,
    input = True)

while True:
    # Read raw microphone data
    rawsamps = stream.read(1024)
    # Convert raw data to NumPy array
    samps = numpy.fromstring(rawsamps, dtype=numpy.int16)
    # Show the volume
    print analyse.loudness(samps)
This is the device I am using(device n°4):

Code: Select all

[email protected] ~ $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 046d:0825 Logitech, Inc. Webcam C270
When I run the code, I get this error:

Code: Select all

[email protected] ~/Desktop $ python soundlevelmeter.py
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.front.0:CARD=0'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM front
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.surround40.0:CARD=0'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM surround40
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM surround41
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM surround50
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM surround51
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.surround71.0:CARD=0'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM surround71
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM iec958
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM spdif
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM spdif
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
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

#according to another topic (http://www.raspberrypi.org/phpBB3/viewtopic.php?p=173947), I can ignore all of the above lines

Expression 'parameters->channelCount <= maxChans' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1438
Expression 'ValidateParameters( inputParameters, hostApi, StreamDirection_In )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2742
Traceback (most recent call last):
  File "soundlevelmeter.py", line 15, in <module>
    input = True)
  File "/usr/lib/pymodules/python2.7/pyaudio.py", line 714, in open
    stream = Stream(self, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/pyaudio.py", line 396, in __init__
    self._stream = pa.open(**arguments)
IOError: [Errno Invalid number of channels] -9998
I don't see why I should change the number of channels...I tried the code with different channel numbers and I still get the same error message. Does anyone know what's wrong with all this?
Thank you for reading.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: [Pyaudio] Error: invalid number of channels -9998

Mon Mar 03, 2014 7:49 pm

Not a clue really but try this..

Code: Select all

stream = pyaud.open(
    format = pyaudio.paInt16,
    channels = 2, 
    rate = 44100,
    input_device_index = 4,
    input = True,
    frames_per_buffer = 1024)
Dave.
Apple say... Monkey do !!

User avatar
jojopi
Posts: 3142
Joined: Tue Oct 11, 2011 8:38 pm

Re: [Pyaudio] Error: invalid number of channels -9998

Mon Mar 03, 2014 8:44 pm

berry150 wrote:Expression 'parameters->channelCount <= maxChans' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1438
Perhaps the device is output-only and supports zero input channels.

Device indexes in pyaudio are pretty arbitrary. They do not correspond to USB device numbers. Not all sound cards are USB and a card may appear as more than one device. To check that you are using the correct index:

Code: Select all

import pyaudio
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
  dev = p.get_device_info_by_index(i)
  print((i,dev['name'],dev['maxInputChannels']))
I do not have a USB sound card, so my Pi supports no inputs at all:

Code: Select all

(0, 'bcm2835 ALSA: bcm2835 ALSA (hw:0,0)', 0)
(1, 'sysdefault', 0)
(2, 'default', 0)
(3, 'dmix', 0)

berry150
Posts: 8
Joined: Wed Feb 26, 2014 2:39 am

Re: [Pyaudio] Error: invalid number of channels -9998

Mon Mar 03, 2014 11:40 pm

Thanks to both of you for replying.


@davef21370:
I tried your snippet but I still get the same error...maybe pyaud.open() doesn't take more than 5 arguments.

@jojopi:
I run your code and i have this result:

Code: Select all

(0, 'bcm2835 ALSA: bcm2835 ALSA (hw:0,0)', 0)
(1, 'USB Device 0x46d:0x825: USB Audio (hw:1,0)', 1)
(2, 'sysdefault', 0)
(3, 'default', 0)
(4, 'dmix', 0)
Then I modified the original code:

Code: Select all

stream = pyaud.open(
    format = pyaudio.paInt16,
    channels = 1, 
    rate = 48000,
    input_device_index = 1,
    input = True)
And this is much better:

Code: Select all

-20.4912874626 None
-20.5311052437 None
-20.4270372949 None
-20.5171490551 None
-20.6255732658 None
-20.3921369755 None
-20.4665571088 None
-20.7583248666 None
-20.1849986838 None
-20.8682596106 None
-21.2911382134 None
-20.7960161724 None
-18.2517231232 None
-17.6431994384 None
-1.02182452929 None
-1.85977443132 None
-2.90894927686 None
-5.07392121541 None
-7.45630230977 None
-9.70045732417 None
-11.5230765244 None
-13.2130402112 None
-14.5276577974 None
-16.249119716 None
-17.762980104 None
-18.3778447711 None
-20.2067447273 None
-19.2561990265 None
-20.5692626705 None
-20.2429273379 None
-20.4216331442 None
-20.4075019494 None
-20.1814315778 None
-20.5595312305 None
-20.2786517366 None
-20.910582026 None
-20.132992927 None
-20.6084899292 None
-20.3353506545 None
-20.1859005204 None
-21.0566117671 None
-19.9364184975 None
-20.5227582225 None
-20.2107329741 None
-20.8744663629 None
-20.4138931948 None
-20.3259295572 None
-20.8532533967 None
-20.2321569589 None
-20.7677876907 None
-20.2736873245 None
-20.7028733081 None
-20.1717114355 None
-20.445851139 None
-20.8737251786 None
-20.0757983096 None
-20.3275817592 None
-20.5603998095 None
-20.6962784161 None
-20.5629785571 None
-20.8739653203 None
-20.5436908121 None
-20.5503946971 None
-20.4131236987 None
-20.1331790506 None
-20.5466323641 None
-20.151133336 None
-20.1946558121 None
-20.8211270106 None
-20.3629395351 None
-20.7117461108 None
-20.4856901832 None
-20.6870493533 None
-20.581121935 None
-20.4545216669 None
-20.1409065982 None
-20.440899214 None
-20.5624432074 None
-20.3289424621 None
-20.6601352203 None
-20.3604179145 None
-20.2548882595 None
-20.4905199794 None
-21.0078590033 None
-19.3540947005 None
-21.2121183406 None
-20.0097202672 None
-20.7548545898 None
-20.5042856452 None
-20.5981617943 None
-20.4611756704 None
-20.6050636419 None
-20.4293191202 None
-20.9619046099 None
-20.3649161678 None
-20.4325442749 None
-20.5469957048 None
-20.1255520997 None
-20.398581159 None
-20.1616350386 None
-20.3177060058 None
-20.5862018568 None
-20.8117766271 None
-20.5466239304 None
-20.7467186965 None
-20.1972063399 None
-20.3556996994 None
-20.6254498249 None
-20.1376498417 None
-20.4830140098 None
-20.7631037886 None
-20.288949238 None

Traceback (most recent call last):
  File "soundlevelmeter.py", line 19, in <module>
    rawsamps = stream.read(1024)
  File "/usr/lib/pymodules/python2.7/pyaudio.py", line 564, in read
    return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981
The last error makes me think that I need to change the sampling rate, but I guess the problem is solved now. Thank you for helping me.

User avatar
scruss
Posts: 2891
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: [Pyaudio] Error: invalid number of channels -9998

Fri Apr 03, 2015 3:38 pm

Or, if you know you have one input device, commend out the line containing input_device_index, and pyaudio will use the default device
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

BartoJG
Posts: 1
Joined: Sun May 31, 2015 5:08 pm

Re: [Pyaudio] Error: invalid number of channels -9998

Thu Jun 29, 2017 9:02 am

Gents,
'import analyse' <-- is not valid in my Raspian instance. Could you clairify what module you are referring to here? I found 'analyse-1.0.0.tar.gz' and installed it, but I do not think this is correct. I'm running Python 3.x, so is 'analyse' depricated?

It also appears that 'loudness' is not a valid function within 'pyaudio' library. Could someone comment on this error as well?

My project is to have Raspberry Pi 3 trip relays on PiFace Digital 2 via Python script (already written) when the microphone detects a loud noise.

Thanks!

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: [Pyaudio] Error: invalid number of channels -9998

Thu Jun 29, 2017 12:57 pm

It's a very old post that you're replying to.

Did you see the link in the first post? That mentions a python package called SoundAnalyse - my guess is that will be the module you need.

You are also right, "loudness" is not a method of pyaudio but the code here didn't say it was. The only place I can see it used is in the line:

Code: Select all

print analyse.loudness(samps)
So, hopefully, if that link in the first post is what you're after, it will also solve your second question.

Good luck!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

JayjeetAtGithub
Posts: 1
Joined: Thu Oct 17, 2019 7:29 pm

Re: [Pyaudio] Error: invalid number of channels -9998

Thu Oct 17, 2019 7:56 pm

Code: Select all

Expression 'parameters->channelCount <= maxChans' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1514
Expression 'ValidateParameters( inputParameters, hostApi, StreamDirection_In )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2818
Traceback (most recent call last):
  File "/home/pi/AT-Software-v3/sse.py", line 51, in <module>
    start_voice_recognition()
  File "/home/pi/AT-Software-v3/sse.py", line 46, in start_voice_recognition
    with m as source:
  File "/home/pi/venv/lib/python3.5/site-packages/speech_recognition/__init__.py", line 141, in __enter__
    input=True,  # stream is an input stream
  File "/home/pi/venv/lib/python3.5/site-packages/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "/home/pi/venv/lib/python3.5/site-packages/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels
I can't really figure out what to do. I went through the above discussions what i am still stuck. Can someone please help ? Thanks.

davekimble
Posts: 10
Joined: Sat Dec 15, 2012 3:20 am

Re: [Pyaudio] Error: invalid number of channels -9998

Thu Oct 24, 2019 3:14 am

You can find your devices maxChannels with:
print("Channels = "+str(audio.get_device_info_by_index(input_device_index).get('channels')))
In my case it replied "None" on a USB webcam with microphone, which works in other apps like guvcview.

Return to “Python”