callBackWire.py 965 Bytes
"""PyAudio Example: Audio wire between input and output. Callback version."""

import time
import sys
import pyaudio
import numpy as np


DURATION = 5  # seconds

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

p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(2),
                channels=1,
                rate=44100,
                input=True,
                output=True,
                frames_per_buffer=1024,

                stream_callback=callback)

start = time.time()
while stream.is_active() and (time.time() - start) < DURATION:
    time.sleep(0.1)

stream.close()
p.terminate()