def set_as_pattern(param, pattern): """ Sets a peaudiosys parameter as per a given pattern. This applies only for 'xo', 'drc' and 'target' (result: string) """ result = '' if param not in ('xo', 'drc', 'target'): return "parameter mus be 'xo', 'drc' or 'target'" sets = send_cmd(f'get_{param}_sets') try: sets = json_loads(sets) except: return result for setName in sets: if pattern in setName: result = send_cmd(f'set_{param} {setName}', sender='radio_macro', verbose=True) break return result
def main_PL(): # LOOPING: reading packetLength bytes lastTimeStamp = time() # helper to avoid bouncing irpacket = b'' while True: irpacket = s.read( packetLength ) cmd = irpacket2cmd(irpacket) if cmd: if time() - lastTimeStamp >= antibound: send_cmd(cmd, sender='ir.py', verbose=True) lastTimeStamp = time()
def stop(): for item in REMOTES: source, raddr, rport = item # REMOTE remotecmd = f'aux zita_client {MY_IP} stop' send_cmd(remotecmd, host=raddr, port=rport) # LOCAL Popen('pkill -KILL zita-n2j'.split()) sleep(1)
def manage_amp_switch(mode): def get_amp_state(): result = 'n/a' try: with open(f'{AMP_STATE_PATH}', 'r') as f: tmp = f.read().strip() if tmp.lower() in ('0', 'off'): result = 'off' elif tmp.lower() in ('1', 'on'): result = 'on' except: pass return result def set_amp_state(mode): if 'amp_manager' in CONFIG: AMP_MANAGER = CONFIG['amp_manager'] else: return '(aux) amp_manager not configured' print(f'(aux) running \'{AMP_MANAGER.split("/")[-1]} {mode}\'') Popen(f'{AMP_MANAGER} {mode}', shell=True) sleep(1) return get_amp_state() cur_state = get_amp_state() new_state = '' if mode == 'state': result = cur_state elif mode == 'toggle': # if unknown state, this switch defaults to 'on' new_state = {'on': 'off', 'off': 'on'}.get(cur_state, 'on') elif mode in ('on', 'off'): new_state = mode else: result = '(aux) bad amp_switch option' if new_state: result = set_amp_state(new_state) # Optionally will stop the current player as per CONFIG if new_state == 'off': if 'amp_off_stops_player' in CONFIG and CONFIG['amp_off_stops_player']: curr_input = read_state_from_disk()['input'] if not curr_input.startswith('remote'): send_cmd('player pause', timeout=1) return result
def start(): for item in REMOTES: source, raddr, rport = item # RUN REMOTE SENDER: remotecmd = f'aux zita_client {MY_IP} start' send_cmd(remotecmd, host=raddr, port=rport) # RUN LOCAL RECEIVER: zitacmd = f'zita-n2j --jname {raddr} --buff {BUFFER} {MY_IP} {MY_ZITAPORT}' Popen(zitacmd.split()) print( f'(zita-n2j) listening for {MY_IP}:{MY_ZITAPORT}:UDP from {source} @ {raddr}' )
def remote_player_control(cmd, arg, host, port=9990): """ Controls the playback on a remote pe.audio.sys system """ # short timeout for remote LAN machine conn failure timeout = .5 rem_state = send_cmd(cmd=f'player {cmd} {arg}', host=host, port=port, timeout=timeout) return rem_state
def remote_get_meta(host, port=9990): """ Get metadata from a remote pe.audio.sys system """ md = METATEMPLATE.copy() try: tmp = send_cmd(cmd='player get_meta', host=host, port=port, timeout=1) md = json.loads(tmp) except: pass return md
def find_cd_macro(): """ Looks for a macro named 'NN_cd' or similar, if not found then returns a fake macro name. """ result = '-' # a fake macro name mNames = json_loads(send_cmd('aux get_macros')) for mName in mNames: if '_cd' in mName.lower(): result = mName break return result
def istreams_query(url): """ Order the istreams daemon script to playback an internet stream url """ error = False # Tune the radio station (Mplayer jack ports will dissapear for a while) Popen(f'{UHOME}/pe.audio.sys/share/scripts/istreams.py url {url}'. split()) # Waits a bit to Mplayer ports to dissapear from jack while loading a new stream. sleep(2) # Waiting for mplayer ports to re-emerge if not wait4ports(f'mplayer_istreams'): print(f'(aux) ERROR jack ports \'mplayer_istreams\'' ' not found') error = True if not error: # Switching the preamp input send_cmd('preamp input istreams') return True else: return False
def main_EOP(): # LOOPING: reading byte by byte as received lastTimeStamp = time() # helper to avoid bouncing irpacket = b'' while True: rx = s.read( 1 ) # Detecting the endOfPacket byte with some tolerance if abs( int.from_bytes(rx, "big") - int(endOfPacket, 16) ) <= EOPtolerance: irpacket += rx # try to translate it to a command according to the keymap table cmd = irpacket2cmd(irpacket) if cmd: if time() - lastTimeStamp >= antibound: send_cmd(cmd, sender='ir.py', verbose=True) lastTimeStamp = time() else: print( Fmt.CYAN + 'too fast' + Fmt.END ) irpacket = b'' else: irpacket += rx
def main_loop(alertdB=CFG['alertdB'], beep=CFG['beep']): level_ups = False beeped = False while True: # Reading the mouse ev = getMouseEvent() # Sending the order to pe.audio.sys if ev == 'buttonLeftDown': # Level -- send_cmd(f'level -{CFG["STEPdB"]} add', sender='mouse_volume', verbose=True) level_ups = False elif ev == 'buttonRightDown': # Level ++ send_cmd(f'level +{CFG["STEPdB"]} add', sender='mouse_volume', verbose=True) level_ups = True elif ev == 'buttonMid': # Mute toggle send_cmd('mute toggle', sender='mouse_volume', verbose=True) # Alert if crossed the headroom threshold if level_ups: level = read_state_from_disk()['level'] if (level + CFG['STEPdB']) >= alertdB: if not beeped and beep: print('(mouse_volume_daemon) BEEEEEEP, BEEEEEP') beeps() beeped = True else: beeped = False
def main(): # Prepare to use the proper share/scripts/xxxx.py and preamp input if mplayer_profile == 'dvb': script = 'DVB-T.py' preinput = 'tdt' elif mplayer_profile == 'istreams': script = 'istreams.py' preinput = 'istreams' else: print( f'{Fmt.RED}(macros) Bad Mplayer profile \'{mplayer_profile}\'{Fmt.END}' ) sys.exit() # Pausing the current player send_cmd(f'player pause', sender=ME, verbose=True) # Warning message send_cmd(f'aux warning clear') send_cmd(f'aux warning set tuning takes a while ...') # Tune the radio station (Mplayer jack ports will dissapear for a while) Popen(f'{UHOME}/pe.audio.sys/share/scripts/{script} preset {str(preset)}'. split()) # Wait a bit for current ports to disappear sleep(3) # Check for Mplayer ports to re-emerge # (some streaming urls take several seconds to load) if not wait4ports(f'mplayer_{mplayer_profile}', timeout=45): print( f'{Fmt.RED}(radio_macro) ERROR jack ports \'mplayer_{mplayer_profile}\' not found, ' f'bye :-/{Fmt.END}') # Warning message send_cmd(f'aux warning clear') sys.exit(-1) sleep(.5) # Warning message send_cmd(f'aux warning clear') # Switching the preamp input send_cmd(f'input {preinput}', sender=ME, verbose=True) sleep(.5) # Loudness compensation on|off send_cmd(f'loudness {loudness_comp}', sender=ME, verbose=True) sleep(.5) # LU level compensation reference send_cmd(f'lu_offset {lu_offset}', sender=ME, verbose=True) sleep(.5) # XO if xo_pattern: set_as_pattern('xo', xo_pattern) sleep(.5) # DRC if drc_pattern: set_as_pattern('drc', drc_pattern) sleep(.5)
def remote_cmd(cli_addr, cmd): print(f'(remote_volume) remote {cli_addr} sending \'{cmd}\'') send_cmd(cmd, host=cli_addr, verbose=False)
def get_state(): return json.loads(send_cmd('state'))
for o in outs: if not o.isdigit(): # currently not necessary continue oname = outs[o]['name'] delay = outs[o]['delay'] ms = round(delay / 44100 * 1000, 3) print(o, oname.ljust(8), f'{str(delay).rjust(5)} samples', f'({ms} ms)') if __name__ == '__main__': # Resuming Brutefir from powesave if not bf.is_running(): print('resuming Brutefir from powersave ...') print(send_cmd('convolver on')) # Test if Brutefir is available test = bf.cli('') if not test: print('Brutefir not available') sys.exit() # Reading command line for opc in sys.argv[1:]: if '-h' in opc: print(__doc__) sys.exit() elif '-l' in opc:
def autoplay_CDDA(): cd_macro_found = False if USE_CD_MACRO: mName = find_cd_macro() if mName == '-': cd_macro_found = True send_cmd(f'aux run_macro {mName}') if not cd_macro_found: send_cmd('player pause', sender=ME, verbose=True) send_cmd('aux warning clear', sender=ME, verbose=True, timeout=1) send_cmd('aux warning set disc loading ...', sender=ME, verbose=True, timeout=1) send_cmd('aux warning expire 10', sender=ME, verbose=True, timeout=1) send_cmd('preamp input cd', sender=ME, verbose=True) sleep(.5) # (!) Ordering 'play' will BLOCK the server while # waiting for the disc to be loaded send_cmd('player play', sender=ME, verbose=True)