-
Notifications
You must be signed in to change notification settings - Fork 4
/
FFT_receive.py
111 lines (89 loc) · 3.07 KB
/
FFT_receive.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
#!/usr/bin/python3
#
# Copyright (C) 2019 Michele Scuttari, Marina Nikolic
#
# Usage: FFT_receive.py serial_port_name filename
# Example: ./FFT_receive.py COM1 fft.csv
import sys, serial, os
from serial import SerialException
from subprocess import call
# bytes representing the expected size of the audio data chunk
# what it does is read bytes until it reach the head of a new chunk of data
# i.e. the size of the chunk (that should be the expected batch size)
def recoveryProcedure(expectedBatchSize):
""" tries to perform recovery in case of a communication error """
s = ser.read(size=1)
while True :
if s != expectedBatchSize[0:1]:
s = ser.read(size=1)
else :
s = ser.read(size=1)
if s == expectedBatchSize[1:2] :
s = ser.read(size=1)
if s == expectedBatchSize[2:3]:
s = ser.read(size=1)
if s == expectedBatchSize[3:4]:
return int.from_bytes(expectedBatchSize,byteorder='little', signed=True)
beginSignal = "#start"
defaultFileName = "fft.csv"
# Setup serial port (no timeout specified)
portName = "COM1"
if len(sys.argv) == 1:
print("[WARNING] No serial port specified. Assuming %s" % portName)
else:
portName = sys.argv[1]
try:
ser = serial.Serial(port = portName, \
baudrate = 115200, \
stopbits = serial.STOPBITS_ONE, \
parity = serial.PARITY_NONE, \
bytesize = serial.EIGHTBITS, \
timeout = None, \
rtscts = False, \
dsrdtr = False, \
xonxoff = False
)
except ValueError:
print("[ERROR] Invalid port configuration")
sys.exit(-1)
except SerialException:
print("[ERROR] Can't open port", portName)
sys.exit(-1)
if len(sys.argv) <= 2:
fileName = defaultFileName
print("Default name '" + defaultFileName + "' assigned.\nUse ./FFT_receive.py filename to specify a different output file name.")
else:
fileName = sys.argv[2]
outputFile = open("fft.raw", "wb")
# Wait for the begin signal
print("Waiting for the recording to start...")
sample = ""
while sample != beginSignal:
sample = ser.readline().decode()
sample = sample.split("\r\n")[0]
print("Started recording.", flush=True)
# Get expected batch size
expectedBatchSize = ser.read(4)
expectedBatchSizeInt = int.from_bytes(expectedBatchSize, byteorder='little', signed=True)
print("Expected batch size: " + str(expectedBatchSizeInt) + ".")
nextBatchSizeInt = -1
while nextBatchSizeInt != 0:
# Read size of next batch
nextBatchSize = ser.read(4)
nextBatchSizeInt = int.from_bytes(nextBatchSize, byteorder='little', signed=True)
print("Batch size: " + str(nextBatchSizeInt) + ".")
# Check if an error occured
if nextBatchSizeInt != expectedBatchSizeInt and nextBatchSizeInt != 0 :
nextBatchSizeInt = recoveryProcedure(expectedBatchSize)
print('Error detected. Trying recovery.', flush=True)
# Read the batch
sample = ser.read(size=nextBatchSizeInt)
# Write data to file
outputFile.write(sample)
print("Recording completed with success!")
# decode adpcm to wav file
dataExtraction = call(["FFT_extract", "fft.raw", fileName])
if dataExtraction == 0:
print("FFT extraction completed")
else:
print("Error in FFT extraction")