def make_mixed_segmented_fields_score():

    score = Score([])

    staff = Staff([])
    staff.accidental.style = 'forget'

    score.append(staff)

    duration = Fraction(1, 16)
    for field in manifolds.etc.pitch.mixed_segmented_fields:
        for segment in field:
            measure = AnonymousMeasure([])
            measure.denominator = 16
            tuplet = FixedDurationTuplet((3, 8), [])
            measure.append(tuplet)
            if min(segment) <= -1:
                measure.clef.forced = Clef('bass')
            else:
                measure.clef.forced = Clef('treble')
            notes = [Note(x, duration) for x in segment]
            tuplet.extend(notes)
            if 31 < max(segment):
                spanner = Octavation(tuplet)
                spanner.start = 1
            measure.insert(0, Rest((1, 16)))
            measure.append(Rest((1, 16)))
            staff.append(measure)
            measure.formatter.number.self = 'comment'
        measure.barline.kind = '||'
        measure.breaks.line = True

    measure.barline.kind = '|.'
    score.rest.transparent = True
    score.meter.break_visibility = Function('end-of-line-invisible')
    score.clef.break_visibility = Function('end-of-line-invisible')
    pitchtools.make_sharp(score)

    score.tupletnumber.stencil = False
    score.tupletbracket.stencil = False

    #indicator = FixedSystemsIndicator(
    #    (0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200), 1)
    #layout.apply_fixed_systems_indicator(staff, indicator)

    staves = StaffAlignmentOffsets(0)
    systems = SystemYOffsets(20, 10, 1)
    positioning = FixedStaffPositioning(systems, staves)
    layout.apply_fixed_staff_positioning(staff, positioning)

    return score
def make_two_staff_unordered_cells_score():

    cells = manifolds.etc.pitch.unordered_cells_sequence
    directions = manifolds.etc.pitch.segmentation_series
    harmonic_walk = sequencetools.flatten_sequence(manifolds.etc.pitch.harmonic_walk)

    score = Score([])
    piano_staff = PianoStaff([])
    score.append(piano_staff)

    treble = Staff([])
    treble.accidental.style = "forget"
    treble.clef.forced = Clef("treble")

    bass = Staff([])
    bass.accidental.style = "forget"
    bass.clef.forced = Clef("bass")

    piano_staff.append(treble)
    piano_staff.append(bass)

    duration = Fraction(1, 16)
    for cell_number, cell in enumerate(cells):
        measure = AnonymousMeasure([])
        notes = [Note(pitch, duration) for pitch in cell]
        measure.extend(notes)

        rest_measure = clone.unspan([measure])[0]
        for i, note in enumerate(rest_measure):
            rest_measure[i] = Rest(note.duration.written)

        pitches = [note.pitch for note in notes]
        clef = pitchtools.suggest_clef(pitches)

        if clef == Clef("treble"):
            treble.append(measure)
            bass.append(rest_measure)
        else:
            treble.append(rest_measure)
            bass.append(measure)

        if 26 < max(cell):
            octavation = Octavation(measure[:])
            octavation.start = 1

        if directions[cell_number % 16] == -1:
            direction = "L"
        else:
            direction = "R"

        field_number = (cell_number + 6) % 8 + 1
        center_pitch = harmonic_walk[cell_number % len(harmonic_walk)]

        chord = Chord(measure[0])
        notehead = NoteHead(None, center_pitch)
        notehead.style = "harmonic"
        notehead.color = "blue"
        chord.append(notehead)

        first_leaf = treble[-1].leaves[0]
        first_leaf.markup.up.append(str(cell_number))

        label = r"\line { %s%s \with-color #blue (%s) }" % (direction, field_number, center_pitch)
        first_leaf.markup.up.append(label)

    front, back = Fraction(1, 16), Fraction(1, 32)
    layout.insert_measure_padding(score, front, back)

    lengths = manifolds.etc.pitch.harmonic_walk_lengths
    parts = sequencetools.partition_by_lengths(treble[:], lengths, cyclic=True)

    for part in parts:
        if 1 < len(part):
            bracket = Bracket(part)
            bracket.staff_padding = None

    parts = sequencetools.partition_by_lengths(bass[:], lengths, cyclic=True)
    for part_number, part in enumerate(parts):
        label = r"\with-color #blue \circle{ %s }" % (part_number + 1)
        part[0][1].markup.down.append(label)

    layout.line_break_every_prolated(treble, Fraction(58, 16))
    layout.line_break_every_prolated(bass, Fraction(58, 16))

    staves = StaffAlignmentOffsets(0, -10)
    systems = SystemYOffsets(45, 5)
    positioning = FixedStaffPositioning(systems, staves)
    layout.apply_fixed_staff_positioning(treble, positioning)

    score.rest.transparent = True

    return score
