Showing
5 changed files
with
127 additions
and
0 deletions
callBackWire.py
0 → 100644
1 | +"""PyAudio Example: Audio wire between input and output. Callback version.""" | ||
2 | + | ||
3 | +import time | ||
4 | +import sys | ||
5 | +import pyaudio | ||
6 | +import numpy as np | ||
7 | + | ||
8 | + | ||
9 | +DURATION = 5 # seconds | ||
10 | + | ||
11 | +delay_buffer = np.zeros((44100, 2), dtype=np.float32) | ||
12 | + | ||
13 | +def callback(in_data, frame_count, time_info, status): | ||
14 | + global delay_buffer | ||
15 | + audio_data = np.frombuffer(in_data, dtype=np.float32).reshape(frame_count, 2) | ||
16 | + delayed_data = np.concatenate((delay_buffer, audio_data)) | ||
17 | + delay_buffer = delayed_data[frame_count:] | ||
18 | + return (audio_data + 0.5 * delay_buffer).tobytes(), pyaudio.paContinue | ||
19 | + | ||
20 | +p = pyaudio.PyAudio() | ||
21 | +stream = p.open(format=p.get_format_from_width(2), | ||
22 | + channels=1, | ||
23 | + rate=44100, | ||
24 | + input=True, | ||
25 | + output=True, | ||
26 | + frames_per_buffer=1024, | ||
27 | + | ||
28 | + stream_callback=callback) | ||
29 | + | ||
30 | +start = time.time() | ||
31 | +while stream.is_active() and (time.time() - start) < DURATION: | ||
32 | + time.sleep(0.1) | ||
33 | + | ||
34 | +stream.close() | ||
35 | +p.terminate() | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
echo.py
0 → 100644
1 | +import pyaudio | ||
2 | +import numpy as np | ||
3 | + | ||
4 | +pa = pyaudio.PyAudio() | ||
5 | +delay_buffer = np.zeros((44100, 2), dtype=np.float32) | ||
6 | + | ||
7 | +def callback(in_data, frame_count, time_info, status): | ||
8 | + global delay_buffer | ||
9 | + audio_data = np.frombuffer(in_data, dtype=np.float32).reshape(frame_count, 2) | ||
10 | + delayed_data = np.concatenate((delay_buffer, audio_data)) | ||
11 | + delay_buffer = delayed_data[frame_count:] | ||
12 | + return (audio_data + 0.5 * delay_buffer).tobytes(), pyaudio.paContinue | ||
13 | +RATE = 44100 | ||
14 | +CHUNK = 1024 | ||
15 | + | ||
16 | +stream = pa.open(format=pyaudio.paFloat32, | ||
17 | + channels=1, | ||
18 | + rate=RATE, | ||
19 | + input=True, | ||
20 | + output=True, | ||
21 | + frames_per_buffer=CHUNK, | ||
22 | + stream_callback=callback) | ||
23 | + | ||
24 | +stream.start_stream() | ||
25 | +# keep the stream running for a few seconds | ||
26 | + | ||
27 | +for i in range(0, int(RATE / CHUNK * 30)): | ||
28 | + stream.write(stream.read(CHUNK)) | ||
29 | + | ||
30 | +stream.stop() | ||
31 | +stream.close() | ||
32 | +pa.terminate() |
output.wav
0 → 100644
No preview for this file type
testForPort.py
0 → 100644
1 | +import numpy as np | ||
2 | +import pyaudio | ||
3 | +import time | ||
4 | + | ||
5 | +pa = pyaudio.PyAudio() | ||
6 | +delay_buffer = np.zeros((44100, 1), dtype=np.float32) | ||
7 | + | ||
8 | +def callback(in_data, frame_count, time_info, status): | ||
9 | + global delay_buffer | ||
10 | + audio_data = np.frombuffer(in_data, dtype=np.float32).reshape(1024, 1) | ||
11 | + delayed_data = np.concatenate((delay_buffer, audio_data)) | ||
12 | + delay_buffer = delayed_data[frame_count:] | ||
13 | + return (audio_data + 0.5 * delay_buffer).tobytes(), pyaudio.paContinue | ||
14 | + | ||
15 | +stream = pa.open(format=pyaudio.paFloat32, | ||
16 | + channels=1, | ||
17 | + rate=1024, | ||
18 | + input=True, | ||
19 | + output=True, | ||
20 | + frames_per_buffer=44100, | ||
21 | + stream_callback=callback) | ||
22 | +start = time.time() | ||
23 | +DURATION = 30 | ||
24 | +# keep the stream running for a few seconds | ||
25 | +while stream.is_active() and (time.time() - start) < DURATION: | ||
26 | + time.sleep(0.1) | ||
27 | + | ||
28 | +stream.stop() | ||
29 | +stream.close() | ||
30 | +pa.terminate() | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
wire.py
0 → 100644
1 | +import sys | ||
2 | + | ||
3 | +import pyaudio | ||
4 | + | ||
5 | +RECORD_SECONDS = 5 | ||
6 | +CHUNK = 1024 | ||
7 | +RATE = 44100 | ||
8 | + | ||
9 | +p = pyaudio.PyAudio() | ||
10 | +stream = p.open(format=p.get_format_from_width(2), | ||
11 | + channels=1 if sys.platform == 'darwin' else 2, | ||
12 | + rate=RATE, | ||
13 | + input=True, | ||
14 | + output=True, | ||
15 | + frames_per_buffer=CHUNK) | ||
16 | + | ||
17 | +print('* recording') | ||
18 | + | ||
19 | +def add_echo(data, output_stream): | ||
20 | + output_stream.write(data) | ||
21 | + | ||
22 | + | ||
23 | +for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): | ||
24 | + add_echo(stream.read(CHUNK), stream) | ||
25 | + | ||
26 | + | ||
27 | +print('* done') | ||
28 | + | ||
29 | +stream.close() | ||
30 | +p.terminate() | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment