Skip to content

Commit

Permalink
trying to fix up threading so it's faster
Browse files Browse the repository at this point in the history
  • Loading branch information
pussinboot committed Oct 23, 2015
1 parent d53e85b commit 212528e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 34 deletions.
56 changes: 43 additions & 13 deletions v0.5.0/control_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self,gui):

# midi control
self.queue = None
self.MC = MidiClient(self) # midi thread
self.MC = MidiClient(self,refresh_int=25) # midi thread


self.last_pad = None # store last pad
Expand Down Expand Up @@ -69,46 +69,76 @@ def __init__(self,gui):
def set_queue(self,queue):
self.queue = queue

def processIncoming(self):
def processIncoming(self): # change queue to only handle events that would cause gui changes
while self.queue.qsize():
try:
msg = self.queue.get(0)
# Check contents of message and do what it says
# As a test, we simply print it
#print( msg)
if msg[0] in self.key_to_fun:
self.key_to_fun[msg[0]](msg[1])
print( msg)
if msg[0] == 'pad':
self.select_pad_gui(*msg[1])
elif msg[0] == 'lr':
if msg[1] == 0:
self.gui.go_l()
else:
self.gui.go_r()

except queue.Empty:
pass

def midi_start(self,inp=None):
self.MC.MC.set_inp(inp)
def process_midi(queue):
while self.MC.running:
msg = self.MC.MC.test_inp()
if msg:
#queue.put(mg)
if msg[0] in self.key_to_fun:
resp = self.key_to_fun[msg[0]](msg[1])
if resp: queue.put(resp)

self.MC.start(process_midi)

###
# update gui to correspond with action

def get_pad_container(self,lr,padno):
if lr in [0,1] and padno in [0,1,2,3,4,5,6,7]:
return self.gui.padgroups[lr].preset_containers[padno]

def select_pad(self,lr,padno):
pc = self.get_pad_container(lr,padno)
self.mdc.select_preset(pc.preset)
self.queue.put(['pad',[lr, padno]])

def select_pad_gui(self,lr,padno):
if self.last_pad and self.last_pad != [lr, padno]: # deselect (in gui) last pad
lastpc = self.get_pad_container(*self.last_pad)
if lastpc: lastpc.deselected()
#self.gui.padgroups[self.last_pad[0]].preset_containers[self.last_pad[1]].deselected()
pc = self.get_pad_container(lr,padno)
if pc:
pc.selected()
self.mdc.select_preset(pc.preset)
self.last_pad = [lr, padno]

def go_lr(self,lr):
#print(lr)
# left - lr = 0, right - lr = 1

if lr == 0 and self.gui.padgroup_l_n > 0:
self.queue.put(['lr',lr])
if self.last_pad:
self.last_pad[0] += 1
pc = self.get_pad_container(*self.last_pad)
if pc: pc.selected()
if pc: self.queue.put(['pad',self.last_pad])#pc.selected()

elif lr == 1 and self.gui.padgroup_r_n < len(self.gui.db) - 1:
self.queue.put(['lr',lr])
if self.last_pad:
self.last_pad[0] -= 1
pc = self.get_pad_container(*self.last_pad)
if pc: pc.selected()
if pc: self.queue.put(['pad',self.last_pad])#pc.selected()

### midi
# 3 types:
Expand Down Expand Up @@ -151,7 +181,7 @@ def type_o(self,key,n):
else:
for _ in range(-1*n):
kp.typer(self.key_to_key[key][1])
self.mdc.alt_tab('mdvj')
#self.mdc.alt_tab('mdvj')

def type_s(self,key,n):
self.mdc.alt_tab('MilkDrop 2')
Expand All @@ -161,7 +191,7 @@ def type_s(self,key,n):
else:
for _ in range(n):
kp.typer(self.key_to_key[key][1])
self.mdc.alt_tab('mdvj')
#self.mdc.alt_tab('mdvj')

def type_p(self,key,n):
if n == 127:
Expand All @@ -172,6 +202,7 @@ def type_p(self,key,n):
def type_pad(self,lr,pad,n):
if n == 127:
self.select_pad(lr,pad)
# return ['pad',[lr, pad]]

def load(self,fname):
if os.path.exists(fname):
Expand All @@ -190,9 +221,8 @@ def load(self,fname):
print(o,'failed to load')
# print(self.key_to_key)
# print(self.key_to_fun)
# return int(Config.get('IO','Input ID'))
self.MC.MC.set_inp(int(Config.get('IO','Input ID')))
self.MC.start()
return int(Config.get('IO','Input ID'))


