-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwire_callback.py
45 lines (34 loc) · 1000 Bytes
/
wire_callback.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).
This is the callback (non-blocking) version.
"""
import pyaudio
import time
import socket
WIDTH = 2
CHANNELS = 1
RATE = 48000
CHUNK = RATE / 100
p = pyaudio.PyAudio()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = '127.0.0.1'
port = 8008
def callback(in_data, frame_count, time_info, status):
#print('in_data\n{}\n---------\n\n'.format(np.fromstring(in_data, dtype=np.int16)))
s.sendto(in_data, (host, port))
return (in_data, pyaudio.paContinue)
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.3)
stream.stop_stream()
stream.close()
p.terminate()