'Detects when the composition takes a musical leap, and if it is resolved.
When the composition jumps up or down by an interval of a fifth or more,
it is a \'leap\'. The model then remembers that is has a \'leap direction\'. The
function detects if it then takes another leap in the same direction, if it
leaps back, or if it gradually resolves the leap.
Args:
action: One-hot encoding of the chosen action.
steps_between_leaps: Leaping back immediately does not constitute a
satisfactory resolution of a leap. Therefore the composition must wait
\'steps_between_leaps\' beats before leaping back.
Returns:
0 if there is no leap, \'LEAP_RESOLVED\' if an existing leap has been
resolved, \'LEAP_DOUBLED\' if 2 leaps in the same direction were made.'

def detect_leap_up_back(self, action, steps_between_leaps=6):

    if (not self.composition):
        return 0
    outcome = 0
    (interval, action_note, prev_note) = self.detect_sequential_interval(action)
    if ((action_note == NOTE_OFF) or (action_note == NO_EVENT)):
        self.steps_since_last_leap += 1
        tf.logging.debug('Rest,    adding    to    steps    since    last    leap.    It    isnow:    %s', self.steps_since_last_leap)
        return 0
    if ((interval >= rl_tuner_ops.FIFTH) or (interval == rl_tuner_ops.IN_KEY_FIFTH)):
        if (action_note > prev_note):
            leap_direction = rl_tuner_ops.ASCENDING
            tf.logging.debug('Detected    an    ascending    leap')
        else:
            leap_direction = rl_tuner_ops.DESCENDING
            tf.logging.debug('Detected    a    descending    leap')
        if (self.composition_direction != 0):
            if (self.composition_direction != leap_direction):
                tf.logging.debug('Detected    a    resolved    leap')
                tf.logging.debug('Num    steps    since    last    leap:    %s', self.steps_since_last_leap)
                if (self.steps_since_last_leap > steps_between_leaps):
                    outcome = rl_tuner_ops.LEAP_RESOLVED
                    tf.logging.debug('Sufficient    steps    before    leap    resolved,    awarding    bonus')
                self.composition_direction = 0
                self.leapt_from = None
            else:
                tf.logging.debug('Detected    a    double    leap')
                outcome = rl_tuner_ops.LEAP_DOUBLED
        else:
            tf.logging.debug('There    was    no    previous    leap    direction')
            self.composition_direction = leap_direction
            self.leapt_from = prev_note
        self.steps_since_last_leap = 0
    else:
        self.steps_since_last_leap += 1
        tf.logging.debug('No    leap,    adding    to    steps    since    last    leap.    It    is    now:    %s', self.steps_since_last_leap)
        if (((self.composition_direction == rl_tuner_ops.ASCENDING) and (action_note <= self.leapt_from)) or ((self.composition_direction == rl_tuner_ops.DESCENDING) and (action_note >= self.leapt_from))):
            tf.logging.debug('detected    a    gradually    resolved    leap')
            outcome = rl_tuner_ops.LEAP_RESOLVED
            self.composition_direction = 0
            self.leapt_from = None
    return outcome
