The Python – Getargs format is not a tuple that runs PyAudio using live input

Getargs format is not a tuple that runs PyAudio using live input… here is a solution to the problem.

Getargs format is not a tuple that runs PyAudio using live input

I’m trying to process real-time data from a microphone using PyAudio. I followed the example and on the PyAudio Documentation PyAudio Documentation this blog post When I use stream.is_active() without a callback, I have no error and everything works fine, but if I use a callback I get the following “SystemError: new style getargs format but argument is not a tuple”

I’ve seen the question< a href="https://stackoverflow.com/questions/47271905/where-is-the-error-systemerror-new-style-getargs-format-but-argument-is-not-a" rel="noreferrer noopener." nofollow">Where is the error? “SystemError: new style getargs format but argument is not a tuple”, however, this does not translate into my problem. I’ll explain what I tried below my code:

FORMAT = pyaudio.paInt32
CHANNELS = 2
RATE = 44100
CHUNK = 1024  

def callback(in_data, frame_count, time_info, flag):
    print('Hi')

audio = pyaudio. PyAudio()

# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
                rate=RATE, input=True,
                frames_per_buffer=CHUNK,stream_callback = callback)

stream.start_stream()   

while stream.is_active():
    time.sleep(0.1)
    print('1')
stream.stop_stream()
stream.close()
audio.terminate()

I’ve determined that the error is any time I add stream_callback=callback. If I delete it, I don’t get an error. When I only include the above statement, the exact code in the callback doesn’t seem to affect the error (although my entire code calculates the time delay between the two channels). My callback parameters are defined directly from the documentation. I’m not quite sure what else to try because I didn’t do anything directly in the callback.

Solution

As the example you linked said:

In callback mode, PyAudio will call a specified callback function (2) whenever it needs new audio data (to play) and/or when there is new (recorded) audio data available. Note that PyAudio calls the callback function in a separate thread. The function has the following signature callback(<input_data>, <frame_count>, <time_info>, <status_flag>) and must return a tuple containing frame_count frames of audio data and a flag signifying whether there are more frames to play/record.

But your callback function won’t do this :

def callback(in_data, frame_count, time_info, flag):
    print('Hi')

This does not have a return statement, so it simply returns None

Compare it to the referenced example, which returns a buffer and a flag. That’s what you need to do.

The reason you’re getting a specific error instead of an easier one to understand is that somewhere behind the scenes, PyAudio is parsing your return value by using C API functions that take Python tuples, without doing anything explicit error handling. This is not ideal because it is harder to read when you don’t know the meaning of the error message….

Related Problems and Solutions