2 converstion d'un fichier musicxml en objet song minwii.
8 from types
import StringTypes
9 from xml
.dom
.minidom
import parse
10 from optparse
import OptionParser
14 DIATO_SCALE
= {'C' : 60,
25 def __init__(self
, node
, divisions
) :
26 self
.name
= _getNodeValue(node
, 'pitch/step')
27 self
.octave
= int(_getNodeValue(node
, 'pitch/octave'))
28 self
._duration
= float(_getNodeValue(node
, 'duration'))
29 self
.divisions
= divisions
33 mid
= DIATO_SCALE
[self
.name
]
34 mid
= mid
+ (self
.octave
- OCTAVE_REF
) * 12
39 return self
._duration
/ self
.divisions
43 def _getNodeValue(node
, path
, default
=_marker
) :
45 for name
in path
.split('/') :
46 node
= node
.getElementsByTagName(name
)[0]
47 return node
.firstChild
.nodeValue
49 if default
is _marker
:
54 def musicXml2Song(input, output
) :
55 if isinstance(input, StringTypes
) :
56 input = open(input, 'r')
59 doc
= d
.documentElement
61 # TODO conversion préalable score-timewise -> score-partwise
62 assert doc
.nodeName
== u
'score-partwise'
64 parts
= doc
.getElementsByTagName('part')
65 # on suppose que la première partie est le chant
68 # divisions de la noire
70 for measureNode
in leadPart
.getElementsByTagName('measure') :
71 divisions
= int(_getNodeValue(measureNode
, 'attributes/divisions', divisions
))
72 for noteNode
in measureNode
.getElementsByTagName('note') :
73 note
= Note(noteNode
, divisions
)
74 print note
.name
, note
.octave
, note
.midi
, note
.duration
78 usage
= "%prog musicXmlFile.xml outputSongFile.smwi [options]"
79 op
= OptionParser(usage
)
81 options
, args
= op
.parse_args()
83 raise SystemExit(op
.format_help())
89 if __name__
== '__main__' :