'Adjusts the energy threshold dynamically using audio from ``source`` (an ``AudioSource`` instance) to account for ambient noise.
Intended to calibrate the energy threshold with the ambient energy level. Should be used on periods of audio without speech - will stop early if any speech is detected.
The ``duration`` parameter is the maximum number of seconds that it will dynamically adjust the threshold for before returning. This value should be at least 0.5 in order to get a representative sample of the ambient noise.'

def adjust_for_ambient_noise(self, source, duration=1):

    assert isinstance(source, AudioSource), 'Source    must    be    an    audio    source'
    assert (source.stream is not None), 'Audio    source    must    be    entered    before    adjusting,    see    documentation    for    ``AudioSource``;    are    you    using    ``source``    outside    of    a    ``with``    statement?'
    assert (self.pause_threshold >= self.non_speaking_duration >= 0)
    seconds_per_buffer = ((source.CHUNK + 0.0) / source.SAMPLE_RATE)
    elapsed_time = 0
    while True:
        elapsed_time += seconds_per_buffer
        if (elapsed_time > duration):
            break
        buffer = source.stream.read(source.CHUNK)
        energy = audioop.rms(buffer, source.SAMPLE_WIDTH)
        damping = (self.dynamic_energy_adjustment_damping ** seconds_per_buffer)
        target_energy = (energy * self.dynamic_energy_ratio)
        self.energy_threshold = ((self.energy_threshold * damping) + (target_energy * (1 - damping)))
