Skip to content

Commit 1784fb7

Browse files
makeRests now makes rests that can be exported to MusicXML (non-complex duration).
1 parent 0ea1e00 commit 1784fb7

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

music21/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
'''
5151
from __future__ import annotations
5252

53-
__version__ = '9.7.1'
53+
__version__ = '9.7.2a6'
5454

5555
def get_version_tuple(vv):
5656
v = vv.split('.')

music21/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<class 'music21.base.Music21Object'>
2828
2929
>>> music21.VERSION_STR
30-
'9.7.1'
30+
'9.7.2a6'
3131
3232
Alternatively, after doing a complete import, these classes are available
3333
under the module "base":

music21/stream/makeNotation.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -761,14 +761,17 @@ def makeRests(
761761
762762
>>> b = a.makeRests(inPlace=False)
763763
>>> len(b)
764-
2
764+
3
765765
>>> b.lowestOffset
766766
0.0
767767
>>> b.show('text')
768-
{0.0} <music21.note.Rest 20ql>
768+
{0.0} <music21.note.Rest 16ql>
769+
{16.0} <music21.note.Rest whole>
769770
{20.0} <music21.note.Note C>
770771
>>> b[0].duration.quarterLength
771-
20.0
772+
16.0
773+
>>> b[1].duration.quarterLength
774+
4.0
772775
773776
Same thing, but this time, with gaps, and hidden rests:
774777
@@ -784,13 +787,15 @@ def makeRests(
784787
{30.0} <music21.note.Note D>
785788
>>> b = a.makeRests(fillGaps=True, inPlace=False, hideRests=True)
786789
>>> len(b)
787-
4
790+
6
788791
>>> b.lowestOffset
789792
0.0
790793
>>> b.show('text')
791-
{0.0} <music21.note.Rest 20ql>
794+
{0.0} <music21.note.Rest 16ql>
795+
{16.0} <music21.note.Rest whole>
792796
{20.0} <music21.note.Note C>
793-
{21.0} <music21.note.Rest 9ql>
797+
{21.0} <music21.note.Rest breve>
798+
{29.0} <music21.note.Rest quarter>
794799
{30.0} <music21.note.Note D>
795800
>>> b[0].style.hideObjectOnPrint
796801
True
@@ -949,18 +954,26 @@ def oHighTargetForMeasure(
949954
r = note.Rest()
950955
r.duration.quarterLength = qLen
951956
r.style.hideObjectOnPrint = hideRests
957+
rList = r.splitAtDurations()
952958
# environLocal.printDebug(['makeRests(): add rests', r, r.duration])
953959
# place at oLowTarget to reach to oLow
954-
component.insert(oLowTarget, r)
960+
off: OffsetQL = oLowTarget
961+
for r in rList:
962+
component.insert(off, r)
963+
off = opFrac(off + r.quarterLength)
955964

956965
# create rest from end to highest
957966
qLen = oHighTarget - oHigh
958967
if qLen > 0:
959968
r = note.Rest()
960969
r.duration.quarterLength = qLen
961970
r.style.hideObjectOnPrint = hideRests
971+
rList = r.splitAtDurations()
962972
# place at oHigh to reach to oHighTarget
963-
component.insert(oHigh, r)
973+
off = oHigh
974+
for r in rList:
975+
component.insert(off, r)
976+
off = opFrac(off + r.quarterLength)
964977

965978
if fillGaps:
966979
gapStream = component.findGaps()
@@ -969,7 +982,11 @@ def oHighTargetForMeasure(
969982
r = note.Rest()
970983
r.duration.quarterLength = e.duration.quarterLength
971984
r.style.hideObjectOnPrint = hideRests
972-
component.insert(e.offset, r)
985+
rList = r.splitAtDurations()
986+
off = e.offset
987+
for r in rList:
988+
component.insert(off, r)
989+
off = opFrac(off + r.quarterLength)
973990

974991
if returnObj.hasMeasures():
975992
# split rests at measure boundaries

music21/stream/tests.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,7 +2143,15 @@ def testContextNestedD(self):
21432143
def testMakeRestsA(self):
21442144
a = ['c', 'g#', 'd-', 'f#', 'e', 'f'] * 4
21452145
partOffsetShift = 1.25
2146-
partOffset = 2 # start at non zero
2146+
partOffset = 2. # start at non zero
2147+
partOffsetToNumRests = {
2148+
2.: 1, # half rest
2149+
3.25: 3, # half rest, quarter rest, 16th rest
2150+
4.5: 2, # whole rest, eighth rest
2151+
5.75: 2, # whole rest, double-dotted quarter rest
2152+
7.0: 1, # double dotted whole rest
2153+
8.25: 2, # breve rest, 16th rest
2154+
}
21472155
for unused_part in range(6):
21482156
p = Stream()
21492157
for pitchName in a:
@@ -2162,12 +2170,11 @@ def testMakeRestsA(self):
21622170
# environLocal.printDebug(['first element', p[0], p[0].duration])
21632171
# by default, initial rest should be made
21642172
sub = p.getElementsByClass(note.Rest).stream()
2165-
self.assertEqual(len(sub), 1)
2166-
2173+
self.assertEqual(len(sub), partOffsetToNumRests[partOffset])
21672174
self.assertEqual(sub.duration.quarterLength, partOffset)
21682175

2169-
# first element should have offset of first dur
2170-
self.assertEqual(p[1].offset, sub.duration.quarterLength)
2176+
# first element after rests should have offset of first dur
2177+
self.assertEqual(p[len(sub)].offset, sub.duration.quarterLength)
21712178

21722179
partOffset += partOffsetShift
21732180

@@ -5860,16 +5867,21 @@ def testVoicesC(self):
58605867
sPost = s.makeRests(fillGaps=True, inPlace=False)
58615868
self.assertEqual(str(list(sPost.voices[0].notesAndRests)),
58625869
'[<music21.note.Rest half>, <music21.note.Note C>, '
5863-
+ '<music21.note.Rest 2.25ql>, '
5870+
+ '<music21.note.Rest half>, '
5871+
+ '<music21.note.Rest 16th>, '
58645872
+ '<music21.note.Note C>, '
5865-
+ '<music21.note.Rest 2.5ql>, '
5873+
+ '<music21.note.Rest half>, '
5874+
+ '<music21.note.Rest eighth>, '
58665875
+ '<music21.note.Note C>, '
5867-
+ '<music21.note.Rest 4.25ql>, '
5876+
+ '<music21.note.Rest whole>, '
5877+
+ '<music21.note.Rest 16th>, '
58685878
+ '<music21.note.Note C>, '
58695879
+ '<music21.note.Rest half>]')
58705880
self.assertEqual(str(list(sPost.voices[1].notesAndRests)),
58715881
'[<music21.note.Rest 16th>, <music21.note.Note C>, '
5872-
+ '<music21.note.Rest 3.25ql>, '
5882+
+ '<music21.note.Rest half>, '
5883+
+ '<music21.note.Rest quarter>, '
5884+
+ '<music21.note.Rest 16th>, '
58735885
+ '<music21.note.Note C>, '
58745886
+ '<music21.note.Rest dotted-quarter>, <music21.note.Note C>, '
58755887
+ '<music21.note.Rest breve>, <music21.note.Note C>]')

0 commit comments

Comments
 (0)