Esempio n. 1
0
def build_data(preset, encoder=None):
    """ Build Sysex preset data to given encoder instance
    Returns the length of newly-built data.
    If encoder is None, create a throw-away instance of the encoder, useful for
    calculating data length in caller
    """
    if encoder is None:
        encoder = SyxEncoder()

    rotaries = structs.build_rotaries()
    longsliders = structs.build_longsliders()
    hsliders = structs.build_hsliders()
    vsliders = structs.build_vsliders()

    # pads, sliders, buttons, etc.
    for i in range(16):
        _unwrap(encoder, preset[structs.PADS]["Pad%s" % i], structs.pads)

    for i in range(2):
        _unwrap(encoder, preset[structs.ROTARIES]["Rotary%s" % i], rotaries)

    _unwrap(encoder, preset[structs.LONG_SLIDERS]["LongSlider0"], longsliders)

    for i in range(4):
        _unwrap(encoder, preset[structs.H_SLIDERS]["HSlider%s" % i], hsliders)

    for i in range(4):
        _unwrap(encoder, preset[structs.V_SLIDERS]["VSlider%s" % i], vsliders)

    for i in range(4):
        _unwrap(encoder, preset[structs.LR_BUTTONS]["LeftRightButton%s" % i], structs.lrswitches)

    for i in range(2):
        _unwrap(encoder, preset[structs.UD_BUTTONS]["UpDownButton%s" % i], structs.udswitches)

    _unwrap(encoder, preset[structs.RHOMBUS_BUTTONS]["RhombusButton0"], structs.rhswitches)

    for i in range(3):
        _unwrap(encoder, preset[structs.TRANSPORT_BUTTONS]["TransportButton%s" % i], structs.tbuttons)

    _unwrap(encoder, preset[structs.MODE_BUTTONS]["ModeButton0"], structs.mswitches)

    # globals
    for path in structs.globalpaths:
        x = preset
        for item in path:
            x = x[item]
        encoder.sum_encode(x)

    # special case: velocity table
    for velocity in preset[structs.PADS]['padVelocityTable']:
        encoder.sum_encode(velocity)

    data_length = encoder.counter
    return data_length
Esempio n. 2
0
def json2syx(preset, num):
    """ Encode json-format preset to SysEx dump for preset number 'num'.
    """
    rotaries = structs.build_rotaries()
    longsliders = structs.build_longsliders()
    hsliders = structs.build_hsliders()
    vsliders = structs.build_vsliders()

    #
    # Preamble
    #
    data = SyxEncoder()
    data.start()
    for magic in syx_ident_magic:
        data.write(magic)
    # pad with zeroes until offset 0x21
    for i in range(13*2):
        data.write(0)

    data.start_packet()
    data.crc_init()
    data.chunk_init()

    data.encode_crc(0x00)   # TYPE_MSB
    data.encode_crc(0x02)   # TYPE_LSB
    # ID: 0x1110 = firmware, 0x2220 = preset data
    # 30xx = load preset
    data.encode_crc(0x22)
    data.encode_crc(0x20)

    data.encode_crc_int()
    data.flush()

    #
    # Preset header
    #
    data.reset_sum_byte()
    data.encode(syx_preset_start)
    data.encode(num)
    data_length = build_data(preset, encoder=None)

    syx_preset_length_msb = (data_length >> 8) & 0xff
    syx_preset_length_lsb = data_length & 0xff
    data.encode(syx_preset_length_msb)
    data.encode(syx_preset_length_lsb)
    data.crc_init()

    #
    # Preset data
    #
    build_data(preset, encoder=data)

    data.encode(num) # index of this preset
    data.encode(num) # index of last preset in this SysEx
    data.encode_sum_byte()
    data.flush()

    data.stop()

    #print "data_length=%s, msb=%s, lsb=%s" % (data_length, hex(syx_preset_length_msb), hex(syx_preset_length_lsb))

    return ''.join(struct.pack("B", x) for x in data._buf)