I'm currently working on multithreading with PyAudio and Pygame, but when I try to run the audio, I get an error saying "OSError: [Errno -9996] Invalid output device (no default output device)."

So far I've tried these on Terminal:

>>> import pyaudio
>>> pa = pyaudio.PyAudio()

>>> pa.get_default_input_device_info()
{'name': 'Built-in Microphone', 'defaultHighInputLatency': 0.013151927437641724, 
'defaultLowInputLatency': 0.002993197278911565, 'maxInputChannels': 2, 
'defaultSampleRate': 44100.0, 'structVersion': 2, 'maxOutputChannels': 0, 
'defaultHighOutputLatency': 0.1, 'hostApi': 0, 'index': 0, 
'defaultLowOutputLatency': 0.01}

>>> pa.get_default_output_device_info()
{'name': 'Built-in Output', 'defaultHighInputLatency': 0.1, 
'defaultLowInputLatency': 0.01, 'maxInputChannels': 0, 
'defaultSampleRate': 44100.0, 'structVersion': 2, 'maxOutputChannels': 2, 
'defaultHighOutputLatency': 0.01401360544217687, 'hostApi': 0, 'index': 1, 
'defaultLowOutputLatency': 0.003854875283446712}

>>> pyaudio.pa.__file__
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-
packages/_portaudio.cpython-35m-darwin.so'

>>> print(pa.get_device_count())
3

So it seems like I do have input/output devices but somehow my default device is not working. Could someone please help me solve this issue?

This is my code:

import os, platform, sys
import math, pygame, time, threading, random
from multiprocessing import Process, Array, Value
import multiprocessing, array

import pyaudio, sys, aubio
import numpy as np

# modified from demo_pyaudio.py from aubio-0.4.4 demos
curFreq = Value('f', 0)

def startAnalysis():

    print('audio running...')

    p = pyaudio.PyAudio()

    # open stream
    buffer_size = 1024*4
    pyaudio_format = pyaudio.paFloat32
    n_channels = 1
    samplerate = 44100
    stream = p.open(format=pyaudio_format,
                    channels=n_channels,
                    rate=samplerate,
                    input=True, output=True,
                    frames_per_buffer=buffer_size,
                    input_device_index=0,
                    output_device_index=1)

    if len(sys.argv) > 1:
        # record 5 seconds
        output_filename = sys.argv[1]
        record_duration = 5 # exit 1
        outputsink = aubio.sink(sys.argv[1], samplerate)
        total_frames = 0
    else:
        # run forever
        outputsink = None
        record_duration = None

    # setup pitch
    tolerance = 0.8
    win_s = 4096 # fft size
    hop_s = buffer_size # hop size
    pitch_o = aubio.pitch("default", win_s, hop_s, samplerate)
    pitch_o.set_unit("midi")
    pitch_o.set_tolerance(tolerance)

    print("*** starting recording")
    while True:
        try:
            audiobuffer = stream.read(buffer_size)
            signal = np.fromstring(audiobuffer, dtype=np.float32)

            pitch = pitch_o(signal)[0]
            confidence = pitch_o.get_confidence()

            curFreq.value = pitch

            if outputsink:
                outputsink(signal, len(signal))

            if record_duration:
                total_frames += len(signal)
                if record_duration * samplerate < total_frames:
                    break
        except KeyboardInterrupt:
            print("*** Ctrl+C pressed, exiting")
            break

    print("*** done recording")
    stream.stop_stream()
    stream.close()
    p.terminate()

Yes, still desperately waiting for some help on both forums.

As a test try working apps first before you write new code. I can see this happen if say a Pi was connected to a monitor. Most monitors don't have speakers so the audio over HDMI would fail. As to your Pyaudio, I can't guess your setup.

Using https://www.google.com/search?q=raspberry+pi+pyaudio+configuration here the second article is about setup.

My advice is to COPY what others have done before you try it on your own.

I'm using my Mac, and audio actually seems to be working just fine with other code files in my computer that involve pyaudio and aubio. It's just this one file that seems to be raising an error.

I'm guessing it has to do something with the way I am multithreading it with pygame, but I can't quite figure out how that would all of a sudden create this error, especially considering the fact that I've tried copying basically every possible solution that others have suggested online. Thank you though!

I can't know you were coding and running this on an Apple Mac. Alter my link to reflect that change in detail. As I don't have an Apple Mac but the iPhone and iPad I can't duplicate your issue.

Just a thought. Review where you got this code from. Was it running on some Apple like yours? They may have it on some PC so your choice of devices may be off. Remember I don't own hardware like yours and you forgot to tell in your opening post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.