]> marstr Code Repo - Pilot2AWS/commitdiff
Initial commit of script to git repo
authorMarStr <marcus@marstr.online>
Fri, 5 Jul 2024 13:46:19 +0000 (15:46 +0200)
committerMarStr <marcus@marstr.online>
Fri, 5 Jul 2024 13:46:19 +0000 (15:46 +0200)
Pilot2AWS.py [new file with mode: 0644]

diff --git a/Pilot2AWS.py b/Pilot2AWS.py
new file mode 100644 (file)
index 0000000..87cfd92
--- /dev/null
@@ -0,0 +1,126 @@
+\r
+import boto3        # pip install boto3\r
+import pygame       # pip install pygame\r
+import time\r
+import io\r
+import random\r
+import os\r
+\r
+# -------------------------------------------------------------------\r
+# Enter your access data and your AWS region\r
+# -------------------------------------------------------------------\r
+atc_aws_key    = "YOUR_AWS_KEY"\r
+atc_aws_secret = "YOUR_AWS_SECRET"\r
+atc_aws_region = "YOUR_REGION"\r
+# -------------------------------------------------------------------\r
+\r
+# -------------------------------------------------------------------\r
+# Do you want to see ATC messages in the console also?\r
+# -------------------------------------------------------------------\r
+atc_show_responses = True\r
+\r
+# -------------------------------------------------------------------\r
+# The voice model you want to use.\r
+#\r
+# IMPORTANT:\r
+# Depending on the model you want, the prices are per 1m characters\r
+# generated vary *drastically*.\r
+# 'standard' is the cheapest, 'neural' the most expensive one.\r
+# Depending on the model you pick, the list of voices vary also.\r
+#\r
+# Pricing: https://aws.amazon.com/polly/pricing/\r
+# -------------------------------------------------------------------\r
+atc_aws_voicemodel = 'standard'\r
+# atc_aws_voicemodel = 'neural'\r
+\r
+\r
+# -------------------------------------------------------------------\r
+# Define where your Pilot2ATC conversation file is located\r
+# This file can be anywhere and have any name - just make sure you\r
+# put in the correct absolute path into this variable.\r
+# -------------------------------------------------------------------\r
+atc_pilot2atc_log = "C:\\Users\\Marcus\\Desktop\\ConversationText.txt"\r
+\r
+\r
+# -------------------------------------------------------------------\r
+# NO TRESPASSING BEYOND THIS POINT\r
+# -------------------------------------------------------------------\r
+\r
+os.system("cls")\r
+print("  ")\r
+print(" ---------------------------------------------- ")\r
+print(" Pilot2AWS")\r
+print(" Making Pilot2ATC sound more natural")\r
+print(" ---------------------------------------------- ")\r
+print(" Developed by MarStrMind")\r
+print(" License: MIT")\r
+print(" ---------------------------------------------- ")\r
+print(" Using file: " + atc_pilot2atc_log)\r
+print(" ---------------------------------------------- ")\r
+print("  ")\r
+\r
+# The voice names available for playback, depending on the model\r
+# Of course, you can remove any that you do not like\r
+# Sources for voice names:\r
+# - https://docs.aws.amazon.com/polly/latest/dg/neural-voices.html\r
+# - https://docs.aws.amazon.com/polly/latest/dg/standard-voices.html\r
+atc_voices = None\r
+if atc_aws_voicemodel == "standard":\r
+    atc_voices = ["Nicole", "Russell", "Amy", "Emma", "Brian", "Aditi", "Raveena", "Joanna", "Kendra", "Kimberly", "Salli", "Joey", "Geraint"]\r
+    # After testing, I removed Ivy from this list. It sounded too child-like.\r
+if atc_aws_voicemodel == "neural":\r
+    atc_voices = ["Olivia", "Amy", "Emma", "Brian", "Arthur", "Kajal", "Niamh", "Aria", "Ayanda", "Danielle", "Gregory", "Ivy", "Joanna", "Kendra", "Kimberly", "Salli", "Joey", "Matthew", "Ruth", "Stephen"]\r
+    # Missing in this list are Justin and Kevin -\r
+    # they are declared as being young child voices - you normally do not hear children on an ATC transmission.\r
+\r
+\r
+# Setup our Polly session to AWS\r
+atc_polly_client = boto3.Session(\r
+                aws_access_key_id=atc_aws_key,                     \r
+                aws_secret_access_key=atc_aws_secret,\r
+                region_name=atc_aws_region).client('polly')\r
+\r
+# Last read line\r
+atc_last_line = -1\r
+\r
+# Init pygame and its mixer\r
+pygame.init()\r
+pygame.mixer.init()\r
+\r
+# Open file before main loop\r
+atc_log = open(atc_pilot2atc_log)\r
+\r
+# Main run loop\r
+while True:\r
+    atc_lines = atc_log.readlines()\r
+    idx = 0\r
+    for line in atc_lines:\r
+        if "      ATC: " in line:\r
+            line = line.replace("      ATC: ", "")\r
+            if idx > atc_last_line:\r
+                # Update last read line\r
+                atc_last_line = idx\r
+\r
+                # Pick a voice\r
+                voice_to_use = random.randrange(len(atc_voices))\r
+\r
+                # Show what has been said so that it can be verified outside of Pilot2ATC - if the user wants it\r
+                if atc_show_responses == True:\r
+                    print(" > ATC - [" + atc_voices[voice_to_use] + "]: " + line)\r
+\r
+                # Generate voice!\r
+                # Let's keep it at OGG - best compromize between data transfer size and quality\r
+                response = atc_polly_client.synthesize_speech(VoiceId=atc_voices[voice_to_use], OutputFormat='ogg_vorbis', Text = line, Engine = atc_aws_voicemodel)\r
+                # And place that into a binary block\r
+                data = io.BytesIO(response['AudioStream'].read())\r
+\r
+                # Place the data into pygame and play it\r
+                pygame.mixer.music.load(data)\r
+                pygame.mixer.music.play()\r
+                pygame.event.wait()\r
+        \r
+        # Increase loop count\r
+        idx = idx + 1\r
+\r
+    atc_log.seek(0)\r
+    time.sleep(3)\r