'Listens for PERSONA in everyday sound. Times out after LISTEN_TIME, so
needs to be restarted.'

def passiveListen(self, PERSONA):

    THRESHOLD_MULTIPLIER = 1.8
    RATE = 16000
    CHUNK = 1024
    THRESHOLD_TIME = 1
    LISTEN_TIME = 10
    stream = self._audio.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)
    frames = []
    lastN = [i for i in range(30)]
    for i in range(0, ((RATE / CHUNK) * THRESHOLD_TIME)):
        data = stream.read(CHUNK)
        frames.append(data)
        lastN.pop(0)
        lastN.append(self.getScore(data))
        average = (sum(lastN) / len(lastN))
    THRESHOLD = (average * THRESHOLD_MULTIPLIER)
    frames = []
    didDetect = False
    for i in range(0, ((RATE / CHUNK) * LISTEN_TIME)):
        data = stream.read(CHUNK)
        frames.append(data)
        score = self.getScore(data)
        if (score > THRESHOLD):
            didDetect = True
            break
    if (not didDetect):
        print 'No    disturbance    detected'
        stream.stop_stream()
        stream.close()
        return (None, None)
    frames = frames[(-20):]
    DELAY_MULTIPLIER = 1
    for i in range(0, ((RATE / CHUNK) * DELAY_MULTIPLIER)):
        data = stream.read(CHUNK)
        frames.append(data)
    stream.stop_stream()
    stream.close()
    with tempfile.NamedTemporaryFile(mode='w+b') as f:
        wav_fp = wave.open(f, 'wb')
        wav_fp.setnchannels(1)
        wav_fp.setsampwidth(pyaudio.get_sample_size(pyaudio.paInt16))
        wav_fp.setframerate(RATE)
        wav_fp.writeframes(''.join(frames))
        wav_fp.close()
        f.seek(0)
        transcribed = self.passive_stt_engine.transcribe(f)
    if any(((PERSONA in phrase) for phrase in transcribed)):
        return (THRESHOLD, PERSONA)
    return (False, transcribed)
