0db98be7809cd50e7d3a86fa3ee751f4032842ce
1 # -*- coding: utf-8 -*-
3 converstion d'un fichier musicxml en objet song minwii.
9 from types
import StringTypes
10 from xml
.dom
.minidom
import parse
11 from optparse
import OptionParser
16 DIATO_SCALE
= {'C' : 60,
27 def __init__(self
, node
, divisions
) :
28 self
.step
= _getNodeValue(node
, 'pitch/step')
29 self
.octave
= int(_getNodeValue(node
, 'pitch/octave'))
30 self
._duration
= float(_getNodeValue(node
, 'duration'))
31 self
.lyric
= _getNodeValue(node
, 'lyric/text')
33 self
.divisions
= divisions
37 mid
= DIATO_SCALE
[self
.step
]
38 mid
= mid
+ (self
.octave
- OCTAVE_REF
) * 12
43 return self
._duration
/ self
.divisions
47 def _getNodeValue(node
, path
, default
=_marker
) :
49 for name
in path
.split('/') :
50 node
= node
.getElementsByTagName(name
)[0]
51 return node
.firstChild
.nodeValue
53 if default
is _marker
:
58 def musicXml2Song(input, output
, partIndex
=0) :
59 if isinstance(input, StringTypes
) :
60 input = open(input, 'r')
63 doc
= d
.documentElement
65 # TODO conversion préalable score-timewise -> score-partwise
66 assert doc
.nodeName
== u
'score-partwise'
68 parts
= doc
.getElementsByTagName('part')
69 leadPart
= parts
[partIndex
]
71 # divisions de la noire
73 midiNotes
, durations
, lyrics
= [], [], []
75 for measureNode
in leadPart
.getElementsByTagName('measure') :
76 divisions
= int(_getNodeValue(measureNode
, 'attributes/divisions', divisions
))
77 for noteNode
in measureNode
.getElementsByTagName('note') :
78 note
= Note(noteNode
, divisions
)
79 midiNotes
.append(note
.midi
)
80 durations
.append(note
.duration
)
81 lyrics
.append(note
.lyric
)
84 midiNoteNumbers
= midiNotes
,
85 noteLengths
= durations
,
87 notesInExtendedScale
=None)
92 usage
= "%prog musicXmlFile.xml outputSongFile.smwi [options]"
93 op
= OptionParser(usage
)
94 op
.add_option("-i", "--part-index", dest
="partIndex"
96 , help = "Index de la partie qui contient le champ.")
98 options
, args
= op
.parse_args()
101 raise SystemExit(op
.format_help())
107 if __name__
== '__main__' :