def record_phrase()

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


def record_phrase():

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

        #info = p.get_host_api_info_by_index(0)
        #numdevices = info.get('deviceCount')
        #for i in range(0, numdevices):
        #    if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
        #        print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i))


        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 = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=2048)
            #stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024, input_device_index=1)
    
        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

        #try:
        #    wavmaker(stream)
        #except Exception as e:
        #    print('wavemaker issue: ', e)
        stream.stop_stream()
        stream.close()
        p.terminate()
        if platform_sys != 'Darwin':
            set_color("off")
            print("turning off leds and returning recorded voice")

        return recording