def make_cells_with_prominences_score():

    from manifolds.etc.pitch import prominent_section_numbers
    from manifolds.etc.pitch import sequence_with_prominences as sections
    from manifolds.etc.pitch._helpers import field_id_to_segmented_field
    from manifolds.etc.pitch._helpers import section_index_to_polar_value

    assert len(sections) == 46

    score, treble, bass = make_piano_staff()
    prominence_lengths = _prominence_lengths()
    duration = Fraction(1, 16)

    output_cells = []
    cell_index_in_score = 0

    ## enumerate forty-six separate sections
    for section_index, section in enumerate(sections):

        cur_polar_value = section_index_to_polar_value(section_index)
        next_polar_value = section_index_to_polar_value(section_index + 1)
        prominence_length = prominence_lengths[section_index]
        first_prominent_triple = len(section) - prominence_length - 1
        cell_number_in_section = 1
        first_field_id_in_prominence = None
        second_field_id_in_prominence = None

        for triple_index, triple in enumerate(section):

            section_number = section_index + 1
            field_id, middle, cell = triple
            segmented_field = field_id_to_segmented_field(field_id)
            cell_number_in_field = segmented_field.index(cell) + 1

            if 0 < prominence_length and first_prominent_triple <= triple_index:
                if cell_number_in_prominence is None:
                    cell_number_in_prominence = 1
                    first_field_id_in_prominence = field_id
                else:
                    cell_number_in_prominence += 1
                    if not field_id == first_field_id_in_prominence:
                        second_field_id_in_prominence = field_id
            else:
                cell_number_in_prominence = None

            measure = AnonymousMeasure([])
            notes = [Note(pitch, duration) for pitch in cell]
            measure.extend(notes)

            if triple_index + 1 == len(section):
                measure.barline.kind = '||'

            if cell_number_in_prominence is not None:
                measure.notehead.style = 'harmonic'
                for leaf in measure.leaves:
                    leaf.notehead.color = 'red'

            rest_measure = clone.unspan([measure])[0]
            for i, note in enumerate(rest_measure):
                rest_measure[i] = Rest(note.duration.written)

            pitches = [note.pitch for note in notes]
            cell_clef = pitchtools.suggest_clef(pitches)

            if cell_clef == Clef('treble'):
                treble.append(measure)
                bass.append(rest_measure)
            else:
                treble.append(rest_measure)
                bass.append(measure)

            if 26 < max(cell):
                octavation = Octavation(measure[:])
                octavation.start = 1

            notated_polar_value = cur_polar_value
            if cell_number_in_prominence is not None:
                if field_id == second_field_id_in_prominence:
                    notated_polar_value = next_polar_value

            polar_clef = pitchtools.suggest_clef([Pitch(notated_polar_value)])

            if polar_clef == cell_clef:
                chord = Chord(measure[0])
                notehead = NoteHead(None, notated_polar_value)
                chord.append(notehead)
            else:
                note = Note(rest_measure[0])
                note.pitch = notated_polar_value
                notehead = note.notehead

            notehead.style = 'harmonic'
            notehead.color = 'blue'

            first_leaf = bass[-1].leaves[0]

            if cell_number_in_prominence is not None:
                first_markup = r'\small \line { %s / %s-%s-%s }' % (
                cell_index_in_score, section_number,
                first_prominent_triple + 1, cell_number_in_prominence)
            else:
                first_markup = r'\small \line { %s / %s-%s }' % (
                cell_index_in_score, section_number, cell_number_in_section)
            first_leaf.markup.down.append(first_markup)

            second_markup = r'\small \line { %s-%s \with-color #blue (%s) }' % (
                field_id, cell_number_in_field, notated_polar_value)
            first_leaf.markup.down.append(second_markup)

            ## make output to write to disk
            output_cell = (
                cell_index_in_score,
                field_id,
                cell_number_in_field,
                cell,
                section_number,
                cell_number_in_section,
                cell_number_in_prominence,
                notated_polar_value)

            output_cells.append(output_cell)

            cell_number_in_section += 1
            cell_index_in_score += 1

    lower_bass_markup_blocks(bass)

    front, back = Fraction(1, 16), Fraction(1, 32)
    layout.insert_measure_padding(score, front, back)

    section_lengths = [len(section) for section in sections]
    label_sections_in_treble_in_blue(treble, section_lengths)

    treble.text.X_offset = 'center'
    treble.text.self_alignment_X = 'center'
    bass.text.staff_padding = 4

    layout.line_break_every_prolated(treble, Fraction(54, 16))
    layout.line_break_every_prolated(bass, Fraction(54, 16))

    staves = StaffAlignmentOffsets(0, -10)
    systems = SystemYOffsets(45, 5)
    positioning = FixedStaffPositioning(systems, staves)
    layout.apply_fixed_staff_positioning(treble, positioning)

    score.rest.transparent = True
    score.formatter.number.measures = 'comment'

    return score, output_cells
