victor@1
|
1 '''
|
victor@1
|
2 Created on 24/09/2014
|
victor@1
|
3
|
victor@1
|
4 @author: victor
|
victor@1
|
5
|
victor@1
|
6 Class for adding and joining measures
|
victor@1
|
7 (It is under development)
|
victor@1
|
8 '''
|
victor@1
|
9 import os
|
victor@1
|
10 from music21 import stream
|
victor@1
|
11 from music21 import converter
|
victor@1
|
12 from music21 import meter
|
victor@1
|
13 from music21 import key
|
victor@1
|
14 from music21 import clef
|
victor@1
|
15 from music21 import note
|
victor@1
|
16 from music21 import interval
|
victor@1
|
17
|
victor@1
|
18 class AddingXMLSingleMeasures:
|
victor@1
|
19 def getAllFiles(self,path):
|
victor@1
|
20 omr_files=[]
|
victor@1
|
21 dir_content = os.listdir(path)
|
victor@1
|
22 dir_content.sort()
|
victor@1
|
23 for myfile in dir_content:
|
victor@1
|
24 directory = os.path.join(path,myfile)
|
victor@1
|
25 omr_files.append(os.path.abspath(directory))
|
victor@1
|
26 print omr_files
|
victor@1
|
27 return omr_files
|
victor@1
|
28
|
victor@1
|
29 def runViewJoinXML(self,dirname):
|
victor@1
|
30 path = dirname
|
victor@1
|
31 files=self.getAllFiles(path)
|
victor@1
|
32 filesM21=self.getFilesM21(files,path)
|
victor@1
|
33 part=stream.Part()
|
victor@1
|
34 ks_array=[]
|
victor@1
|
35 for f in filesM21:
|
victor@1
|
36 measure=f.parts[0].getElementsByClass(stream.Measure)[0]
|
victor@1
|
37 if len(part)==0:
|
victor@1
|
38 ks_array=self.getKeySignatureArray(measure)
|
victor@1
|
39
|
victor@1
|
40 if len(part)>0:
|
victor@1
|
41 measure=self.removeTimeSignature(measure)
|
victor@1
|
42 measure=self.removeKeySignature(measure)
|
victor@1
|
43 measure=self.removeClef(measure)
|
victor@1
|
44 # measure=self.transpose(measure,-10)
|
victor@1
|
45 part.append(measure)
|
victor@1
|
46 # part=self.correctKeySignature(part,["C "])
|
victor@1
|
47 # the correct key signature
|
victor@1
|
48 part=self.correctKeySignature(part,ks_array)
|
victor@1
|
49 part.show()
|
victor@1
|
50 def transpose(self,measure,semitones):
|
victor@1
|
51 newMeasure=stream.Measure()
|
victor@1
|
52 for element in measure:
|
victor@1
|
53 if isinstance(element,note.Note):
|
victor@1
|
54 element=element.transpose(interval.Interval(semitones))
|
victor@1
|
55 newMeasure.append(element)
|
victor@1
|
56 return newMeasure
|
victor@1
|
57 def getKeySignatureArray(self,measure):
|
victor@1
|
58 ks_array=[]
|
victor@1
|
59 for element in measure:
|
victor@1
|
60 if isinstance(element,key.KeySignature):
|
victor@1
|
61 for p in element.alteredPitches:
|
victor@1
|
62 ks_array.append(str(p))
|
victor@1
|
63 return ks_array
|
victor@1
|
64 def correctKeySignature(self,part,altered):
|
victor@1
|
65 newPart=stream.Part()
|
victor@1
|
66 for measure in part.getElementsByClass(stream.Measure):
|
victor@1
|
67 newMeasure=stream.Measure()
|
victor@1
|
68 for element in measure:
|
victor@1
|
69 if isinstance(element,note.Note):
|
victor@1
|
70 for alter in altered:
|
victor@1
|
71 print element.pitch.name,alter
|
victor@1
|
72 if element.pitch.name[0:1]==alter[0:1] and alter[1:2]=="#":
|
victor@1
|
73 element.accidental=1
|
victor@1
|
74 if element.pitch.name[0:1]==alter[0:1] and alter[1:2]=="-":
|
victor@1
|
75 element.accidental=-1
|
victor@1
|
76 if element.pitch.name[0:1]==alter[0:1] and alter[1:2]==" ":
|
victor@1
|
77 element.accidental=0
|
victor@1
|
78 newMeasure.append(element)
|
victor@1
|
79 newPart.append(newMeasure)
|
victor@1
|
80 return newPart
|
victor@1
|
81
|
victor@1
|
82 def removeTimeSignature(self,measure):
|
victor@1
|
83 newmeasure=stream.Measure()
|
victor@1
|
84 for element in measure:
|
victor@1
|
85 if not isinstance(element,meter.TimeSignature):
|
victor@1
|
86 newmeasure.append(element)
|
victor@1
|
87 return newmeasure
|
victor@1
|
88 def removeKeySignature(self,measure):
|
victor@1
|
89 newmeasure=stream.Measure()
|
victor@1
|
90 for element in measure:
|
victor@1
|
91 if not isinstance(element,key.KeySignature):
|
victor@1
|
92 newmeasure.append(element)
|
victor@1
|
93 return newmeasure
|
victor@1
|
94 def removeClef(self,measure):
|
victor@1
|
95 newmeasure=stream.Measure()
|
victor@1
|
96 for element in measure:
|
victor@1
|
97 if not isinstance(element,clef.GClef) and not isinstance(element,clef.NoClef) :
|
victor@1
|
98 newmeasure.append(element)
|
victor@1
|
99 return newmeasure
|
victor@1
|
100
|
victor@1
|
101
|
victor@1
|
102
|
victor@1
|
103
|
victor@1
|
104 def getFilesM21(self,files,path):
|
victor@1
|
105 filesM21=[]
|
victor@1
|
106 print "---Converting Files---"
|
victor@1
|
107 for i in range(len(files)):
|
victor@1
|
108 f=path+"\M"+str(i+1)+".xml"
|
victor@1
|
109 f_m21=converter.parse(f)
|
victor@1
|
110 filesM21.append(f_m21)
|
victor@1
|
111 return filesM21 |