-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphing.py
82 lines (75 loc) · 2.66 KB
/
graphing.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
def getMalhun():
paths = corpus.getLocalPaths()
malhun = []
for p in paths:
if 'malhun' in p:
this = corpus.parse(p)
malhun.append(this)
return malhun
# file name = title-singer-refrain-Final
def createMetadata(fileName):
metadata = {}
metadata["fileName"] = fileName
splitName = fileName.split('-')
metadata["title"] = splitName[0]
metadata["artist"] = splitName[1]
metadata["refrain"] = splitName[2].split('.')[0]
if len(splitName) == 4:
metadata["isFinal"] = 1
else:
metadata["isFinal"] = 0
return metadata
def fromRoot(pitchFrequency, rootFrequency, pitchCollection):
# return value from pitchCollection - root's value
rootIndex = pitchCollection.index(rootFrequency)
pitchIndex = pitchCollection.index(pitchFrequency)
return pitchIndex - rootIndex
def getPitchCollection(flatScore):
pitches = [];
for n in flatScore:
if n.pitch.frequency not in pitches:
pitches.append(n.pitch.frequency)
pitches.sort()
return pitches
def getNotes(recursiveScore, rootFrequency):
notes = []
allNotes = recursiveScore.notes
pitchCollection = getPitchCollection(allNotes)
transposingUp = rootFrequency < 312 # just above D# in the middle of the bass clef
initialRestOffset = allNotes[0].offset
for n in allNotes:
noteEntry = {}
noteEntry["offset"] = float(n.getOffsetBySite(recursiveScore)) - initialRestOffset
noteEntry["duration"] = float(n.quarterLength)
noteEntry["fromRoot"] = 'rest'
noteEntry["frequency"] = 'rest'
if n.isNote:
noteEntry["fromRoot"] = fromRoot(n.pitch.frequency, rootFrequency, pitchCollection)
noteEntry["frequency"] = n.pitch.frequency
if transposingUp:
noteEntry["frequency"] *= 2
notes.append(noteEntry)
return notes
def createEntry(thisScore):
entry = {}
recursiveScore = thisScore.recurse()
entry["metadata"] = createMetadata(thisScore.metadata.title)
entry["root"] = recursiveScore.notes[-1].pitch.frequency
entry["notes"] = getNotes(recursiveScore, entry["root"])
return entry
def getData(collection):
data = []
total = len(collection)
for i, s in enumerate(collection):
data.append(createEntry(s))
progress = i * 1.0 / total * 100
sys.stdout.write(" -- Processing melodies: %d%%\r" % progress)
sys.stdout.flush()
savePath = 'contour_chart/malhun.json'
with open(savePath, 'w') as outfile:
json.dump(data, outfile)
print ' -- Data saved to', savePath
from music21 import *
import json
import sys
getData(getMalhun())