def sleep():
    """Sleeps for a dynamic duration of time as determined by set_rate() and true().
    
    :raises: BeatError: if this function is called before calling set_rate() or \
            before calling true()
    """
    if loop_duration == 0:
        raise BeatError("call beat.set_rate() before calling sleep")
    if loop_start_time == None:
        raise BeatError("call beat.true() before calling sleep")
    td = datetime.datetime.now() - loop_start_time
    duration_to_sleep = loop_duration - td.total_seconds()
    if duration_to_sleep < 0:
        raise BeatError("skipping sleep. Too much work!")
    time.sleep(duration_to_sleep)