def unwrapVerticalities(verticalities): # noinspection PyShadowingNames r''' Unwraps a sequence of `Verticality` objects into a dictionary of `Part`:`Horizontality` key/value pairs. >>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans(classList=(note.Note,)) >>> iterator = scoreTree.iterateVerticalitiesNwise() >>> verticalities = next(iterator) >>> unwrapped = scoreTree.unwrapVerticalities(verticalities) >>> for part in sorted(unwrapped, key=lambda x: x.partName): ... print(part) ... horizontality = unwrapped[part] ... for timespan in horizontality: ... print('\t%r' % timespan) ... <music21.stream.Part Alto> <PitchedTimespan (0.0 to 1.0) <music21.note.Note E>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note F#>> <music21.stream.Part Bass> <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note G#>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note F#>> <music21.stream.Part Soprano> <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note A>> <music21.stream.Part Tenor> <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note C#>> ''' from music21.tree.verticality import VerticalitySequence sequence = VerticalitySequence(verticalities) unwrapped = sequence.unwrap() return unwrapped
def unwrapVerticalities(verticalities): r''' Unwraps a sequence of `Verticality` objects into a dictionary of `Part`:`Horizontality` key/value pairs. >>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans(classList=(note.Note,)) >>> iterator = scoreTree.iterateVerticalitiesNwise() >>> verticalities = next(iterator) >>> unwrapped = scoreTree.unwrapVerticalities(verticalities) >>> for part in sorted(unwrapped, key=lambda x: x.partName): ... print(part) ... horizontality = unwrapped[part] ... for timespan in horizontality: ... print('\t%r' % timespan) ... <music21.stream.Part Alto> <PitchedTimespan (0.0 to 1.0) <music21.note.Note E>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note F#>> <music21.stream.Part Bass> <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note G#>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note F#>> <music21.stream.Part Soprano> <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note A>> <music21.stream.Part Tenor> <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note C#>> ''' from music21.tree.verticality import VerticalitySequence sequence = VerticalitySequence(verticalities) unwrapped = sequence.unwrap() return unwrapped
def iterateVerticalitiesNwise( self, n=3, reverse=False, ): r''' Iterates verticalities in groups of length `n`. .. note:: The offset-tree can be mutated while its verticalities are iterated over. Each verticality holds a reference back to the offset-tree and will ask for the start-offset after (or before) its own start offset in order to determine the next verticality to yield. If you mutate the tree by adding or deleting timespans, the next verticality will reflect those changes. >>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans(classList=(note.Note,)) >>> iterator = scoreTree.iterateVerticalitiesNwise(n=2) >>> for _ in range(4): ... print(next(iterator)) ... <VerticalitySequence: [ <Verticality 0.0 {A3 E4 C#5}>, <Verticality 0.5 {G#3 B3 E4 B4}> ]> <VerticalitySequence: [ <Verticality 0.5 {G#3 B3 E4 B4}>, <Verticality 1.0 {F#3 C#4 F#4 A4}> ]> <VerticalitySequence: [ <Verticality 1.0 {F#3 C#4 F#4 A4}>, <Verticality 2.0 {G#3 B3 E4 B4}> ]> <VerticalitySequence: [ <Verticality 2.0 {G#3 B3 E4 B4}>, <Verticality 3.0 {A3 E4 C#5}> ]> Grouped verticalities can also be iterated in reverse: >>> iterator = scoreTree.iterateVerticalitiesNwise(n=2, reverse=True) >>> for _ in range(4): ... print(next(iterator)) ... <VerticalitySequence: [ <Verticality 34.5 {B2 B3 D4 E#4}>, <Verticality 35.0 {F#3 A#3 C#4 F#4}> ]> <VerticalitySequence: [ <Verticality 34.0 {B2 B3 D4 F#4}>, <Verticality 34.5 {B2 B3 D4 E#4}> ]> <VerticalitySequence: [ <Verticality 33.5 {D3 B3 C#4 F#4}>, <Verticality 34.0 {B2 B3 D4 F#4}> ]> <VerticalitySequence: [ <Verticality 33.0 {D3 B3 F#4}>, <Verticality 33.5 {D3 B3 C#4 F#4}> ]> ''' from music21.tree.verticality import VerticalitySequence n = int(n) if (n <= 0): message = "The number of verticalities in the group must be at " message += "least one. Got {}".format(n) raise TimespanTreeException(message) if reverse: for v in self.iterateVerticalities(reverse=True): verticalities = [v] while len(verticalities) < n: nextVerticality = verticalities[-1].nextVerticality if nextVerticality is None: break verticalities.append(nextVerticality) if len(verticalities) == n: yield VerticalitySequence(verticalities) else: for v in self.iterateVerticalities(): verticalities = [v] while len(verticalities) < n: previousVerticality = verticalities[-1].previousVerticality if previousVerticality is None: break verticalities.append(previousVerticality) if len(verticalities) == n: yield VerticalitySequence(reversed(verticalities))