pyaudio_portaudio icon indicating copy to clipboard operation
pyaudio_portaudio copied to clipboard

Is stream_callback supported?

Open rmnaderdev opened this issue 3 years ago • 0 comments

I am working on a small project that involves non-blocking stream reading. When I use the stream_callback property, the python script freezes for a second before exiting without any errors. Here is my code snippet where this issue is happening:

import pyaudio

# Setup settings
CHUNK = 1024 * 4
FORMAT = pyaudio.paInt16
CHANNELS = 1

p = pyaudio.PyAudio()

device_id = 6

print("Default device is " + str(device_id))

device_info = p.get_device_info_by_index(device_id)

def non_blocking_stream_read(in_data, frame_count, time_info, status):
    print("Non-blocking func ran!")
    return in_data, pyaudio.paContinue

print("Creating stream")
stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = int(device_info["defaultSampleRate"]),
                input = True,
                frames_per_buffer = CHUNK,
                input_device_index = device_info["index"],
                stream_callback=non_blocking_stream_read,
                as_loopback = True)

stream.start_stream()

print("Stream created!")

input("Press any key to continue...")

The only output I get is:

Default device is 6
Creating stream
Stream created!
Press any key to continue...
PS C:\Users\$USER\Desktop\Python Audio Testing>

Does anyone know if this kind of usage is possible? Any insight is appreciated.

NOTE: The program should have paused on the input function call, I did not press enter.

Edit: When switching the device to a input audio device and removing as_loopback, this script works fine.

rmnaderdev avatar Dec 22 '21 08:12 rmnaderdev