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
.alter
= int(_getNodeValue(node
, 'pitch/alter', 0))
31 self
._duration
= float(_getNodeValue(node
, 'duration'))
32 self
.lyric
= _getNodeValue(node
, 'lyric/text')
34 self
.divisions
= divisions
38 mid
= DIATO_SCALE
[self
.step
]
39 mid
= mid
+ (self
.octave
- OCTAVE_REF
) * 12
40 mid
= mid
+ self
.alter
45 return self
._duration
/ self
.divisions
49 name
= '%s%d' % (self
.step
, self
.octave
)
54 name
= '%s%s' % (name
, abs(self
.alter
) * alterext
)
60 def _getNodeValue(node
, path
, default
=_marker
) :
62 for name
in path
.split('/') :
63 node
= node
.getElementsByTagName(name
)[0]
64 return node
.firstChild
.nodeValue
66 if default
is _marker
:
71 def musicXml2Song(input, output
, partIndex
=0, printNotes
=False) :
72 if isinstance(input, StringTypes
) :
73 input = open(input, 'r')
76 doc
= d
.documentElement
78 # TODO conversion préalable score-timewise -> score-partwise
79 assert doc
.nodeName
== u
'score-partwise'
81 parts
= doc
.getElementsByTagName('part')
82 leadPart
= parts
[partIndex
]
84 # divisions de la noire
86 midiNotes
, durations
, lyrics
= [], [], []
88 for measureNode
in leadPart
.getElementsByTagName('measure') :
89 divisions
= int(_getNodeValue(measureNode
, 'attributes/divisions', divisions
))
90 for noteNode
in measureNode
.getElementsByTagName('note') :
91 note
= Note(noteNode
, divisions
)
93 print note
.name
, note
.midi
, note
.duration
, note
.lyric
94 midiNotes
.append(note
.midi
)
95 durations
.append(note
.duration
)
96 lyrics
.append(note
.lyric
)
99 midiNoteNumbers
= midiNotes
,
100 noteLengths
= durations
,
102 notesInExtendedScale
=None)
107 usage
= "%prog musicXmlFile.xml outputSongFile.smwi [options]"
108 op
= OptionParser(usage
)
109 op
.add_option("-i", "--part-index", dest
="partIndex"
111 , help = "Index de la partie qui contient le champ.")
112 op
.add_option("-p", '--print', dest
='printNotes'
113 , action
="store_true"
115 , help = "Affiche les notes sur la sortie standard (debug)")
117 options
, args
= op
.parse_args()
120 raise SystemExit(op
.format_help())
122 musicXml2Song(args
[0], args
[1], partIndex
=options
.partIndex
, printNotes
=options
.printNotes
)
126 if __name__
== '__main__' :