You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB
Python

#!/usr/bin/env python
import json, random, os, subprocess
from pprint import pprint
from time import sleep
from LEDfunctions import *
# this script plays the content of the announcer (from json file)
import subprocess
# is the guru running?
try:
pirate_pid = subprocess.check_output(
["pgrep", "-f", "guru-pirate.py"]
)
print("guru pirate running:",pirate_pid)
subprocess.call(["kill", pirate_pid.replace("\n","")])
except:
print("guru-pirate is not running")
sleep(4)
# when characters speak the LEDs light up and perform effects
def vu_2_leds(color):
while True:
data = play_process.stdout.readline()
if not data:
pixels.clear() # make LEDs dark
pixels.show()
break
data = data.rstrip()
if data.endswith("%"):
vu = float(data[:-1][-3:])/100 # 0-100
leds_color_intensity(color, vu)
# open announcer.json files
pwd = os.path.dirname( os.path.realpath(__file__) ) + "/"
announcer_f = open(pwd + "announcer.json", "r").read()
announcer = json.loads(announcer_f)
# print(announcer)
position_file = open(pwd + "announcements_position.txt", "r")
#choosing next message in sequence
msg_sf_len=len( announcer['messages'] )
position= (int( position_file.read() ) ) + 1
if position == msg_sf_len:
position = 0
print(position)
position_file = open(pwd + "announcements_position.txt", "w")
position_file.write(str(position))
position_file.close()
print ("position", position)
sound_dir = pwd + "Audio_recordings/Announcements/"
intro_sf = sound_dir + random.choice( announcer['introductions'] )
msg_sf = sound_dir + announcer['messages'][position]
print(intro_sf, msg_sf)
#play audio
print(intro_sf)
os.system('play -q "{}" gain 5'.format(intro_sf) )
play_process = subprocess.Popen(["aplay", msg_sf, "-f", "cd", "--vumeter=mono"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
vu_2_leds(color_announcer)