-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDrone.py
More file actions
192 lines (142 loc) · 5.1 KB
/
Drone.py
File metadata and controls
192 lines (142 loc) · 5.1 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import time
import fluidsynth
import numpy
import os
import sys
import serial
from serial.tools import list_ports
import matplotlib.pyplot as plt
import scipy.stats
import matplotlib.animation as animation
UseTrueRNG = False#set to True only if you have TrueRNG hardware
ChordSound = 101#voice of background hum
ChordVol = 100#volume of background hum
PentSound = 79#voice of plucking sound
PentVol = 100#volume of plucking sound
Key = 48#starting key, 60 = middle C
Kmax = 70#highest key before dropping octave
RNG_Interval = 0.2
RNG_BytesPerInterval = 25
ChordThres = 25#threshold of coherence in bitstream that triggers key change
PentThres = 13#threshold of coherence in bitstream that triggers a key pluck. Must be less than ChordThres
Rmks = sys.argv[1]
OutPath = os.getcwd()
Pmod = scipy.stats.norm.sf(ChordThres/((RNG_BytesPerInterval*2)**0.5))*2
def Modulate():
K = KeyList[-1]
fs.noteoff(0, K)
fs.noteoff(0, K+4)
fs.noteoff(0, K+7)
fs.noteoff(0, K+12)
if (K>Kmax):
K+= -7
else:
K+=5
fs.noteon(0, K, ChordVol)
fs.noteon(0, K+4, ChordVol)
fs.noteon(0, K+7, ChordVol)
fs.noteon(0, K+12, ChordVol)
#NewPent = [K-12,K-10,K-8,K-5,K-3,K,K+2,K+4,K+7,K+9,K+12]
KeyList.append(K)
#return NewPent
totaltime=[]
totalmods=[]
P1=[]
def animate(i):
KK = KeyList[-1]
Pent = [KK-12,KK-10,KK-8,KK-5,KK-3,KK,KK+2,KK+4,KK+7,KK+9,KK+12]
current_time = time.time()
totaltime.append(current_time-(starttime/1000.0))
if (UseTrueRNG==True):
ser.flushInput()
x = ser.read(RNG_BytesPerInterval)
else:
x = numpy.random.randint(0,256,RNG_BytesPerInterval)
rc=0
for a in range (0,RNG_BytesPerInterval):
rc+=lib[x[a]]
outfile.write('%d,'%(x[a]))
outfile.write('%d\n'%(int(current_time*1000)))
if (Pul<=rc<Cul) or (Cll<rc<=Pll):
if (len(Gigasavenote)==0):
fs1.noteoff(0, 0)
else:
fs1.noteoff(0, Gigasavenote[-1])
note = int(x[0]/23.1819)
Gigasavenote.append(Pent[note])
fs1.noteon(0, Pent[note], PentVol)
#time.sleep(RNG_Interval)
#fs1.noteoff(0, Pent[note])
#switch = numpy.random.randint(0,20)
if (rc>=Cul) or (rc<=Cll):
Modulate()
fs1.noteoff(0, 0)
outfile.write('modulation\n')
#print(current_time)
EX = len(totaltime)*Pmod
totalmods.append((len(KeyList)-2)-EX)
P1.append(((len(totaltime)*Pmod*(1-Pmod))**0.5)*1.65)
MinY = numpy.amin([numpy.amin(P1),numpy.amin(totalmods)])
MaxY = numpy.amax([numpy.amax(P1),numpy.amax(totalmods)])
MaxX = totaltime[-1]+1
ax1.clear()
# ax1.imshow(im, aspect='auto', extent=(0,MaxX,MinY,MaxY))
ax1.set_xlim(0,MaxX)
ax1.set_ylim(MinY,MaxY)
ax1.plot(totaltime,totalmods)
ax1.plot(totaltime,P1)
#print('ok')
Gigasavenote=[]
starttime = int(time.time()*1000)
outfile = open('%s/Drone_%d_%s.txt'%(OutPath,starttime,Rmks),'w')
outfile.write('%s,%d,%d,%d,%d,%d,%d,%f,%d,%d,%d\n'%(UseTrueRNG,ChordSound,ChordVol,PentSound,PentVol,Key,Kmax,RNG_Interval,RNG_BytesPerInterval,ChordThres,PentThres))
lib=[]
readFile = open('%s\Conversion.txt'%(OutPath), 'r')
sepfile = readFile.read().split('\n')
for b in range (0,len(sepfile)):
xandy = sepfile[b].split('\t')
lib.append(float(xandy[0]))
if (UseTrueRNG==True):
ports=dict()
ports_avaiable = list(list_ports.comports())
rng_com_port = None
for temp in ports_avaiable:
if "pro" in temp[1]:
print('Found: ' + str(temp))
if rng_com_port == None: # always chooses the 1st TrueRNG found
rng_com_port=str(temp[0])
print('Using com port: ' + str(rng_com_port))
print('==================================================')
sys.stdout.flush()
try:
ser = serial.Serial(port=rng_com_port,timeout=10) # timeout set at 10 seconds in case the read fails
except:
print('Port Not Usable!')
print('Do you have permissions set to read ' + rng_com_port + ' ?')
if(ser.isOpen() == False):
ser.open()
ser.setDTR(True)
ser.flushInput()
sys.stdout.flush()
fs = fluidsynth.Synth()
fs.start(driver = 'dsound') # use DirectSound driver
sfid = fs.sfload(r'FluidR3_GM.sf2')
fs1 = fluidsynth.Synth()
fs1.start(driver = 'dsound') # use DirectSound driver
sfid1 = fs1.sfload(r'FluidR3_GM.sf2')
fs1.program_select(0, sfid, 0, PentSound)
fs.program_select(0, sfid, 0, ChordSound)
Pll = (RNG_BytesPerInterval*4)-PentThres
Pul = (RNG_BytesPerInterval*4)+PentThres
Cll = (RNG_BytesPerInterval*4)-ChordThres
Cul = (RNG_BytesPerInterval*4)+ChordThres
KeyList = [Key-5]
fig = plt.figure()
ax1 = fig.add_subplot(111)
savenote = 0
#Pent = [K-12,K-10,K-8,K-5,K-3,K,K+2,K+4,K+7,K+9,K+12]
#fs.noteon(60, 60, ChordVol)
Modulate()
print('ok')
ani = animation.FuncAnimation(fig, animate, interval=int(RNG_Interval*1000))
plt.show()