#!/usr/bin/env python2.7 # *-* coding: utf-8 *-* from fcntl import fcntl, F_GETFL, F_SETFL from os import O_NONBLOCK, read import subprocess import argparse from commands import getoutput def channel_number(c): try: c = int(c) except: raise argparse.ArgumentTypeError('Channel number must be an integer') if c < 0: raise argparse.ArgumentTypeError('Please specify a channel number greater than 0') return c def delay_var(d): try: d = int(d) except: raise argparse.ArgumentTypeError('Delay interval must be an integer') if d < 100: d = 100 return d parser = argparse.ArgumentParser() parser.add_argument('-d', '--delay', metavar='N', type=delay_var, default=100, help='Delay interval in milliseconds') parser.add_argument('-s', '--scale', type=int, help='use dB scale; output range 0- (integer); if not specified, output range is linear [0..1]') parser.add_argument('-f', '--file', help='Output file') parser.add_argument('-c', '--channels', metavar='C', type=int, help='Channel number', default=1) parser.add_argument('ports', nargs='*') args = parser.parse_args() delay = args.delay if not args.channels and not args.ports: print 'At least a channel value or output port must be specified' parser.print_help() exit() elif args.ports: jack_port_list = [] port_iter = iter(getoutput('jack_lsp -p').split('\n')) while True: try: port = port_iter.next().strip() props = port_iter.next().strip().replace('properties: ', '').rstrip(',').split(',') if not 'output' in props: continue jack_port_list.append(port) except: break not_valid = [] not_found = [] for port in args.ports: if not ':' in port: not_valid.append(port) continue if not port in jack_port_list: not_found.append(port) if not_valid: print 'The following specified port names are not valid (the ports will still be created, you have to connect them manually:' print '\n'.join(['- {}'.format(p) for p in not_valid]) if not_found: print 'The following ports are not available (thus will not be connected, do it manually):' print '\n'.join(['- {}'.format(p) for p in not_found]) ports = ' '.join(args.ports) else: ports = ' '.join(['input_{}'.format(i + 1) for i in xrange(args.channels)]) cmd = './jack-peak -q {scale} -d {delay} {ports}'.format(scale='-i {}'.format(args.scale) if args.scale else '', delay=delay, ports=ports) proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, ) flags = fcntl(proc.stdout, F_GETFL) fcntl(proc.stdout, F_SETFL, flags | O_NONBLOCK) empty = 0 while True: try: output = read(proc.stdout.fileno(), 1024) if output: if args.file: with open(args.file, 'a') as output_file: output_file.write(output) output_file.write('\n') else: print output else: break except KeyboardInterrupt: proc.terminate() break except OSError: pass except: break