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.

94 lines
3.3 KiB
Python

#!/usr/bin/env python
# this script combines the content of guru and pirate (from json files) to play their messages
# it also integrates leds when the characters speak
import json, random, os, subprocess
6 years ago
from pprint import pprint
from time import sleep
from LEDfunctions import *
import sys
import subprocess
# is the announcements.py running?
try: # try: if no error - the process is running.
# therefore exit()
ann_pid = subprocess.check_output(["pgrep", "-f", "announcements.py"])
print("announcements.py ARE running. Will stop here", ann_pid)
except:
print("announcements.py are not running. Will run guru")
if 'ann_pid' in vars() or 'ann_pid' in globals():
sys.exit(1)
# 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)
def leds_start_stop(color):
while True:
data = play_process.stdout.readline()
data = data.rstrip()
print('data:',data)
print('process:', play_process.stdout.readline()) #_handle_exitstatus
leds_pirate_bounce(color_pirate)
6 years ago
6 years ago
# open json files, where are all the phrases
6 years ago
pwd = os.path.dirname( os.path.realpath(__file__) ) + "/"
oracle_f = open(pwd + "guru.json", "r").read()
6 years ago
oracle = json.loads(oracle_f)
6 years ago
rebel_f = open(pwd + "rebel.json", "r").read()
6 years ago
rebel = json.loads(rebel_f)
6 years ago
# guru part, using audio recordings
6 years ago
keys = list( oracle.keys())
part = random.choice(keys) # choose: Repeat OR Ask Yourself
6 years ago
print(part)
6 years ago
sound_dir = pwd + "Audio_recordings/" + oracle[part]['title'] + "/"
6 years ago
intro = random.choice(oracle[part]['introductions'])
intro_txt = intro[0]
intro_sound = sound_dir + intro[1]
6 years ago
print('Intro:', intro_txt, intro_sound)
play_process = subprocess.Popen(["aplay", intro_sound, "-f", "cd", "--vumeter=mono"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
vu_2_leds(color_guru)
6 years ago
# pirate reply
rebel_run = random.choice([True, False]) # 50/50 chances of intervening
if rebel_run is True:
rebel_reply_snippet = intro[2] # comes from guru/oracle json file
rebel_reply = random.choice( rebel[part] )
rebel_sentence = rebel_reply.format(rebel_reply_snippet)
6 years ago
# guru part, using audio recordings
for i in range( random.randint(1,4) ):
sleep( random.randint(1,1) )
6 years ago
msg = random.choice(oracle[part]['messages'])
msg_txt = msg[0]
msg_sound = sound_dir + msg[1]
print('MSG:', msg_txt, msg_sound)
play_process = subprocess.Popen(["aplay", msg_sound, "-f", "cd", "--vumeter=mono"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
vu_2_leds(color_guru) #LEDs guru
6 years ago
# pirate part, using espeak
if rebel_run is True:
print('rebel sentence:', rebel_sentence)
#rebel_cmd = 'echo "{}" | espeak -ven+whisper -s 150'.format(rebel_sentence)
#os.system(rebel_cmd)
play_process = subprocess.Popen(["espeak", rebel_sentence, "-ven+whisper", "-s", "150"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) # text to speech using espeak
leds_pirate_bounce(color_pirate) # LEDs pirate