[go: nahoru, domu]

Skip to content

Commit

Permalink
Update chord2code.py
Browse files Browse the repository at this point in the history
added names to keys
misc fixes, 
better datatype typing
  • Loading branch information
tripstych committed Jun 27, 2023
1 parent ccee76b commit ddb958c
Showing 1 changed file with 64 additions and 52 deletions.
116 changes: 64 additions & 52 deletions chord2code.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@
SETTINGS_PATH = './settings.cfg'


INTRO_TEXT ="""Chord2Key::
Select your MIDI Device from the inputs
Select an Output - required for passthru -
INTRO_TEXT ="""::Chord2Key::
Select & connect a MIDI device,
capture notes or chords and assign a
list of keystrokes / scancodes
to have them playback.
Turn the MIDI capture off when
you're done, Playback mode is
disabled until then.
Use [MIDI Capture] to set the MIDI note/chord to record,
or use the GUI keys to toggle the keys you want as your trip.
Alternatively, use the GUI keyboard keys
to toggle the keys you want as your trigger.
Use the [Keyboard Capture] button, or press
an assigned keyboard key, to start and stop
recording keystrokes. The currently assigned
hotkey is shown in the status bar above.
Use [Keyboard Capture] to record keystrokes for your selcection.
"""

Expand Down Expand Up @@ -64,10 +73,10 @@ def __init__(self):
sg.theme('darkblue')
#sg.set_options(button_color = "#ffffff")
pygame.init()

bwKeyNames = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B']
# make a button and store it
def pianoKeyBtn(text = "", key = None, size=None, button_color = None):
self.pianoKeys[key] = sg.Button('', key = f"^{key}", size=size, button_color = button_color)
self.pianoKeys[key] = sg.Button(bwKeyNames[key],key = f"^{key}", size=size, button_color = button_color)
return self.pianoKeys[key]

# make a button and store it
Expand Down Expand Up @@ -181,7 +190,6 @@ def scanCodeBtn(val, key = None, button_color = None, size=None):




#self.disconnectBtn = sg.Button ( _('Disconnect'),k='_disconnect',disabled=True)


Expand Down Expand Up @@ -222,7 +230,7 @@ def scanCodeBtn(val, key = None, button_color = None, size=None):


self.statusBar = sg.Text(self.statusBarText())
self.verbLines = sg.Multiline(INTRO_TEXT,size=(50,50),write_only=True, reroute_stdout=True)
self.verbLines = sg.Multiline(INTRO_TEXT,font="Serif 12",autoscroll=True,text_color="#ffffff",size=(50,50),write_only=True, reroute_stdout=True)

pksFrame = sg.Frame(
'',
Expand Down Expand Up @@ -264,6 +272,7 @@ def scanCodeBtn(val, key = None, button_color = None, size=None):
]]

self.factory((layout,outlayout))


def factory(self,box):
(layout,outlayout)=box
Expand All @@ -277,6 +286,10 @@ def factory(self,box):
#factory gears - gloo for the view
while True:
event, values = window.read()

if event == sg.WIN_CLOSED:
break

self.inTheLoop = True
if values is None:
continue
Expand All @@ -292,45 +305,42 @@ def factory(self,box):
self.statusBar.update(self.statusBarText())

# self.keystrokeList.update(values=self.keystrokes)

sEvent = F"{event}"
if sEvent.isnumeric():
verbox("DEFUNC?")
#self.scanCodeSet(sEvent)
continue

if event == sg.WIN_CLOSED:
break
else:
sEvent = F"{event}"
if sEvent.isnumeric():
verbox("DEFUNC?")
#self.scanCodeSet(sEvent)
continue

if sEvent[0]=='^':
sEvent = sEvent[1:]
nEvent = int(sEvent)
self.midiBtnPress(nEvent)
continue
if sEvent[0]=='^':
sEvent = sEvent[1:]
nEvent = int(sEvent)
self.midiBtnPress(nEvent)
continue

sEvent = sEvent[1:] if sEvent[0]=='_' else sEvent
if hasattr(self,sEvent):
#my microFhactory[tm]}:'P
#object to fill with options passed to function named by its key/ the event value strignified sEvent
dic = {}
for k, v in values.items():
k=F"{k}"
#buid a dict for the named func call
if type(v) == type(""):
spill = v.split('::')
#for midi list/option values
if k[0] == '_':
dic[ k[1:] ] = v
if len(spill) > 1:
if spill[0].isnumeric():
spill[0] = int(spill[0])
dic[ k[1:] ] = spill[0]

#get the method / 'attribute' from this class
attr = getattr(self,sEvent)
if attr:
attr(dic)

sEvent = sEvent[1:] if sEvent[0]=='_' else sEvent
if hasattr(self,sEvent):
#my microFhactory[tm]}:'P
#object to fill with options passed to function named by its key/ the event value strignified sEvent
dic = {}
for k, v in values.items():
k=F"{k}"
#buid a dict for the named func call
if type(v) == type(""):
spill = v.split('::')
#for midi list/option values
if k[0] == '_':
dic[ k[1:] ] = v
if len(spill) > 1:
if spill[0].isnumeric():
spill[0] = int(spill[0])
dic[ k[1:] ] = spill[0]

#get the method / 'attribute' from this class
attr = getattr(self,sEvent)
if attr:
attr(dic)

window.close()

Expand Down Expand Up @@ -428,6 +438,7 @@ def midiBtnPress(self,nEvent):

def midiThru(self,event):
self.midiThruState = not self.midiThruState
verbox("Send recognized keys/chords through MIDI Out." if self.midiThruState else "Silence recognized keys/chords from MIDI Out.")
self.thruBtn.update(button_color = ACTIVE_COLOR if self.midiThruState else sg.DEFAULT_BUTTON_COLOR)

def recMidiToggle(self,event):
Expand Down Expand Up @@ -514,7 +525,7 @@ def connect(self,values = False):
self.daemon = MTKB_Daemon(Client = self)

if not values:
print("no values!",values)
#print("no values!",values)
return

inPort = values.get('midiIn')
Expand Down Expand Up @@ -552,14 +563,14 @@ def connect(self,values = False):
midi.quit()

#now, start daemon
verbox(F"MIDI connect initiated.")
verbox(F"ignition ignitioned.")

self.daemon.start( (int(inPort),int(outPort)) )


def disconnect(self):
if self.daemon:
#verbox("disconnect")
verbox("stopping MIDI daemon")
self.daemon.stop()

def getMidiDevices(self):
Expand Down Expand Up @@ -600,7 +611,7 @@ def start(self,ioPorts):
#don't start what you haven't called destruct on
return

verbox("starting MIDI daemon")
verbox("starting MIDI service.")

if not midi.get_init():
midi.quit()
Expand All @@ -617,6 +628,7 @@ def start(self,ioPorts):
self.midiOut = midi.Output( midiOutPort )
self.thread.start()
#debuggery("daemon@start:thread.start invoked")
verbox("Polling for MIDI note events.")
verbox(F"In::{midiInPort} Out::{midiOutPort} initialised :{midi.get_init()}")

def stop(self):
Expand All @@ -627,7 +639,7 @@ def stop(self):


def poll(self):
verbox("started polling.")
verbox("started MIDI service, polling.")
keysDown = 0
notes = []

Expand Down

0 comments on commit ddb958c

Please sign in to comment.