from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
import os
import sys
import subprocess
from tempfile import gettempdir
import pingcap.pe.flash.util as util
session = util.get_boto_session()
polly = session.client("polly")
# input_file_name = sys.argv[1]
input_aerocode = sys.argv[1]
# voice_id = sys.argv[2]
# output_file_name = input_file_name + "_" + voice_id + ".mp3"
output_file_name = input_aerocode + ".mp3"
# audio_source_file = open(input_file_name)
# text = "".join(audio_source_file.readlines())
aerocode_to_pronounce = {
"A": "Alpha",
"B": "Bravo",
"C": "Charlie",
"D": "Delta",
"E": "Echo",
"F": "Foxtrot",
"G": "Golf",
"H": "Hotel",
"I": "India",
"J": "Juliet",
"K": "Kilo",
"L": "Lima",
"M": "Mike",
"N": "November",
"O": "Oscar",
"P": "Papa",
"Q": "Quebec",
"R": "Romeo",
"S": "Sierra",
"T": "Tango",
"U": "Uniform",
"V": "Victor",
"W": "Whiskey",
"X": "X-ray",
"Y": "Yankee",
"Z": "Zulu",
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
}
code_array = input_aerocode.split(None)
code_pronounce_array = [
aerocode_to_pronounce[i] + '' for i in input_aerocode
]
text = " ".join(code_pronounce_array)
try:
response = polly.synthesize_speech(
Text='' + text + "",
Engine="neural",
OutputFormat="mp3",
VoiceId="Brian",
TextType="ssml",
)
except (BotoCoreError, ClientError) as error:
print(error)
sys.exit(-1)
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
# output = os.path.join(gettempdir(), output_file_name)
output = os.path.join(output_file_name)
try:
with open(output, "wb") as file:
file.write(stream.read())
except IOError as error:
print(error)
sys.exit(-1)
else:
print("Could not stream audio")
sys.exit(-1)
# if sys.platform == "win32":
# os.startfile(output)
# else:
# opener = "open" if sys.platform == "darwin" else "xdg-open"
# subprocess.call([opener, output])