def make_labeled_mixed_fields_score():

    score = Score([])
    segmentation_series = manifolds.etc.pitch.segmentation_series
    fields = manifolds.etc.pitch.mixed_segmented_fields
    duration = Fraction(1, 16)

    measures_per_line = 6

    ## for eight *pairs of* fields
    for i in range(len(fields) / 2):

        ## for the first or second member of each pair
        for member in ('first', 'second'):

            ## find a sequence between 0 and 15, inclusive
            if member == 'first':
                field_sequence = i
            else:
                field_sequence = i + 8

            ## find a field number between 1 and 8, inclusive
            field_number = (field_sequence + 6) % 8 + 1

            ## find direction of segmentation
            if segmentation_series[field_sequence] == -1:
                direction = 'L'
            elif segmentation_series[field_sequence] == 1:
                direction = 'R'
            else:
                raise ValueError

            ## find name for first system
            name = Markup(
                r'\hcenter-in #12 \box \pad-markup #1 \bold { %s%s }' % (
                direction, field_number))

            ## find name for second system
            if direction == 'L':
                second_system_direction = 'R'
            else:
                second_system_direction = 'L'
            short_name = Markup(
                r'\hcenter-in #12 \box \pad-markup #1 \bold { %s%s }' % (
                second_system_direction, field_number))

            ## if first field in pair
            if member == 'first':

                ## set up a staff
                staff = Staff([])
                staff.accidental.style = 'forget'
                staff.instrument.name = name
                staff.instrument.short_name = short_name
                score.append(staff)

            ## get field to lay out, from 0 to 15, inclusive
            field = fields[field_sequence]

            ## lay out field as a single line of music
            for segment_number, segment in enumerate(field):
                measure = AnonymousMeasure([])
                measure.denominator = 16

#            if segment_number == 0:
#               if member == 'second':
#                  print name
#                  directive = r'\set Staff.shortInstrumentName = %s'
#                  directive %= name.format
#                  measure.directives.before.append(directive)

                tuplet = FixedDurationTuplet((3, 8), [])
                measure.append(tuplet)
                if min(segment) <= -1:
                    measure.clef.forced = Clef('bass')
                else:
                    measure.clef.forced = Clef('treble')
                notes = [Note(x, duration) for x in segment]
                tuplet.extend(notes)
                if 31 < max(segment) < 36:
                    spanner = Octavation(tuplet)
                    spanner.start = 1
                elif 36 <= max(segment):
                    spanner = Octavation(tuplet)
                    spanner.start = 2
                measure.insert(0, Rest((1, 16)))
                measure.append(Rest((1, 16)))
                staff.append(measure)

