#!/usr/bin/python from subprocess import * import os, sys, re print "Welcome to jackctl. 'h' at the prompt gives help...." def jackctl_help(): print """jackctl help....hello!!! commands: a - enter audio select mode (default) m - enter midi select mode, also toggles between jack-midi and alsa-midi connections l - list (audio or MIDI, depends on mode) ports - connect numbered ports d - disconnect numbered ports q - quit f - grow fruit out of your computer""" listp = [] mode = 'a' #### start out in audio mode def pager(string): out = string.splitlines() for x in range(len(out)): sys.stdout.write("%s\n" % out[x]) if x % 22 == 21: sys.stdout.write("hit return for more") raw_input() def get_list(): global listp, mode, listp2 output = '' if mode == 'a' or mode == 'm': listp = Popen(['jack_lsp','-t'],stdout=PIPE).communicate()[0] listp1 = re.findall(r'.*\n\t.*\n', listp) # print # print "listp1 is %s" % listp1 listp_conn = Popen(['jack_lsp','-c', '-t'], stdout=PIPE).communicate()[0] listp_conn1 = re.findall(r'.*\n(?:[\t ]+.*\n)*', listp_conn) # print # print "listp_conn1 is %s" % listp_conn1 ##### sort audio and MIDI: if mode == 'a': listp2 = filter(lambda(x): 'midi' not in x, listp1) listp_conn2 = filter(lambda(x): 'midi' not in x, listp_conn1) elif mode == 'm': listp2 = filter(lambda(x): 'midi' in x, listp1) listp_conn2 = filter(lambda(x): 'midi' in x, listp_conn1) # print # print "listp2 is %s" % listp2 # print "listp_conn2 is %s" % listp_conn2 ##### strip type info: for x in range(len(listp2)): listp2[x] = listp2[x].split('\n')[0] for x in range(len(listp_conn2)): #### last field is type, remove it: listp_conn2[x] = '\n'.join(listp_conn2[x].split('\n')[:-1]) # print # print "now listp2 is %s" % listp2 # print "now listp_conn2 is %s" % listp_conn2 ##### format for output: for i in listp_conn2: i = ' '.join(i.split('\n')[:-1]) if ' ' in i: #### if connected, that is connection = i.split() # print # print "connection is %s" % connection output = output + "%s) %s\n" % (listp2.index(connection[0].strip()), connection[0].lstrip()) output = output + " %s) %s\n" % (listp2.index(connection[1].strip()), connection[1].lstrip()) else: output = output + "%s) %s\n" % (listp2.index(i), i) elif mode == 'm-alsa': listp = Popen(['aconnect', '-li'], stdout=PIPE).communicate()[0].split('\n')[:-1] output = output + "READABLE PORTS:\n" for i in listp: output = output + i + "\n" listq = Popen(['aconnect', '-lo'], stdout=PIPE).communicate()[0].split('\n')[:-1] output = output + "WRITEABLE PORTS:\n" for i in listq: output = output + i + "\n" pager(output) def outtahere(): print "Goodbye!" exit() while 1: try: if mode == 'a': prompt = raw_input('jackctl(audio)--> ') elif mode == 'm': prompt = raw_input('jackctl(midi)--> ') elif mode == 'm-alsa': prompt = raw_input('jackctl(alsa-midi)--> ') if prompt == 'h': jackctl_help() elif prompt == 'l': get_list() elif prompt == 'a': mode = 'a' elif prompt == 'm': if mode == 'm': mode = 'm-alsa' else: mode = 'm' elif prompt == 'q': break elif prompt == 'f': print "it was just a joke...you took me seriously? really?" elif prompt == '': continue else: ports = prompt.split() if ports[0] == 'd': print "disconnecting specified ports" if mode == 'a' or mode == 'm': Popen(['jack_disconnect','%s' % listp2[int(ports[1])], '%s' % listp2[int(ports[2])]]).communicate() elif mode == 'm-alsa': Popen(['aconnect', '-d', '%s' % ports[1], '%s' % ports[2]]).communicate() else: if mode == 'a' and (int(ports[0]) < 4) and (int(ports[1]) < 4): print """you shouldn't try connect either connections that give either feedback or connections that are nonsensical""" else: print "connecting specified ports" if mode == 'a' or mode == 'm': Popen(['jack_connect','%s' % listp2[int(ports[0])], '%s' % listp2[int(ports[1])]]).communicate() elif mode == 'm-alsa': Popen(['aconnect', '%s' % ports[0], '%s' % ports[1]]).communicate() except EOFError: print "\nGoodbye!" exit() except: print "nonsense!!!" pass outtahere()