Quantization
============

Quantization is the process of constraining one set of values to another set of discrete values. In
music, this typically means the transformation of performed rhythms into notated ones, and the
accompanying reduction of imprecision.  More generally, we can define rhythmic quantization as the
transformation of one sequence of rhythms into another, converting tempi, time signatures, grace notes
and even rhythmic complexity in the process.

<abjad>
from abjad import *
from experimental import quantizationtools
</abjad>

<abjad>
quantizer = quantizationtools.Quantizer()
</abjad>

<abjad>
input_string = "abj: | 4/4 c'16 d' e' f' g' a' b' c'' ~ "
input_string += "c'' b' a' g' f' e' d' c' |"
original_staff = Staff(input_string)
tempo = indicatortools.Tempo((1, 4), 60)
attach(tempo, original_staff[0])
q_event_sequence = quantizationtools.QEventSequence.from_tempo_scaled_leaves(
    original_staff.select_leaves(),
    tempo,
    )
</abjad>

<abjad>
triplets =    quantizationtools.UnweightedSearchTree({3: None})
quintuplets = quantizationtools.UnweightedSearchTree({5: None})
septuplets =  quantizationtools.UnweightedSearchTree({7: None})
search_trees = [triplets, quintuplets, septuplets]
</abjad>

<abjad>
staves = [original_staff]
for search_tree in search_trees:
    q_schema = quantizationtools.MeasurewiseQSchema(search_tree=search_tree)
    result = quantizer(q_event_sequence, q_schema)
    staves.append(scoretools.Staff([result]))
</abjad>

<abjad>
q_schema = quantizationtools.MeasurewiseQSchema(
    search_tree=quantizationtools.UnweightedSearchTree({11: None}),
    use_full_measure=True)
result = quantizer(q_event_sequence, q_schema)
staves.append(scoretools.Staff([result]))
</abjad>

<abjad>
staff_group = scoretools.StaffGroup(staves)
score = scoretools.Score([staff_group])
show(score)
</abjad>

Abjad's `quantizationtools` provides a powerful, generalized and customizable collection of tools for
musical quantization.  Its model is based in large part on Paul Nauert's **"A theory of complexity to
constrain the approximation of arbitrary sequences of timepoints"**.
