def quantize_duration(dur):
    """Quantize the duration to the nearest 500 milliseconds.

    This function was adapted from Sven Marnach's answer to this question:
       http://stackoverflow.com/questions/9810391/round-to-the-nearest-500-python

    Arguments:
      dur (int)

    Returns:
      An int
    """
    if (dur <= 500):
        return 500
    elif (dur >= 2000):
        return 2000
    else:
        return int(round(dur / 500.0) * 500.0)