TSA-demo/TSA_voice_assistant/raspberry_voice_assistant.py [33:152]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pa = None
audio_stream = None

from ctypes import *
from contextlib import contextmanager

if platform_sys != "Darwin":
    from gpiozero import LED
    from apa102 import APA102

    COLORS_RGB = dict(
        blue=(0, 0, 255),
        green=(0, 255, 0),
        orange=(255, 128, 0),
        pink=(255, 51, 153),
        purple=(128, 0, 128),
        red=(255, 0, 0),
        white=(255, 255, 255),
        yellow=(255, 255, 51),
        off=(0, 0, 0),
    )

    KEYWORDS_COLOR = {
        'picovoice': 'green',
        'porcupine': 'blue',
        'blueberry': 'orange',
        'terminator': 'off',
    }

    driver = APA102(num_led=12)
    power = LED(5)
    power.on()

    keywords = list(KEYWORDS_COLOR.keys())

#Change to reflect the bot and alias you created
#bot_id = 'AVLBAKUHJ9'
#bot_alias_id='4ZTMYYWUQA'
bot_id = 'ISXIO344FF'
bot_alias_id='ZWIGXTFIAU'

THRESHOLD = 500
CHUNK_SIZE = 2048
FORMAT = pyaudio.paInt16
RATE = 16000
#RATE = 44100
VOICE_ID = None

polly = session.client('polly')
lexv2 = session.client('lexv2-runtime')
translate = session.client(service_name='translate', region_name='us-east-1', use_ssl=True)

ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p)

def py_error_handler(filename, line, function, err, fmt):
    pass

c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)

@contextmanager
def noalsaerr():
    if platform_sys != 'Darwin':
        asound = cdll.LoadLibrary('libasound.so')
        asound.snd_lib_error_set_handler(c_error_handler)
        yield
        asound.snd_lib_error_set_handler(None)
        print("noalsoerr activated")
    else:
        yield
        print('no alsa err mode deactivated')

def is_silent(data):
    return max(data) < THRESHOLD

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

def play_audio(audio):
    p = pyaudio.PyAudio()
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



TSA-demo/TSA_voice_assistant/raspberry_voice_assistant_local.py [45:179]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pa = None
audio_stream = None

from ctypes import *
from contextlib import contextmanager

if platform_sys != "Darwin":
    from gpiozero import LED
    from apa102 import APA102

    COLORS_RGB = dict(
        blue=(0, 0, 255),
        green=(0, 255, 0),
        orange=(255, 128, 0),
        pink=(255, 51, 153),
        purple=(128, 0, 128),
        red=(255, 0, 0),
        white=(255, 255, 255),
        yellow=(255, 255, 51),
        off=(0, 0, 0),
    )

    KEYWORDS_COLOR = {
        'picovoice': 'green',
        'porcupine': 'blue',
        'blueberry': 'orange',
        'terminator': 'off',
    }

    driver = APA102(num_led=12)
    power = LED(5)
    power.on()

    keywords = list(KEYWORDS_COLOR.keys())

#Change to reflect the bot and alias you created
#bot_id = 'AVLBAKUHJ9'
#bot_alias_id='4ZTMYYWUQA'
bot_id = 'ISXIO344FF'
bot_alias_id='ZWIGXTFIAU'

THRESHOLD = 500
CHUNK_SIZE = 2048
FORMAT = pyaudio.paInt16
RATE = 16000
#RATE = 44100
VOICE_ID = None

polly = session.client('polly')
lexv2 = session.client('lexv2-runtime')
translate = session.client(service_name='translate', region_name='us-east-1', use_ssl=True)

ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p)

def py_error_handler(filename, line, function, err, fmt):
    pass

c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)

@contextmanager
def noalsaerr():
    if platform_sys != 'Darwin':
        asound = cdll.LoadLibrary('libasound.so')
        asound.snd_lib_error_set_handler(c_error_handler)
        yield
        asound.snd_lib_error_set_handler(None)
        print("noalsoerr activated")
    else:
        yield
        print('no alsa err mode deactivated')

def is_silent(data):
    return max(data) < THRESHOLD

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

def play_audio(audio):
    p = pyaudio.PyAudio()
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



