# Path: mididings/extra/per_channel.py
# class PerChannel(object):
#     """
#     Utility class, usable with Process() and Call(), that delegates events
#     to different instances of the same class, using one instance per
#     channel/port combination.
#     New instances are created as needed, using the given factory function,
#     when the first event with a new channel/port arrives.
#     """
#     def __init__(self, factory):
#         self.channel_proc = {}
#         self.factory = factory
# 
#     def __call__(self, ev):
#         k = (ev.port, ev.channel)
#         if k not in self.channel_proc:
#             self.channel_proc[k] = self.factory()
#         return self.channel_proc[k](ev)


import mididings as _m
import mididings.event as _event
from mididings.extra.per_channel import PerChannel as _PerChannel
            else:
                return None


class _MakeMonophonic(object):
    def __init__(self):
        self.notes = []

    def __call__(self, ev):
        if ev.type == _m.NOTEON:
            if len(self.notes):
                # send note off for previous note, and note on for current note
                noteoff = _event.NoteOffEvent(ev.port, ev.channel,
                                              self.notes[-1][0], 0)
                self.notes.append((ev.note, ev.velocity))
                return [noteoff, ev]
            else:
                # send note on for current note
                self.notes.append((ev.note, ev.velocity))
                return ev

        elif ev.type == _m.NOTEOFF:
            if len(self.notes) and ev.note == self.notes[-1][0]:
                # note off for currently sounding note
                if len(self.notes) == 1:
                    # only one note left, send note off
                    r = ev
                else:
                    # send note off, and retrigger previous note
                    noteon = _event.NoteOnEvent(ev.port, ev.channel,

# Path: mididings/extra/per_channel.py
# class PerChannel(object):
#     """
#     Utility class, usable with Process() and Call(), that delegates events
#     to different instances of the same class, using one instance per
#     channel/port combination.
#     New instances are created as needed, using the given factory function,
#     when the first event with a new channel/port arrives.
#     """
#     def __init__(self, factory):
#         self.channel_proc = {}
#         self.factory = factory
# 
#     def __call__(self, ev):
#         k = (ev.port, ev.channel)
#         if k not in self.channel_proc:
#             self.channel_proc[k] = self.factory()
#         return self.channel_proc[k](ev)


# Path: mididings/extra/polyphony.py
import mididings as _m
import mididings.event as _event
from mididings.extra.per_channel import PerChannel as _PerChannel

            else:
                return None


class _MakeMonophonic(object):
    def __init__(self):
        self.notes = []

    def __call__(self, ev):
        if ev.type == _m.NOTEON:
            if len(self.notes):
                # send note off for previous note, and note on for current note
                noteoff = _event.NoteOffEvent(ev.port, ev.channel,
                                              self.notes[-1][0], 0)
                self.notes.append((ev.note, ev.velocity))
                return [noteoff, ev]
            else:
                # send note on for current note
                self.notes.append((ev.note, ev.velocity))
                return ev

        elif ev.type == _m.NOTEOFF:
            if len(self.notes) and ev.note == self.notes[-1][0]:
                # note off for currently sounding note
                if len(self.notes) == 1:
                    # only one note left, send note off
                    r = ev
                else:
                    # send note off, and retrigger previous note
                    noteon = _event.NoteOnEvent(ev.port, ev.channel,

                                        self.notes[-2][0], self.notes[-2][1])