def timeSignatureToMidiEvents(ts):
    '''Translate a m21 TiemSignature to a pair of events, including a DeltaTime.

    >>> from music21 import *
    >>> ts = meter.TimeSignature('5/4')
    >>> eventList = timeSignatureToMidiEvents(ts)
    >>> eventList[0]
    <MidiEvent DeltaTime, t=0, track=None, channel=None>
    >>> eventList[1]
    <MidiEvent TIME_SIGNATURE, t=None, track=None, channel=1, data='\\x05\\x02\\x18\\x08'>
    '''
    mt = None # use a midi track set to None

    eventList = []

    dt = midiModule.DeltaTime(mt)
    dt.time = 0 # set to zero; will be shifted later as necessary
    # add to track events
    eventList.append(dt)

    n = ts.numerator
    # need log base 2 to solve for exponent of 2
    # 1 is 0, 2 is 1, 4 is 2, 16 is 4, etc
    d = int(math.log(ts.denominator, 2))
    metroClick = 24 # clock signals per click, clicks are 24 per quarter
    subCount = 8 # number of 32 notes in a quarternote

    me = midiModule.MidiEvent(mt)
    me.type = "TIME_SIGNATURE"
    me.channel = 1
    me.time = None # not required
    me.data = midiModule.putNumbersAsList([n, d, metroClick, subCount])
    eventList.append(me)

    return eventList 
def keySignatureToMidiEvents(ks):
    '''Convert a single MIDI event into a music21 TimeSignature object.

    >>> from music21 import key
    >>> ks = key.KeySignature(2)
    >>> ks
    <music21.key.KeySignature of 2 sharps>
    >>> eventList = keySignatureToMidiEvents(ks)
    >>> eventList[1]
    <MidiEvent KEY_SIGNATURE, t=None, track=None, channel=1, data='\\x02\\x00'>

    >>> ks = key.KeySignature(-5)
    >>> ks
    <music21.key.KeySignature of 5 flats>
    >>> eventList = keySignatureToMidiEvents(ks)
    >>> eventList[1]
    <MidiEvent KEY_SIGNATURE, t=None, track=None, channel=1, data='\\xfb\\x00'>
    '''

    mt = None # use a midi track set to None

    eventList = []

    dt = midiModule.DeltaTime(mt)
    dt.time = 0 # set to zero; will be shifted later as necessary
    # add to track events
    eventList.append(dt)


    sharpCount = ks.sharps
    if ks.mode == 'minor':        
        mode = 1
    else: # major or None; must define one
        mode = 0

    me = midiModule.MidiEvent(mt)
    me.type = "KEY_SIGNATURE"
    me.channel = 1
    me.time = None # not required
    me.data = midiModule.putNumbersAsList([sharpCount, mode])
    eventList.append(me)

    return eventList