## Alexa Speaks Announcement Text In Variable Through Airfoil To House Speakers
import os, boto3, time ## Amazon's boto3 Python module must be installed on Mac
SpeakThis = indigo.variables[740708485].value
str (SpeakThis) ## assure the variable to be spoken is a string
## Settings taken from Amazon's sample Polly code
defaultRegion = 'us-east-1'
defaultUrl = 'https://polly.us-east-1.amazonaws.com'
indigo.actionGroup.execute(348168416) ## Enable Default Announce Airfoil House Speakers Volume Settings
indigo.actionGroup.execute(1112870329) ## Enable Airfoil For System Audio
time.sleep(3) ## Delay for Airfoil to set up speakers
## boto3 functions provided in Amazon's Polly example code
def connectToPolly(regionName=defaultRegion, endpointUrl=defaultUrl):
return boto3.client('polly', region_name=regionName, endpoint_url=endpointUrl)
def speak(polly, text, format='mp3', voice='Joanna'): ## Joanna is Alexa
resp = polly.synthesize_speech(OutputFormat=format, Text=text, VoiceId=voice)
soundfile = open('/tmp/sound.mp3', 'w')
soundBytes = resp['AudioStream'].read()
soundfile.write(soundBytes)
soundfile.close()
os.system('afplay /tmp/sound.mp3')
os.remove('/tmp/sound.mp3')
polly = connectToPolly()
speak(polly, str(SpeakThis))
time.sleep(2) ## Time required for processing and speaking
indigo.actionGroup.execute(278671718) ## Restore Current Speakers Saved Volume Levels & Connections
indigo.actionGroup.execute(1082305604) ## Enable Airfoil For Line-In Audio
## Alexa Speaks To Alexa
import os, boto3, time ## Amazon's boto3 module must be installed on Mac
SpeakThis = indigo.variables["AlexaSaysThisVariable"].value
str (SpeakThis) ## Assure that the variable value to be spoken is a strig
defaultRegion = 'us-east-1' ## values assigned in Polly example code
defaultUrl = 'https://polly.us-east-1.amazonaws.com'
## the Python module "SwitchAudioSource" must be installed on the Mac
## to make use of the USB audio adapter
os.system("switchaudiosource -t output -s 'USB Audio Device'")
def connectToPolly(regionName=defaultRegion, endpointUrl=defaultUrl):
return boto3.client('polly', region_name=regionName, endpoint_url=endpointUrl)
def speak(polly, text, format='mp3', voice='Joanna'): ## Joanna is Alexa
resp = polly.synthesize_speech(OutputFormat=format, Text=text, VoiceId=voice)
soundfile = open('/tmp/sound.mp3', 'w')
soundBytes = resp['AudioStream'].read()
soundfile.write(soundBytes)
soundfile.close()
os.system('afplay /tmp/sound.mp3')
os.remove('/tmp/sound.mp3')
polly = connectToPolly()
speak(polly, str(SpeakThis))
time.sleep(2) ## Delay needed for Polly to process and speak
os.system("switchaudiosource -t output -s 'Built-in Output'")
## Alexa Records mp3 Announcement From Text In Variable
import os, boto3, time ## Amazon's boto3 Python module must be installed on Mac
SpeakThis = indigo.variables["AlexaSaysThisVariable"].value
str (SpeakThis) ## assure the variable to be spoken is a string
## Settings taken from Amazon's sample Polly code
defaultRegion = 'us-east-1'
defaultUrl = 'https://polly.us-east-1.amazonaws.com'
newFileName = SpeakThis + ".mp3" # new file named with what is spoken
newFilePath = "/Users/bentley/AnnouncementRecordings/"+newFileName
indigo.server.log(unicode(newFilePath))
## boto3 functions provided in Amazon's Polly example code
def connectToPolly(regionName=defaultRegion, endpointUrl=defaultUrl):
return boto3.client('polly', region_name=regionName, endpoint_url=endpointUrl)
def speak(polly, text, format='mp3', voice='Joanna'): ## Joanna is Alexa
resp = polly.synthesize_speech(OutputFormat=format, Text=text, VoiceId=voice)
soundfile = open(newFilePath, 'w')
soundBytes = resp['AudioStream'].read()
soundfile.write(soundBytes)
soundfile.close() # New recordings left in Announcements Directory
os.system('afplay /newFilePath')
polly = connectToPolly()
speak(polly, str(SpeakThis))