# Path: mididings/extra/osc.py
# class OSCInterface(object):
#     """
#     Allows controlling mididings via OSC.
# 
#     :param port:
#         the OSC port to listen on.
# 
#     :param notify_ports:
#         a list of OSC ports to notify when the current scene changes.
# 
#     These messages are currently understood:
# 
#     - **/mididings/switch_scene ,i**: switch to the given scene number.
#     - **/mididings/switch_subscene ,i**: switch to the given subscene number.
#     - **/mididings/prev_scene**: switch to the previous scene.
#     - **/mididings/next_scene**: switch to the next scene.
#     - **/mididings/prev_subscene**: switch to the previous subscene.
#     - **/mididings/next_subscene**: switch to the next subscene.
#     - **/mididings/panic**: send all-notes-off on all channels and on all
#       output ports.
#     - **/mididings/quit**: terminate mididings.
#     """
#     def __init__(self, port=56418, notify_ports=[56419]):
#         self.port = port
#         if _misc.issequence(notify_ports):
#             self.notify_ports = notify_ports
#         else:
#             self.notify_ports = [notify_ports]
# 
#     def on_start(self):
#         if self.port is not None:
#             self.server = _liblo.ServerThread(self.port)
#             self.server.register_methods(self)
#             self.server.start()
# 
#         self.send_config()
# 
#     def on_exit(self):
#         if self.port is not None:
#             self.server.stop()
#             del self.server
# 
#     def on_switch_scene(self, scene, subscene):
#         for p in self.notify_ports:
#             _liblo.send(p, '/mididings/current_scene', scene, subscene)
# 
#     def send_config(self):
#         for p in self.notify_ports:
#             # send data offset
#             _liblo.send(p, '/mididings/data_offset',
#                         _setup.get_config('data_offset'))
# 
#             # send list of scenes
#             _liblo.send(p, '/mididings/begin_scenes')
#             s = _engine.scenes()
#             for n in sorted(s.keys()):
#                 _liblo.send(p, '/mididings/add_scene', n, s[n][0], *s[n][1])
#             _liblo.send(p, '/mididings/end_scenes')
# 
#     @_liblo.make_method('/mididings/query', '')
#     def query_cb(self, path, args):
#         self.send_config()
#         for p in self.notify_ports:
#             _liblo.send(p, '/mididings/current_scene',
#                         _engine.current_scene(), _engine.current_subscene())
# 
#     @_liblo.make_method('/mididings/switch_scene', 'i')
#     @_liblo.make_method('/mididings/switch_scene', 'ii')
#     def switch_scene_cb(self, path, args):
#         _engine.switch_scene(*args)
# 
#     @_liblo.make_method('/mididings/switch_subscene', 'i')
#     def switch_subscene_cb(self, path, args):
#         _engine.switch_subscene(*args)
# 
#     @_liblo.make_method('/mididings/prev_scene', '')
#     def prev_scene_cb(self, path, args):
#         s = sorted(_engine.scenes().keys())
#         n = s.index(_engine.current_scene()) - 1
#         if n >= 0:
#             _engine.switch_scene(s[n])
# 
#     @_liblo.make_method('/mididings/next_scene', '')
#     def next_scene_cb(self, path, args):
#         s = sorted(_engine.scenes().keys())
#         n = s.index(_engine.current_scene()) + 1
#         if n < len(s):
#             _engine.switch_scene(s[n])
# 
#     @_liblo.make_method('/mididings/prev_subscene', '')
#     @_liblo.make_method('/mididings/prev_subscene', 'i')
#     def prev_subscene_cb(self, path, args):
#         s = _engine.scenes()[_engine.current_scene()]
#         n = _util.actual(_engine.current_subscene()) - 1
#         if len(s[1]) and len(args) and args[0]:
#             n %= len(s[1])
#         if n >= 0:
#             _engine.switch_subscene(_util.offset(n))
# 
#     @_liblo.make_method('/mididings/next_subscene', '')
#     @_liblo.make_method('/mididings/next_subscene', 'i')
#     def next_subscene_cb(self, path, args):
#         s = _engine.scenes()[_engine.current_scene()]
#         n = _util.actual(_engine.current_subscene()) + 1
#         if len(s[1]) and len(args) and args[0]:
#             n %= len(s[1])
#         if n < len(s[1]):
#             _engine.switch_subscene(_util.offset(n))
# 
#     @_liblo.make_method('/mididings/panic', '')
#     def panic_cb(self, path, args):
#         _panic._panic_bypass()
# 
#     @_liblo.make_method('/mididings/restart', '')
#     def restart_cb(self, path, args):
#         _engine.restart()
# 
#     @_liblo.make_method('/mididings/quit', '')
#     def quit_cb(self, path, args):
#         _engine.quit()


from mididings import *
from mididings.extra.osc import OSCInterface
#
# router.py - A simple OSC-controlled MIDI router that sends all incoming
# events to the output port/channel determined by the current scene/subscene.
#
# To route the output to a different MIDI port/channel, send an OSC message
# /mididings/switch_scene <port> <channel>
# to UDP port 56418.
#
# For example, using the send_osc command from pyliblo:
# $ send_osc 56418 /mididings/switch_scene 13 1
#


NPORTS = 16

config(
    out_ports=NPORTS,
)

