#!/usr/bin/env python import Tkinter as tk import threading import sys import subprocess import socket import Queue import csnd6 class ui(object): def __init__(self, parent): self.myparent = parent self.color = "#555555" self.myparent.config(bg=self.color) self.cbport = 5899 self.outport = 5880 self.playing = 0 self.allowed2play = 1 self.myparent.bind("",self.keypress) def colorchange(self, dummy): pass # print "colorchange" # if self.color == "#555555": # self.color = "#dddddd" # else: # self.color = "#555555" # self.myparent.config(bg=self.color) ######## prepare csd file def preparecsd(self): print "preparing..." self.csdopt = '-m0d -+rtaudio=portaudio -odac0' self.csdinst = ''' sr = 44100 ksmps = 16 nchnls = 2 pyinit instr 1 aosc oscil 25000, 440 outs aosc, aosc pycalli "ae.callable", 0 endin ''' self.csdsco = ''' f0 15 i1 0 .2 i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . i1 + . ''' def waitforconnect(self, sock, q): conn = sock.accept() q.put(conn) def delegatecallbacks(self, sock): cbtext = '' while self.playing == 1: try: cbtext += sock.recv(32) while cbtext.count('CB'): cb, cbtext = cbtext.split('CB', 1) if cb == 'END': self.stop() except: # print "Callback Socket Unavailable:", sys.exc_info()[0] # print sock pass ### Play ### def play(self): self.preparecsd() #create socket to receive callbacks cbwait = socket.socket(socket.AF_INET, socket.SOCK_STREAM) count = 0 while count < 30000: try: cbwait.bind(('127.0.0.1', self.cbport)) print 'Callback Port: %s' % str(self.cbport) count = 30000 except: self.cbport += 1 count += 1 if count == 30000: print "NO PORTS AVAILABLE FOR CALLBACK" cbwait.listen(2) #thread to wait for audio to connect q = Queue.Queue() wait = threading.Thread(target=self.waitforconnect, args=(cbwait, q)) wait.start() args = [sys.executable, 'csdex-audio.py', str(self.cbport)] self.ae = subprocess.Popen(args) wait.join() self.cbsock = q.get()[0] self.playing = 1 threading.Thread(target=self.delegatecallbacks, args=(self.cbsock,)).start() try: self.cbsock.sendall('csdopt:%sRATENDMESSAGE' % self.csdopt) self.cbsock.sendall('csdorc:%sRATENDMESSAGE' % self.csdinst) self.cbsock.sendall('csdsco:%sRATENDMESSAGE' % self.csdsco) self.cbsock.sendall('csdgozRATENDMESSAGE') except: print "Unable to start" def stop(self): if self.playing == 1: try: print "stopping" self.playing = 0 self.cbsock.sendall('RATENDMESSAGEcsdstpRATENDMESSAGE') ended = self.ae.communicate() self.cbsock.close() except: print "Unable to Close Audio Engine" def keypress(self, event): if event.keysym == "space": if self.playing == 0: self.play() else: self.stop() def colorchange(self, dummy): print "callable" # if self.color == "#555555": # self.color = "#dddddd" # else: # self.color = "#555555" # self.myparent.config(bg=self.color) root = tk.Tk() root.geometry('140x300+200+100') root.lift() root.focus_set() ui = ui(root) root.mainloop()