#!/usr/bin/env python3 SOUNDCARD_DEVICE_FILE = "/proc/asound/sndallodigione/pcm0p/sub0/status" # Path of souncard status file SHUTDOWN_TIMEOUT = 15 # Delay before amplifier power down after soundcard released #---------------------------8-------- import os import time import subprocess import argparse parser = argparse.ArgumentParser(description='check sound is playing and call ~/start.sh and ~/stop.sh when status change is detected') parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity") args = parser.parse_args() deviceFile = None lastIdle = None def Now(): return int(time.time()) def CheckSoundcardStatus(): global deviceFile buf = "" lastException = None for i in range(3): try: os.lseek(deviceFile, 0, os.SEEK_SET) buf = os.read(deviceFile, 14) return (len(buf) == 14 and buf[7] == 82 and buf[8] == 85 and \ buf[9] == 78 and buf[10] == 78 and buf[11] == 73 and \ buf[12] == 78 and buf[13] == 71) # Check for "RUNNING" except Exception as ex: # Attempt to open device file again try: os.close(deviceFile) except: pass try: deviceFile = os.open(SOUNDCARD_DEVICE_FILE, os.O_RDONLY) except Exception as ex: lastException = ex raise lastException # All attempt failed def PowerOn(): res = subprocess.run("~/start.sh", shell=True, check=True) if args.verbose: print(f"Power ON: {res}") def PowerOff(): res = subprocess.run("~/stop.sh", shell=True, check=True) if args.verbose: print(f"Power OFF: {res}") lastStatus = CheckSoundcardStatus() while True: try: nowStatus = CheckSoundcardStatus() except Exception as ex: print(ex) time.sleep(1) continue if nowStatus: if not lastStatus: if args.verbose: print("Soundcard started") PowerOn() lastStatus = True else: if lastStatus: if args.verbose: print("Soundcard released") lastStatus = False lastIdle = Now() elif (not lastStatus) and lastIdle: if Now() - lastIdle >= SHUTDOWN_TIMEOUT: if args.verbose: print("Timeout Reached") PowerOff() lastIdle = None time.sleep(0.5)