#if __name__ == '__main__':
# Config = configparser.RawConfigParser()
Expand Down
26 changes: 16 additions & 10 deletions v0.5.0/mdvj.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def __init__(self,root,path="C:/Program Files (x86)/Winamp/plugins/milkdrop2/pre
self.padframe = tk.Frame(self.frame)

self.control = Controller(self)
self.control.load('Twitch.ini') # for testing purposes
inp = self.control.load('Twitch.ini') # for testing purposes
self.control.midi_start(inp)

self.padgroup_l_n = -1
self.padgroup_r_n = -1
Expand All @@ -46,8 +47,8 @@ def __init__(self,root,path="C:/Program Files (x86)/Winamp/plugins/milkdrop2/pre


self.controlframe = tk.Frame(self.frame)
self.prev_col_button = tk.Button(self.controlframe,text="<",command=self.go_l,pady=0)
self.next_col_button = tk.Button(self.controlframe,text=">",command=self.go_r,pady=0)
self.prev_col_button = tk.Button(self.controlframe,text="<",command=lambda: self.control.go_lr(0),pady=0)
self.next_col_button = tk.Button(self.controlframe,text=">",command=lambda: self.control.go_lr(1),pady=0)
self.check_lr()
self.next_col_button.pack(side=tk.RIGHT,anchor=tk.SE)
self.prev_col_button.pack(side=tk.RIGHT,anchor=tk.SE)
Expand All @@ -70,20 +71,24 @@ def check_lr(self):
self.next_col_button.config(state='disabled')

def go_l(self):
self.padgroup_l.init_containers(self.db[self.padgroup_l_n-1])
self.padgroup_r.init_containers(self.db[self.padgroup_r_n-1])
self.control.go_lr(0)
self.control.MC.pause()
self.padgroup_l_n -= 1
self.padgroup_r_n -= 1
self.padgroup_l.init_containers(self.db[self.padgroup_l_n])
self.padgroup_r.init_containers(self.db[self.padgroup_r_n])
self.root.update_idletasks()
self.check_lr()
self.control.MC.resume()

def go_r(self):
self.padgroup_l.init_containers(self.db[self.padgroup_l_n+1])
self.padgroup_r.init_containers(self.db[self.padgroup_r_n+1])
self.control.go_lr(1)
self.control.MC.pause()
self.padgroup_l_n += 1
self.padgroup_r_n += 1
self.padgroup_l.init_containers(self.db[self.padgroup_l_n])
self.padgroup_r.init_containers(self.db[self.padgroup_r_n])
self.root.update_idletasks()
self.check_lr()
self.control.MC.resume()

def quit(self):
#self.root.destroy()
Expand Down Expand Up @@ -136,6 +141,7 @@ def change_text(self,new_text):

def select(self,event=None):
self.padgroup.parent.control.select_pad(*self.coords)
#self.padgroup.parent.control.select_pad_gui(*self.coords)
#self.selected()

def selected(self):
Expand All @@ -162,7 +168,7 @@ def main():
root = tk.Tk()
root.title("mdvj")
root.resizable(0,0)
gui = MainGui(root)#,"C:/Code/python/mdvj/v0.5.0/scrot") # fake data
gui = MainGui(root,"C:/Code/python/mdvj/v0.5.0/scrot") # fake data
root.mainloop()

if __name__ == '__main__':
Expand Down
34 changes: 23 additions & 11 deletions v0.5.0/midi_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ def __init__(self):
pygame.midi.init()
self.inp = None #pygame.midi.Input(pygame.midi.get_default_input_id())

def set_inp(self,inp):
try:
self.inp = pygame.midi.Input(inp)
except:
self.inp = pygame.midi.Input(pygame.midi.get_default_input_id())
def set_inp(self,inp=None):
if inp:
try:
self.inp = pygame.midi.Input(inp)
return
except:
pass
self.inp = pygame.midi.Input(pygame.midi.get_default_input_id())

def collect_device_info(self): # using this can construct list of inputs/outputs and have their corresponding channels
inputs = {}
Expand Down Expand Up @@ -78,7 +81,7 @@ def __init__(self, gui, mc=None, refresh_int=25):
# Set up the GUI part
self.gui = gui # setup elsewhere, needs to have a set_queue fxn and a processIncoming fxn
self.gui.set_queue(self.queue)

self.run_periodic = None


def start(self,action=None):
Expand All @@ -96,17 +99,29 @@ def start(self,action=None):
# anything
self.periodicCall()

def pause(self):
self.gui.master.after_cancel(self.run_periodic)

def periodicCall(self):
"""
Check every 100 ms if there is something new in the queue.
Check every _ ms if there is something new in the queue.
"""
self.gui.processIncoming()
if not self.running:
# maybe gui.quit
self.gui.master.after_cancel(self.run_periodic)
self.MC.quit()
self.gui.quit()
self.gui.master.destroy()
self.gui.master.after(self.refresh_int, self.periodicCall)
self.run_periodic = self.gui.master.after(self.refresh_int, self.periodicCall)

def pause(self):
if self.run_periodic:
self.gui.master.after_cancel(self.run_periodic)

def resume(self):
if self.run_periodic:
self.run_periodic = self.gui.master.after(self.refresh_int, self.periodicCall)

def workerThread1(self,queue):
"""
Expand All @@ -116,9 +131,6 @@ def workerThread1(self,queue):
control.
"""
while self.running:
# To simulate asynchronous I/O, we create a random number at
# random intervals. Replace the following 2 lines with the real
# thing.

msg = self.MC.test_inp()
if msg:
Expand Down

0 comments on commit 212528e

Please sign in to comment.