forked from Tecnologias-multimedia/InterCom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinter.py
More file actions
165 lines (133 loc) · 5.09 KB
/
inter.py
File metadata and controls
165 lines (133 loc) · 5.09 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import pyaudio
import socket
from threading import Thread
import numpy as np
import pywt as wt
import scipy
from scipy.signal import firwin
from scipy.signal import lfilter
from io import BytesIO
import codec as comp
import matplotlib.pyplot as plt
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
ITERACIONESDWT = 9
FRAMES_TO_SEND = []
FRAMES_TO_PLAY = []
P = pyaudio.PyAudio()
INPUT_STREAM = P.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = CHUNK,
)
OUTPUT_STREAM = P.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
output = True,
frames_per_buffer = CHUNK,
)
def udpSenderStream():
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#while True:
#if len(FRAMES_TO_SEND) > 0:
#udp.sendto(FRAMES_TO_SEND.pop(0), ("127.0.0.1", 12345))
udp.close()
def udpReceiverStream():
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.bind(("127.0.0.1", 12345))
#while True:
#soundData, addr = udp.recvfrom(CHUNK * CHANNELS *2)
#FRAMES_TO_PLAY.append(soundData)
udp.close()
def record(stream):
while True:
data = stream.read(CHUNK)
#x = np.arange(0, 2048)
#plt.plot(x, np.fromstring(data, np.int16), linewidth=2, linestyle="-", c="b")
#plt.show()
#plt.close()
#print('--------------------------------------------------')
#print("DATA RECORDED")
#print('--------------------------------------------------')
#print(np.fromstring(data, np.int16))
real, imaginaria = comp.trasnformada(np.fromstring(data, np.int16))
xrn = comp.normalizar(real, True)
xin = comp.normalizar(imaginaria, False)
imagR = comp.comprimirImagen(xrn)
imagI = comp.comprimirImagen(xin)
#FRAMES.append(imagR)
#FRAMES.append(imagI)
imagRObtenida = comp.Image.open(BytesIO(imagR))
imagIObtenida = comp.Image.open(BytesIO(imagI))
xr = comp.denormalizar(imagRObtenida.getdata(), True)
xi = comp.denormalizar(imagIObtenida.getdata(), False)
complejo = comp.unir(xr, xi)
inversa = comp.fourier.ifft(complejo)
print(np.int16(inversa.real))
# Esto es trampa
FRAMES_TO_PLAY.append(np.int16(inversa.real))
#print('--------------------------------------------------')
#print("DATA DECODED")
#print('--------------------------------------------------')
#print(inversa.real)
#input()
def play(stream):
BUFFER = 10
while True:
#if len(FRAMES_TO_PLAY) == BUFFER:
while True:
if len(FRAMES_TO_PLAY) > 0:
# POR AHORA EL AUDIO A REPRODUCIR LO COGEMOS DIRECTAMENTE
# DEL INVERSA.REAL QUE SE DECODIFICA ANTES DE ENVIARLO A
# TRAVÉS DE LA RED
audioData = FRAMES_TO_PLAY.pop(0).copy(order='C')
# FILTER 1
# spell out the args that were passed to the Matlab function
N = 10
Fc = 40
Fs = 1600
# provide them to firwin
h = scipy.signal.firwin(numtaps=N, cutoff=40, nyq=Fs/2)
# 'x' is the time-series data you are filtering
audioData1 = scipy.signal.lfilter(h, 1.0, audioData)
# FILTER 2
#n = 15 # the larger n is, the smoother curve will be
#b = [1.0 / n] * n
#a = 1
#audioData2 = lfilter(b, a, audioData)
# PLOTS
#x = np.arange(0, 2048)
#plt.plot(x, audioData1, linewidth=2, linestyle="-", c="b")
#plt.show()
#plt.close()
#plt.plot(x, audioData2, linewidth=2, linestyle="-", c="b")
#plt.show()
#plt.close()
stream.write(audioData1, CHUNK)
#print('--------------------------------------------------')
#print("DATA RECEIVED")
#print('--------------------------------------------------')
#print(audioData)
#input()
def main():
t_r = Thread(target = record, args = (INPUT_STREAM,))
t_p = Thread(target = play, args=(OUTPUT_STREAM,))
t_us = Thread(target = udpSenderStream)
t_ur = Thread(target = udpReceiverStream)
t_r.setDaemon(True)
t_p.setDaemon(True)
t_us.setDaemon(True)
t_ur.setDaemon(True)
t_r.start()
t_p.start()
t_us.start()
t_ur.start()
t_r.join()
t_p.join()
t_us.join()
t_ur.join()
if __name__ == '__main__':
main()