-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCADENZA.py
1310 lines (1223 loc) · 48.3 KB
/
CADENZA.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
############# CADENZA ###################
##### Wynne Yao 15-112 Term Project #####
#########################################
import pyaudio
import wave
from array import array
from struct import pack
from PIL import ImageTk, Image
import PIL.Image
import os
import random
from pydub import AudioSegment
from pydub.playback import play
from tkinter import messagebox
from tkinter import simpledialog
import copy
#########################################
# Pydub Reference Section Taken from Tara Stentz's Audio Lecture
def soundFromFile(file):
return AudioSegment.from_wav(file)
def getLen(file):
return len(soundFromFile(file))
def changeVolume(file, amount):
sound = soundFromFile(file)
loudSound = sound + amount
exportToFile(loudSound, "loudSound.wav")
return "loudSound.wav"
def exportToFile(sound, file):
sound.export(file, format="wav")
return file
def concatNotes(sound1, sound2, filename):
return exportToFile(sound1 + sound2, filename)
def repeatSound(file, amount):
sound = soundFromFile(file)
return exportToFile(sound * amount, "repeat.wav")
def getSection(file, start, end):
start *= 1000
end *= 1000
sound = soundFromFile(file)
newSound = sound[start:end]
return exportToFile(newSound, "slice.wav")
# End Pydub Reference Section
##########################################
from tkinter import *
def init(data):
data.trebleNotes = []
data.mode = 'splashScreen'
data.prevMode = None
data.scale = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
data.notesPositions = []
data.notesY = []
data.notesX = []
data.topLines = []
data.chords = []
data.dottedQuarterNote = 375 #milliseconds
data.eighthNote = 125
data.quarterNote = 250
data.halfNote = 500
data.timeSig = None
data.keySig = None
data.beatsPerMeasure = None
data.measures = 5
data.difficultyLevel = 1
data.song = []
data.notes = []
data.missingChords = []
data.notesOfMissingChords = []
data.score = 0
data.drawingC = False
data.drawingCPrevMode = False
data.notesWithMissingChords = []
data.jazzMode = False
data.jazzPrevMode = False
data.isCreatingNotes = False
data.isCreatingNotesPrev = False
data.inversion = None
data.currRhythm = None
data.pastRhythm = None
data.missingChords1dList = []
data.numMissingChords = 1
data.solved = False
data.printingSong = False
data.validKeySigs = {'C major', 'A minor', 'G major', 'E minor',
'D major', 'B minor', 'A major', 'F# minor', 'E major', 'C# minor',
'B major', 'G# minor', 'F# major', 'D# minor', 'C# major', 'A# minor',
'F major', 'D minor', 'Bb major', 'G minor', 'Eb major', 'C minor',
'Ab major', 'F minor', 'Db major', 'Bb minor', 'Gb major',
'Eb minor', 'Cb major', 'Ab minor'}
class Button(object):
textMargin = 5
def __init__(self, buttonWidth, buttonHeight, x, y, text,
textSize=15, textColor="gray5", boxColor='light grey'):
self.width = buttonWidth
self.height = buttonHeight
self.text = text
self.textSize = textSize
self.x = x
self.y = y
self.textColor = textColor
self.boxColor = boxColor
def draw(self, canvas, data):
if (data.mode == 'cadence'):
canvas.create_rectangle(self.x, self.y,
self.x+self.width,
self.y+self.height,
fill = self.boxColor)
canvas.create_text(self.x+self.width//2,
self.y+self.height//2,
text = self.text,
font = "Helvetica "+str(self.textSize),
fill = self.textColor)
def makeJazzBeat(data):
jazzPattern1 = [data.dottedQuarterNote, data.eighthNote, data.quarterNote,
data.quarterNote]
jazzPattern2 = [data.eighthNote, data.dottedQuarterNote, data.eighthNote,
data.dottedQuarterNote]
jazzPattern3 = [data.dottedQuarterNote, data.dottedQuarterNote,
data.eighthNote, data.eighthNote]
jazzPattern4 = [data.dottedQuarterNote, data.eighthNote,
data.dottedQuarterNote, data.eighthNote]
jazzPattern5 = [data.dottedQuarterNote, data.eighthNote,
data.dottedQuarterNote, data.eighthNote]
jazzBeats = [jazzPattern1, jazzPattern2,
jazzPattern3, jazzPattern4, jazzPattern5]
# change this to a markov chain that ends when run out of the number of
# notes
while(data.currRhythm == None or data.currRhythm == data.pastRhythm):
baseRhythm = random.choice(jazzBeats)
data.currRhythm = baseRhythm
#keep choosing until you get one different from last
#choose the rhythm off of which to make variations
data.pastRhythm = baseRhythm
measuresPerLine = 5
fullRhythm = baseRhythm*measuresPerLine
chordList = []
for chord in data.chords:
chordList += [changeChordStringToList(chord)]
#chordList in form [['C', 'E', 'G'], ['F', 'A', 'C']]
#data.chords is in form ['CEG', 'FAC'...]
indicesOfThree = divideByThree(data.missingChords)
for i in range(len(fullRhythm)):
if (i in indicesOfThree and data.solved == False):
silence = AudioSegment.from_wav(
"Media/Piano/silence4.wav")
play(silence)
else:
note1 = AudioSegment.from_wav(
"Media/Saxophone/"+chordList[i][0]+"3.wav"
)[:fullRhythm[i]]
note2 = AudioSegment.from_wav(
"Media/Saxophone/"+chordList[i][1]+"3.wav"
)[:fullRhythm[i]]
note3 = AudioSegment.from_wav(
"Media/Saxophone/"+chordList[i][2]+"3.wav"
)[:fullRhythm[i]]
chordTriad = note1.overlay(note2)
chordFull = chordTriad.overlay(note3)
play(chordFull)
def createBeat(data):
if (data.genre == "Jazz"):
makeJazzBeat(data)
def mousePressed(event, data):
if (data.mode == "splashScreen"): splashScreenMousePressed(event, data)
elif (data.mode == "cadence"): cadenceMousePressed(event, data)
elif (data.mode == "difficulty"): difficultyLevelMousePressed(event, data)
def keyPressed(event, data):
if (data.mode == "splashScreen"): splashScreenKeyPressed(event, data)
elif (data.mode == "cadence"): cadenceKeyPressed(event, data)
elif (data.mode == "difficulty"): difficultyLevelKeyPressed(event, data)
elif(data.mode == "helpScreen"): helpScreenKeyPressed(event, data)
elif (data.mode == "cadenceRef"): cadenceRefKeyPressed(event, data)
elif (data.mode == 'success'): successKeyPressed(event, data)
def timerFired(data):
if (data.mode == "splashScreen"): splashScreenTimerFired(data)
elif (data.mode == "cadence"): cadenceTimerFired(data)
elif (data.mode == "difficulty"): difficultyLevelTimerFired(data)
def redrawAll(canvas, data):
if (data.mode == "splashScreen"): splashScreenRedrawAll(canvas, data)
elif (data.mode == "cadence"): cadenceRedrawAll(canvas, data)
elif (data.mode == "difficulty"): difficultyLevelRedrawALl(canvas, data)
elif (data.mode == "success"): successRedrawAll(canvas, data)
elif (data.mode == 'helpScreen'): helpScreenRedrawAll(canvas, data)
elif (data.mode == 'cadenceRef'): cadenceRefRedrawAll(canvas, data)
##############################
# splashScreen mode
##############################
def splashScreenMousePressed(event, data):
# use event.x and event.y
widthPlay = 90
widthHow = 210
widthCadences = 170
height = 50
margin = 70
xPlay = data.width//2-widthPlay//2
xHelp = data.width//2-widthHow//2
xCadences = data.width//2-widthCadences//2
yPlay = data.height//2+margin
yHelp = data.height//2+margin*2
yCadences = data.height//2+margin*3
if (xPlay < event.x < xPlay+widthPlay and
yPlay < event.y < yPlay+height):
data.mode = 'difficulty'
data.prevMode = 'splashScreen'
elif (xHelp< event.x < xHelp+widthHow and
yHelp < event.y < yHelp+height):
data.mode = 'helpScreen'
data.prevMode = 'splashScreen'
elif (xCadences < event.x < xCadences+widthCadences and
yCadences < event.y < yCadences+height):
data.mode = 'cadenceRef'
data.prevMode = 'splashScreen'
def splashScreenKeyPressed(event, data):
pass
def splashScreenTimerFired(data):
pass
def drawTitle(canvas, data):
width = data.width//2
height = data.height//3
margin = 10
dFromTop = 100
canvas.create_text(data.width//2, data.height//2-dFromTop,
text = "CADENZA", font = "Helvetica 75 bold",
fill = "white")
def splashScreenRedrawAll(canvas, data):
drawImage(canvas, "Media/ending.jpg",
data.width, data.height, data.width//2, data.height//2)
drawTitle(canvas, data)
widthPlay = 90
widthHow = 210
widthCadences = 170
height = 50
margin = 70
xPlay = data.width//2-widthPlay//2
xHelp = data.width//2-widthHow//2
xCadences = data.width//2-widthCadences//2
yPlay = data.height//2+margin
yHelp = data.height//2+margin*2
yCadences = data.height//2+margin*3
textSize = 35
Button(widthPlay, height, xPlay, yPlay,
"Play!", textSize, 'white').draw(canvas, data)
Button(widthHow, height, xHelp, yHelp,
"How To Play", textSize, 'white').draw(canvas, data)
Button(widthCadences, height,
xCadences, yCadences, "Cadences", textSize,
'white').draw(canvas, data)
###########################################
# helpScreen Mode
###########################################
def helpScreenKeyPressed(event, data):
if (event.keysym == 'm'):
data.mode = "splashScreen"
data.prevMode = "helpScreen"
elif (event.keysym == 'r' and data.prevMode == 'cadence'):
data.mode = 'cadence'
data.prevMode = 'helpScreen'
def helpScreenRedrawAll(canvas, data):
addedWidth = 350
addedHeight = 250
drawImage(canvas, "Media/musicnotebg.jpg",
data.width, data.height, data.width//2, data.height//2)
drawImage(canvas, "Media/helpscreenbg.jpg",
data.width//2+addedWidth, data.height//2+addedHeight,
data.width//2, data.height//2)
marginFromTop = 55
canvas.create_text(data.width//2, marginFromTop, text = "HOW TO PLAY",
font = "Helvetica 50 bold", fill = "white")
startMargin = data.width//7
margin = 30
xHelp = data.width//8
canvas.create_text(xHelp, startMargin, anchor = NW,
text = "1. Enter a key and time signature."
"\n2. Click 'Make Song' and fill in the missing chords using"
"\n progressions."
"\nIf you need a hint, press 'Play' to hear the song."
"\n3. To fill in the missing chords, press the key 'n' and click"
"\nwhere you want the note."
"\n4. For the beginner level, if you accidentally create an inversion,"
"\nit's still counted as correct. For advanced, you must be"
"\ncompletely correct."
"\n\n Cadence guidelines: In music, there are progressions"
"\n - chords change to other chords."
"\n Go to 'Cadence' for more."
"\n\n5. Just for fun, you can 'Jazzify' the song!"
"\n\nPress 'm' for Main Menu and 'r' to return to the game.",
font = "Helvetica 20")
###########################################
# cadenceRef Mode
###########################################
def cadenceRefKeyPressed(event, data):
if (event.keysym == 'r' and data.prevMode == 'cadence'):
data.mode = 'cadence'
data.prevMode = 'cadenceRef'
elif (event.keysym == 'm'):
data.mode = 'splashScreen'
data.prevMode = 'cadenceRef'
def cadenceRefRedrawAll(canvas, data):
addedWidth = 300
addedHeight = 200
drawImage(canvas, "Media/musicnotebg.jpg",
data.width, data.height, data.width//2, data.height//2)
drawImage(canvas, "Media/helpscreenbg.jpg",
data.width//2+addedWidth, data.height//2+addedHeight,
data.width//2, data.height//2)
marginFromTop = data.height//10
canvas.create_text(data.width//2, marginFromTop, text = "CADENCE REFERENCE",
font = "Helvetica 40 bold", fill = "white")
startMargin = data.width//6
margin = 30
xInstructions = data.width//8
canvas.create_text(xInstructions, startMargin, anchor = NW,
text = " Types of cadences:"
"\n\n1. Perfect Cadence: V - I. Usually reserved for resolutions."
"\n2. Plagal Cadence: IV - I. Usually used to add tension."
"\n3. Imperfect Cadence: ? - V. Also used to add tension."
"\n\n Common progressions:"
"\n (from most likely to least likely to occur):"
"\n\n I to IV, V"
"\n II to I, V"
"\n IV to I, V"
"\n V to I, IV, II"
"\n\n Press 'r' to return to the game and 'm' for Main Menu.",
font = "Helvetica 20")
###########################################
# Difficulty Mode
###########################################
def difficultyLevelMousePressed(event, data):
widthButton = 300
heightButton = 70
xBeginner = xAdvanced = data.width//3.5
yBeginner = data.height//2-50
yAdvanced = data.height//2+50
if (xBeginner < event.x < xBeginner+widthButton and
yBeginner < event.y < yBeginner+heightButton):
data.difficultyLevel = 1
data.mode = "cadence"
elif (xAdvanced < event.x < xAdvanced+widthButton and
yAdvanced < event.y < yAdvanced+heightButton):
data.difficultyLevel = 2
data.mode = "cadence"
def difficultyLevelKeyPressed(event, data):
pass
def difficultyLevelTimerFired(data):
pass
def difficultyTitle(canvas, data):
margin = data.height//8
canvas.create_text(data.width//2, margin,
text = "Choose Your Difficulty Level", font = "Helvetica 50",
fill = "white")
def difficultyLevelRedrawALl(canvas, data):
widthButton = 300
heightButton = 70
xBeginner = xAdvanced = data.width//3.5
yBeginner = data.height//2-70
yAdvanced = data.height//2+70
margin = 10
height = 3.5
drawImage(canvas, "Media/ending.jpg",
data.width, data.height, data.width//2, data.height//2)
Button(widthButton, heightButton, xBeginner, yBeginner,
"Beginner", 45, 'white').draw(canvas, data)
Button(widthButton, heightButton, xAdvanced, yAdvanced,
"Advanced", 45, 'white').draw(canvas, data)
difficultyTitle(canvas, data)
##############################
# Cadence mode
##############################
def cadenceMousePressed(event, data):
xTime = xUndo = 70
y = 50
width = 150
height = 40
xKey = xDone = 230
xSong = xIsDrawingC = 390
xPlay = xJazz = 550
xCheck = 50
yCheck = data.height//2-50
xScore = 50
yScore = data.height//2
xAnswer = 50
yAnswer = data.height//2+50
xNote = data.width-150
yNote = data.height//2-10
xDiff = xNote-width//2
yDiff = yNote+35
if (xTime < event.x < xTime+width and
y < event.y < y+height):
data.printingSong = False
getTime(data)
validBeatsPerMeasure = [2, 4, 8]
if (data.timeSig != None and int(data.timeSig[2]) not in
validBeatsPerMeasure):
messagebox.showinfo("CADENZA", "Make sure you have entered"
" a time signature with 2, 4, or 8 on the bottom!")
getTime(data)
try:
data.beatsPerMeasure = data.timeSig[0]
#timeSig is in form of 3/8 or 4/4
except:
messagebox.showinfo("CADENZA", "Make sure you have entered"
" a valid time signature!")
getTime(data)
elif (xKey < event.x < xKey+width and
y < event.y < y+height):
getKey(data)
if (data.keySig not in data.validKeySigs):
messagebox.showinfo("CADENZA", "Make sure you have entered"
" a valid key signature!")
elif (xSong < event.x < xSong+width and
y < event.y < y+height):
if (data.keySig == None or data.timeSig == None):
messagebox.showinfo("CADENZA", "Make sure you have entered"
" a key and time signature!")
else:
if (data.song != []):
oldTimeSig = data.timeSig
oldKeySig = data.keySig
oldMode = data.mode
oldDiff = data.difficultyLevel
oldBPM = data.beatsPerMeasure
oldNumMissingChords = data.numMissingChords
init(data)
data.mode = oldMode
data.timeSig = oldTimeSig
data.beatsPerMeasure = oldBPM
data.keySig = oldKeySig
data.difficultyLevel = oldDiff
data.numMissingChords = oldNumMissingChords
data.printingSong = True
data.song = generateNewSong(data)
transcribeSongToStaff(data)
createListOfNotesOfMissingChords(data)
elif (xPlay < event.x < xPlay+width and
y < event.y < y+height):
playSong(data)
elif (xUndo < event.x < xUndo+width and
data.height-y < event.y < data.height-y+height):
undo(data)
elif (xDone < event.x < xDone+width and
data.height-y < event.y < data.height-y+height):
data.mode = 'success'
elif (xIsDrawingC < event.x < xIsDrawingC+width and
data.height-y < event.y < data.height-y+height):
if (data.drawingCPrevMode == False):
data.drawingC = True
data.drawingCPrevMode = True
else:
data.drawingC = False #we were drawing C, so now stop drawing C
data.drawingCPrevMode = False
elif (xJazz < event.x < xJazz+width and
data.height-y < event.y < data.height-y+height):
if (data.jazzPrevMode == False):
data.jazzMode = True
data.jazzPrevMode = True
else:
data.jazzMode = False
data.jazzPrevMode = False
elif (xCheck < event.x < xCheck+width and
yCheck < event.y < yCheck+height):
checkCorrect(data)
elif (xDiff < event.x < xDiff+width and
yDiff < event.y < yDiff+height):
getDiff(data)
elif (xAnswer < event.x < xAnswer+width and
yAnswer < event.y < yAnswer+height):
answer = ""
missingChords = makeIntoGroupsOfThreeWithAcc(data.notesOfMissingChords)
for i in range(len(missingChords)):
answer += "".join(missingChords[i])
if (i != len(missingChords)-1):
answer += ", "
messagebox.showinfo("CADENZA", "The answer is: "+str(answer))
else:
xMargin = 50
yMargin = 80
lineHeight = 15
staffHeight = 5*lineHeight+20 #add a margin for error
if (data.isCreatingNotes and
xMargin < event.x < data.width-xMargin
and yMargin < event.y < yMargin+staffHeight+lineHeight):
data.notesPositions += [(event.x, event.y, data.drawingC)]
data.notesY += [(event.y)]
data.notesX += [(event.x)]
detectTrebleNote(data)
def cadenceKeyPressed(event, data):
if (event.keysym == "n"):
if (data.isCreatingNotesPrev == False):
data.isCreatingNotes = True
data.isCreatingNotesPrev = True
else:
data.isCreatingNotes = False
data.isCreatingNotesPrev = False
elif (event.keysym == 'h'):
data.mode = 'helpScreen'
data.prevMode = 'cadence'
elif (event.keysym == 'c'):
data.mode = 'cadenceRef'
data.prevMode = 'cadence'
elif (event.keysym == 'm'):
data.mode = 'splashScreen'
data.prevMode = 'cadence'
def cadenceTimerFired(data):
pass
############################
# For all modes
############################
def getKey(data):
root = Tk()
data.keySig = simpledialog.askstring("Key","Enter key signature "
"- ex. F# major, Bb minor, C major")
def getTime(data):
root = Tk()
data.timeSig = simpledialog.askstring("Time Signature",
"Enter time signature"
" - ex. 4/4, 3/8, 2/2")
def getDiff(data):
root = Tk()
data.numMissingChords = int(simpledialog.askstring("Challenge",
"Enter how"
" many chords you want to guess!"))
def keySignatures(key):
if (key == 'C major' or key == 'A minor'):
keysig = []
elif (key == 'G major' or key == 'E minor'): #sharps
keysig = ['#', 'F']
elif (key == 'D major' or key == 'B minor'):
keysig = ['#', 'F', 'C']
elif (key == 'A major' or key == 'F# minor'):
keysig = ['#','F', 'C', 'G']
elif (key == 'E major' or key == 'C# minor'):
keysig = ['#','F', 'C', 'G', 'D']
elif (key == 'B major' or key == 'G# minor'):
keysig = ['#','F', 'C', 'G', 'D', 'A']
elif (key == 'F# major' or key == 'D# minor'):
keysig = ['#','F', 'C', 'G', 'D', 'A', 'E']
elif (key == 'C# major' or key == 'A# minor'):
keysig = ['#','F', 'C', 'G', 'D', 'A', 'E', 'B']
elif (key == 'F major' or key == 'D minor'): #flats
keysig = ['b','B']
elif (key == 'Bb major' or key == 'G minor'):
keysig = ['b','B', 'E']
elif (key == 'Eb major' or key == 'C minor'):
keysig = ['b','B', 'E', 'A']
elif (key == 'Ab major' or key == 'F minor'):
keysig = ['b','B', 'E', 'A', 'D']
elif (key == 'Db major' or key == 'Bb minor'):
keysig = ['b','B', 'E', 'A', 'D', 'G']
elif (key == 'Gb major' or key == 'Eb minor'):
keysig = ['b','B', 'E', 'A', 'D', 'G', 'C']
elif (key == 'Cb major' or key == 'Ab minor'):
keysig = ['b','B', 'E', 'A', 'D', 'G', 'C', 'F']
try:
return keysig
except:
messagebox.showinfo("Cadenza",
"Make sure you enter a valid key!")
def possibleChordsInKeySig(data, possibleChords):
#modifies possibleChords fcn with correct key signatures
keySigList = keySignatures(data.keySig)
chordsInKey = []
realChord = ''
for chord in possibleChords:
for note in chord:
realChord += note
if note in keySigList:
realChord += keySigList[0] #the sharp or the flat
chordsInKey += [realChord]
realChord = ''
return chordsInKey
def possibleChords(data):
#gets the chord in I, II, IV, and V without key signature,
#then adds key sig
possibleChords = []
tonic = 1
second = 2
fourth = 4
fifth = 5
numNotesInScale = 7
interval = 2
chordTypes = [tonic, second, fourth, fifth] #different types of chords
try:
for chordType in chordTypes:
tonicKeyIndex = data.scale.index(data.keySig[0])
startingKey = data.scale[(tonicKeyIndex+chordType-1)%numNotesInScale]
#data.keySig is in format 'A major'
startingKeyIndex = data.scale.index(startingKey)
possibleChords += [(startingKey+
data.scale[(startingKeyIndex+interval)%numNotesInScale]+
data.scale[(startingKeyIndex+2*interval)%numNotesInScale])]
return possibleChordsInKeySig(data, possibleChords)
except:
messagebox.showinfo("Cadenza",
"Make sure you enter a valid key!")
def divideByThree(anyList):
three = 3
newList = []
for val in anyList:
newList += [val//three]
return newList
def playChord(data, chordList, count):
indicesOfThree = divideByThree(data.missingChords)
if (count in indicesOfThree and data.solved == False):
silence = AudioSegment.from_wav(
"Media/Piano/silence4.wav")
play(silence)
else:
noteWithBeat = int(data.timeSig[2])
if (noteWithBeat == 4):
time = data.quarterNote
elif (noteWithBeat == 2):
time = data.halfNote
elif (noteWithBeat == 8):
time = data.eighthNote
note1 = AudioSegment.from_wav(
"Media/Piano/"+chordList[0]+"4.wav"
)[:time]
note2 = AudioSegment.from_wav(
"Media/Piano/"+chordList[1]+"4.wav"
)[:time]
note3 = AudioSegment.from_wav(
"Media/Piano/"+chordList[2]+"4.wav"
)[:time]
chordtriad = note1.overlay(note2)
chordactual = chordtriad.overlay(note3)
play(chordactual)
def changeChordStringToList(chord):
chordList = []
for i in range(len(chord)): #letter or accidental
if (chord[i] == '#' or chord[i] == 'b'):
chordList.pop()
accidental = chord[i]
accNote = chord[i-1]
#the note right before is the note with the accidental
chordList += [accNote+accidental]
#if chord = 'C#EG#', chordList = ['C#', 'E', 'G#']
else:
chordList += [chord[i]]
return chordList
def chordListWithOctaves(chord):
chordNoOctaves = changeChordStringToList(chord)
chordListWithOctaves = []
for note in chordNoOctaves:
chordListWithOctaves += [note+'4']
#so every note is played in octave 4
return chordListWithOctaves
#chordListWithOctaves returns ['C#4', 'E4', 'G4']
def convertSongOfChordTypesToNotes(data):
chordNotes = [] #chordNotesSong is a 2d list
chordNotesSong = []
listOfChords = possibleChords(data)
#possibleChords returns ['C#EG', 'DFA', 'FAC', 'GBD']
for chordType in data.song: #data.song is in form [1, 2, 4, 5]
if (chordType == 1):
chordNotes += [listOfChords[0]]
elif (chordType == 2):
chordNotes += [listOfChords[1]]
elif (chordType == 4):
chordNotes += [listOfChords[2]]
elif (chordType == 5):
chordNotes += [listOfChords[3]]
#chordNotes is in form ['CEG', 'FAC'...]
data.chords = chordNotes
return chordNotes
def playSong(data):
if (data.jazzMode):
makeJazzBeat(data)
else:
count = -1
for chord in convertSongOfChordTypesToNotes(data):
#['CEG', 'FAC', ...]
count += 1 #counts the chords
playChord(data, changeChordStringToList(chord), count) #[C, E, G]
# Research on Markov Chains done by talking with
# Anja Kalaba and Sara Adkins from RobOrchestra
def generateNewSong(data, currChord=None, song=None):
numNotesPerMeasure = int(data.beatsPerMeasure)
numMeasures = 5
first = 1
second = 2
fourth = 4
fifth = 5 # these are types of cadences
thirtyPerc = 30
sixtyPerc = 60
oneHPercent = 100
seventyPerc = 70
fiftyPerc = 50
sixtyPerc = 60
eightyPerc = 80
if (song == None):
song = []
if (len(song) == numNotesPerMeasure*numMeasures):
return song #use backtracking to make sure it ends on a 1 chord!
if (currChord == None):
currChord = random.choice([first, second, fourth, fifth])
randomNum = random.randint(0, oneHPercent)
if (currChord == fourth):
if (0 < randomNum < sixtyPerc): #60% chance it goes to chord I
song += [first]
elif (sixtyPerc < randomNum < oneHPercent): #40% chance it goes to chord V
song += [fifth]
if (currChord == fifth):
if (0 < randomNum < fiftyPerc):
song += [first]
elif (fiftyPerc < randomNum < eightyPerc):
song += [fourth]
elif (eightyPerc < randomNum < oneHPercent):
song += [second]
if (currChord == first):
if (0 < randomNum < seventyPerc):
song += [fourth]
elif (seventyPerc < randomNum < oneHPercent):
song += [fifth]
if (currChord == second):
if (0 < randomNum < thirtyPerc):
song += [fifth]
elif (thirtyPerc < randomNum < oneHPercent):
song += [first]
return generateNewSong(data, song[-1], song)
def isLegalCadence(data, currNoteIndex, cadences):
if (cadences[-1] != 'I' or cadences[-1] != 'V'):
return False
if (cadences[-1] == cadences[-2]):
return False
if (cadences[-1] == 'I'):
if (cadences[-2] != 'V' or cadences[-2] != 'IV'):
return False
def drawStaff(canvas, data):
xMargin = 50 #from edges of canvas
yMargin = 90
lineHeight = 15 #distance between each line of the staff
lineMargin = 40 #distance between bottom line of one staff and top of next
measuresPerLine = 5
totalStaffHeight = lineHeight*(measuresPerLine-1)
staffWidth = data.width-2*xMargin
measureWidth = staffWidth//measuresPerLine
numberOfStaffs = data.height//(totalStaffHeight+lineMargin)
for measure in range(1, measuresPerLine+1):
canvas.create_line(xMargin,
yMargin+measure*lineHeight,
data.width-xMargin,
yMargin+measure*lineHeight)
margin = 32
canvas.create_text(data.width//2, margin, text = "Create Your Cadence!",
font = "Helvetica 30 bold")
for measure in range(0, measuresPerLine+1):
canvas.create_line(xMargin+measureWidth*(measure),
yMargin+lineHeight,
xMargin+measureWidth*(measure),
yMargin+lineHeight+
totalStaffHeight)
def detectTrebleNote(data):
#make a list of all the top lines of each staff
yMargin = 90
xMargin = 50
measuresPerLine = 5
lineHeight = 15
lineMargin = 40 #distance between bottom line of one staff and top of next
totalStaffHeight = lineHeight*(measuresPerLine-1)
numberOfStaffs = data.height//(totalStaffHeight+lineMargin)
topLine = yMargin
mouseToEyeError = 10 # since when you click on a spot,
#its y position is 10 pixels off from where you think you clicked
r = 0.3
G5, F5, E5, D5, C5 = -0.5, 0, 0.5, 1, 1.5
B4, A4, G4, F4, E4, D4, C4 = 2, 2.5, 3, 3.5, 4, 4.5, 5
distanceFromTopLine = (data.notesY[-1]-topLine-mouseToEyeError)/lineHeight
if (G5-r < distanceFromTopLine < G5+r):
data.trebleNotes += ['G']
if (F5-r < distanceFromTopLine < F5+r):
data.trebleNotes += ['F']
elif (E5-r < distanceFromTopLine < E5+r):
data.trebleNotes += ['E']
elif (D5-r < distanceFromTopLine < D5+r):
data.trebleNotes += ['D']
elif (C5-r < distanceFromTopLine < C5+r):
data.trebleNotes += ['C']
elif (B4-r < distanceFromTopLine < B4+r):
data.trebleNotes += ['B']
elif (A4-r < distanceFromTopLine < A4+r):
data.trebleNotes += ['A']
elif (G4-r < distanceFromTopLine < G4+r):
data.trebleNotes += ['G']
elif (F4-r < distanceFromTopLine < F4+r):
data.trebleNotes += ['F']
elif (E4-r < distanceFromTopLine < E4+r):
data.trebleNotes += ['E']
elif (D4-r < distanceFromTopLine < D4+r):
data.trebleNotes += ['D']
elif (C4-r < distanceFromTopLine < C4+r):
data.trebleNotes += ['C']
def undo(data): #unplaces a note
data.notesPositions.pop()
data.notesY.pop()
data.notesX.pop()
data.trebleNotes.pop()
def inversion(data, currChord, missingChords):
#checks if the currchord is an inversion of any of the missingChords
root = (currChord[0])
third = (currChord[1])
fifth = (currChord[2])
regular = [root]+[third]+[fifth]
firstInv = [third]+[fifth]+[root]
secondInv = [fifth]+[root]+[third]
if regular in missingChords:
return regular
elif (firstInv in missingChords):
data.inversion = 'first inversion'
return firstInv
elif (secondInv in missingChords):
data.inversion = 'second inversion'
return secondInv
else:
#if is currChord in treble chords not inversion of any of missing chords
return False
def makeIntoGroupsOfThreeWithAcc(anyList):
groupOfThree = 3
if (len(anyList) % groupOfThree != 0): return None
count = -1
chord = []
numNotesInChord = 3
chordList = []
for note in anyList:
if (isinstance(note, Note)):
note = str(note)[:2] #makes C#4 into 'C#'!!
count += 1
chord += [note]
if (count == numNotesInChord-1):
chordList += [chord]
count = -1
chord = []
return chordList
def makeIntoGroupsOfThree(data, anyList):
groupOfThree = 3
if (len(anyList) % groupOfThree != 0): return None
count = -1
chord = []
numNotesInChord = 3
chordList = []
for note in anyList:
if (isinstance(note, Note)):
note = str(note)[0] #makes C4 into 'C'!!
count += 1
chord += [note]
if (count == numNotesInChord-1):
chordList += [chord]
count = -1
chord = []
return chordList
def checkCorrect(data):
correctInversion = False
score = 10
numNotes = 3
if (len(data.trebleNotes)%numNotes != 0):
messagebox.showinfo("Cadenza", "Make sure the"
" number of notes you have is a multiple of three!")
return None
trebleListInChords = makeIntoGroupsOfThree(data, data.trebleNotes)
missingChords = makeIntoGroupsOfThree(data, data.notesOfMissingChords)
#missingChords is a list of objects
if (data.difficultyLevel == 2): #hard - must get the chords exactly
for chord in trebleListInChords:
if chord in missingChords:
missingChords.remove(chord)
if (missingChords == []): #then you did it correctly!
data.score += score
data.solved = True
messagebox.showinfo("Cadenza", "Awesome job!"
" Your score has been updated. "
"Click 'Make Song' to generate"
" a new song, or click 'Done' to exit the game.")
elif (missingChords != []):
messagebox.showinfo("Cadenza", "Not quite!"
" You can edit your chord and try again.")
if (data.difficultyLevel == 1): #easy - allows for inversions
for chord in trebleListInChords:
if chord in missingChords: # is it exactly there?
missingChords.remove(chord)
elif (chord not in missingChords):
# if not, check if it is an inversion
correctInversion = inversion(data, chord, missingChords)
if (correctInversion != False): #if doesn't return False
missingChords.remove(correctInversion)
if (missingChords == [] and correctInversion == False):
data.score += score #then you did it but w/o inversions
data.solved = True
messagebox.showinfo("Cadenza", "Awesome job!"
" Your score has been updated. Click 'Make Song' to generate"
" a new song, or click 'Done' to exit the game.")
elif (missingChords == [] and correctInversion != False):
messagebox.showinfo("Cadenza", "Great "+data.inversion+"!"
" Your score has been updated.")
data.score += 2*score #bonus points if did an inversion
data.solved = True
elif (missingChords != []):
messagebox.showinfo("Cadenza", "Not quite!"
" You can keep going or edit your chord and try again.")
###############################
# success mode
###############################
def successKeyPressed(event, data):
if (event.keysym == 'r'):
init(data)
def successRedrawAll(canvas, data):
drawImage(canvas, "Media/ending.jpg",
data.width, data.height, data.width//2, data.height//2)
margin = 120
if (data.notes != []):
numNotes = 3
randomNotes = random.sample(data.notes, numNotes)
for note in data.notes:
if not (note in randomNotes):
note.draw(data, canvas)
blockm = 10
sIncrement = 5
canvas.create_rectangle(0, data.height//2-blockm, data.width//sIncrement,
data.height//2+blockm, fill = "white", outline = "white")
canvas.create_rectangle(data.width-data.width//sIncrement+blockm,
data.height//2-blockm,
data.width, data.height//2+blockm, fill = "white", outline = "white")
canvas.create_text(data.width//2, data.height//2, text = "Well Done",
font = "Helvetica 100 bold", fill = "white")
canvas.create_text(data.width//2, data.height//2+margin,
text = "Press R To Play Again", font = "Helvetica 50 bold",
fill = "white")
################################
class Note(object):
def __init__(self, note, chordNumber, measureNumber):
self.note = note #in form of 'F5'
self.measureNumber = measureNumber
self.chordNumber = chordNumber