Created
February 9, 2018 17:56
-
-
Save MaurizioB/260d831fb5ebcc666130368e027c1529 to your computer and use it in GitHub Desktop.
Mididings scene switch callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
Two alternatives for scene switch callback. | |
1. Using a custom made Hook() object, which responds only *after* a scene | |
change has been made. This means that if a scene switch request contains | |
a non existing scene/subscene, nothing happens. | |
2. Rewriting the private Engine.scene_switch_callback function, which | |
intercepts scene switch requests and can do some further actions before | |
the change is actually made. | |
This example uses a "Control" port and changes scene according to the octave | |
of a NOTEON event. The event is then discarded here, which is not required; if | |
you want to keep it, remember to reroute if there are less outputs than inputs. | |
''' | |
from mididings import * | |
from mididings.engine import Engine, current_scene, current_subscene, switch_scene | |
in_ports = ( | |
('Some keyboard', ), | |
('Control', ), | |
) | |
config( | |
backend='alsa', | |
client_name='Chatty router', | |
in_ports=in_ports, | |
silent=True, | |
) | |
scene_dict = {s: Pass() for s in range(1, 5)} | |
class Messenger(object): | |
def __init__(self): | |
self.scene = 0 | |
self.subscene = 0 | |
self.on_switch_scene = self.startup_switch_scene | |
def on_start(self): | |
print('Mididings started!') | |
def startup_switch_scene(self, *args): | |
#Startup callback showing "default" scene info; | |
#scene and subscene are 1 index based | |
self.scene = current_scene() + 1 | |
self.subscene = current_subscene() + 1 | |
print('Start scene: {}\nStart subscene: {}\n'.format(self.scene, self.subscene)) | |
self.on_switch_scene = self.real_switch_scene | |
def real_switch_scene(self, scene, subscene): | |
#Do something when a scene/subscene has changed; here we show a message | |
#only when a scene/subscene actually changes | |
if scene != self.scene: | |
print('Scene changed from {} to {}'.format(self.scene, scene)) | |
elif subscene != self.subscene: | |
print('Subscene changed from {} to {}'.format(self.subscene, subscene)) | |
self.scene = scene | |
self.subscene = subscene | |
started = False | |
old_scene = 0 | |
old_subscene = 0 | |
def scene_switch_callback(cb, engine, scene, subscene): | |
#This is called *before* trying to change the scene | |
global started | |
global old_scene | |
global old_subscene | |
#call the original Engine() class method | |
cb(engine, scene, subscene) | |
if not started: | |
print('Mididings started!\nCurrent scene: {}\nCurrent subscene: {}'.format(old_scene + 1, old_subscene + 1)) | |
started = True | |
else: | |
#Do something before trying to change the scene | |
if old_scene != scene: | |
print('Scene changing from {} to {}'.format(old_scene + 1, scene + 1)) | |
elif old_subscene != subscene: | |
print('Subscene changing from {} to {}'.format(old_subscene, current_subscene())) | |
old_scene = scene | |
old_subscene = subscene | |
#Uncomment to use the hook class | |
#hook(Messenger()) | |
#Uncomment to use method renaming | |
#Engine.scene_switch_callback = lambda engine, scene, subscene, cb=Engine.scene_switch_callback: scene_switch_callback(cb, engine, scene, subscene) | |
def switch(event): | |
switch_scene(event.note // 12) | |
return(event) | |
run(scenes=scene_dict, control=PortFilter('Control')>>Filter(NOTEON)>>Process(switch)>>Discard()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment