testForPort.py 912 Bytes
import numpy as np
import pyaudio
import time

pa = pyaudio.PyAudio()
delay_buffer = np.zeros((44100, 1), dtype=np.float32)

def callback(in_data, frame_count, time_info, status):
    global delay_buffer
    audio_data = np.frombuffer(in_data, dtype=np.float32).reshape(1024, 1)
    delayed_data = np.concatenate((delay_buffer, audio_data))
    delay_buffer = delayed_data[frame_count:]
    return (audio_data + 0.5 * delay_buffer).tobytes(), pyaudio.paContinue

stream = pa.open(format=pyaudio.paFloat32,
                 channels=1,
                 rate=1024,
                 input=True,
                 output=True,
                 frames_per_buffer=44100,
                 stream_callback=callback)
start = time.time()
DURATION = 30
# keep the stream running for a few seconds
while stream.is_active() and (time.time() - start) < DURATION:
    time.sleep(0.1)

stream.stop()
stream.close()
pa.terminate()