def record_phrase()

in TSA-demo/TSA_voice_assistant/raspberry_voice_assistant.py [0:0]


def record_phrase():

    with noalsaerr():
        num_silent = 0
        start = False
        p = pyaudio.PyAudio()

        if platform_sys == 'Darwin':
            #stream config for mac
            stream = p.open(format=FORMAT, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK_SIZE)
        else:
            #stream config for RaspPiI
            #stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK_SIZE, input_device_index=2)
            stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK_SIZE)
    
        stream.start_stream()
        recording = array('h')
        while True:
            data = array('h', stream.read(CHUNK_SIZE))
            if byteorder == 'big':
                data.byteswap()
            recording.extend(data)
            silent = is_silent(data)
            if silent and start:
                num_silent += 1
                sessionlength +=1
            elif not silent and not start:
                start = True
                num_silent = 0
                sessionlength = 0
            if start:
                sessionlength +=1
            if start and num_silent > 15:
                break
            if start and sessionlength > 80:
                break
        stream.stop_stream()
        stream.close()
        p.terminate()
        if platform_sys != 'Darwin':
            set_color("off")
            print("turning off leds and returning recorded voice")
        return recording