-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaud.py
72 lines (60 loc) · 1.83 KB
/
aud.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
import serial
onionCode = 0
#Init buffer containing the received data
buffSize = 360
done = False
sampleSizeRCV = False
audioSample = []
counter = 0
if onionCode == 1:
port = '/dev/ttyACM0'
else:
port = 'COM6'
baudrate = 4000000
try:
#Create serial object
ser = serial.Serial(None,baudrate, timeout = 0.1)
#Open serial
ser.port = port
ser.open()
#Start data transmission
ser.write('start'.encode('utf-8'))
#Start looking for the message
while len(audioSample) < 60000 :
counter = 0
inBuff = ser.read(buffSize)
#print("Waiting packets: ",ser.in_waiting)
while counter < buffSize:
if inBuff[counter:counter+1] == ('0').encode():
audioSample.append(int.from_bytes(inBuff[counter+1:counter+2]+inBuff[counter+2:counter+3],"big", signed="True"))
counter = counter + 3
else:
counter = counter + 1
if counter + 2 > buffSize:
inBuff = bytearray(buffSize)
break
except :
print('Program exit !')
finally :
ser.write('stop'.encode('utf-8'))
maxTest = 0
ser.reset_input_buffer()
while sampleSizeRCV != True and maxTest < 10:
mss = str(ser.read_until(expected= '\n'))
index = mss.rfind("Sample sent:")
if index == -1:
maxTest = maxTest + 1
else:
sampleRCV = mss[index:len(mss)-5]
sampleSizeRCV = True
ser.close()
#Save normalized file to text
if onionCode == 1:
fHandler = open('/home/microphone/audio.txt', 'w')
else:
fHandler = open('C:/Users/ruben/Desktop/Test_9.txt', 'w')
for line in audioSample:
fHandler.write(str(line))
fHandler.write("\n")
fHandler.close()
pass