echo.py 921 Bytes
import pyaudio
import numpy as np

pa = pyaudio.PyAudio()
delay_buffer = np.zeros((44100, 2), 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(frame_count, 2)
    delayed_data = np.concatenate((delay_buffer, audio_data))
    delay_buffer = delayed_data[frame_count:]
    return (audio_data + 0.5 * delay_buffer).tobytes(), pyaudio.paContinue
RATE = 44100
CHUNK = 1024

stream = pa.open(format=pyaudio.paFloat32,
                 channels=1,
                 rate=RATE,
                 input=True,
                 output=True,
                 frames_per_buffer=CHUNK,
                 stream_callback=callback)

stream.start_stream()
# keep the stream running for a few seconds

for i in range(0, int(RATE / CHUNK * 30)):
    stream.write(stream.read(CHUNK))

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