hook(

# Path: mididings/extra/osc.py
# class OSCInterface(object):
#     """
#     Allows controlling mididings via OSC.
# 
#     :param port:
#         the OSC port to listen on.
# 
#     :param notify_ports:
#         a list of OSC ports to notify when the current scene changes.
# 
#     These messages are currently understood:
# 
#     - **/mididings/switch_scene ,i**: switch to the given scene number.
#     - **/mididings/switch_subscene ,i**: switch to the given subscene number.
#     - **/mididings/prev_scene**: switch to the previous scene.
#     - **/mididings/next_scene**: switch to the next scene.
#     - **/mididings/prev_subscene**: switch to the previous subscene.
#     - **/mididings/next_subscene**: switch to the next subscene.
#     - **/mididings/panic**: send all-notes-off on all channels and on all
#       output ports.
#     - **/mididings/quit**: terminate mididings.
#     """
#     def __init__(self, port=56418, notify_ports=[56419]):
#         self.port = port
#         if _misc.issequence(notify_ports):
#             self.notify_ports = notify_ports
#         else:
#             self.notify_ports = [notify_ports]
# 
#     def on_start(self):
#         if self.port is not None:
#             self.server = _liblo.ServerThread(self.port)
#             self.server.register_methods(self)
#             self.server.start()
# 
#         self.send_config()
# 
#     def on_exit(self):
#         if self.port is not None:
#             self.server.stop()
#             del self.server
# 
#     def on_switch_scene(self, scene, subscene):
#         for p in self.notify_ports:
#             _liblo.send(p, '/mididings/current_scene', scene, subscene)
# 
#     def send_config(self):
#         for p in self.notify_ports:
#             # send data offset
#             _liblo.send(p, '/mididings/data_offset',
#                         _setup.get_config('data_offset'))
# 
#             # send list of scenes
#             _liblo.send(p, '/mididings/begin_scenes')
#             s = _engine.scenes()
#             for n in sorted(s.keys()):
#                 _liblo.send(p, '/mididings/add_scene', n, s[n][0], *s[n][1])
#             _liblo.send(p, '/mididings/end_scenes')
# 
#     @_liblo.make_method('/mididings/query', '')
#     def query_cb(self, path, args):
#         self.send_config()
#         for p in self.notify_ports:
#             _liblo.send(p, '/mididings/current_scene',
#                         _engine.current_scene(), _engine.current_subscene())
# 
#     @_liblo.make_method('/mididings/switch_scene', 'i')
#     @_liblo.make_method('/mididings/switch_scene', 'ii')
#     def switch_scene_cb(self, path, args):
#         _engine.switch_scene(*args)
# 
#     @_liblo.make_method('/mididings/switch_subscene', 'i')
#     def switch_subscene_cb(self, path, args):
#         _engine.switch_subscene(*args)
# 
#     @_liblo.make_method('/mididings/prev_scene', '')
#     def prev_scene_cb(self, path, args):
#         s = sorted(_engine.scenes().keys())
#         n = s.index(_engine.current_scene()) - 1
#         if n >= 0:
#             _engine.switch_scene(s[n])
# 
#     @_liblo.make_method('/mididings/next_scene', '')
#     def next_scene_cb(self, path, args):
#         s = sorted(_engine.scenes().keys())
#         n = s.index(_engine.current_scene()) + 1
#         if n < len(s):
#             _engine.switch_scene(s[n])
# 
#     @_liblo.make_method('/mididings/prev_subscene', '')
#     @_liblo.make_method('/mididings/prev_subscene', 'i')
#     def prev_subscene_cb(self, path, args):
#         s = _engine.scenes()[_engine.current_scene()]
#         n = _util.actual(_engine.current_subscene()) - 1
#         if len(s[1]) and len(args) and args[0]:
#             n %= len(s[1])
#         if n >= 0:
#             _engine.switch_subscene(_util.offset(n))
# 
#     @_liblo.make_method('/mididings/next_subscene', '')
#     @_liblo.make_method('/mididings/next_subscene', 'i')
#     def next_subscene_cb(self, path, args):
#         s = _engine.scenes()[_engine.current_scene()]
#         n = _util.actual(_engine.current_subscene()) + 1
#         if len(s[1]) and len(args) and args[0]:
#             n %= len(s[1])
#         if n < len(s[1]):
#             _engine.switch_subscene(_util.offset(n))
# 
#     @_liblo.make_method('/mididings/panic', '')
#     def panic_cb(self, path, args):
#         _panic._panic_bypass()
# 
#     @_liblo.make_method('/mididings/restart', '')
#     def restart_cb(self, path, args):
#         _engine.restart()
# 
#     @_liblo.make_method('/mididings/quit', '')
#     def quit_cb(self, path, args):
#         _engine.quit()


# Path: doc/examples/router.py
from mididings import *
from mididings.extra.osc import OSCInterface

#
# router.py - A simple OSC-controlled MIDI router that sends all incoming
# events to the output port/channel determined by the current scene/subscene.
#
# To route the output to a different MIDI port/channel, send an OSC message
# /mididings/switch_scene <port> <channel>
# to UDP port 56418.
#
# For example, using the send_osc command from pyliblo:
# $ send_osc 56418 /mididings/switch_scene 13 1
#


NPORTS = 16

config(
    out_ports=NPORTS,
)

hook(

    OSCInterface(56418, 56419),