Skip to content

Commit f2f7d60

Browse files
Add more tests.
1 parent 3431144 commit f2f7d60

File tree

3 files changed

+328
-13
lines changed

3 files changed

+328
-13
lines changed

music21/musicxml/testPrimitive.py

Lines changed: 278 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,283 @@
11921192
</score-partwise>
11931193
"""
11941194

1195+
pedals = '''<?xml version="1.0" encoding="UTF-8"?>
1196+
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN"
1197+
"http://www.musicxml.org/dtds/partwise.dtd">
1198+
<score-partwise version="2.0">
1199+
<movement-title>MusicXML pedals</movement-title>
1200+
<identification>
1201+
<miscellaneous>
1202+
<miscellaneous-field name="description">All &lt;pedal&gt; element contents
1203+
defined in MusicXML. The lyrics for each note describes the pedal
1204+
element assigned to that note.</miscellaneous-field>
1205+
</miscellaneous>
1206+
</identification>
1207+
<part-list>
1208+
<score-part id="P1">
1209+
<part-name print-object="no">MusicXML Part</part-name>
1210+
</score-part>
1211+
</part-list>
1212+
<!--=========================================================-->
1213+
<part id="P1">
1214+
<!--=======================================================-->
1215+
<measure number="1" width="273">
1216+
<attributes>
1217+
<divisions>8</divisions>
1218+
<key>
1219+
<fifths>0</fifths>
1220+
<mode>major</mode>
1221+
</key>
1222+
<time>
1223+
<beats>6</beats>
1224+
<beat-type>8</beat-type>
1225+
</time>
1226+
<clef number="1">
1227+
<sign>F</sign>
1228+
<line>4</line>
1229+
</clef>
1230+
</attributes>
1231+
<direction>
1232+
<direction-type>
1233+
<pedal line="yes" sign="no" type="start" default-y="-79" relative-x="-12"/>
1234+
</direction-type>
1235+
</direction>
1236+
<note default-x="78">
1237+
<pitch>
1238+
<step>D</step>
1239+
<octave>3</octave>
1240+
</pitch>
1241+
<duration>4</duration>
1242+
<voice>1</voice>
1243+
<type>eighth</type>
1244+
<stem default-y="-55">down</stem>
1245+
<beam number="1">begin</beam>
1246+
</note>
1247+
<note default-x="110">
1248+
<pitch>
1249+
<step>D</step>
1250+
<octave>3</octave>
1251+
</pitch>
1252+
<duration>4</duration>
1253+
<voice>1</voice>
1254+
<type>eighth</type>
1255+
<stem default-y="-55">down</stem>
1256+
<beam number="1">continue</beam>
1257+
</note>
1258+
<note default-x="143">
1259+
<pitch>
1260+
<step>D</step>
1261+
<octave>3</octave>
1262+
</pitch>
1263+
<duration>4</duration>
1264+
<voice>1</voice>
1265+
<type>eighth</type>
1266+
<stem default-y="-55">down</stem>
1267+
<beam number="1">end</beam>
1268+
</note>
1269+
<direction>
1270+
<direction-type>
1271+
<pedal line="yes" type="change"/>
1272+
</direction-type>
1273+
</direction>
1274+
<note default-x="175">
1275+
<pitch>
1276+
<step>D</step>
1277+
<octave>3</octave>
1278+
</pitch>
1279+
<duration>4</duration>
1280+
<voice>1</voice>
1281+
<type>eighth</type>
1282+
<stem default-y="-55">down</stem>
1283+
<beam number="1">begin</beam>
1284+
</note>
1285+
<note default-x="207">
1286+
<pitch>
1287+
<step>D</step>
1288+
<octave>3</octave>
1289+
</pitch>
1290+
<duration>4</duration>
1291+
<voice>1</voice>
1292+
<type>eighth</type>
1293+
<stem default-y="-55">down</stem>
1294+
<beam number="1">continue</beam>
1295+
</note>
1296+
<direction>
1297+
<direction-type>
1298+
<pedal line="yes" type="discontinue"/>
1299+
</direction-type>
1300+
<direction-type>
1301+
<words default-y="-79" font-size="7.2" font-style="italic">simile</words>
1302+
</direction-type>
1303+
</direction>
1304+
<note default-x="240">
1305+
<pitch>
1306+
<step>D</step>
1307+
<octave>3</octave>
1308+
</pitch>
1309+
<duration>4</duration>
1310+
<voice>1</voice>
1311+
<type>eighth</type>
1312+
<stem default-y="-55">down</stem>
1313+
<beam number="1">end</beam>
1314+
</note>
1315+
</measure>
1316+
<measure number="2" width="208">
1317+
<note default-x="13">
1318+
<pitch>
1319+
<step>D</step>
1320+
<octave>3</octave>
1321+
</pitch>
1322+
<duration>4</duration>
1323+
<voice>1</voice>
1324+
<type>eighth</type>
1325+
<stem default-y="-55">down</stem>
1326+
<beam number="1">begin</beam>
1327+
</note>
1328+
<note default-x="46">
1329+
<pitch>
1330+
<step>D</step>
1331+
<octave>3</octave>
1332+
</pitch>
1333+
<duration>4</duration>
1334+
<voice>1</voice>
1335+
<type>eighth</type>
1336+
<stem default-y="-55">down</stem>
1337+
<beam number="1">continue</beam>
1338+
</note>
1339+
<note default-x="78">
1340+
<pitch>
1341+
<step>D</step>
1342+
<octave>3</octave>
1343+
</pitch>
1344+
<duration>4</duration>
1345+
<voice>1</voice>
1346+
<type>eighth</type>
1347+
<stem default-y="-55">down</stem>
1348+
<beam number="1">end</beam>
1349+
</note>
1350+
<note default-x="110">
1351+
<pitch>
1352+
<step>D</step>
1353+
<octave>3</octave>
1354+
</pitch>
1355+
<duration>4</duration>
1356+
<voice>1</voice>
1357+
<type>eighth</type>
1358+
<stem default-y="-55">down</stem>
1359+
<beam number="1">begin</beam>
1360+
</note>
1361+
<note default-x="143">
1362+
<pitch>
1363+
<step>D</step>
1364+
<octave>3</octave>
1365+
</pitch>
1366+
<duration>4</duration>
1367+
<voice>1</voice>
1368+
<type>eighth</type>
1369+
<stem default-y="-55">down</stem>
1370+
<beam number="1">continue</beam>
1371+
</note>
1372+
<note default-x="175">
1373+
<pitch>
1374+
<step>D</step>
1375+
<octave>3</octave>
1376+
</pitch>
1377+
<duration>4</duration>
1378+
<voice>1</voice>
1379+
<type>eighth</type>
1380+
<stem default-y="-55">down</stem>
1381+
<beam number="1">end</beam>
1382+
</note>
1383+
</measure>
1384+
<measure number="3" width="208">
1385+
<note default-x="13">
1386+
<pitch>
1387+
<step>D</step>
1388+
<octave>3</octave>
1389+
</pitch>
1390+
<duration>4</duration>
1391+
<voice>1</voice>
1392+
<type>eighth</type>
1393+
<stem default-y="-55">down</stem>
1394+
<beam number="1">begin</beam>
1395+
</note>
1396+
<note default-x="46">
1397+
<pitch>
1398+
<step>D</step>
1399+
<octave>3</octave>
1400+
</pitch>
1401+
<duration>4</duration>
1402+
<voice>1</voice>
1403+
<type>eighth</type>
1404+
<stem default-y="-55">down</stem>
1405+
<beam number="1">continue</beam>
1406+
</note>
1407+
<direction>
1408+
<direction-type>
1409+
<pedal line="yes" type="resume" default-y="-79" relative-x="-3"/>
1410+
</direction-type>
1411+
</direction>
1412+
<note default-x="78">
1413+
<pitch>
1414+
<step>D</step>
1415+
<octave>3</octave>
1416+
</pitch>
1417+
<duration>4</duration>
1418+
<voice>1</voice>
1419+
<type>eighth</type>
1420+
<stem default-y="-55">down</stem>
1421+
<beam number="1">end</beam>
1422+
</note>
1423+
<direction>
1424+
<direction-type>
1425+
<pedal line="yes" type="change"/>
1426+
</direction-type>
1427+
</direction>
1428+
<note default-x="111">
1429+
<pitch>
1430+
<step>D</step>
1431+
<octave>3</octave>
1432+
</pitch>
1433+
<duration>4</duration>
1434+
<voice>1</voice>
1435+
<type>eighth</type>
1436+
<stem default-y="-55">down</stem>
1437+
<beam number="1">begin</beam>
1438+
</note>
1439+
<note default-x="142">
1440+
<pitch>
1441+
<step>D</step>
1442+
<octave>3</octave>
1443+
</pitch>
1444+
<duration>4</duration>
1445+
<voice>1</voice>
1446+
<type>eighth</type>
1447+
<stem default-y="-55">down</stem>
1448+
<beam number="1">continue</beam>
1449+
</note>
1450+
<direction>
1451+
<direction-type>
1452+
<pedal line="yes" type="stop"/>
1453+
</direction-type>
1454+
</direction>
1455+
<note default-x="175">
1456+
<pitch>
1457+
<step>D</step>
1458+
<octave>3</octave>
1459+
</pitch>
1460+
<duration>4</duration>
1461+
<voice>1</voice>
1462+
<type>eighth</type>
1463+
<stem default-y="-55">down</stem>
1464+
<beam number="1">end</beam>
1465+
</note>
1466+
</measure>
1467+
</part>
1468+
<!--=========================================================-->
1469+
</score-partwise>
1470+
'''
1471+
11951472
# noinspection SpellCheckingInspection
11961473
directions31a = '''<?xml version="1.0" encoding="UTF-8"?>
11971474
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN"
@@ -20044,7 +20321,7 @@
2004420321
tremoloTest, hiddenRests, multiDigitEnding, tupletsImplied, pianoStaffPolymeter, # 46
2004520322
arpeggio32d, multiStaffArpeggios, multiMeasureEnding, # 51
2004620323
pianoStaffPolymeterWithClefOctaveChange, multipleFingeringsOnChord, # 54
20047-
pianoStaffWithOttava # 56
20324+
pianoStaffWithOttava, pedals # 56
2004820325
]
2004920326

2005020327

music21/musicxml/test_m21ToXml.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,24 @@ def testOutOfBoundsExpressionDoesNotCreateForward(self):
684684
int(tree.findall('.//direction/offset')[0].text),
685685
defaults.divisionsPerQuarter)
686686

687+
def testPedals(self):
688+
expectedResults = (
689+
'start',
690+
'change',
691+
'discontinue',
692+
'resume',
693+
'change',
694+
'stop'
695+
)
696+
s = converter.parse(testPrimitive.pedals)
697+
x = self.getET(s)
698+
mxPart = x.find('part')
699+
for i, mxPedal in enumerate(mxPart.findall('.//pedal')):
700+
self.assertEqual(mxPedal.get('type', ''), expectedResults[i])
701+
self.assertEqual(mxPedal.get('line', ''), 'yes')
702+
if i in (0, 5):
703+
self.assertEqual(mxPedal.get('number', ''), '1')
704+
687705
def testArpeggios(self):
688706
expectedResults = (
689707
'arpeggiate',

music21/musicxml/xmlToM21.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,8 +4079,8 @@ def xmlDirectionTypeToSpanners(
40794079
and ottava are encoded as MusicXML directions.
40804080
40814081
:param mxObj: the specific direction element (e.g. <wedge>).
4082-
:param staffKey: staff number (for directions that will insert into measure here)
4083-
:param totalOffset: offset in measure of this direction (similarly for insertion here)
4082+
:param staffKey: staff number (required for <pedal>)
4083+
:param totalOffset: offset in measure of this direction (required for <pedal>)
40844084
40854085
>>> from xml.etree.ElementTree import fromstring as EL
40864086
>>> MP = musicxml.xmlToM21.MeasureParser()
@@ -4115,29 +4115,49 @@ def xmlDirectionTypeToSpanners(
41154115
<music21.dynamics.Crescendo <music21.note.Note D>>
41164116
41174117
>>> mxDirection = EL('<direction place="below"/>')
4118-
>>> mxDirectionType = EL('<pedal type="sostenuto" number="2"/>')
4119-
>>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType)
4118+
>>> mxDirectionType = EL('<pedal type="sostenuto" sign="yes" number="2"/>')
4119+
>>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType, 1, 0.5)
41204120
>>> retList
41214121
[<music21.expressions.PedalMark>]
4122-
>>> retList[0].pedalType
4123-
<PedalType.Sostenuto>
41244122
>>> pedalMark = retList[0]
4123+
>>> pedalMark.pedalType
4124+
<PedalType.Sostenuto>
4125+
>>> pedalMark.pedalForm
4126+
<PedalForm.Symbol>
4127+
4128+
>>> mxDirectionType1a = EL('<pedal type="resume" line="yes" number="2"/>')
4129+
>>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType1a, 1, 0.5)
4130+
>>> retList
4131+
[]
4132+
>>> pedalMark.pedalForm
4133+
<PedalForm.SymbolLine>
4134+
4135+
>>> mxDirectionType2 = EL('<pedal type="change" line="yes" number="2"/>')
4136+
>>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType2, 1, 1.0)
4137+
>>> retList
4138+
[]
4139+
4140+
>>> mxDirectionType3 = EL('<pedal type="discontinue" line="yes" number="2"/>')
4141+
>>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType3, 1, 2.0)
4142+
>>> retList
4143+
[]
41254144
4126-
>>> mxDirectionType2 = EL('<pedal type="change" number="2"/>')
4127-
>>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType2, 1, 1.5)
4145+
>>> mxDirectionType4 = EL('<pedal type="resume" line="yes" number="2"/>')
4146+
>>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType4, 1, 3.5)
41284147
>>> retList
41294148
[]
41304149
4131-
>>> mxDirectionType3 = EL('<pedal type="stop" number="2"/>')
4132-
>>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType3)
4150+
>>> mxDirectionType5 = EL('<pedal type="stop" line="yes" number="2"/>')
4151+
>>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType5, 1, 4.0)
41334152
>>> retList
41344153
[]
41354154
>>> pedalMark.getFirst()
4136-
<music21.expressions.PedalBounce at 1.5>
4155+
<music21.expressions.PedalBounce at 1.0>
41374156
>>> pedalMark.getLast() is n1
41384157
True
41394158
>>> MP.stream.elements
4140-
(<music21.expressions.PedalBounce at 1.5>,)
4159+
(<music21.expressions.PedalBounce at 1.0>, <music21.expressions.PedalGapStart at 2.0>,
4160+
<music21.expressions.PedalGapEnd at 3.5>)
41414161
'''
41424162
targetLast = self.nLast
41434163
returnList = []

0 commit comments

Comments
 (0)