I can't run a simple code using pyaudio - [Errno -9996] Invalid output device (no default output device)

Gustavo picture Gustavo · May 6, 2016 · Viewed 15.1k times · Source

(I'm new at python)

I'm trying to run a simple code about pyaudio. I just copied and pasted a code that I found on the pyaudio web site.

I get this error:

    OSError                                   Traceback (most recent call last)
<ipython-input-7-3fc52ceecbf3> in <module>()
     15                 channels=wf.getnchannels(),
     16                 rate=wf.getframerate(),
---> 17                 output=True)
     18 
     19 # read data

/home/gustavolg/anaconda3/lib/python3.5/site-packages/pyaudio.py in open(self, *args, **kwargs)
    748         """
    749 
--> 750         stream = Stream(self, *args, **kwargs)
    751         self._streams.add(stream)
    752         return stream

/home/gustavolg/anaconda3/lib/python3.5/site-packages/pyaudio.py in __init__(self, PA_manager, rate, channels, format, input, output, input_device_index, output_device_index, frames_per_buffer, start, input_host_api_specific_stream_info, output_host_api_specific_stream_info, stream_callback)
    439 
    440         # calling pa.open returns a stream object
--> 441         self._stream = pa.open(**arguments)
    442 
    443         self._input_latency = self._stream.inputLatency

OSError: [Errno -9996] Invalid output device (no default output device)

I can not figure out how to solve this error. I don't know if this has something to do with audio driver or if the code needs an output declaration. I mean, if I have to select an output.

The code:

import pyaudio
import wave
import sys

CHUNK = 1024


wf = wave.open("/home/gustavolg/anaconda3/aPython/file.wav", 'rb')

# instantiate PyAudio (1)
p = pyaudio.PyAudio()

# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

# read data
data = wf.readframes(CHUNK)

# play stream (3)
while len(data) > 0:
    stream.write(data)
    data = wf.readframes(CHUNK)

# stop stream (4)
stream.stop_stream()
stream.close()

# close PyAudio (5)
p.terminate()

I'm using python3 on Jupyter notebook.

Answer

iliul picture iliul · May 6, 2016

check the following steps:

>>> import pyaudio
>>> pa = pyaudio.PyAudio()
>>> pa.get_default_input_device_info()
{'defaultLowOutputLatency': 0.008707482993197279, 
 'maxOutputChannels': 32, 
 'hostApi': 0, 
 'defaultSampleRate': 44100.0, 
 'defaultHighOutputLatency': 0.034829931972789115, 
 'name': 'default', 
 'index': 15, 
 'maxInputChannels': 32,
 'defaultHighInputLatency': 0.034829931972789115, 
 'defaultLowInputLatency': 0.008707482993197279, 
 'structVersion': 2}
>>> pyaudio.pa.__file__
'/root/.virtualenvs/py3k/lib/python3.4/site-packages/_portaudio.cpython-34m.so'

make sure you have a default input device,if not you can refer to here

I want it's useful for you!