#            segment_letter = chr(ord('a') + segment_number)
#            segment_id = r'\circle \pad-markup #0.25 { %s }'
#            segment_id %= segment_letter
#            tuplet.leaves[0].markup.up.append(segment_id)

                segment_center = float(sum(segment)) / len(segment)
                segment_center = round(segment_center, 1)
                if int(segment_center) == segment_center:
                    markup = r'\bold { %s }' % int(segment_center)
                else:
                    markup = r'\bold { %.1f }' % segment_center
                tuplet.leaves[0].markup.down.append(markup)

            extra_measure_count = measures_per_line - (segment_number + 1)
            for j in range(extra_measure_count):
                measure = AnonymousMeasure([])
                measure.extend(construct.rests([(1, 16), (3, 8), (1, 16)]))
                measure.dots.transparent = True
                staff.append(measure)
            measure.barline.kind = '||'
            measure.breaks.line = True

    measure.barline.kind = '|.'
    score.rest.transparent = True
    score.meter.break_visibility = Function('end-of-line-invisible')
    #score.clef.break_visibility = Function('end-of-line-invisible')
    pitchtools.make_sharp(score)

    for i, measure in enumerate(iterate.naive(score, AnonymousMeasure)):
        measure.formatter.number.self = 'comment'
        if not i % 6 == 5:
            measure.breaks.whitespace = Fraction(1, 16)

    score.tupletnumber.stencil = False
    score.tupletbracket.stencil = False

    score[0][measures_per_line - 1].breaks.page = True

    alignment_offsets = [-25 * x for x in range(8)]

    score[0][0].breaks.x = 10
    score[0][0].breaks.y = 30
    score[0][0].breaks.alignment_offsets = alignment_offsets

    score[0][measures_per_line].breaks.x = 10
    score[0][measures_per_line].breaks.y = 15
    score[0][measures_per_line].breaks.alignment_offsets = alignment_offsets

    return score
def make_unordered_cells_score():

    cells = manifolds.etc.pitch.unordered_cells_sequence
    directions = manifolds.etc.pitch.segmentation_series
    harmonic_walk = sequencetools.flatten_sequence(manifolds.etc.pitch.harmonic_walk)

    score = Score([])
    staff = Staff([])
    staff.accidental.style = 'forget'
    score.append(staff)

    duration = Fraction(1, 16)
    for cell_number, cell in enumerate(cells):
        measure = AnonymousMeasure([])
        notes = [Note(pitch, duration) for pitch in cell]
        measure.extend(notes)
        pitches = [note.pitch for note in notes]
        clef = pitchtools.suggest_clef(pitches)
        measure.clef.forced = clef

        if 26 < max(cell):
            octavation = Octavation(measure[:])
            octavation.start = 1

        measure.formatter.number.self = 'comment'
        staff.append(measure)

        if directions[cell_number % 16] == -1:
            direction = 'L'
        else:
            direction = 'R'

        field_number = (cell_number + 6) % 8 + 1
        center_pitch = harmonic_walk[cell_number % len(harmonic_walk)]

        chord = Chord(measure[0])
        notehead = NoteHead(None, center_pitch)
        notehead.style = 'harmonic'
        notehead.color = 'blue'
        chord.append(notehead)

        first_leaf = measure.leaves[0]
        first_leaf.markup.up.append(str(cell_number))

        label = r'\line { %s%s \with-color #blue (%s) }' % (
            direction, field_number, center_pitch)
        first_leaf.markup.up.append(label)

        front, back = Fraction(1, 16), Fraction(1, 32)
        layout.insert_measure_padding(measure, front, back)

    lengths = manifolds.etc.pitch.harmonic_walk_lengths
    parts = sequencetools.partition_by_lengths(staff[:], lengths, cyclic = True)
    spacing_voice = Voice([])

    for part_number, part in enumerate(parts):
        if 1 < len(part):
            bracket = Bracket(part)
            bracket.staff_padding = None

        skip = Skip((1, 16))
        spacing_voice.append(skip)

        skip = Skip((1, 1))
        skip.duration.multiplier = durationtools.sum_prolated(part) - Fraction(1, 16)
        spacing_voice.append(skip)

        label = r'\with-color #blue \circle { %s }' % (part_number + 1)
        skip.markup.down.append(label)

    spacing_voice.text.staff_padding = 4

    Voice(staff[:])
    staff.parallel = True
    staff.append(spacing_voice)

    lines = Fraction(58, 16)
    systems = SystemYOffsets(33, 7, 1)
    positioning = FixedStaffPositioning(systems)
    layout.line_break_every_prolated(staff, lines)
    layout.apply_fixed_staff_positioning(staff, positioning)

    score.rest.transparent = True

    return score