-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.py
402 lines (376 loc) · 12.7 KB
/
init.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from encoder import encode
from decoder import decode
from aes import encrypt,decrypt
import random
import xlrd
import pyglet
import time
import os
iterations = 10
mutationRate = .03
crossoverRate = .8
sampleSize = 10
data = None #the excel file
genSize = 30
generation = []
database = [] #each combination corresponds to index 0-168 and holds list of notes
#index 0 - 80 and holds rhythms
bestNotes = []
bestRhythms = []
#----------------GENETIC ALGORITHM---------------------#
class Individual:
bestChromosome = None
bestFitness = 0
def __init__(self, chromosome, maxElement):
self.chromosome = chromosome
fitness = 0
for i in range(0, maxElement * maxElement):
#compare each value to the frequency in database
#print("chrom")
#print(chromosome[i])
freq = 0
for j in database[i]:
if j == chromosome[i]:
freq = freq + 1
fitness = fitness + freq / len(database[i])
#print(freq)
self.fitness = fitness
if fitness > Individual.bestFitness:
Individual.bestChromosome = chromosome
Individual.bestFitness = fitness
def getFitness (self):
return self.fitness
def getChromosome (self):
return self.chromosome
def setChromosome(self, newChrom):
self.chromosome = newChrom
def randomChrom(maxElement):
chrom = []
for i in range (0, maxElement * maxElement):
chrom.append(random.randint(0, maxElement - 1))
return chrom
def createDatabase(startDatabase, endDatabase, maxElement):
global database
global data
#notes
for column in range (startDatabase, endDatabase, 3):
#find number of rows in each column
rowSize = 0
i = 0
while (i < data.nrows and data.cell(i, column).value != ''):
rowSize = rowSize + 1
i = i + 1
for row in range (2, rowSize - 2):
if maxElement == 13:
index = notes_numbs(row, column) * maxElement + notes_numbs(row + 1, column)
database[index].append(notes_numbs(row + 2, column))
else:
index = int(data.cell(row, column).value) * maxElement + int(data.cell(row + 1, column).value)
database[index].append(int(data.cell(row + 2, column).value))
def notes_numbs(row, column):
global data
switcher = {
"A": 1,
"A#": 2,
"B": 3,
"C": 4,
"C#": 5,
"D": 6,
"D#": 7,
"E": 8,
"F": 9,
"F#": 10,
"G": 11,
"G#": 12,
}
return switcher.get(str(data.cell(row, column).value), 0)
def temp (maxElement):
global database
for i in range (0, len(database)):
if database[i] == []:
database[i].append(random.randint(0,maxElement - 1))
def setup(startDatabase, endDatabase, maxElement):
global generation
global genSize
global data
global database
database = []
generation = []
Individual.bestChromosome = None
Individual.bestFitness = 0
for i in range (0, maxElement * maxElement):
database.append([])
#open the database file
data = xlrd.open_workbook("Music database.xlsx").sheet_by_index(0)
createDatabase (startDatabase, endDatabase, maxElement)
#DEBUG ONLY
temp(maxElement)
#print(database)
#generation 0: all random lists of chromosomes
for i in range (0, genSize):
generation.append(Individual(randomChrom(maxElement), maxElement))
#print(database)
def crossover(individuals):
#input: list of two Individuals to cross
global crossoverRate
if random.random() > crossoverRate:
return [individuals[0].getChromosome(), individuals[1].getChromosome()]
length = len(individuals[0].getChromosome())
splitpoint = random.randint(1, length - 2)
return [individuals[0].getChromosome()[:splitpoint]+individuals[1].getChromosome()[splitpoint:],
individuals[1].getChromosome()[:splitpoint]+individuals[0].getChromosome()[splitpoint:]]
def mutation(gen, maxElement):
#input: generation list of individuals of size genSize
global mutationRate
#for each individual
for ind in gen:
#for each index in the chromosome
newChrom = ind.getChromosome()
for i in range(0, len(newChrom)):
if random.random() < mutationRate:
#mutate
newChrom[i] = random.randint(0, maxElement - 1)
ind.setChromosome(newChrom)
return gen
def createNextGen(maxElement):
global generation
newGen = []
for i in range(0, int(genSize / 2)):
#choose random sample with sampleSize and mate top two
sublist = random.sample(generation, sampleSize)
bestFitness = [0, 0]
bestIndividuals = [None, None]
#get parents
for ind in sublist:
if ind.getFitness() > bestFitness[0] or ind.getFitness() > bestFitness[1]:
#replace with one of these
if bestFitness[0] > bestFitness[1]:
bestFitness[1] = ind.getFitness()
bestIndividuals[1] = ind
else:
bestFitness[0] = ind.getFitness()
bestIndividuals[0] = ind
#mate parents
newChromosomes = crossover(bestIndividuals)
#print(newChromosomes[0])
newGen.append(Individual(newChromosomes[0], maxElement))
newGen.append(Individual(newChromosomes[1], maxElement))
newGen = mutation(newGen, maxElement)
generation = newGen
def doGA(startDatabase, endDatabase, maxElement):
#startDatabase- what column to start creating the database from
#maxElement- max integer for each slot in the list
global bestNotes
global bestRhythms
setup(startDatabase, endDatabase, maxElement)
#print("Old")
#print(Individual.bestFitness)
#print(getCloseness())
for i in range (0, iterations):
createNextGen(maxElement)
#print("New")
#print(Individual.bestFitness)
#print(getCloseness())
if maxElement == 13:
bestNotes = Individual.bestChromosome
else:
bestRhythms = Individual.bestChromosome
#print(Individual.bestChromosome)
def most_common(lst):
return max(set(lst), key=lst.count)
def getNew():
global database
newDatabase = []
for i in database:
if i == []:
newDatabase.append(0)
else:
newDatabase.append(most_common(i))
return newDatabase
def getCloseness():
database1 = getNew()
database2 = Individual.bestChromosome
score = 0
for i in range(0, len(database1)):
if database1[i] == database2[i]:
score += 1
return score
#----------------PLAY BACK MUSIC--------------------#
def createMusic ():
global bestNotes
global bestRhythms
songLength = 50
switcherNote = {
0:"rest",
1:"A",
2:"A#",
3:"B",
4:"C",
5:"C#",
6:"D",
7:"D#",
8:"E",
9:"F",
10:"F#",
11:"G",
12:"G#"
}
switcherRhythm = {
0: "Dotted Eighth",
1: "Sixteenth",
2: "Eighth",
3: "Quarter",
4: "Dotted quarter",
5: "Half",
6: "Dotted half",
7: "Whole"
}
#notes
song = []
song.append(random.randint(0, 12))
song.append(random.randint(0, 12))
for i in range(2, songLength):
index = song[i-2] * 13 + song[i-1]
#print("%d: %d, %d" % (index, song[i-2], song[i-1]))
song.append(bestNotes[index])
print("song")
sng = ""
rhy = ""
for i in song:
sng+= switcherNote[i]+" "
print(sng)
#print(song)
#rhythms
rhythm = []
rhythm.append(random.randint(0, 7))
rhythm.append(random.randint(0, 7))
for i in range(2, songLength):
index = rhythm[i-2] * 8 + rhythm[i-1]
rhythm.append(bestRhythms[index])
print("rhythm")
for j in rhythm:
rhy+= switcherRhythm[j]+ " "
print(rhy)
#print(rhythm)
#playMusic(song, rhythm)
d = {'Dotted half':'-dhn.wav','Whole':'.wav','Eighth':'-en.wav','Half':'-hn.wav','Quarter':'-qn.wav','Dotted quarter':'-dqn.wav', 'Dotted Eighth':'-den.wav','Sixteenth':'-sn.wav'}
f = open('notes/mylist1.txt','w')
nn = []
bb = []
for i in range(len(song)):
nn.append(switcherNote[song[i]])
bb.append(switcherRhythm[rhythm[i]])
for i in range(len(nn)):
nn[i] = nn[i].replace('#','s',1)
bb[i] = d[(bb[i])]
f.write("file '{}{}'\n".format(nn[i],bb[i]))
f.close()
os.chdir('notes/')
#for i in range(len(notes))
concatenate = 'ffmpeg -f concat -i mylist1.txt -c copy output.wav'
os.system(concatenate)
print("Song generated")
def playMusic(song, rhythm):
pyglet.options['audio'] = ('openal', 'silent')
switcherNotes = {
#pyglet.media.load("A.wav").play()
1: "A.wav", #A
2: "A#.wav",#A#
3: "B.wav",#B"
4: "C.wav",#C"
5: "C#.wav",#C#"
6: "D.wav",#D"
7: "D#.wav",#D#"
8: "E.wav",#E"
9: "F.wav",#F"
10: "F#.wav",#F#"
11: "G.wav",#G"
12: "G#.wav" #G#"
}
switcherRhythms = {
0: 0.75,
1: 0.0625,
2: 0.125,
3: 0.25,
4: 1.5,
5: 0.5,
6: 3,
7: 1
}
#player = pyglet.media.Player()
for i in range(0, len(song)):
totalTime = 0
while(totalTime < switcherRhythms[rhythm[i]]):
if song[i] != 0:
pyglet.media.load(switcherNotes[song[i]], streaming=False).play()
#time.sleep(switcherRhythms[rhythm[i]]*.5)
time.sleep(.0625*1.5)
totalTime += .0625
#########################################################################
print("\n#####################################################################\n")
print("Please enter the text you want to hide: ")
plain_text = input()
start = time.time()
#Encrypting the text
print("\nEncrypting the text for encoding ... \n")
temp_cipher = encrypt(plain_text)
print("Cipher text generated is ",temp_cipher)
#Generating the music using Genetic Algorithm
print("\n#####################################################################\n")
print("\nGenerating music using AI ...")
doGA(3, 33, 13)
#bestNotes = getNew()
#print(bestNotes)
#getNew()
#rhythms
doGA(4, 33, 8)
#bestRhythms = getNew()
#print(bestRhythms)
#getNew()
createMusic()
#Encoding the cipher in the generated music
print("\n#####################################################################\n")
print("\nEncoding the cipher in the generated music...... ")
encode(temp_cipher)
end = time.time()
print(end - start)
print("Encoding done, new music file generated !")
#Decoding the message from the music
print("\n#####################################################################\n")
print("\nDecoding to find the cipher from song started ... ")
start = time.time()
decoded_string = decode()
print("\n#####################################################################\n")
#Decrpting the cipher text and Finding the plain text
print("\nDecrypting string to find the original text ... ")
decrypted_text = decrypt(decoded_string)
end = time.time()
print(end - start)
print(decrypted_text)
print("\n#####################################################################\n")
"""
songs = [
[ 7, 10, 5, 10, 9, 7, 8, 5, 8, 6, 11, 11, 0, 8, 6, 11, 11, 0, 8, 6],#: 300
[0, 3, 1, 1, 4, 1, 0, 1, 2, 3, 2, 6, 9, 7, 12, 9, 3, 0, 4, 3],#: 400
[9, 3, 4, 2, 10, 7, 8, 6, 8, 10, 6, 3, 3, 12, 3, 2, 3, 4, 2, 10],#: 500
[5, 12, 10, 9, 2, 7, 9, 5, 8, 6, 4, 3, 9, 10, 3, 4, 6, 9, 8, 6],#: 600
[6, 12, 4, 11, 4, 7, 5, 4, 10, 1, 6, 9, 4, 2, 11, 3, 2, 3, 11, 6],#: 700
[2, 2, 3, 8, 10, 11, 1, 0, 3, 9, 9, 8, 1, 8, 10, 11, 1, 0, 3, 9],#: 800
[5, 1, 1, 4, 6, 11, 0, 9, 1, 0, 11, 6, 6, 8, 2, 10, 6, 1, 1, 4],#: 900
[7, 2, 9, 6, 2, 9, 6, 2, 9, 6, 2, 9, 6, 2, 9, 6, 2, 9, 6, 2],#: 1000
[2, 0, 0, 9, 5, 12, 11, 10, 8, 2, 8, 0, 2, 10, 3, 5, 9, 7, 4, 4],#: 2000
[12, 6, 0, 10, 7, 10, 3, 5, 1, 8, 8, 1, 4, 2, 4, 11, 1, 3, 5, 1]#: 3000
]
rhythms = [
[3, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],#: 300
[2, 7, 3, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2],#: 400
[6, 4, 5, 5, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],#: 500
[7, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],#: 600
[3, 5, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],#: 700
[1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],#: 800
[2, 3, 1, 1, 4, 4, 5, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],#: 900
[7, 2, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],#: 1000
[0, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],#: 2000
[7, 2, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]#: 3000
]
"""