def test_mathtools_difference_series_01(): r'''Returns generator of differences l_i+1 - l_i for l_i in list l. ''' t = [1, 1, 2, 3, 5, 5, 6] d = mathtools.difference_series(t) assert list(d) == [0, 1, 1, 2, 0, 1]
def from_selection( cls, selection, item_class=None, custom_identifier=None, ): r'''Makes interval segment from component `selection`. :: >>> staff = Staff("c'8 d'8 e'8 f'8 g'8 a'8 b'8 c''8") >>> pitchtools.IntervalSegment.from_selection( ... staff, item_class=pitchtools.NumberedInterval) IntervalSegment([2, 2, 1, 2, 2, 2, 1]) Returns interval segment. ''' from abjad.tools import pitchtools pitch_segment = pitchtools.PitchSegment.from_selection(selection) intervals = (-x for x in mathtools.difference_series(pitch_segment)) return cls( tokens=intervals, item_class=item_class, custom_identifier=custom_identifier, )
def merge_duration_sequences(*sequences): r'''Merge duration `sequences`: :: >>> sequencetools.merge_duration_sequences([10, 10, 10], [7]) [7, 3, 10, 10] Merge more duration sequences: :: >>> sequencetools.merge_duration_sequences([10, 10, 10], [10, 10]) [10, 10, 10] The idea is that each sequence element represents a duration. Returns list. ''' from abjad.tools import sequencetools offset_lists = [] for sequence in sequences: offset_list = mathtools.cumulative_sums(sequence, start=None) offset_lists.append(offset_list) all_offsets = sequencetools.join_subsequences(offset_lists) all_offsets = list(sorted(set(all_offsets))) all_offsets.insert(0, 0) all_durations = mathtools.difference_series(all_offsets) return all_durations
def _get_composite_offset_difference_series_from_leaves_in_expr(expr): offsets = [] for leaf in iterate(expr).by_class(scoretools.Leaf): start_offset = leaf._get_timespan().start_offset if start_offset not in offsets: offsets.append(start_offset) stop_offset = leaf._get_timespan().stop_offset if stop_offset not in offsets: offsets.append(stop_offset) offsets.sort() return list(mathtools.difference_series(offsets))
def _get_leaf_offsets(expr): from abjad.tools import scoretools offsets = [] for leaf in iterate(expr).by_class(scoretools.Leaf): start_offset = leaf._get_timespan().start_offset if start_offset not in offsets: offsets.append(start_offset) stop_offset = leaf._get_timespan().stop_offset if stop_offset not in offsets: offsets.append(stop_offset) offsets.sort() return list(mathtools.difference_series(offsets))
def _get_composite_offset_difference_series_from_leaves_in_expr(expr): from abjad.tools import scoretools offsets = [] for leaf in iterate(expr).by_class(scoretools.Leaf): start_offset = leaf._get_timespan().start_offset if start_offset not in offsets: offsets.append(start_offset) stop_offset = leaf._get_timespan().stop_offset if stop_offset not in offsets: offsets.append(stop_offset) offsets.sort() return list(mathtools.difference_series(offsets))
def _get_composite_offset_difference_series_from_leaves_in_expr(expr): from abjad.tools import iterationtools offsets = [] for leaf in iterationtools.iterate_leaves_in_expr(expr): start_offset = leaf._get_timespan().start_offset if start_offset not in offsets: offsets.append(start_offset) stop_offset = leaf._get_timespan().stop_offset if stop_offset not in offsets: offsets.append(stop_offset) offsets.sort() return list(mathtools.difference_series(offsets))
def _make_timespans( self, layer=None, music_specifiers=None, target_timespan=None, timespan_inventory=None, ): import consort new_timespans = abjad.TimespanList() if not self.voice_names and not self.labels: return new_timespans rotation_indices = self.rotation_indices or (0,) rotation_indices = abjad.CyclicTuple(rotation_indices) context_counter = collections.Counter() preexisting_timespans = self._collect_preexisting_timespans( target_timespan=target_timespan, timespan_inventory=timespan_inventory, ) partitioned_timespans = self._partition_preexisting_timespans( preexisting_timespans) for group_index, group in enumerate(partitioned_timespans): rotation_index = rotation_indices[group_index] offsets = set() offsets.add(group.start_offset) offsets.add(group.stop_offset) for timespan in group: if self.include_inner_starts: offsets.add(timespan.start_offset) if self.include_inner_stops: offsets.add(timespan.stop_offset) offsets = tuple(sorted(offsets)) durations = mathtools.difference_series(offsets) durations = consort.rotate(durations, rotation_index) start_offset = offsets[0] for context_name, music_specifier in music_specifiers.items(): context_seed = context_counter[context_name] timespans = music_specifier( durations=durations, layer=layer, division_masks=self.division_masks, padding=self.padding, seed=context_seed, start_offset=start_offset, timespan_specifier=self.timespan_specifier, voice_name=context_name, ) context_counter[context_name] += 1 new_timespans.extend(timespans) return new_timespans
def from_selection(class_, selection, item_class=None): r'''Initialize interval-class segment from component selection: :: >>> staff_1 = Staff("c'4 <d' fs' a'>4 b2") >>> staff_2 = Staff("c4. r8 g2") >>> selection = select((staff_1, staff_2)) >>> pitchtools.IntervalClassSegment.from_selection(selection) IntervalClassSegment(['-M2', '-M3', '-m3', '+m7', '+M7', '-P5']) Returns interval-class segment. ''' from abjad.tools import pitchtools pitch_segment = pitchtools.PitchSegment.from_selection(selection) intervals = mathtools.difference_series(pitch_segment) return class_( items=intervals, item_class=item_class, )
def from_selection(cls, selection, item_class=None): r'''Initialize interval-class segment from component selection: :: >>> staff_1 = Staff("c'4 <d' fs' a'>4 b2") >>> staff_2 = Staff("c4. r8 g2") >>> selection = select((staff_1, staff_2)) >>> pitchtools.IntervalClassSegment.from_selection(selection) IntervalClassSegment(['-M2', '-M3', '-m3', '+m7', '+M7', '-P5']) Returns interval-class segment. ''' from abjad.tools import pitchtools pitch_segment = pitchtools.PitchSegment.from_selection(selection) intervals = mathtools.difference_series(pitch_segment) return cls( items=intervals, item_class=item_class, )
def from_selection(cls, selection, item_class=None, name=None): r'''Initialize interval segment from component selection: :: >>> staff = Staff("c'8 d'8 e'8 f'8 g'8 a'8 b'8 c''8") >>> pitchtools.IntervalSegment.from_selection( ... staff, item_class=pitchtools.NumberedInterval) IntervalSegment([+2, +2, +1, +2, +2, +2, +1]) Returns interval segment. ''' from abjad.tools import pitchtools pitch_segment = pitchtools.PitchSegment.from_selection(selection) intervals = (-x for x in mathtools.difference_series(pitch_segment)) return cls( tokens=intervals, item_class=item_class, name=name, )
def from_selection(class_, selection, item_class=None): r'''Initializes interval-class segment from component selection. .. container:: example >>> staff_1 = abjad.Staff("c'4 <d' fs' a'>4 b2") >>> staff_2 = abjad.Staff("c4. r8 g2") >>> selection = abjad.select((staff_1, staff_2)) >>> abjad.IntervalClassSegment.from_selection(selection) IntervalClassSegment(['-M2', '-M3', '-m3', '+m7', '+M7', '-P5']) Returns interval-class segment. ''' import abjad pitch_segment = abjad.PitchSegment.from_selection(selection) pitches = [_ for _ in pitch_segment] intervals = mathtools.difference_series(pitches) return class_( items=intervals, item_class=item_class, )
def from_selection( class_, selection, item_class=None, ): r'''Makes interval segment from component `selection`. :: >>> staff = Staff("c'8 d'8 e'8 f'8 g'8 a'8 b'8 c''8") >>> pitchtools.IntervalSegment.from_selection( ... staff, item_class=pitchtools.NumberedInterval) IntervalSegment([2, 2, 1, 2, 2, 2, 1]) Returns interval segment. ''' from abjad.tools import pitchtools pitch_segment = pitchtools.PitchSegment.from_selection(selection) intervals = (-x for x in mathtools.difference_series(pitch_segment)) return class_( items=intervals, item_class=item_class, )
def _analyze_chord(expr): from abjad.tools import tonalanalysistools pitches = pitchtools.PitchSegment.from_selection(expr) npcset = pitchtools.PitchClassSet( pitches, item_class=pitchtools.NamedPitchClass, ) ordered_npcs = [] letters = ('c', 'e', 'g', 'b', 'd', 'f', 'a') for letter in letters: for npc in npcset: if npc.diatonic_pitch_class_name == letter: ordered_npcs.append(npc) ordered_npcs = pitchtools.PitchClassSegment( ordered_npcs, item_class=pitchtools.NamedPitchClass) for x in range(len(ordered_npcs)): ordered_npcs = ordered_npcs.rotate(1) segment = \ pitchtools.IntervalClassSegment( items=mathtools.difference_series(ordered_npcs), item_class=pitchtools.NamedInversionEquivalentIntervalClass, ) if segment.is_tertian: break else: return None root = ordered_npcs[0] class_ = tonalanalysistools.RootlessChordClass rootless_chord_class = class_.from_interval_class_segment(segment) bass = min(pitches).named_pitch_class inversion = ordered_npcs.index(bass) return tonalanalysistools.RootedChordClass( root, rootless_chord_class.quality_string, rootless_chord_class.extent, inversion, )
def test_quantizationtools_QEventSequence_from_millisecond_durations_01(): r'''Test basic functionality. ''' durations = mathtools.difference_series( [x[0] for x in quantizationtools.make_test_time_segments()]) q_events = quantizationtools.QEventSequence.from_millisecond_durations( durations) assert q_events == quantizationtools.QEventSequence(( quantizationtools.PitchedQEvent( durationtools.Offset(0, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(163, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(511, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(627, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(1208, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(1417, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(1997, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(3204, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(3297, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(4087, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(4296, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(4830, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(6362, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(6595, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(6687, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(7013, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(7245, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(7872, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(8197, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(8359, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(8638, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(8731, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(8917, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(9288, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(9404, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(10240, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(10356, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(10496, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(10890, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(11169, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(11285, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(11424, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(12307, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(12957, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(13073, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(13653, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(13979, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(14234, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(15256, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(15883, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(16022, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(16649, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(16927, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(17044, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.PitchedQEvent( durationtools.Offset(17276, 1), (pitchtools.NamedPitch("c'"),) ), quantizationtools.TerminalQEvent( durationtools.Offset(18483, 1) ) ))