4

OS: Mac OSX 10.7.5 Python: Python 2.7.3 (homebrew) pyaudio: 0.2.7 portaudio: 19.20111121 (homebrew - portaudio)

The following script outputs the following and displays the issues I am having:

#!/usr/bin/env python
import pyaudio
from pprint import pprint

p = pyaudio.PyAudio()


# SUCCEEDS
pprint(p.is_format_supported(input_format=pyaudio.paInt8,input_channels=1,rate=44100,input_device=0)) # => True
try:
    stream = p.open(format=pyaudio.paInt8,channels=1,rate=44100,input=True,frames_per_buffer=1024)
    data = stream.read(1024)
except IOError as e:
    print 'This never happens: '+str(e)

# FAILS
pprint(p.is_format_supported(input_format=pyaudio.paInt8,input_channels=1,rate=22050,input_device=0)) # => True
try:
    stream = p.open(format=pyaudio.paInt8,channels=1,rate=22050,input=True,frames_per_buffer=1024)
    data = stream.read(1024)
except IOError as e:
    print 'This fails: '+str(e)

# FAILS
pprint(p.is_format_supported(input_format=pyaudio.paInt8,input_channels=1,rate=22050,input_device=0)) # => True
try:
    stream = p.open(format=pyaudio.paInt8,channels=1,rate=22050,input=True,frames_per_buffer=512)
    data = stream.read(1024)
except IOError as e:
    print 'This also fails: '+str(e)

# FAILS
pprint(p.is_format_supported(input_format=pyaudio.paInt8,input_channels=1,rate=11025,input_device=0)) # => True
try:
    stream = p.open(format=pyaudio.paInt8,channels=1,rate=11025,input=True,frames_per_buffer=512)
    data = stream.read(1024)
except IOError as e:
    print 'This also fails as well: '+str(e)

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

The above outputs the following:

True
True
This fails: [Errno Input overflowed] -9981
True
This also fails: [Errno Input overflowed] -9981
True
This also fails as well: [Errno Input overflowed] -9981
the_real_one
  • 467
  • 2
  • 4
  • 13
  • For what it's worth, testing this on my Mac (10.8.3) with Apple Python 2.7.2, Homebrew 2.7.3, and python.org 3.3.0 (same `portaudio`/`pyaudio` versions), it always gives me the same error 100% of the time on the second and all later calls, even if they're all using the same format as the first one. So I suspect the problem isn't the format at all. – abarnert Apr 23 '13 at 23:44
  • My suspicion was that the problem is that you're not calling `stop_stream` and `close` on each `stream` before creating a new one, which means you're leaking something that prevents future streams from working. But, from a quick test, it seems like I get the same errors even with that fix. (You should make that fix anyway, of course.) – abarnert Apr 23 '13 at 23:51

2 Answers2

7

If you want to check whether the desired settings of format, channels, rate, etc. are supported by your OS and hardware, do the following:

import pyaudio
soundObj = pyaudio.PyAudio()

# Learn what your OS+Hardware can do
defaultCapability = soundObj.get_default_host_api_info()
print defaultCapability

# See if you can make it do what you want
isSupported = soundObj.is_format_supported(input_format=pyaudio.paInt8, input_channels=1, rate=22050, input_device=0)
print isSupported

isSupported will be True incase your system can deal with your settings. The memory overflow errors might be due to some OS+Hardware issues. You must check what your default host API can actually do. You don't need to "open" and "close" the soundObj via the "stream class" for querying it.

Have a look at this SO question: PyAudio Input overflowed

For additional pyaudio documentation and help visit:

http://people.csail.mit.edu/hubert/pyaudio/docs/

Edit:

It turns out that "Errno Input overflowed - 9981" is not a trivial issue: http://trac.macports.org/ticket/39150

I see that you have the latest portaudio version (19.20111121) but 19.20111121_4 claims to have fixed the bug. See if upgrading portaudio works.

Community
  • 1
  • 1
samkhan13
  • 3,315
  • 2
  • 33
  • 54
  • I experienced similar problems, and had portaudio version 19.20111121 as installed by brew. In order to upgrade portaudio, I added the --HEAD option to the install: "brew install portaudio --HEAD". This fixed the overflow issue for me. – Joseph Sheedy Mar 27 '14 at 00:44
0

Even though the above comments indicate that stop_stream() and close() don't fix it, closing them like this works for me:

#!/usr/bin/env python
import pyaudio
from pprint import pprint

p = pyaudio.PyAudio()

pprint(p.is_format_supported(input_format=pyaudio.paInt8,
                             input_channels=1,
                             rate=44100,
                             input_device=0))
try:
    stream = p.open(format=pyaudio.paInt8,
                    channels=1,
                    rate=44100,
                    input=True,
                    input_device_index=0,
                    frames_per_buffer=1024)
    data = stream.read(1024)

except IOError as e:
    print e
stream.stop_stream()
stream.close()

pprint(p.is_format_supported(input_format=pyaudio.paInt8,
                              input_channels=1,
                              rate=22050,
                              input_device=0))

try:
    stream = p.open(format=pyaudio.paInt8,
                    channels=1,
                    rate=22050,
                    input=True,
                    input_device_index=0,
                    frames_per_buffer=1024)
    data = stream.read(1024)

except IOError as e:
    print e
stream.stop_stream()
stream.close()

p.terminate()

This gives the expected result for an unsupported rate(an exception):

Traceback (most recent call last):
  File "testaudio.py", line 28, in <module>
    input_device=2))
  File "/usr/lib64/python2.7/site-packages/pyaudio.py", line 934, in is_format_supported
    return pa.is_format_supported(rate, **kwargs)
ValueError: ('Invalid sample rate', -9997)
Radio-
  • 3,151
  • 21
  • 22
  • I had to modify your script (you use input_device=2 and I use 1) and also you do not have a "input_device_index=X" on the second p.open(...22050...) block and I then get: `Traceback (most recent call last): `File "test.py", line 12, in ` `input_device=1))` File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyAudio-0.2.7-py2.7-macosx-10.7-x86_64.egg/pyaudio.py", line 934, in is_format_supported return pa.is_format_supported(rate, **kwargs) ValueError: ('Invalid number of channels', -9998) – the_real_one Apr 24 '13 at 16:16
  • Yeah, I changed the device, since input device 0 is not a audio recording device on my computer, but I can change it back. – Radio- Apr 24 '13 at 16:24
  • `ValueError` is the expected results according to the docs if it isn't supported: "Returns True if the configuration is supported; throws a ValueError exception otherwise." – Radio- Apr 24 '13 at 16:24