#!/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 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) # open json files, where are all the phrases pwd = os.path.dirname( os.path.realpath(__file__) ) + "/" oracle_f = open(pwd + "guru.json", "r").read() oracle = json.loads(oracle_f) rebel_f = open(pwd + "rebel.json", "r").read() rebel = json.loads(rebel_f) # guru part, using audio recordings keys = list( oracle.keys()) part = random.choice(keys) # choose: Repeat OR Ask Yourself print(part) sound_dir = pwd + "Audio_recordings/" + oracle[part]['title'] + "/" intro = random.choice(oracle[part]['introductions']) intro_txt = intro[0] intro_sound = sound_dir + intro[1] 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) # 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) # guru part, using audio recordings for i in range( random.randint(1,4) ): sleep( random.randint(1,1) ) 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 # 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