'Records a single phrase from ``source`` (an ``AudioSource`` instance) into an ``AudioData`` instance, which it returns.
This is done by waiting until the audio has an energy above ``recognizer_instance.energy_threshold`` (the user has started speaking), and then recording until it encounters ``recognizer_instance.pause_threshold`` seconds of non-speaking or there is no more audio input. The ending silence is not included.
The ``timeout`` parameter is the maximum number of seconds that this will wait for a phrase to start before giving up and throwing an ``speech_recognition.WaitTimeoutError`` exception. If ``timeout`` is ``None``, there will be no wait timeout.
The ``phrase_time_limit`` parameter is the maximum number of seconds that this will allow a phrase to continue before stopping and returning the part of the phrase processed before the time limit was reached. The resulting audio will be the phrase cut off at the time limit. If ``phrase_timeout`` is ``None``, there will be no phrase time limit.
This operation will always complete within ``timeout + phrase_timeout`` seconds if both are numbers, either by returning the audio data, or by raising an exception.'

def listen(self, source, timeout=None, phrase_time_limit=None):

    assert isinstance(source, AudioSource), 'Source    must    be    an    audio    source'
    assert (source.stream is not None), 'Audio    source    must    be    entered    before    listening,    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)
    pause_buffer_count = int(math.ceil((self.pause_threshold / seconds_per_buffer)))
    phrase_buffer_count = int(math.ceil((self.phrase_threshold / seconds_per_buffer)))
    non_speaking_buffer_count = int(math.ceil((self.non_speaking_duration / seconds_per_buffer)))
    elapsed_time = 0
    buffer = ''
    while True:
        frames = collections.deque()
        while True:
            elapsed_time += seconds_per_buffer
            if (timeout and (elapsed_time > timeout)):
                raise WaitTimeoutError('listening    timed    out    while    waiting    for    phrase    to    start')
            buffer = source.stream.read(source.CHUNK)
            if (len(buffer) == 0):
                break
            frames.append(buffer)
            if (len(frames) > non_speaking_buffer_count):
                frames.popleft()
            energy = audioop.rms(buffer, source.SAMPLE_WIDTH)
            if (energy > self.energy_threshold):
                break
            if self.dynamic_energy_threshold:
                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)))
        (pause_count, phrase_count) = (0, 0)
        phrase_start_time = elapsed_time
        while True:
            elapsed_time += seconds_per_buffer
            if (phrase_time_limit and ((elapsed_time - phrase_start_time) > phrase_time_limit)):
                break
            buffer = source.stream.read(source.CHUNK)
            if (len(buffer) == 0):
                break
            frames.append(buffer)
            phrase_count += 1
            energy = audioop.rms(buffer, source.SAMPLE_WIDTH)
            if (energy > self.energy_threshold):
                pause_count = 0
            else:
                pause_count += 1
            if (pause_count > pause_buffer_count):
                break
        phrase_count -= pause_count
        if ((phrase_count >= phrase_buffer_count) or (len(buffer) == 0)):
            break
    for i in range((pause_count - non_speaking_buffer_count)):
        frames.pop()
    frame_data = ''.join(list(frames))
    return AudioData(frame_data, source.SAMPLE_RATE, source.SAMPLE_WIDTH)
