'Populate self with a melody from the given quantized NoteSequence.
A monophonic melody is extracted from the given `instrument` starting at
`search_start_step`. `instrument` and `search_start_step` can be used to
drive extraction of multiple melodies from the same quantized sequence. The
end step of the extracted melody will be stored in `self._end_step`.
0 velocity notes are ignored. The melody extraction is ended when there are
no held notes for a time stretch of `gap_bars` in bars (measures) of music.
The number of time steps per bar is computed from the time signature in
`quantized_sequence`.
`ignore_polyphonic_notes` determines what happens when polyphonic (multiple
notes start at the same time) data is encountered. If
`ignore_polyphonic_notes` is true, the highest pitch is used in the melody
when multiple notes start at the same time. If false, an exception is
raised.
Args:
quantized_sequence: A NoteSequence quantized with
sequences_lib.quantize_note_sequence.
search_start_step: Start searching for a melody at this time step. Assumed
to be the first step of a bar.
instrument: Search for a melody in this instrument number.
gap_bars: If this many bars or more follow a NOTE_OFF event, the melody
is ended.
ignore_polyphonic_notes: If True, the highest pitch is used in the melody
when multiple notes start at the same time. If False,
PolyphonicMelodyException will be raised if multiple notes start at
the same time.
pad_end: If True, the end of the melody will be padded with NO_EVENTs so
that it will end at a bar boundary.
filter_drums: If True, notes for which `is_drum` is True will be ignored.
Raises:
NonIntegerStepsPerBarException: If `quantized_sequence`\'s bar length
(derived from its time signature) is not an integer number of time
steps.
PolyphonicMelodyException: If any of the notes start on the same step
and `ignore_polyphonic_notes` is False.'

def from_quantized_sequence(self, quantized_sequence, search_start_step=0, instrument=0, gap_bars=1, ignore_polyphonic_notes=False, pad_end=False, filter_drums=True):

    sequences_lib.assert_is_relative_quantized_sequence(quantized_sequence)
    self._reset()
    steps_per_bar_float = sequences_lib.steps_per_bar_in_quantized_sequence(quantized_sequence)
    if ((steps_per_bar_float % 1) != 0):
        raise events_lib.NonIntegerStepsPerBarException(('There    are    %f    timesteps    per    bar.    Time    signature:    %d/%d' % (steps_per_bar_float, quantized_sequence.time_signatures[0].numerator, quantized_sequence.time_signatures[0].denominator)))
    self._steps_per_bar = steps_per_bar = int(steps_per_bar_float)
    self._steps_per_quarter = quantized_sequence.quantization_info.steps_per_quarter
    notes = sorted([n for n in quantized_sequence.notes if ((n.instrument == instrument) and (n.quantized_start_step >= search_start_step))], key=(lambda note: (note.quantized_start_step, (- note.pitch))))
    if (not notes):
        return
    melody_start_step = (notes[0].quantized_start_step - ((notes[0].quantized_start_step - search_start_step) % steps_per_bar))
    for note in notes:
        if (filter_drums and note.is_drum):
            continue
        if (not note.velocity):
            continue
        start_index = (note.quantized_start_step - melody_start_step)
        end_index = (note.quantized_end_step - melody_start_step)
        if (not self._events):
            self._add_note(note.pitch, start_index, end_index)
            continue
        (last_on, last_off) = self._get_last_on_off_events()
        on_distance = (start_index - last_on)
        off_distance = (start_index - last_off)
        if (on_distance == 0):
            if ignore_polyphonic_notes:
                continue
            else:
                self._reset()
                raise PolyphonicMelodyException()
        elif (on_distance < 0):
            raise PolyphonicMelodyException('Unexpected    note.    Not    in    ascending    order.')
        if (len(self) and (off_distance >= (gap_bars * steps_per_bar))):
            break
        self._add_note(note.pitch, start_index, end_index)
    if (not self._events):
        return
    self._start_step = melody_start_step
    if (self._events[(-1)] == MELODY_NOTE_OFF):
        del self._events[(-1)]
    length = len(self)
    if pad_end:
        length += ((- len(self)) % steps_per_bar)
    self.set_length(length)
