-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioFileFakeMicrophone.py
160 lines (143 loc) · 6.45 KB
/
AudioFileFakeMicrophone.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
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
from multiprocessing import Process
import os
import subprocess
import wave
import threading
import socket
import sys
import time
import PacketsHandler
import UDP_Socket
from Packets import ReportPacket
from Packets import BeaconPacket
from Packets import Packets
from Packets import DataPacket
import node_variables
import init_config
from configparser import ConfigParser
import serial
config = ConfigParser()
config.read('config.ini')
nodeIP = init_config.GetIp(config['GENERAL']['StationInterface'] )
seconds_to_wait_before_notify = 2
# class ThreadSendDataAudio (threading.Thread):
# def __init__(self, threadID, name):
# threading.Thread.__init__(self)
# self.threadID = threadID
# self.name = name
# def run(self):
# print("Starting " + self.name)
# #UdpSocketReceiver( config.get(socket.gethostname(),'IpStation') , int(config['GENERAL']['Port']) )
# AudioFile()
def GetAudio(action):
# time.sleep(5)
#nodeIP = init_config.GetIp(config['GENERAL']['StationInterface'] )
# while True:
if action.decode('utf-8') == "ON":
t = ThreadAud("AudioThread", action)
t.start()
# audioT.join()
print("Thread Audio Started")
# return
if action.decode('utf-8') == "OFF":
#print("Microphone "+action.decode('utf-8'))
node_variables.MicStatus = action
#os.system("kill -SIGKILL " + str(node_variables.ThreadId))
#obj = wave.open('/tmp/SDNPy-master/sound.wav', 'r')
# print("Number of channels", obj.getnchannels())
#print("Sample width", obj.getsampwidth())
#print("Frame rate.", obj.getframerate())
#print("Number of frames", obj.getnframes())
#print("parameters:", obj.getparams())
#frame = 0
# for x in range(0, 2601600, 80):
#tmp = obj.readframes(int(config['FileWave']['Frame']))
#pckData = DataPacket(config['GENERAL']['NetId'],config['GENERAL']['IpSink'], nodeIP, "100",config['GENERAL']['IpSink'],'CIAOOONE')
# UDP_Socket.SendUdpPacketUnicast(pckData.getBytesFromPackets(),config['GENERAL']['IpSink'],int(config['GENERAL']['Port']))
#frame = frame + len(tmp)
#print("Frame ["+ str(x) + "] lette: " + str(frame))
# time.sleep(float(config['FileWave']['TimeSleepBetweenTwoFrame']))
# obj.close()
# time.sleep(int(config['FileWave']['TimeSleepBetweenTwoPlay']))
class ThreadAud(threading.Thread):
def __init__(self, name, action):
threading.Thread.__init__(self)
self.name = name
self.action = action.decode('utf-8')
def run(self):
if config.getboolean('DEBUG','PRINT_LOGS') is True:
print("CIAOOOOOOO SONO QUIIIIIIIIIII")
node_variables.MicStatus = self.action
if config.getboolean('DEBUG','PRINT_LOGS') is True:
print("Avvio il Thread all'interno")
# ser = serial.Serial('/dev/ttyUSB1', baudrate=12000000,rtscts=True,dsrdtr=True, timeout=0)
# print(ser.isOpen())
# pos=0
# buffSize = 3
# inBuff = [bytes(0)] * buffSize
# messRCV = False
# sampleSizeRCV = False
# audioSample = []
# print("Eccomi")
counter = 0
# while node_variables.MicStatus == "ON":
# print("Microphone " + self.action)
audioSample = bytearray()
total_bytes_sent = 0
latest_notification_time = time.time()
while node_variables.MicStatus == "ON":
counter = counter + 1
if counter <= 255:
audioSample.append(counter)
if counter > 255:
counter = 1
audioSample.append(counter)
while len(audioSample) <= 1750:
audioSample.append(0)
# messRCV = False
# strVal = ser.read(1)
#print(strVal)
# if strVal == ('<').encode():
# while not messRCV:
# if pos == buffSize: # and val == ('\r').encode():
#print("---------->", int.from_bytes(inBuff[0] + inBuff[1] + inBuff[2], "big", signed="True") )
# print("---------->",inBuff[0] + inBuff[1] + inBuff[2])
# audioSample.extend(inBuff[0] + inBuff[1] + inBuff[2])
#audioSample.append(int.from_bytes(inBuff[0] + inBuff[1] + inBuff[2], "big", signed="True"))
# messRCV = True
# pos = 0
# inBuff = [bytes(0)] * buffSize
# else:
# val = ser.read(1)
# inBuff[pos] = bytes(val)
# pos = pos + 1
if config.getboolean('DEBUG','PRINT_LOGS') is True:
print("send mic data:", str(counter))
total_bytes_sent += len(audioSample)
pckData = DataPacket(
config['GENERAL']['NetId'], config['GENERAL']['IpSink'],
init_config.GetIp(config['GENERAL']['StationInterface']),
"100", config['GENERAL']['IpSink'], audioSample)
# pckData="-1--38----192.168.3.1----192.168.3.1-2100----192.168.3.1"+payload
# pckData=pckData.replace("192.168.3.1-2100",init_config.GetIp(config['GENERAL']['StationInterface'])+"-2100")
#pckData=pckData.replace("38", str(38+len(payload)))
UDP_Socket.SendUdpPacketUnicast(pckData.getBytesFromPackets(),
config['GENERAL']['IpSink'],
int(config['GENERAL']['Port']))
now = time.time()
if now - latest_notification_time > seconds_to_wait_before_notify:
latest_notification_time = now
UDP_Socket.SendUdpPacketMicStatus(int(config['GENERAL']['Port']),
nodeIP,
config['GENERAL']['IpSink'],
"ON")
time.sleep(0.250)
counter = 0
print("Total bytes sent: {}".format(total_bytes_sent))
for i in range(5):
time.sleep(0.1)
UDP_Socket.SendUdpPacketMicStatus(int(config['GENERAL']['Port']),
nodeIP,
config['GENERAL']['IpSink'],
"OFF")
# UDP_Socket.SendUdpPacketUnicast(pckData.getBytesFromPackets(),config['GENERAL']['IpSink'],int(config['GENERAL']['Port']))