-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
94 lines (80 loc) · 1.72 KB
/
main.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
from random import random
import scipy.io
import os
import matplotlib.pyplot as plt
import numpy as np
import serial
import time
'''
a function
input: matrice 1 * n
scale all data between 0 to 255
output: matrice 1 * n
'''
def scale(signal):
# min = 0
mx = float(max(signal))
signal = abs(signal)
ret = []
for sig in signal:
ret.append((float(sig) / mx) * 255)
return ret
# note that you should pass a valid integer
def sandewich(x):
x = float(x) / 127.0
if x <= 1:
return int(x * 25) + 65
else:
x = x - 1
return int(x * 25) + 97
'''
input : An array of (0, 255)
output : An array of [65, 90] + [97, 122]
'''
def toString(signal):
ret = ""
# signal = signal / 2
for sig in signal:
if sig < 130:
ret = ret + chr(int(sig / 5) + 65)
else:
ret = ret + chr(int(sig / 5) + 71)
return ret
def randomChoose(signal, part):
ln = len(signal)
ln /= part
ln = int(ln)
ret = []
for i in range(0, ln):
ret.append(signal[i * 200 + int(random()) % 200])
return ret
file_name = "cheers.wav"
file_path = os.path.join("", file_name)
sr, signal = scipy.io.wavfile.read(file_path)
time = len(signal) / sr
y = scale(signal)
y = randomChoose(y, 200)
# sr /= 200
# t = np.arange(len(y)) / float(sr)
# arrange(3) / sr = [1 / sr, 2 / sr, 3 / sr]
# show on the plot
'''
plt.figure()
plt.plot(t, y)
plt.show()
'''
# sending nhe data to the arduino
sent = toString(y)
print(sent)
com = serial.Serial("COM3", 9600)
com.timeout = 1
com.write(sent.encode())
'''
for sig in y:
sent = str(int(sig)) + ','
com.write(sent.encode())
time.sleep(.9)
#print(com.readline().decode("ascii"))
print(sig)
'''
com.close()