-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioStream.py
68 lines (63 loc) · 2.52 KB
/
AudioStream.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pyaudio
import time
import socket
import audioop
from collections import deque
class AudioStream(object):
class __Singleton:
def __init__(self, addr):
self.talk = time.time() + 2
self.group = {}
self.MyName = socket.gethostname()
self.MyAddr = socket.gethostbyname(self.MyName)
try:
netifaces.ifaddresses('wlan0')
self.MyAddr = netifaces.ifaddresses('wlan0')[2][0]['addr']
except:
pass
self.SrvrPort = 8000
self.Chn = 1
self.InRt = 44100
#self.OutRt = 6000
self.Chk = 2048
self.Tmt = 6
self.RcvChk = len(self.group.keys()) if len(self.group.keys()) > 1 else 2
self.Fmt = pyaudio.paInt16
with open('member.config','r') as file:
for line in file:
x = line.index(':')
ip = line[0:x]
port = line[x+1:]
port = port.strip('\n')
if (ip == str(self.MyAddr)):
self.SrvrPort = int(port)
else:
self.group[ip]=int(port)
self.RecData = deque([],maxlen=self.Chn * len(self.group.keys()))
self.PlyData = deque([],maxlen=self.Chn * len(self.group.keys()) * 16)
p = pyaudio.PyAudio()
self.Ply = p.open(format=self.Fmt,
channels=self.Chn,
rate=self.InRt,
output=True,
frames_per_buffer=self.Chk
#stream_callback=pCallback
)
self.Rec = p.open(format=self.Fmt,
channels=self.Chn,
rate=self.InRt,
input=True,
frames_per_buffer=self.Chk,
stream_callback=self.RecCallback
)
self.Rec.start_stream
def RecCallback(self,in_data,frame_count,time_info,status):
#converted = audioop.ratecv(in_data, 2, 1, self.InRt, self.OutRt, None)
self.RecData.append(in_data)
return (in_data, pyaudio.paContinue)
instance = None
def __init__(self, **kwargs):
if not AudioStream.instance:
AudioStream.instance = AudioStream.__Singleton(kwargs.get('addr'))
def __getattr__(self, name):
return getattr(self.instance, name)