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()