-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicJud.py
79 lines (70 loc) · 2.59 KB
/
musicJud.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
#judges music
from musicGen import *
import musicData1 #data stored in here
#startLet, majMin, chordProgression, melodyRhyth, pitches = generateNewMusic()
## reformat
def convertPitchesToNumbers(startLet, melodyPitches, majMin):
baseScale = generateScale(startLet, majMin)
numberList = []
for pitch in range(len(melodyPitches)):
ind = baseScale.index(melodyPitches[pitch])
numberList.append(ind)
return numberList
def getMelodyContour(startLet, melodyPitch, majMin): #gets differences in pitches
pitchInNumbers = convertPitchesToNumbers(startLet, melodyPitch, majMin)
firstNumber = 0
contour = []
for i in range(0, len(pitchInNumbers)):
contour.append(pitchInNumbers[i]-firstNumber)
firstNumber = pitchInNumbers[i]
return contour
## machine learning
def distance(l1, l2):
#square of differences
diffs = []
for i in range(len(l1)):
diffs.append((l2[i]-l1[i])**2)
return sum(diffs)
## ADAPTED FROM Google tutorial on Youtube in Machine Learning
class myClassifier(): #finds closest point
def fit(self, xTrain, yTrain):
self.X = xTrain
self.Y = yTrain
def predict(self, test):
currBestDiff = distance(test, self.X[0])
bestIndex = 0
for i in range(1, len(self.X)):
compareThisDist = distance(test, self.X[i])
if compareThisDist < currBestDiff:
currBestDiff = compareThisDist
bestIndex = i
return self.Y[bestIndex]
def judgeMusic(startLet, majorBool, chordProg, melodyRhyth, melodyPitches): #returns a mood
if majorBool == True:
isMajor = 1
else:
isMajor = 0
#match data based on major/minor (pitch jumps are different between major and minor)
if isMajor == 1:
xData = musicData1.XMajor
yData = musicData1.YMajor
else:
xData = musicData1.XMinor
yData = musicData1.YMinor
#format judged data
judgeThis = []
melodyContour = getMelodyContour(startLet, melodyPitches, majorBool)
# print(melodyContour)
judgeThis.append(len(melodyPitches))
#gets percentage count of jumps 0 - 6, in absolute value
for i in range(7):
count = 0
for j in range(len(melodyContour)):
if abs(melodyContour[j]) == i:
count += 1
judgeThis.append(count * 100 / len(melodyPitches))
# print(judgeThis)
my_classifier = myClassifier() #uses written classifier above (creates an instance to judge with)
my_classifier.fit(xData, yData)
prediction = my_classifier.predict(judgeThis)
return prediction