import random
from enum import IntEnum

from Utils import int16_as_bytes, snes_to_pc


class SFX(object):
    def __init__(self, name, sfx_set, orig_id, addr, chain, accomp=False):
        self.name = name
        self.sfx_set = sfx_set
        self.orig_id = orig_id
        self.addr = addr
        self.chain = chain
        self.accomp = accomp

        self.target_set = None
        self.target_id = None
        self.target_chain = None

class SPCMusicType(IntEnum):
    NONE = 0x00,
    Ambient = 0x01,
    Melody = 0x02,
    Rhythm = 0x04,
    Beat = 0x08

class SFXType(IntEnum):
    NONE = 0x00,
    Ambient = 0x01,
    Melody = 0x02,
    Beat = 0x04,
    Short = 0x08,
    Long = 0x10,
    Soft = 0x20,
    Hard = 0x40,
    ALL = 0xFF

class Instrument(object):
    def __init__(self, name, id, srcn, adsr, gain, mult):
        self.name = name
        self.id = id
        self.srcn = srcn
        self.adsr = adsr
        self.gain = gain
        self.mult = mult

        self.primary_type = 0

        self.target_id = None
        self.replacements = []

    def add_type(self, type, force_primary):
        if force_primary or self.type == 0:
            self.primary_type |= type
        self.type |= type
        return self

class SFXInstrument(Instrument):
    def __init__(self, name, id, srcn, adsr, gain, mult):
        Instrument.__init__(self, name, id, srcn, adsr, gain, mult)

        self.type = SFXType.NONE

    def amb(self, force_primary=False): return self.add_type(SFXType.Ambient, force_primary)
    def mel(self, force_primary=False): return self.add_type(SFXType.Melody, force_primary)
    def beat(self, force_primary=False): return self.add_type(SFXType.Beat, force_primary)
    def short(self, force_primary=False): return self.add_type(SFXType.Short, force_primary)
    def long(self, force_primary=False): return self.add_type(SFXType.Long, force_primary)
    def soft(self, force_primary=False): return self.add_type(SFXType.Soft, force_primary)
    def hard(self, force_primary=False): return self.add_type(SFXType.Hard, force_primary)

class SPCInstrument(Instrument):
    def __init__(self, name, srcn, adsr, gain, mult, min=9999, max=0):
        Instrument.__init__(self, name, srcn, srcn, adsr, gain, mult)

        self.type = SPCMusicType.NONE

    def amb(self, force_primary=False): return self.add_type(SPCMusicType.Ambient, force_primary)
    def mel(self, force_primary=False): return self.add_type(SPCMusicType.Melody, force_primary)
    def bass(self, force_primary=False): return self.add_type(SPCMusicType.Rhythm, force_primary)
    def beat(self, force_primary=False): return self.add_type(SPCMusicType.Beat, force_primary)

class InstrumentChange(object):
    def __init__(self, orig_instrument, type, ban=[], inc=[]):
        self.orig_instrument = orig_instrument
        self.type = type
        self.banned_list = ban
        self.include_list = inc
        self.target_instrument = None

class SFXInstrumentChange(InstrumentChange):
    def __init__(self, sfx_set, sfx_id, orig_instrument, addresses, type=SFXType.Ambient | SFXType.Melody | SFXType.Beat, ban=[], inc=[]):
        InstrumentChange.__init__(self, orig_instrument, type, ban, inc)
        self.sfx_set = sfx_set
        self.sfx_id = sfx_id
        self.addresses = addresses

class SPCInstrumentChange(InstrumentChange):
    def __init__(self, song_id, segment_id, tracks, orig_instrument, type=SPCMusicType.NONE, ban=[], inc=[]):
        InstrumentChange.__init__(self, orig_instrument, type, ban, inc)
        self.song_id = song_id
        self.segment_id = segment_id
        self.tracks = tracks


def init_sfx_data():
    sfx_pool = [SFX('Slash1', 0x02, 0x01, 0x2614, []), SFX('Slash2', 0x02, 0x02, 0x2625, []),
                SFX('Slash3', 0x02, 0x03, 0x2634, []), SFX('Slash4', 0x02, 0x04, 0x2643, []),
                SFX('Wall clink', 0x02, 0x05, 0x25DD, []), SFX('Bombable door clink', 0x02, 0x06, 0x25D7, []),
                SFX('Fwoosh shooting', 0x02, 0x07, 0x25B7, []), SFX('Arrow hitting wall', 0x02, 0x08, 0x25E3, []),
                SFX('Boomerang whooshing', 0x02, 0x09, 0x25AD, []), SFX('Hookshot', 0x02, 0x0A, 0x25C7, []),
                SFX('Placing bomb', 0x02, 0x0B, 0x2478, []),
                SFX('Bomb exploding/Quake/Bombos/Exploding wall', 0x02, 0x0C, 0x269C, []),
                SFX('Powder', 0x02, 0x0D, 0x2414, [0x3f]), SFX('Fire rod shot', 0x02, 0x0E, 0x2404, []),
                SFX('Ice rod shot', 0x02, 0x0F, 0x24C3, []), SFX('Hammer use', 0x02, 0x10, 0x23FA, []),
                SFX('Hammering peg', 0x02, 0x11, 0x23F0, []), SFX('Digging', 0x02, 0x12, 0x23CD, []),
                SFX('Flute use', 0x02, 0x13, 0x23A0, [0x3e]), SFX('Cape on', 0x02, 0x14, 0x2380, []),
                SFX('Cape off/Wallmaster grab', 0x02, 0x15, 0x2390, []), SFX('Staircase', 0x02, 0x16, 0x232C, []),
                SFX('Staircase', 0x02, 0x17, 0x2344, []), SFX('Staircase', 0x02, 0x18, 0x2356, []),
                SFX('Staircase', 0x02, 0x19, 0x236E, []), SFX('Tall grass/Hammer hitting bush', 0x02, 0x1A, 0x2316, []),
                SFX('Mire shallow water', 0x02, 0x1B, 0x2307, []), SFX('Shallow water', 0x02, 0x1C, 0x2301, []),
                SFX('Lifting object', 0x02, 0x1D, 0x22BB, []), SFX('Cutting grass', 0x02, 0x1E, 0x2577, []),
                SFX('Item breaking', 0x02, 0x1F, 0x22E9, []), SFX('Item falling in pit', 0x02, 0x20, 0x22DA, []),
                SFX('Bomb hitting ground/General bang', 0x02, 0x21, 0x22CF, []),
                SFX('Pushing object/Armos bounce', 0x02, 0x22, 0x2107, []), SFX('Boots dust', 0x02, 0x23, 0x22B1, []),
                SFX('Splashing', 0x02, 0x24, 0x22A5, [0x3d]), SFX('Mire shallow water again?', 0x02, 0x25, 0x2296, []),
                SFX('Link taking damage', 0x02, 0x26, 0x2844, []), SFX('Fainting', 0x02, 0x27, 0x2252, []),
                SFX('Item splash', 0x02, 0x28, 0x2287, []), SFX('Rupee refill', 0x02, 0x29, 0x243F, [0x3b]),
                SFX('Fire rod shot hitting wall/Bombos spell', 0x02, 0x2A, 0x2033, []),
                SFX('Heart beep/Text box', 0x02, 0x2B, 0x1FF2, []), SFX('Sword up', 0x02, 0x2C, 0x1FD9, [0x3a]),
                SFX('Magic drain', 0x02, 0x2D, 0x20A6, []), SFX('GT opening', 0x02, 0x2E, 0x1FCA, [0x39]),
                SFX('GT opening/Water drain', 0x02, 0x2F, 0x1F47, [0x38]), SFX('Cucco', 0x02, 0x30, 0x1EF1, []),
                SFX('Fairy', 0x02, 0x31, 0x20CE, []), SFX('Bug net', 0x02, 0x32, 0x1D47, []),
                SFX('Teleport2', 0x02, 0x33, 0x1CDC, [], True), SFX('Teleport1', 0x02, 0x34, 0x1F6F, [0x33]),
                SFX('Quake/Vitreous/Zora king/Armos/Pyramid/Lanmo', 0x02, 0x35, 0x1C67, [0x36]),
                SFX('Mire entrance (extends above)', 0x02, 0x36, 0x1C64, [], True),
                SFX('Spin charged', 0x02, 0x37, 0x1A43, []), SFX('Water sound', 0x02, 0x38, 0x1F6F, [], True),
                SFX('GT opening thunder', 0x02, 0x39, 0x1F9C, [], True), SFX('Sword up', 0x02, 0x3A, 0x1FE7, [], True),
                SFX('Quiet rupees', 0x02, 0x3B, 0x2462, [], True), SFX('Error beep', 0x02, 0x3C, 0x1A37, []),
                SFX('Big splash', 0x02, 0x3D, 0x22AB, [], True), SFX('Flute again', 0x02, 0x3E, 0x23B5, [], True),
                SFX('Powder paired', 0x02, 0x3F, 0x2435, [], True),

                SFX('Sword beam', 0x03, 0x01, 0x1A18, []),
                SFX('TR opening', 0x03, 0x02, 0x254E, []), SFX('Pyramid hole', 0x03, 0x03, 0x224A, []),
                SFX('Angry soldier', 0x03, 0x04, 0x220E, []), SFX('Lynel shot/Javelin toss', 0x03, 0x05, 0x25B7, []),
                SFX('BNC swing/Phantom ganon/Helma tail/Arrghus swoosh', 0x03, 0x06, 0x21F5, []),
                SFX('Cannon fire', 0x03, 0x07, 0x223D, []), SFX('Damage to enemy; $0BEX.4=1', 0x03, 0x08, 0x21E6, []),
                SFX('Enemy death', 0x03, 0x09, 0x21C1, []), SFX('Collecting rupee', 0x03, 0x0A, 0x21A9, []),
                SFX('Collecting heart', 0x03, 0x0B, 0x2198, []),
                SFX('Non-blank text character', 0x03, 0x0C, 0x218E, []),
                SFX('HUD heart (used explicitly by sanc heart?)', 0x03, 0x0D, 0x21B5, []),
                SFX('Opening chest', 0x03, 0x0E, 0x2182, []),
                SFX('♪Do do do doooooo♫', 0x03, 0x0F, 0x24B9, [0x3C, 0x3D, 0x3E, 0x3F]),
                SFX('Opening/Closing map (paired)', 0x03, 0x10, 0x216D, [0x3b]),
                SFX('Opening item menu/Bomb shop guy breathing', 0x03, 0x11, 0x214F, []),
                SFX('Closing item menu/Bomb shop guy breathing', 0x03, 0x12, 0x215E, []),
                SFX('Throwing object (sprites use it as well)/Stalfos jump', 0x03, 0x13, 0x213B, []),
                SFX('Key door/Trinecks/Dash key landing/Stalfos Knight collapse', 0x03, 0x14, 0x246C, []),
                SFX('Door closing/OW door opening/Chest opening (w/ $29 in $012E)', 0x03, 0x15, 0x212F, []),
                SFX('Armos Knight thud', 0x03, 0x16, 0x2123, []), SFX('Rat squeak', 0x03, 0x17, 0x25A6, []),
                SFX('Dragging/Mantle moving', 0x03, 0x18, 0x20DD, []),
                SFX('Fireball/Laser shot; Somehow used by Trinexx???', 0x03, 0x19, 0x250A, []),
                SFX('Chest reveal jingle ', 0x03, 0x1A, 0x1E8A, [0x38]),
                SFX('Puzzle jingle', 0x03, 0x1B, 0x20B6, [0x3a]), SFX('Damage to enemy', 0x03, 0x1C, 0x1A62, []),
                SFX('Potion refill/Magic drain', 0x03, 0x1D, 0x20A6, []),
                SFX('Flapping (Duck/Cucco swarm/Ganon bats/Keese/Raven/Vulture)', 0x03, 0x1E, 0x2091, []),
                SFX('Link falling', 0x03, 0x1F, 0x204B, []), SFX('Menu/Text cursor moved', 0x03, 0x20, 0x276C, []),
                SFX('Damage to boss', 0x03, 0x21, 0x27E2, []), SFX('Boss dying/Deleting file', 0x03, 0x22, 0x26CF, []),
                SFX('Spin attack/Medallion swoosh', 0x03, 0x23, 0x2001, [0x39]),
                SFX('OW map perspective change', 0x03, 0x24, 0x2043, []),
                SFX('Pressure switch', 0x03, 0x25, 0x1E9D, []),
                SFX('Lightning/Game over/Laser/Ganon bat/Trinexx lunge', 0x03, 0x26, 0x1E7B, []),
                SFX('Agahnim charge', 0x03, 0x27, 0x1E40, []), SFX('Agahnim/Ganon teleport', 0x03, 0x28, 0x26F7, []),
                SFX('Agahnim shot', 0x03, 0x29, 0x1E21, []),
                SFX('Somaria/Byrna/Ether spell/Helma fire ball', 0x03, 0x2A, 0x1E12, []),
                SFX('Electrocution', 0x03, 0x2B, 0x1DF3, []), SFX('Bees', 0x03, 0x2C, 0x1DC0, []),
                SFX('Milestone, also via text', 0x03, 0x2D, 0x1DA9, [0x37]),
                SFX('Collecting heart container', 0x03, 0x2E, 0x1D5D, [0x35, 0x34]),
                SFX('Collecting absorbable key', 0x03, 0x2F, 0x1D80, [0x33]),
                SFX('Byrna spark/Item plop/Magic bat zap/Blob emerge', 0x03, 0x30, 0x1B53, []),
                SFX('Sprite falling/Moldorm shuffle', 0x03, 0x31, 0x1ACA, []),
                SFX('Bumper boing/Somaria punt/Blob transmutation/Sprite boings', 0x03, 0x32, 0x1A78, []),
                SFX('Jingle (paired $2F→$33)', 0x03, 0x33, 0x1D93, [], True),
                SFX('Depressing jingle (paired $2E→$35→$34)', 0x03, 0x34, 0x1D66, [], True),
                SFX('Ugly jingle (paired $2E→$35→$34)', 0x03, 0x35, 0x1D73, [], True),
                SFX('Wizzrobe shot/Helma fireball split/Mothula beam/Blue balls', 0x03, 0x36, 0x1AA7, []),
                SFX('Dinky jingle (paired $2D→$37)', 0x03, 0x37, 0x1DB4, [], True),
                SFX('Apathetic jingle (paired $1A→$38)', 0x03, 0x38, 0x1E93, [], True),
                SFX('Quiet swish (paired $23→$39)', 0x03, 0x39, 0x2017, [], True),
                SFX('Defective jingle (paired $1B→$3A)', 0x03, 0x3A, 0x20C0, [], True),
                SFX('Petulant jingle (paired $10→$3B)', 0x03, 0x3B, 0x2176, [], True),
                SFX('Triumphant jingle (paired $0F→$3C→$3D→$3E→$3F)', 0x03, 0x3C, 0x248A, [], True),
                SFX('Less triumphant jingle ($0F→$3C→$3D→$3E→$3F)', 0x03, 0x3D, 0x2494, [], True),
                SFX('"You tried, I guess" jingle (paired $0F→$3C→$3D→$3E→$3F)', 0x03, 0x3E, 0x249E, [], True),
                SFX('"You didn\'t really try" jingle (paired $0F→$3C→$3D→$3E→$3F)', 0x03, 0x3F, 0x2480, [], True)]
    return sfx_pool


def shuffle_sfx_data():
    sfx_pool = init_sfx_data()
    sfx_map = {2: {}, 3: {}}
    accompaniment_map = {2: set(), 3: set()}
    candidates = []
    for sfx in sfx_pool:
        sfx_map[sfx.sfx_set][sfx.orig_id] = sfx
        if not sfx.accomp:
            candidates.append((sfx.sfx_set, sfx.orig_id))
        else:
            accompaniment_map[sfx.sfx_set].add(sfx.orig_id)
    chained_sfx = [x for x in sfx_pool if len(x.chain) > 0]

    random.shuffle(candidates)

    # place chained sfx first
    random.shuffle(chained_sfx)
    chained_sfx = sorted(chained_sfx, key=lambda x: len(x.chain), reverse=True)
    for chained in chained_sfx:
        chosen_slot = next(x for x in candidates if len(accompaniment_map[x[0]]) - len(chained.chain) >= 0)
        if chosen_slot is None:
            raise Exception('Something went wrong with sfx chains')
        chosen_set, chosen_id = chosen_slot
        chained.target_set, chained.target_id = chosen_slot
        chained.target_chain = []
        for downstream in chained.chain:
            next_slot = accompaniment_map[chosen_set].pop()
            ds_acc = sfx_map[chained.sfx_set][downstream]
            ds_acc.target_set, ds_acc.target_id = chosen_set, next_slot
            chained.target_chain.append(next_slot)
        candidates.remove(chosen_slot)
        sfx_pool.remove(chained)

    unchained_sfx = [x for x in sfx_pool if not x.accomp]
    # do the rest
    for sfx in unchained_sfx:
        chosen_slot = candidates.pop()
        sfx.target_set, sfx.target_id = chosen_slot

    return sfx_map


sfx_table = {
    2: 0x1A8BD0,
    3: 0x1A8CCC
}

sfx_accompaniment_table = {
    2: 0x1A8C4E,
    3: 0x1A8D4A
}


def randomize_sfx(rom):
    sfx_map = shuffle_sfx_data()

    for shuffled_sfx in sfx_map.values():
        for sfx in shuffled_sfx.values():
            base_address = snes_to_pc(sfx_table[sfx.target_set])
            rom.write_bytes(base_address + (sfx.target_id * 2) - 2, int16_as_bytes(sfx.addr))
            ac_base = snes_to_pc(sfx_accompaniment_table[sfx.target_set])
            last = sfx.target_id
            if sfx.target_chain:
                for chained in sfx.target_chain:
                    rom.write_byte(ac_base + last - 1, chained)
                    last = chained
            rom.write_byte(ac_base + last - 1, 0)


def output_song_data(rom, filepath, outfilebase):
    with open(filepath, 'w') as outfile:
        last_song = 0
        last_segment = -1
        outfile.write(f'{outfilebase}\n')
        if "SFX" in filepath:
            for change in sfx_instrument_changes:
                outfile.write(f'\nSFX{change.sfx_set:1d}.{change.sfx_id:02X} = {rom.read_byte(snes_to_pc(change.addresses[0])):02X}')
        else:
            for change in spc_instrument_changes:
                if last_song != change.song_id:
                    last_song = change.song_id
                    last_segment = change.segment_id
                    outfile.write(f'\nSong{change.song_id:02X}.{change.segment_id:02X}.')
                elif last_segment != change.segment_id:
                    last_segment = change.segment_id
                    outfile.write(f'\n       {change.segment_id:02X}.')
                else:
                    outfile.write(f'\n          ')
                tracks = 8
                for track_id in change.tracks.keys():
                    outfile.write(f'{track_id:01X}')
                    tracks -= 1
                if tracks > 0:
                    outfile.write(' ' * tracks)
                outfile.write(f' = {rom.read_byte(snes_to_pc(next(iter(change.tracks.values()))[0])):02X}')


def prep_instruments(instruments, types):
    for instrument in instruments.values():
        for ins, lst in types.items():
            if instrument.type & ins > 0:
                lst.append(instrument)
    if hasattr(instruments[0], 'primary_type'):
        for instrument in instruments.values():
            for ins, lst in types.items():
                if instrument.primary_type & ins > 0:
                    instrument.replacements += [i for i in lst if i not in instrument.replacements]

def shuffle_instruments(instrument_changes, instruments, types, match_patterns=[0xFF]):
    # randomize each instrument change
    for change in instrument_changes:
        if type(change.type) is list:
            candidates = [instruments[i] for i in change.type]
        elif change.type != 0:
            candidates = []
            for n, match in enumerate(match_patterns):
                cand = []
                for ins, lst in types.items():
                    if match & ins > 0 and (change.type & ins > 0 or (n == 0 and change.type & match == 0)):
                        cand += [i for i in lst if i not in cand]
                if n == 0:
                    if not len(cand):
                        for ins, lst in types.items():
                            if match & ins > 0:
                                cand += [i for i in lst if i not in cand]
                    candidates = cand.copy()
                elif len(cand):
                    candidates = [i for i in cand if i in candidates]
        else:
            candidates = instruments[change.orig_instrument].replacements
        candidates += [instruments[i] for i in change.include_list if instruments[i] not in candidates]
        candidates = [i for i in candidates if i.id not in change.banned_list]
        change.target_instrument = random.choice(candidates).id

def randomize_sfxinstruments(rom):
    # categorize instruments in pools
    ambients, melodies, beats, shorts, longs, softs, hards = [], [], [], [], [], [], []
    inst_lists = {
        SFXType.Ambient: ambients,
        SFXType.Melody: melodies,
        SFXType.Beat: beats,
        SFXType.Short: shorts,
        SFXType.Long: longs,
        SFXType.Soft: softs,
        SFXType.Hard: hards,
    }
    match_patterns = [SFXType.Ambient | SFXType.Melody | SFXType.Beat,
                      SFXType.Short | SFXType.Long,
                      SFXType.Soft | SFXType.Hard]
    prep_instruments(sfx_instruments, inst_lists)
    shuffle_instruments(sfx_instrument_changes, sfx_instruments, inst_lists, match_patterns)

    for change in sfx_instrument_changes:
        for addr in change.addresses:
            rom.write_byte(snes_to_pc(addr), change.target_instrument)

def randomize_songinstruments(rom):
    # categorize instruments in pools
    ambients, melodies, rhythms, beats = [], [], [], []
    inst_lists = {
        SPCMusicType.Ambient: ambients,
        SPCMusicType.Melody: melodies,
        SPCMusicType.Rhythm: rhythms,
        SPCMusicType.Beat: beats
    }
    prep_instruments(spc_instruments, inst_lists)
    shuffle_instruments(spc_instrument_changes, spc_instruments, inst_lists)

    for change in spc_instrument_changes:
        for track_addresses in change.tracks.values():
            for addr in track_addresses:
                rom.write_byte(snes_to_pc(addr), change.target_instrument)


sfx_instruments = { # table @ $1A9C04
    0x00: SFXInstrument("Fwoosh",  0x00, 0x00, [0xF6, 0x6A], 0xB8, 0x03).amb().long().soft(),
    0x01: SFXInstrument("Swish",   0x01, 0x01, [0x8E, 0xE0], 0xB8, 0x02).amb().long().soft(),
    0x02: SFXInstrument("Bomp",    0x02, 0x14, [0xFE, 0x6A], 0xB8, 0x02).beat().short().hard(),
    0x03: SFXInstrument("Ting",    0x03, 0x03, [0xFE, 0xF8], 0xB8, 0x0D).beat().short().hard(),
    0x04: SFXInstrument("Rrrrr",   0x04, 0x04, [0xFE, 0x6A], 0x7F, 0x03).mel().long().hard(),
    0x05: SFXInstrument("Clunk",   0x05, 0x02, [0xFE, 0x6A], 0x7F, 0x03).beat().short().hard(),
    0x06: SFXInstrument("Ching",   0x06, 0x05, [0xFE, 0x6A], 0x70, 0x03).amb().long().hard(),
    0x07: SFXInstrument("Fwomp",   0x07, 0x06, [0xFE, 0x6A], 0x70, 0x03).mel().long().hard(),
    0x08: SFXInstrument("Squee",   0x08, 0x08, [0xFA, 0x6A], 0x70, 0x03).mel().short().soft(),
    0x09: SFXInstrument("Unused",  0x09, 0x06, [0xFE, 0x6A], 0x70, 0x01).mel().long().hard(),
    0x0A: SFXInstrument("Bzzzrt",  0x0A, 0x07, [0xFE, 0x6A], 0x70, 0x05).mel().long().hard(),
    0x0B: SFXInstrument("Brrfft",  0x0B, 0x0B, [0xFE, 0x6A], 0xB8, 0x03).mel().long().hard(),
    0x0C: SFXInstrument("Brrwwww", 0x0C, 0x0C, [0xFE, 0xE0], 0xB8, 0x02).beat().short().hard(),
    0x0D: SFXInstrument("Twee",    0x0D, 0x0D, [0xF9, 0x6E], 0xB8, 0x03).mel().long().hard(),
    0x0E: SFXInstrument("Pwing",   0x0E, 0x0E, [0xFE, 0xF5], 0xB8, 0x07).mel().short().hard(),#?on melody since flute song has a bad note
    0x0F: SFXInstrument("Pling",   0x0F, 0x0F, [0xFE, 0xF5], 0xB8, 0x06).mel().short().hard(),
    0x10: SFXInstrument("Chshtsh", 0x10, 0x01, [0xFE, 0xFC], 0xB8, 0x03).beat().short().soft(),
    0x11: SFXInstrument("Splssh",  0x11, 0x10, [0x8E, 0xE0], 0xB8, 0x03).amb().short().soft(),
    0x12: SFXInstrument("Weewoo",  0x12, 0x08, [0x8E, 0xE0], 0xB8, 0x02).mel().short().soft(),
    0x13: SFXInstrument("Brbrbrb", 0x13, 0x14, [0x8E, 0xE0], 0xB8, 0x02).amb().beat().short().hard(),
    0x14: SFXInstrument("Bwow",    0x14, 0x0A, [0x88, 0xE0], 0xB8, 0x02).mel().long().hard(),
    0x15: SFXInstrument("Uughf",   0x15, 0x17, [0x8E, 0xE0], 0xB8, 0x02).mel().short().hard(),
    0x16: SFXInstrument("Aaaaaa",  0x16, 0x15, [0xFF, 0xE0], 0xB8, 0x04).mel().long().hard(),
    0x17: SFXInstrument("Twing",   0x17, 0x03, [0xDF, 0x11], 0xB8, 0x0F).beat().short().hard(),
    0x18: SFXInstrument("Whooo",   0x18, 0x01, [0x88, 0xE0], 0xB8, 0x01).amb().long().soft()
}

spc_instruments = { # table @ $19FB1C
    0x00: SPCInstrument("Noise",        0x00, [0xFF, 0xE0], 0xB8, 0x0470),
    0x01: SPCInstrument("Rain",         0x01, [0xFF, 0xE0], 0xB8, 0x0790).amb(),
    0x02: SPCInstrument("Timpani",      0x02, [0xFF, 0xE0], 0xB8, 0x09C0).beat(),
    0x03: SPCInstrument("Square Wave",  0x03, [0xFF, 0xE0], 0xB8, 0x0400).bass().mel(),
    0x04: SPCInstrument("Saw Wave",     0x04, [0xFF, 0xE0], 0xB8, 0x0400).bass(),
    0x05: SPCInstrument("Clink",        0x05, [0xFF, 0xE0], 0xB8, 0x0470),
    0x06: SPCInstrument("Wobbly Lead",  0x06, [0xFF, 0xE0], 0xB8, 0x0470).amb(),
    0x07: SPCInstrument("Compound Saw", 0x07, [0xFF, 0xE0], 0xB8, 0x0470),
    0x08: SPCInstrument("Tweet",        0x08, [0xFF, 0xE0], 0xB8, 0x07A0).amb().beat(),
    0x09: SPCInstrument("Strings A",    0x09, [0x8F, 0xE9], 0xB8, 0x01E0).mel().bass(True),
    0x0A: SPCInstrument("Strings B",    0x0A, [0x8A, 0xE9], 0xB8, 0x01E0).mel().bass(True),
    0x0B: SPCInstrument("Trombone",     0x0B, [0xFF, 0xE0], 0xB8, 0x0300).mel().bass(True).beat(),
    0x0C: SPCInstrument("Cymbal",       0x0C, [0xFF, 0xE0], 0xB8, 0x03A0).beat(),
    0x0D: SPCInstrument("Ocarina",      0x0D, [0xFF, 0xE0], 0xB8, 0x0100).bass(),
    0x0E: SPCInstrument("Chimes",       0x0E, [0xFF, 0xEF], 0xB8, 0x0EA0).amb(),
    0x0F: SPCInstrument("Harp",         0x0F, [0xFF, 0xEF], 0xB8, 0x0600).mel().bass(True).beat(),
    0x10: SPCInstrument("Splash",       0x10, [0xFF, 0xE0], 0xB8, 0x03D0).amb().beat(),
    0x11: SPCInstrument("Trumpet",      0x11, [0x8F, 0xE0], 0xB8, 0x0300).mel().bass(True),
    0x12: SPCInstrument("Horn",         0x12, [0x8F, 0xE0], 0xB8, 0x06F0).mel().bass(True),
    0x13: SPCInstrument("Snare A",      0x13, [0xFD, 0xE0], 0xB8, 0x07A0).beat(),
    0x14: SPCInstrument("Snare B",      0x14, [0xFF, 0xE0], 0xB8, 0x07A0).beat(),
    0x15: SPCInstrument("Choir",        0x15, [0xFF, 0xE0], 0xB8, 0x03D0).mel().bass(True),
    0x16: SPCInstrument("Flute",        0x16, [0x8F, 0xE0], 0xB8, 0x0300).mel().bass(True),
    0x17: SPCInstrument("Oof",          0x17, [0xFF, 0xE0], 0xB8, 0x02C0).amb().beat(True),
    0x18: SPCInstrument("Piano",        0x18, [0xFE, 0x8F], 0xB8, 0x06F0).mel().bass(True)
}

Am = SFXType.Ambient
Me = SFXType.Melody
Be = SFXType.Beat
Sf = SFXType.Soft
Hd = SFXType.Hard
Sh = SFXType.Short
Lg = SFXType.Long

sfx_instrument_changes = [
    SFXInstrumentChange(0x01, 0x01, 0x01, [0x1A9A03, 0x1A9A13], type=Am, ban=[0x00, 0x06, 0x11]),  # accompanied $01+02
    SFXInstrumentChange(0x01, 0x03, 0x01, [0x1A9A28, 0x1A9A38], type=Am, ban=[0x00, 0x06, 0x11]),  # accompanied $03+04
    SFXInstrumentChange(0x01, 0x07, 0x13, [0x1A9AEA], type=Am|Be|Hd, inc=[0x08, 0x12]),
    SFXInstrumentChange(0x01, 0x09, 0x18, [0x1A903F, 0x1A906D], type=Am, ban=[0x00, 0x06, 0x11]),  # accompanied $09+0A
    SFXInstrumentChange(0x01, 0x0B, 0x0D, [0x1A8F54, 0x1A8F13], type=Me, ban=[0x0F]),  # accompanied $0B+0C
    SFXInstrumentChange(0x01, 0x0D, 0x0E, [0x1A8EBF, 0x1A8ECE], ban=[0x00, 0x01, 0x06, 0x0C, 0x18]),  # accompanied $0D+0E
    SFXInstrumentChange(0x01, 0x0F, 0x0E, [0x1A8EDD, 0x1A8EEF], ban=[0x06, 0x0C]),  # accompanied $0F+10
    SFXInstrumentChange(0x01, 0x11, 0x16, [0x1A925D, 0x1A9279], type=Me|Be, ban=[0x02, 0x09, 0x0C, 0x13, 0x15]),  # accompanied $11+12
    SFXInstrumentChange(0x01, 0x13, 0x00, [0x1A8E83, 0x1A8E92], ban=[0x10]),  # accompanied $13+14
    SFXInstrumentChange(0x01, 0x15, 0x0A, [0x1A8EA1, 0x1A8EB0], ban=[0x09, 0x10, 0x15]),  # accompanied $15+16
    SFXInstrumentChange(0x01, 0x17, 0x0D, [0x1A8FD5, 0x1A8F94], type=Me, ban=[0x0F]),  # accompanied $17+18

    SFXInstrumentChange(0x02, 0x01, 0x01, [0x1A99C5], type=Am, ban=[0x06], inc=[0x02]),
    SFXInstrumentChange(0x02, 0x02, 0x01, [0x1A99D6], type=Am, ban=[0x06], inc=[0x02]),
    SFXInstrumentChange(0x02, 0x03, 0x02, [0x1A99E5], type=Am, ban=[0x06], inc=[0x02]),
    SFXInstrumentChange(0x02, 0x04, 0x02, [0x1A99F4], type=Am, ban=[0x06], inc=[0x02]),
    SFXInstrumentChange(0x02, 0x05, 0x06, [0x1A998E], ban=[0x01, 0x03, 0x16, 0x17]),
    SFXInstrumentChange(0x02, 0x06, 0x03, [0x1A9986], type=Hd, ban=[0x06, 0x09], inc=[0x12]),
    SFXInstrumentChange(0x02, 0x08, 0x02, [0x1A9994], ban=[0x10, 0x18]),
    SFXInstrumentChange(0x02, 0x08, 0x02, [0x1A999D], type=Me|Lg, inc=[0x02, 0x0C, 0x13]),
    SFXInstrumentChange(0x02, 0x09, 0x00, [0x1A995E], ban=[0x08, 0x13]),
    SFXInstrumentChange(0x02, 0x0A, 0x06, [0x1A9978], ban=[0x00, 0x03, 0x0C, 0x0E, 0x13, 0x14, 0x17, 0x18]),
    SFXInstrumentChange(0x02, 0x0B, 0x02, [0x1A9829], type=Hd, ban=[0x09], inc=[0x12]),
    SFXInstrumentChange(0x02, 0x0C, 0x13, [0x1A9A4D], type=Sh, ban=[0x10]),
    SFXInstrumentChange(0x02, 0x0D, 0x00, [0x1A97C5], type=Am, ban=[0x13], inc=[0x10]),  # accompanied $0D+3F
    SFXInstrumentChange(0x02, 0x0E, 0x00, [0x1A97B5], type=Lg, ban=[0x09, 0x18], inc=[0x13]),
    SFXInstrumentChange(0x02, 0x0F, 0x06, [0x1A9874], type=Lg, ban=[0x18]),
    SFXInstrumentChange(0x02, 0x10, 0x05, [0x1A97AB], ban=[0x00, 0x09, 0x14, 0x18]),
    SFXInstrumentChange(0x02, 0x11, 0x05, [0x1A97A1], ban=[0x00, 0x08, 0x10, 0x18]),
    SFXInstrumentChange(0x02, 0x12, 0x02, [0x1A977E], ban=[0x00, 0x10]),
    SFXInstrumentChange(0x02, 0x12, 0x01, [0x1A9787], ban=[0x10]),
    SFXInstrumentChange(0x02, 0x13, 0x0D, [0x1A9751, 0x1A9766], type=Me, ban=[0x0F]),  # accompanied $13+3E
    SFXInstrumentChange(0x02, 0x14, 0x07, [0x1A9731], type=Hd, ban=[0x09]),
    SFXInstrumentChange(0x02, 0x15, 0x07, [0x1A9741], type=Hd, ban=[0x09]),
    SFXInstrumentChange(0x02, 0x16, 0x10, [0x1A96DD], ban=[0x00, 0x01, 0x06, 0x0C, 0x18]),
    SFXInstrumentChange(0x02, 0x17, 0x10, [0x1A96F5], ban=[0x00, 0x01, 0x06, 0x0C, 0x18]),
    SFXInstrumentChange(0x02, 0x18, 0x10, [0x1A9707], ban=[0x00, 0x01, 0x06, 0x0C, 0x18]),
    SFXInstrumentChange(0x02, 0x19, 0x10, [0x1A971F], ban=[0x00, 0x01, 0x06, 0x0C, 0x18]),
    SFXInstrumentChange(0x02, 0x1A, 0x01, [0x1A96C7], type=Am, ban=[0x00, 0x06, 0x13], inc=[0x08, 0x0F, 0x10, 0x12, 0x15, 0x17]),
    SFXInstrumentChange(0x02, 0x1B, 0x11, [0x1A96B8], type=Am, ban=[0x00, 0x01, 0x06, 0x13], inc=[0x08, 0x0F, 0x10, 0x12, 0x15, 0x17]),
    SFXInstrumentChange(0x02, 0x1C, 0x11, [0x1A96B2], type=Am, ban=[0x00, 0x01, 0x06, 0x13], inc=[0x08, 0x0F, 0x10, 0x12, 0x15, 0x17]),
    SFXInstrumentChange(0x02, 0x1D, 0x16, [0x1A966C], ban=[0x09, 0x10, 0x11, 0x13, 0x15, 0x18]),
    SFXInstrumentChange(0x02, 0x1E, 0x01, [0x1A9928], type=Lg, ban=[0x06]),
    SFXInstrumentChange(0x02, 0x1F, 0x02, [0x1A969A], type=Me|Be|Hd, ban=[0x09]),
    SFXInstrumentChange(0x02, 0x1F, 0x01, [0x1A96A0], ban=[0x00, 0x06, 0x18]),
    SFXInstrumentChange(0x02, 0x20, 0x0D, [0x1A968B], type=Me|Lg, ban=[0x09]),
    SFXInstrumentChange(0x02, 0x21, 0x02, [0x1A9680], type=Be|Hd),
    SFXInstrumentChange(0x02, 0x21, 0x01, [0x1A9685], type=Sf, ban=[0x18]),
    SFXInstrumentChange(0x02, 0x22, 0x05, [0x1A948E, 0x1A94B8], type=[0x01, 0x05, 0x0E, 0x0F, 0x10, 0x17]),  # chained $3-18->22
    SFXInstrumentChange(0x02, 0x23, 0x01, [0x1A9662], ban=[0x06, 0x0C, 0x0E, 0x13]),
    SFXInstrumentChange(0x02, 0x24, 0x11, [0x1A9656, 0x1A965C], type=Me|Lg, ban=[0x09, 0x0D, 0x16], inc=[0x11]),  # accompanied $24+3D
    SFXInstrumentChange(0x02, 0x25, 0x11, [0x1A9647], ban=[0x00, 0x01, 0x06, 0x0C, 0x10]),
    SFXInstrumentChange(0x02, 0x26, 0x15, [0x1A9BF5], type=Me|Hd, inc=[0x11]),
    SFXInstrumentChange(0x02, 0x27, 0x0B, [0x1A9603], type=Me|Lg, inc=[0x0E, 0x17]),
    SFXInstrumentChange(0x02, 0x27, 0x13, [0x1A9631], type=Hd, inc=[0x08, 0x11, 0x12]),
    SFXInstrumentChange(0x02, 0x28, 0x11, [0x1A9638], type=[0x10, 0x11, 0x12]),
    SFXInstrumentChange(0x02, 0x29, 0x01, [0x1A97F0], type=Sf),  # accompanied $29+3B
    SFXInstrumentChange(0x02, 0x2A, 0x0C, [0x1A93E4], type=Be, inc=[0x08, 0x0D, 0x0E, 0x0F, 0x11, 0x12, 0x16]),
    SFXInstrumentChange(0x02, 0x2B, 0x0E, [0x1A93A3], type=Me, ban=[0x08, 0x09, 0x0D], inc=[0x03, 0x17]),
    SFXInstrumentChange(0x02, 0x2C, 0x00, [0x1A938A, 0x1A9398], type=Sh, ban=[0x13]),  # chained $2C->3A
    SFXInstrumentChange(0x02, 0x2C, 0x17, [0x1A9393, 0x1A939D], type=Me|Sh, ban=[0x0E], inc=[0x03, 0x11, 0x17]),  # chained $2C->3A
    SFXInstrumentChange(0x02, 0x2D, 0x0F, [0x1A9457], type=Me, ban=[0x09], inc=[0x03, 0x17]),
    SFXInstrumentChange(0x02, 0x2E, 0x11, [0x1A937B], type=Sh, ban=[0x03, 0x10, 0x15, 0x17]),  # accompanied $2E+39
    SFXInstrumentChange(0x02, 0x2F, 0x01, [0x1A92F8], type=Lg|Sf, ban=[0x00]),  # accompanied $2F+38
    SFXInstrumentChange(0x02, 0x30, 0x15, [0x1A92A2], type=Me, inc=[0x03, 0x11, 0x17]),
    SFXInstrumentChange(0x02, 0x31, 0x0D, [0x1A947F], type=Me, ban=[0x0E], inc=[0x17]),
    SFXInstrumentChange(0x02, 0x32, 0x01, [0x1A90F8], type=Lg, ban=[0x00, 0x06]),
    SFXInstrumentChange(0x02, 0x33, 0x07, [0x1A908D], type=Me|Lg),
    SFXInstrumentChange(0x02, 0x35, 0x13, [0x1A9018], type=Am|Be, ban=[0x00, 0x06, 0x18], inc=[0x08, 0x12]),
    SFXInstrumentChange(0x02, 0x37, 0x0E, [0x1A8DF4], type=Me, ban=[0x09], inc=[0x03, 0x17]),
    SFXInstrumentChange(0x02, 0x38, 0x11, [0x1A9320], type=Me|Am, ban=[0x00, 0x09], inc=[0x17]),  # accompanied $2F+38
    SFXInstrumentChange(0x02, 0x39, 0x01, [0x1A934D], type=Am|Lg, ban=[0x00]),  # accompanied $2E+39
    SFXInstrumentChange(0x02, 0x3B, 0x06, [0x1A9813], type=Hd, ban=[0x0C, 0x13, 0x14]),  # accompanied $29+3B
    SFXInstrumentChange(0x02, 0x3C, 0x04, [0x1A8DE8], type=Me, ban=[0x09], inc=[0x17]),
    SFXInstrumentChange(0x02, 0x3F, 0x0D, [0x1A97E6], type=Me|Be|Hd, inc=[0x12]),  # accompanied $0D+3F

    SFXInstrumentChange(0x03, 0x01, 0x07, [0x1A8DC9], type=Me, ban=[0x0D, 0x0E, 0x14, 0x16], inc=[0x03, 0x17]),
    SFXInstrumentChange(0x03, 0x02, 0x01, [0x1A98FF], ban=[0x03, 0x06, 0x09, 0x17]),
    SFXInstrumentChange(0x03, 0x03, 0x13, [0x1A95FB], type=Me|Be, ban=[0x03, 0x09, 0x10, 0x14, 0x16, 0x17]),
    SFXInstrumentChange(0x03, 0x04, 0x12, [0x1A95BF], type=Me, ban=[0x09]),
    SFXInstrumentChange(0x03, 0x04, 0x06, [0x1A95C7], ban=[0x00, 0x18]),
    SFXInstrumentChange(0x03, 0x05, 0x00, [0x1A9968], ban=[0x09, 0x10, 0x18]),
    SFXInstrumentChange(0x03, 0x06, 0x02, [0x1A95A6], type=Sh, ban=[0x0C, 0x0E, 0x10, 0x11]),
    SFXInstrumentChange(0x03, 0x07, 0x0C, [0x1A95EE], type=Me|Be, ban=[0x02, 0x09, 0x10, 0x13]),
    SFXInstrumentChange(0x03, 0x08, 0x0C, [0x1A9597], type=Me|Be, ban=[0x02, 0x09, 0x10, 0x13]),
    SFXInstrumentChange(0x03, 0x09, 0x0C, [0x1A9572], type=Me|Be, ban=[0x09, 0x10]),
    SFXInstrumentChange(0x03, 0x0A, 0x03, [0x1A955A], type=Me|Be, ban=[0x10, 0x17], inc=[0x11, 0x13]),
    SFXInstrumentChange(0x03, 0x0B, 0x0B, [0x1A9549], type=Me|Be, ban=[0x02, 0x13], inc=[0x11]),
    SFXInstrumentChange(0x03, 0x0C, 0x0D, [0x1A953F], type=Me, ban=[0x09, 0x0A, 0x0B, 0x14, 0x15], inc=[0x03, 0x11, 0x17]),
    SFXInstrumentChange(0x03, 0x0D, 0x0E, [0x1A9566], type=Me, ban=[0x14], inc=[0x03, 0x11, 0x17]),
    SFXInstrumentChange(0x03, 0x0E, 0x02, [0x1A9533], ban=[0x00, 0x08, 0x0C, 0x0D, 0x0E, 0x13, 0x14, 0x18]),
    SFXInstrumentChange(0x03, 0x0F, 0x0B, [0x1A986A, 0x1A983B, 0x1A9845, 0x1A9831], type=[0x03, 0x04, 0x08, 0x0B, 0x0D, 0x0F, 0x12, 0x17]),  # accompanied $0F+3C+3D+3E+3F
    SFXInstrumentChange(0x03, 0x10, 0x0E, [0x1A951E, 0x1A9527], type=Sh|Hd, ban=[0x03]),  # accompanied $10+3B
    SFXInstrumentChange(0x03, 0x11, 0x07, [0x1A9500], type=Me, ban=[0x09], inc=[0x03, 0x17]),
    SFXInstrumentChange(0x03, 0x12, 0x07, [0x1A950F], type=Me, ban=[0x09], inc=[0x03, 0x17]),
    SFXInstrumentChange(0x03, 0x13, 0x0D, [0x1A94EC], type=Me, ban=[0x09, 0x0B], inc=[0x03, 0x11, 0x17]),
    SFXInstrumentChange(0x03, 0x14, 0x06, [0x1A981D], type=Hd, ban=[0x03, 0x09, 0x14], inc=[0x12]),
    SFXInstrumentChange(0x03, 0x15, 0x13, [0x1A94E0], type=Hd, ban=[0x09, 0x14], inc=[0x12]),
    SFXInstrumentChange(0x03, 0x16, 0x13, [0x1A94D4], type=Be, ban=[0x0C, 0x17]),
    SFXInstrumentChange(0x03, 0x17, 0x08, [0x1A9957], type=Me, ban=[0x09, 0x0A, 0x0E]),
    SFXInstrumentChange(0x03, 0x19, 0x07, [0x1A98BB], type=Me, ban=[0x08, 0x09], inc=[0x03, 0x05, 0x17]),
    SFXInstrumentChange(0x03, 0x1A, 0x0F, [0x1A923B], type=Me|Lg, inc=[0x0F]),  # accompanied $1A+38, but OWR
    SFXInstrumentChange(0x03, 0x1B, 0x0E, [0x1A9467, 0x1A9471], type=Me, ban=[0x08]),  # accompanied $1B+3A
    SFXInstrumentChange(0x03, 0x1C, 0x0A, [0x1A8E13], type=Me, ban=[0x09], inc=[0x03, 0x17]),
    SFXInstrumentChange(0x03, 0x1E, 0x01, [0x1A9442], ban=[0x03, 0x06, 0x0C, 0x0E]),
    SFXInstrumentChange(0x03, 0x1F, 0x16, [0x1A93FC], type=Me|Lg, ban=[0x09]),
    SFXInstrumentChange(0x03, 0x20, 0x0F, [0x1A9B1D], type=Me|Be|Hd),
    SFXInstrumentChange(0x03, 0x21, 0x14, [0x1A9B93], type=Me, ban=[0x09, 0x0B, 0x15], inc=[0x17]),
    SFXInstrumentChange(0x03, 0x22, 0x14, [0x1A9A80], type=Me|Lg, ban=[0x09, 0x0A]),
    SFXInstrumentChange(0x03, 0x23, 0x01, [0x1A93B2], type=Am|Me|Lg, ban=[0x00], inc=[0x13]),  # accompanied $23+39
    SFXInstrumentChange(0x03, 0x24, 0x0D, [0x1A93F4], type=Me, ban=[0x09], inc=[0x03, 0x17]),
    SFXInstrumentChange(0x03, 0x25, 0x13, [0x1A924E], type=Am|Be, ban=[0x00, 0x06, 0x18], inc=[0x09]),
    SFXInstrumentChange(0x03, 0x25, 0x06, [0x1A9254], type=Hd, ban=[0x02, 0x05, 0x09, 0x0C, 0x13, 0x14, 0x15]),
    SFXInstrumentChange(0x03, 0x26, 0x00, [0x1A922C], type=Lg, ban=[0x06]),
    SFXInstrumentChange(0x03, 0x27, 0x07, [0x1A91F1], type=Me|Hd),
    SFXInstrumentChange(0x03, 0x28, 0x07, [0x1A9AA8], type=Me|Lg|Hd),
    SFXInstrumentChange(0x03, 0x29, 0x07, [0x1A91D2], type=Me|Hd),
    SFXInstrumentChange(0x03, 0x2A, 0x0A, [0x1A91C3], type=Me|Lg, inc=[0x03, 0x0E, 0x0F, 0x12, 0x17]),
    SFXInstrumentChange(0x03, 0x2B, 0x0A, [0x1A91A4], ban=[0x06, 0x09, 0x18]),
    SFXInstrumentChange(0x03, 0x2C, 0x0A, [0x1A9171], type=Me, ban=[0x09], inc=[0x17]),
    SFXInstrumentChange(0x03, 0x2D, 0x0F, [0x1A915A, 0x1A9165], type=Me, ban=[0x08, 0x0E, 0x12]),  # accompanied $2D+37
    SFXInstrumentChange(0x03, 0x2E, 0x0B, [0x1A910E, 0x1A9124, 0x1A9117], type=Me|Lg, ban=[0x14, 0x16]),  # accompanied $2E+35+34
    SFXInstrumentChange(0x03, 0x2F, 0x0E, [0x1A9131, 0x1A9144], type=Me|Be|Hd, inc=[0x13]),  # accompanied $2F+33
    SFXInstrumentChange(0x03, 0x30, 0x03, [0x1A8F04], type=Me|Be, ban=[0x0C, 0x13], inc=[0x11]),
    SFXInstrumentChange(0x03, 0x31, 0x01, [0x1A8E7B], ban=[0x03, 0x06, 0x09, 0x0E, 0x17, 0x18]),
    SFXInstrumentChange(0x03, 0x32, 0x04, [0x1A8E29], type=Me, ban=[0x08], inc=[0x03, 0x05, 0x17]),
    SFXInstrumentChange(0x03, 0x36, 0x07, [0x1A8E58], type=Me|Lg, inc=[0x03, 0x0F, 0x17]),
    # SFXInstrumentChange(0x03, 0x38, 0x0F, [0x1A9244]), # sound used for OWR function
    SFXInstrumentChange(0x03, 0x39, 0x07, [0x1A93C8], type=Me|Be),  # accompanied $23+39
    SFXInstrumentChange(0x03, 0x3E, 0x05, [0x1A984F], type=Be, ban=[0x02, 0x0C], inc=[0x0D, 0x0E, 0x0F])
]

Me = SPCMusicType.Melody
Rh = SPCMusicType.Rhythm
Be = SPCMusicType.Beat
Am = SPCMusicType.Ambient

spc_instrument_changes = [
    SPCInstrumentChange(0x01, 0x00, {0x00: [0x1A9F5B],
                                     0x01: [0x1A9F9D],
                                     0x02: [0x1A9FBB],
                                     0x03: [0x1A9FDA],
                                     0x04: [0x1A9FE8]}, 0x0F),
    SPCInstrumentChange(0x01, 0x01, {0x00: [0x1ACA1A],
                                     0x01: [0x1ACA39],
                                     0x02: [0x1ACA5E],
                                     0x07: [0x1ACC01]}, 0x0B),
    SPCInstrumentChange(0x01, 0x01, {0x03: [0x1ACAA3],
                                     0x04: [0x1ACAE2]}, 0x11),
    SPCInstrumentChange(0x01, 0x01, {0x05: [0x1ACB25, 0x1ACC78]}, 0x02),
    SPCInstrumentChange(0x01, 0x01, {0x05: [0x1ACB3A, 0x1ACC51],
                                     0x06: [0x1ACBA9, 0x1ACC7D]}, 0x13),
    SPCInstrumentChange(0x01, 0x01, {0x06: [0x1ACB94, 0x1ACCA3]}, 0x0C),

    SPCInstrumentChange(0x02, 0x00, {0x00: [0x1AA04B],
                                     0x03: [0x1AA10E],
                                     0x04: [0x1AA143],
                                     0x07: [0x1AA1D1]}, 0x0B),
    SPCInstrumentChange(0x02, 0x00, {0x01: [0x1AA087],
                                     0x05: [0x1AA176]}, 0x11),
    SPCInstrumentChange(0x02, 0x00, {0x02: [0x1AA0CC],
                                     0x06: [0x1AA1BF]}, 0x13),
    SPCInstrumentChange(0x02, 0x00, {0x06: [0x1AA1C7]}, 0x0C),
    SPCInstrumentChange(0x02, 0x01, {0x02: [0x1AA27B]}, 0x13),
    SPCInstrumentChange(0x02, 0x01, {0x03: [0x1AA2A2]}, 0x0A),
    SPCInstrumentChange(0x02, 0x01, {0x04: [0x1AA2CD]}, 0x02),
    SPCInstrumentChange(0x02, 0x01, {0x05: [0x1AA2E0],
                                     0x07: [0x1AA34D]}, 0x0B),
    SPCInstrumentChange(0x02, 0x02, {0x00: [0x1AA5A8],
                                     0x05: [0x1AA449]}, 0x0B),
    SPCInstrumentChange(0x02, 0x02, {0x03: [0x1AA3FF],
                                     0x04: [0x1AA42A]}, 0x0A),
    SPCInstrumentChange(0x02, 0x02, {0x06: [0x1AA49E, 0x1AA4CB]}, 0x13),
    SPCInstrumentChange(0x02, 0x02, {0x06: [0x1AA4C0, 0x1AA4EA]}, 0x0C),
    SPCInstrumentChange(0x02, 0x02, {0x06: [0x1AA752]}, 0x02),

    SPCInstrumentChange(0x03, 0x00, {0x00: [0x1AA84A],
                                     0x01: [0x1AA864],
                                     0x03: [0x1AA885]}, 0x0A),
    SPCInstrumentChange(0x03, 0x01, {0x00: [0x1AA89E],
                                     0x01: [0x1AA8B8],
                                     0x03: [0x1AA8D9]}, 0x12),
    SPCInstrumentChange(0x03, 0x01, {0x02: [0x1AAB86],
                                     0x04: [0x1AA8F2],
                                     0x05: [0x1AA93C]}, 0x0A),

    SPCInstrumentChange(0x04, 0x00, {0x00: [0x1AACA3],
                                     0x01: [0x1AACB1],
                                     0x02: [0x1AACC7]}, 0x12),
    SPCInstrumentChange(0x04, 0x01, {0x00: [0x1AABF5],
                                     0x01: [0x1AAC0B]}, 0x12),
    SPCInstrumentChange(0x04, 0x01, {0x02: [0x1AAC21]}, 0x12),
    SPCInstrumentChange(0x04, 0x02, {0x01: [0x1AAC55],
                                     0x02: [0x1AAC6B]}, 0x12),
    SPCInstrumentChange(0x04, 0x03, {0x00: [0x1AAD93],
                                     0x01: [0x1AACED],
                                     0x02: [0x1AAD07],
                                     0x03: [0x1AAD75],
                                     0x04: [0x1AADB1]}, 0x12),

    SPCInstrumentChange(0x05, 0x00, {0x00: [0x1AAE3F],
                                     0x02: [0x1AAE72],
                                     0x03: [0x1AAEAD],
                                     0x07: [0x1AAF02]}, 0x0A),
    SPCInstrumentChange(0x05, 0x00, {0x01: [0x1AAE64]}, 0x09),
    SPCInstrumentChange(0x05, 0x00, {0x04: [0x1AAEE8]}, 0x16),
    SPCInstrumentChange(0x05, 0x01, {0x00: [0x1AB156],
                                     0x03: [0x1AAF48],
                                     0x04: [0x1AAF71],
                                     0x07: [0x1AB1D3]}, 0x0A),
    SPCInstrumentChange(0x05, 0x01, {0x02: [0x1AB186]}, 0x16),
    SPCInstrumentChange(0x05, 0x02, {0x00: [0x1AB088],
                                     0x03: [0x1AB0CF],
                                     0x04: [0x1AB0F8],
                                     0x07: [0x1AB11F]}, 0x0A),
    SPCInstrumentChange(0x05, 0x02, {0x02: [0x1AB0A7]}, 0x16),

    SPCInstrumentChange(0x06, 0x00, {0x00: [0x1AB338]}, 0x0A),
    SPCInstrumentChange(0x06, 0x01, {0x01: [0x1AB68F],
                                     0x02: [0x1AB69D],
                                     0x03: [0x1AB6B2],
                                     0x05: [0x1AB3D7]}, 0x0A),
    SPCInstrumentChange(0x06, 0x02, {0x00: [0x1AB622],
                                     0x01: [0x1AB63A],
                                     0x02: [0x1AB648],
                                     0x03: [0x1AB670],
                                     0x04: [0x1AB6C4],
                                     0x05: [0x1AB49D]}, 0x0A),
    SPCInstrumentChange(0x06, 0x03, {0x05: [0x1AB548]}, 0x0A),
    SPCInstrumentChange(0x06, 0x04, {0x00: [0x1AB722],
                                     0x01: [0x1AB739],
                                     0x02: [0x1AB745],
                                     0x03: [0x1AB759],
                                     0x04: [0x1AB765],
                                     0x05: [0x1AB5E3]}, 0x0A),

    SPCInstrumentChange(0x07, 0x00, {0x00: [0x1ABB1F]}, 0x0A),
    SPCInstrumentChange(0x07, 0x00, {0x01: [0x1ABB31],
                                     0x02: [0x1ABBE6],
                                     0x03: [0x1ABC0B]}, 0x09),
    SPCInstrumentChange(0x07, 0x00, {0x04: [0x1ABB53, 0x1AB8C9]}, 0x16),
    SPCInstrumentChange(0x07, 0x01, {0x04: [0x1AB8E6]}, 0x0E, type=Am|Me|Rh, ban=[0x01, 0x05, 0x06, 0x17]),
    SPCInstrumentChange(0x07, 0x01, {0x05: [0x1AB8EB]}, 0x0A),
    SPCInstrumentChange(0x07, 0x02, {0x04: [0x1AB981]}, 0x16),
    SPCInstrumentChange(0x07, 0x03, {0x02: [0x1ABC37],
                                     0x03: [0x1ABC66]}, 0x09),
    SPCInstrumentChange(0x07, 0x03, {0x04: [0x1ABA09]}, 0x16),
    SPCInstrumentChange(0x07, 0x03, {0x05: [0x1ABC8F]}, 0x0A),
    SPCInstrumentChange(0x07, 0x05, {0x02: [0x1ABC9D],
                                     0x03: [0x1ABCBA]}, 0x09),
    SPCInstrumentChange(0x07, 0x05, {0x05: [0x1ABCD1]}, 0x0A),
    SPCInstrumentChange(0x07, 0x06, {0x00: [0x1ABB72]}, 0x0A),
    SPCInstrumentChange(0x07, 0x06, {0x01: [0x1ABB83],
                                     0x02: [0x1ABB95],
                                     0x03: [0x1ABBAA]}, 0x09),
    SPCInstrumentChange(0x07, 0x06, {0x04: [0x1ABBBD]}, 0x16),
    SPCInstrumentChange(0x07, 0x06, {0x05: [0x1ABCE6]}, 0x0A),

    SPCInstrumentChange(0x08, 0x00, {0x00: [0x1ABD3A],
                                     0x01: [0x1ABD5B]}, 0x06, type=Me|Rh|Am, ban=[0x05]),
    SPCInstrumentChange(0x08, 0x00, {0x02: [0x1ABD70],
                                     0x05: [0x1ABE06]}, 0x0F),
    SPCInstrumentChange(0x08, 0x00, {0x03: [0x1ABDAC]}, 0x0A),
    SPCInstrumentChange(0x08, 0x00, {0x04: [0x1ABDC8]}, 0x01),
    SPCInstrumentChange(0x08, 0x00, {0x06: [0x1ABE3A]}, 0x09),

    SPCInstrumentChange(0x09, 0x00, {0x00: [0x1AC25A],
                                     0x05: [0x1AC28E]}, 0x0A),
    SPCInstrumentChange(0x09, 0x00, {0x01: [0x1AC26E]}, 0x14, type=Am|Be, ban=[0x05, 0x06]),
    SPCInstrumentChange(0x09, 0x01, {0x01: [0x1ABF0A]}, 0x14, type=Am|Be, ban=[0x05, 0x06]),
    SPCInstrumentChange(0x09, 0x01, {0x06: [0x1ABF43]}, 0x09),
    SPCInstrumentChange(0x09, 0x02, {0x00: [0x1AC450],
                                     0x05: [0x1AC56D],
                                     0x07: [0x1AC595]}, 0x0A),
    SPCInstrumentChange(0x09, 0x02, {0x01: [0x1AC2AF]}, 0x14, type=Am|Be, ban=[0x05, 0x06]),
    SPCInstrumentChange(0x09, 0x02, {0x03: [0x1AC4B3],
                                     0x04: [0x1AC510]}, 0x11),
    SPCInstrumentChange(0x09, 0x02, {0x06: [0x1AC2E9]}, 0x09),
    SPCInstrumentChange(0x09, 0x03, {0x00: [0x1ABF63]}, 0x0A),
    SPCInstrumentChange(0x09, 0x03, {0x01: [0x1ABF80]}, 0x14, type=Am|Be, ban=[0x05, 0x06]),
    SPCInstrumentChange(0x09, 0x03, {0x03: [0x1ABFA4]}, 0x11),
    SPCInstrumentChange(0x09, 0x03, {0x05: [0x1AC01C]}, 0x16),
    SPCInstrumentChange(0x09, 0x04, {0x00: [0x1AC04D]}, 0x0A),
    SPCInstrumentChange(0x09, 0x04, {0x01: [0x1AC05D]}, 0x14, type=Am|Be, ban=[0x05, 0x06]),
    SPCInstrumentChange(0x09, 0x04, {0x02: [0x1AC5CF]}, 0x18),
    SPCInstrumentChange(0x09, 0x04, {0x03: [0x1AC085],
                                     0x04: [0x1AC5ED]}, 0x11),
    SPCInstrumentChange(0x09, 0x04, {0x05: [0x1AC137]}, 0x16),
    SPCInstrumentChange(0x09, 0x04, {0x06: [0x1AC146]}, 0x12),
    SPCInstrumentChange(0x09, 0x05, {0x00: [0x1AC178],
                                     0x07: [0x1AC229]}, 0x0A),
    SPCInstrumentChange(0x09, 0x05, {0x01: [0x1AC196]}, 0x14, type=Am|Be, ban=[0x05, 0x06]),
    SPCInstrumentChange(0x09, 0x05, {0x02: [0x1AC19E]}, 0x18),
    SPCInstrumentChange(0x09, 0x05, {0x03: [0x1AC1D3],
                                     0x04: [0x1AC1F4]}, 0x12),
    SPCInstrumentChange(0x09, 0x06, {0x00: [0x1AC317],
                                     0x07: [0x1AC3ED]}, 0x0A),
    SPCInstrumentChange(0x09, 0x06, {0x01: [0x1AC332]}, 0x14, type=Am|Be, ban=[0x05, 0x06]),
    SPCInstrumentChange(0x09, 0x06, {0x02: [0x1AC33A]}, 0x18),
    SPCInstrumentChange(0x09, 0x06, {0x03: [0x1AC36F],
                                     0x04: [0x1AC3A4],
                                     0x05: [0x1AC3D9]}, 0x12),
    SPCInstrumentChange(0x09, 0x07, {0x00: [0x1AC40A],
                                     0x05: [0x1AC43C]}, 0x0A),
    SPCInstrumentChange(0x09, 0x07, {0x01: [0x1AC41C]}, 0x14, type=Am|Be, ban=[0x05, 0x06]),
    SPCInstrumentChange(0x09, 0x07, {0x02: [0x1AC492]}, 0x18),
    SPCInstrumentChange(0x09, 0x07, {0x03: [0x1AC680],
                                     0x04: [0x1AC6C1]}, 0x11),

    SPCInstrumentChange(0x0A, 0x00, {0x00: [0x1AC72F],
                                     0x01: [0x1AC751],
                                     0x02: [0x1AC772],
                                     0x03: [0x1AC793]}, 0x0F),
    SPCInstrumentChange(0x0A, 0x00, {0x00: [0x1AC740],
                                     0x01: [0x1AC765],
                                     0x02: [0x1AC786],
                                     0x03: [0x1AC7A7],
                                     0x04: [0x1AC7B4, 0x1AC7C9],
                                     0x05: [0x1AC7D1]}, 0x09),

    SPCInstrumentChange(0x0B, 0x00, {0x00: [0x1A9EEC, 0x1A9D26]}, 0x0F, ban=[0x0B]),
    SPCInstrumentChange(0x0B, 0x01, {0x01: [0x1A9D3F],
                                     0x02: [0x1A9D5A],
                                     0x03: [0x1A9D75],
                                     0x04: [0x1A9D90],
                                     0x05: [0x1A9DBB],
                                     0x06: [0x1A9DE9]}, 0x0F),

    SPCInstrumentChange(0x0C, 0x00, {0x00: [0x1AC83A],
                                     0x01: [0x1AC84A],
                                     0x02: [0x1AC857],
                                     0x03: [0x1AC864],
                                     0x04: [0x1AC871],
                                     0x05: [0x1AC87E]}, 0x0B),
    SPCInstrumentChange(0x0C, 0x01, {0x02: [0x1AC89A],
                                     0x03: [0x1AC8AD]}, 0x11),
    SPCInstrumentChange(0x0C, 0x01, {0x04: [0x1AC8B7]}, 0x0E, type=Rh|Am, ban=[0x05]),
    SPCInstrumentChange(0x0C, 0x01, {0x05: [0x1AC8C3]}, 0x02),
    SPCInstrumentChange(0x0C, 0x02, {0x02: [0x1AC8E0],
                                     0x03: [0x1AC8F3]}, 0x11),
    SPCInstrumentChange(0x0C, 0x02, {0x04: [0x1AC8FD]}, 0x0E, type=Rh|Am, ban=[0x05]),
    SPCInstrumentChange(0x0C, 0x02, {0x05: [0x1AC909]}, 0x02),

    SPCInstrumentChange(0x0D, 0x00, {0x00: [0x1AD003],
                                     0x03: [0x1AD02C],
                                     0x04: [0x1AD03A]}, 0x11),
    SPCInstrumentChange(0x0D, 0x00, {0x01: [0x1AD010]}, 0x02, type=Am|Be, ban=[0x01, 0x05, 0x06, 0x10, 0x17]),
    SPCInstrumentChange(0x0D, 0x00, {0x02: [0x1AD07F]}, 0x14),
    SPCInstrumentChange(0x0D, 0x01, {0x00: [0x1ACD10],
                                     0x04: [0x1ACD9A]}, 0x0B),
    SPCInstrumentChange(0x0D, 0x01, {0x01: [0x1ACD41]}, 0x02, type=Am|Be, ban=[0x01, 0x05, 0x06, 0x10, 0x17]),
    SPCInstrumentChange(0x0D, 0x01, {0x03: [0x1ACD7F],
                                     0x05: [0x1ACDCA]}, 0x11),
    SPCInstrumentChange(0x0D, 0x03, {0x00: [0x1ACE8E],
                                     0x01: [0x1ACEB8]}, 0x0A),
    SPCInstrumentChange(0x0D, 0x03, {0x02: [0x1ACED4]}, 0x14),
    SPCInstrumentChange(0x0D, 0x03, {0x03: [0x1ACEE0],
                                     0x04: [0x1ACF07]}, 0x11),
    SPCInstrumentChange(0x0D, 0x04, {0x05: [0x1ACFE3]}, 0x02, type=Am|Be, ban=[0x01, 0x05, 0x06, 0x10, 0x17]),

    SPCInstrumentChange(0x0E, 0x00, {0x00: [0x1AD29C]}, 0x16),
    SPCInstrumentChange(0x0E, 0x00, {0x01: [0x1AD2AD],
                                     0x03: [0x1AD2CB],
                                     0x04: [0x1AD2D9]}, 0x18),
    SPCInstrumentChange(0x0E, 0x00, {0x02: [0x1AD2BB]}, 0x12),
    SPCInstrumentChange(0x0E, 0x01, {0x00: [0x1AD1A6]}, 0x16),
    SPCInstrumentChange(0x0E, 0x01, {0x01: [0x1AD1AF],
                                     0x03: [0x1AD1CD]}, 0x18),
    SPCInstrumentChange(0x0E, 0x01, {0x02: [0x1AD1C5]}, 0x0A),
    SPCInstrumentChange(0x0E, 0x02, {0x00: [0x1AD24C]}, 0x16),
    SPCInstrumentChange(0x0E, 0x02, {0x01: [0x1AD255],
                                     0x03: [0x1AD273]}, 0x18),
    SPCInstrumentChange(0x0E, 0x02, {0x02: [0x1AD26B]}, 0x0A),
    SPCInstrumentChange(0x0E, 0x03, {0x00: [0x1AD1E4],
                                     0x01: [0x1AD1ED],
                                     0x03: [0x1AD212],
                                     0x04: [0x1AD22F]}, 0x18),
    SPCInstrumentChange(0x0E, 0x03, {0x02: [0x1AD20A]}, 0x12),

    SPCInstrumentChange(0x10, 0x00, {0x00: [0x1B816D],
                                     0x02: [0x1B81A0],
                                     0x03: [0x1B81C0],
                                     0x07: [0x1B827F]}, 0x0B),
    SPCInstrumentChange(0x10, 0x00, {0x01: [0x1B818C]}, 0x11),
    SPCInstrumentChange(0x10, 0x00, {0x04: [0x1B81E0],
                                     0x05: [0x1B8220, 0x1B8229]}, 0x02),
    SPCInstrumentChange(0x10, 0x00, {0x05: [0x1B8224]}, 0x0C),
    SPCInstrumentChange(0x10, 0x00, {0x06: [0x1B825F]}, 0x16),
    SPCInstrumentChange(0x10, 0x01, {0x00: [0x1B811F]}, 0x0B),
    SPCInstrumentChange(0x10, 0x01, {0x03: [0x1B813B],
                                     0x05: [0x1B814E]}, 0x0A),
    SPCInstrumentChange(0x10, 0x02, {0x00: [0x1B829E],
                                     0x05: [0x1B8310]}, 0x0B),
    SPCInstrumentChange(0x10, 0x02, {0x01: [0x1B82C0],
                                     0x03: [0x1B82E3],
                                     0x06: [0x1B8342]}, 0x0A),
    SPCInstrumentChange(0x10, 0x02, {0x04: [0x1B8308],
                                     0x04: [0x1B83F2]}, 0x02),
    SPCInstrumentChange(0x10, 0x03, {0x00: [0x1B8360],
                                     0x02: [0x1B83AD],
                                     0x05: [0x1B83FA]}, 0x0B),
    SPCInstrumentChange(0x10, 0x03, {0x01: [0x1B8396],
                                     0x03: [0x1B83CC],
                                     0x06: [0x1B842E]}, 0x0A),
    SPCInstrumentChange(0x10, 0x04, {0x00: [0x1B844D],
                                     0x02: [0x1B84A0]}, 0x0B),
    SPCInstrumentChange(0x10, 0x04, {0x01: [0x1B847B],
                                     0x03: [0x1B84CD]}, 0x0A),
    SPCInstrumentChange(0x10, 0x04, {0x04: [0x1B84ED],
                                     0x05: [0x1B84F5]}, 0x02),
    SPCInstrumentChange(0x10, 0x05, {0x00: [0x1B8548],
                                     0x01: [0x1B8575],
                                     0x03: [0x1B85B6]}, 0x0A),
    SPCInstrumentChange(0x10, 0x05, {0x02: [0x1B859B],
                                     0x07: [0x1B867F]}, 0x0B),
    SPCInstrumentChange(0x10, 0x05, {0x04: [0x1B85D0],
                                     0x05: [0x1B862C]}, 0x02),
    SPCInstrumentChange(0x10, 0x06, {0x02: [0x1B8726],
                                     0x06: [0x1B8745]}, 0x0A),
    SPCInstrumentChange(0x10, 0x07, {0x00: [0x1B8768]}, 0x0B),
    SPCInstrumentChange(0x10, 0x07, {0x00: [0x1B8B04],
                                     0x01: [0x1B8B10],
                                     0x02: [0x1B8775],
                                     0x06: [0x1B87AD]}, 0x0A),
    SPCInstrumentChange(0x10, 0x07, {0x05: [0x1B8792]}, 0x16),
    SPCInstrumentChange(0x10, 0x08, {0x02: [0x1B86B3]}, 0x0A),
    SPCInstrumentChange(0x10, 0x09, {0x00: [0x1B8A63],
                                     0x05: [0x1B8879],
                                     0x06: [0x1B8AED]}, 0x0B),
    SPCInstrumentChange(0x10, 0x09, {0x01: [0x1B8A87],
                                     0x02: [0x1B87E5]}, 0x0A),
    SPCInstrumentChange(0x10, 0x0A, {0x00: [0x1B88C8],
                                     0x02: [0x1B8946],
                                     0x03: [0x1B88E5]}, 0x0B),
    SPCInstrumentChange(0x10, 0x0A, {0x01: [0x1B892C],
                                     0x07: [0x1B8905]}, 0x0A),
    SPCInstrumentChange(0x10, 0x0A, {0x04: [0x1B897D]}, 0x02),

    SPCInstrumentChange(0x11, 0x00, {0x00: [0x1B8C95],
                                     0x01: [0x1B8CA2],
                                     0x02: [0x1B8CB1],
                                     0x03: [0x1B8CC0],
                                     0x04: [0x1B8CCF]}, 0x0A, type=Me, ban=[0x0D]),
    SPCInstrumentChange(0x11, 0x01, {0x05: [0x1B8CFF]}, 0x0A, ban=[0x04]),
    SPCInstrumentChange(0x11, 0x02, {0x03: [0x1B8D6B]}, 0x09),
    SPCInstrumentChange(0x11, 0x04, {0x04: [0x1B8E2B]}, 0x11),
    SPCInstrumentChange(0x11, 0x05, {0x05: [0x1B90F6]}, 0x11),

    SPCInstrumentChange(0x12, 0x00, {0x00: [0x1B9275],
                                     0x01: [0x1B9282],
                                     0x05: [0x1B92DE]}, 0x0A),
    SPCInstrumentChange(0x12, 0x00, {0x02: [0x1B9290],
                                     0x03: [0x1B92AB]}, 0x11),
    SPCInstrumentChange(0x12, 0x00, {0x04: [0x1B92C6]}, 0x02),
    SPCInstrumentChange(0x12, 0x00, {0x05: [0x1B92D3]}, 0x10, type=[0x08, 0x10, 0x17]),
    SPCInstrumentChange(0x12, 0x01, {0x00: [0x1B917D],
                                     0x01: [0x1B918A],
                                     0x05: [0x1B91E6],
                                     0x06: [0x1B9229]}, 0x0A),
    SPCInstrumentChange(0x12, 0x01, {0x02: [0x1B9198],
                                     0x03: [0x1B91B3]}, 0x11),
    SPCInstrumentChange(0x12, 0x01, {0x04: [0x1B91CE]}, 0x02),
    SPCInstrumentChange(0x12, 0x01, {0x05: [0x1B91DB],
                                     0x06: [0x1B921E]}, 0x10, type=[0x08, 0x10, 0x17]),
    SPCInstrumentChange(0x12, 0x02, {0x00: [0x1B9313],
                                     0x01: [0x1B9320],
                                     0x05: [0x1B937C],
                                     0x06: [0x1B93BF]}, 0x0A),
    SPCInstrumentChange(0x12, 0x02, {0x02: [0x1B932E],
                                     0x03: [0x1B9349]}, 0x11),
    SPCInstrumentChange(0x12, 0x02, {0x04: [0x1B9364]}, 0x02),
    SPCInstrumentChange(0x12, 0x02, {0x05: [0x1B9371],
                                     0x06: [0x1B93B4]}, 0x10, type=[0x08, 0x10, 0x17]),

    SPCInstrumentChange(0x13, 0x00, {0x00: [0x1B9458],
                                     0x02: [0x1B94DA],
                                     0x03: [0x1B953E],
                                     0x04: [0x1B95A2],
                                     0x05: [0x1B9606]}, 0x0B),
    SPCInstrumentChange(0x13, 0x00, {0x01: [0x1B94A4]}, 0x11),
    SPCInstrumentChange(0x13, 0x00, {0x06: [0x1B9650],
                                     0x07: [0x1B9696]}, 0x0F),
    SPCInstrumentChange(0x13, 0x00, {0x06: [0x1B967B],
                                     0x07: [0x1B96C0]}, 0x02, ban=[0x0B]),

    SPCInstrumentChange(0x14, 0x00, {0x00: [0x1B9901, 0x1B97A8]}, 0x15),
    SPCInstrumentChange(0x14, 0x01, {0x01: [0x1B97C4],
                                     0x02: [0x1B97DE],
                                     0x03: [0x1B97FD],
                                     0x04: [0x1B9813],
                                     0x05: [0x1B982A]}, 0x15),

    SPCInstrumentChange(0x15, 0x00, {0x00: [0x1B9A32],
                                     0x01: [0x1B9A50],
                                     0x02: [0x1B9A6D],
                                     0x03: [0x1B9A8A]}, 0x0B),
    SPCInstrumentChange(0x15, 0x00, {0x04: [0x1B9AA0]}, 0x02, type=Am|Be, ban=[0x01, 0x05, 0x10]),
    SPCInstrumentChange(0x15, 0x01, {0x00: [0x1B9971]}, 0x02, type=Am|Be, ban=[0x01, 0x05, 0x10]),
    SPCInstrumentChange(0x15, 0x01, {0x01: [0x1B9984],
                                     0x02: [0x1B99AA],
                                     0x03: [0x1B99D7]}, 0x0B),
    SPCInstrumentChange(0x15, 0x01, {0x04: [0x1B9A04]}, 0x14),
    SPCInstrumentChange(0x15, 0x02, {0x00: [0x1B9B45]}, 0x02, type=Am|Be, ban=[0x01, 0x05, 0x10]),
    SPCInstrumentChange(0x15, 0x02, {0x01: [0x1B9B58],
                                     0x02: [0x1B9B7E],
                                     0x03: [0x1B9BAB]}, 0x0B),
    SPCInstrumentChange(0x15, 0x02, {0x04: [0x1B9BD8]}, 0x14),

    SPCInstrumentChange(0x16, 0x00, {0x00: [0x1B9CE6],
                                     0x01: [0x1B9CF1]}, 0x09),
    SPCInstrumentChange(0x16, 0x01, {0x00: [0x1B9C82],
                                     0x01: [0x1B9C8B],
                                     0x02: [0x1B9C93],
                                     0x03: [0x1B9CAA]}, 0x09),
    SPCInstrumentChange(0x16, 0x01, {0x04: [0x1B9CBA]}, 0x09),
    SPCInstrumentChange(0x16, 0x02, {0x00: [0x1B9CFB],
                                     0x01: [0x1B9D44]}, 0x09),
    SPCInstrumentChange(0x16, 0x03, {0x00: [0x1B9DBE],
                                     0x01: [0x1B9E47],
                                     0x05: [0x1B9EE4]}, 0x09),

    SPCInstrumentChange(0x17, 0x00, {0x00: [0x1BA287],
                                     0x03: [0x1BA26A]}, 0x0E, type=[0x0E, 0x0F, 0x18]),
    SPCInstrumentChange(0x17, 0x01, {0x01: [0x1BA20F],
                                     0x03: [0x1BA24F]}, 0x0E, type=Rh, ban=[0x0B, 0x0D, 0x16]),
    SPCInstrumentChange(0x17, 0x01, {0x02: [0x1BA231]}, 0x0F),

    SPCInstrumentChange(0x19, 0x00, {0x00: [0x1BA476],
                                     0x01: [0x1BA49C],
                                     0x02: [0x1BA4C1],
                                     0x03: [0x1BA4D5],
                                     0x04: [0x1BA4F4],
                                     0x05: [0x1BA513]}, 0x0A),
    SPCInstrumentChange(0x19, 0x01, {0x00: [0x1BA357],
                                     0x01: [0x1BA379],
                                     0x03: [0x1BA38C],
                                     0x04: [0x1BA39D]}, 0x0A),
    SPCInstrumentChange(0x19, 0x02, {0x02: [0x1BA3D3]}, 0x15),
    SPCInstrumentChange(0x19, 0x03, {0x00: [0x1BA3F8]}, 0x16),
    SPCInstrumentChange(0x19, 0x03, {0x02: [0x1BA40D]}, 0x0A),

    SPCInstrumentChange(0x1A, 0x00, {0x00: [0x1BA70E, 0x1BA71A],
                                     0x01: [0x1BA729],
                                     0x02: [0x1BA743],
                                     0x03: [0x1BA763],
                                     0x04: [0x1BA783],
                                     0x05: [0x1BA7A2],
                                     0x06: [0x1BA7B9],
                                     0x07: [0x1BA7D0]}, 0x0E, type=Rh|Am, ban=[0x01, 0x05, 0x06, 0x10]),
    SPCInstrumentChange(0x1A, 0x01, {0x00: [0x1BA5C1],
                                     0x05: [0x1BA68F],
                                     0x06: [0x1BA6A6],
                                     0x07: [0x1BA6DD]}, 0x0A),

    SPCInstrumentChange(0x1B, 0x00, {0x00: [0x1BAA55],
                                     0x01: [0x1BAA66],
                                     0x02: [0x1BAA75],
                                     0x03: [0x1BAA86],
                                     0x04: [0x1BAA97]}, 0x0F),
    SPCInstrumentChange(0x1B, 0x01, {0x00: [0x1BA956],
                                     0x01: [0x1BA96F],
                                     0x02: [0x1BA98A],
                                     0x03: [0x1BA9A5],
                                     0x04: [0x1BA9C0],
                                     0x05: [0x1BA9EB],
                                     0x06: [0x1BAA19]}, 0x0F),

    SPCInstrumentChange(0x1C, 0x00, {0x00: [0x1BACB6, 0x1BABAD]}, 0x09),
    SPCInstrumentChange(0x1C, 0x01, {0x01: [0x1BABC7],
                                     0x02: [0x1BABE0],
                                     0x03: [0x1BABF4],
                                     0x04: [0x1BAC0B]}, 0x09),
    SPCInstrumentChange(0x1C, 0x02, {0x00: [0x1BAC26],
                                     0x01: [0x1BAC44],
                                     0x02: [0x1BAC61]}, 0x09),

    SPCInstrumentChange(0x1D, 0x00, {0x00: [0x1BACEA],
                                     0x01: [0x1BAD06],
                                     0x02: [0x1BAD23],
                                     0x03: [0x1BAD40],
                                     0x04: [0x1BAD5D]}, 0x0B),

    SPCInstrumentChange(0x1E, 0x00, {0x00: [0x1BB14D],
                                     0x01: [0x1BB169],
                                     0x02: [0x1BB17E],
                                     0x03: [0x1BB193],
                                     0x04: [0x1BB1A8]}, 0x09),
    SPCInstrumentChange(0x1E, 0x00, {0x05: [0x1BB1BD]}, 0x02),

    SPCInstrumentChange(0x1F, 0x00, {0x00: [0x1BAE86],
                                     0x03: [0x1BAEC6],
                                     0x04: [0x1BAEE4]}, 0x0B),
    SPCInstrumentChange(0x1F, 0x00, {0x00: [0x1BAE9C]}, 0x18),
    SPCInstrumentChange(0x1F, 0x00, {0x02: [0x1BAEBC]}, 0x13, type=Am|Be, ban=[0x01, 0x05, 0x0F]),
    SPCInstrumentChange(0x1F, 0x00, {0x06: [0x1BAF02]}, 0x02, ban=[0x10]),
    SPCInstrumentChange(0x1F, 0x01, {0x03: [0x1BAE15],
                                     0x04: [0x1BAE32],
                                     0x05: [0x1BAE4F]}, 0x11),
    SPCInstrumentChange(0x1F, 0x02, {0x01: [0x1BAF2C]}, 0x0B),
    SPCInstrumentChange(0x1F, 0x02, {0x03: [0x1BAF53],
                                     0x04: [0x1BAF69],
                                     0x05: [0x1BAF7F]}, 0x11),
    SPCInstrumentChange(0x1F, 0x04, {0x02: [0x1BAFAA]}, 0x13, type=Am|Be, ban=[0x01, 0x05, 0x0F]),

    SPCInstrumentChange(0x20, 0x00, {0x00: [0x1AD49A],
                                     0x01: [0x1AD4BA],
                                     0x02: [0x1AD4D3],
                                     0x03: [0x1AD4EE],
                                     0x04: [0x1AD507]}, 0x0A),
    SPCInstrumentChange(0x20, 0x01, {0x05: [0x1AD475]}, 0x18),

    SPCInstrumentChange(0x21, 0x00, {0x00: [0x1AE8E3],
                                     0x01: [0x1AF18E],
                                     0x02: [0x1AF1A9],
                                     0x03: [0x1AF1B7],
                                     0x04: [0x1AF1D2],
                                     0x05: [0x1AF1E4],
                                     0x06: [0x1AF201],
                                     0x07: [0x1AF21C]}, 0x0F),
    SPCInstrumentChange(0x21, 0x01, {0x00: [0x1AE518]}, 0x12),
    SPCInstrumentChange(0x21, 0x01, {0x01: [0x1AE540],
                                     0x03: [0x1AE58D]}, 0x0B),
    SPCInstrumentChange(0x21, 0x01, {0x02: [0x1AE564]}, 0x09),
    SPCInstrumentChange(0x21, 0x01, {0x04: [0x1AE5B1],
                                     0x05: [0x1AE5D4]}, 0x0A),
    SPCInstrumentChange(0x21, 0x01, {0x06: [0x1AE5EB, 0x1AE60F]}, 0x02, ban=[0x04]),
    SPCInstrumentChange(0x21, 0x01, {0x06: [0x1AE5F9],
                                     0x07: [0x1AE61B]}, 0x0C),
    SPCInstrumentChange(0x21, 0x02, {0x03: [0x1AE651],
                                     0x05: [0x1AE69A]}, 0x0A),
    SPCInstrumentChange(0x21, 0x03, {0x00: [0x1AEC5E],
                                     0x01: [0x1AEC71],
                                     0x02: [0x1AEC90]}, 0x0A),
    SPCInstrumentChange(0x21, 0x04, {0x01: [0x1AECB6]}, 0x0A),
    SPCInstrumentChange(0x21, 0x04, {0x07: [0x1AF3A3]}, 0x0F),
    SPCInstrumentChange(0x21, 0x05, {0x00: [0x1AE6D5],
                                     0x01: [0x1AE6FD],
                                     0x03: [0x1AE748],
                                     0x04: [0x1AE75E]}, 0x0A),
    SPCInstrumentChange(0x21, 0x05, {0x02: [0x1AE72A]}, 0x09),
    SPCInstrumentChange(0x21, 0x06, {0x00: [0x1AE774]}, 0x0A),
    SPCInstrumentChange(0x21, 0x06, {0x02: [0x1AE7BE],
                                     0x04: [0x1AE825]}, 0x09),
    SPCInstrumentChange(0x21, 0x07, {0x00: [0x1AED48],
                                     0x01: [0x1AED7B],
                                     0x03: [0x1AEDCD],
                                     0x04: [0x1AEDF4]}, 0x0A),
    SPCInstrumentChange(0x21, 0x07, {0x02: [0x1AEDB1]}, 0x09),
    SPCInstrumentChange(0x21, 0x08, {0x00: [0x1AE876],
                                     0x01: [0x1AE881]}, 0x0A),
    SPCInstrumentChange(0x21, 0x0A, {0x02: [0x1AF17C],
                                     0x03: [0x1AE8BB]}, 0x09),
    SPCInstrumentChange(0x21, 0x0B, {0x00: [0x1AEE0F]}, 0x11),
    SPCInstrumentChange(0x21, 0x0B, {0x01: [0x1AEE22],
                                     0x03: [0x1AEE46],
                                     0x04: [0x1AEE74]}, 0x0A),
    SPCInstrumentChange(0x21, 0x0C, {0x01: [0x1AEECD],
                                     0x03: [0x1AEEF9],
                                     0x04: [0x1AEF2B]}, 0x0A),
    SPCInstrumentChange(0x21, 0x0D, {0x00: [0x1AE954],
                                     0x03: [0x1AE9D4],
                                     0x04: [0x1AEA03]}, 0x18),
    SPCInstrumentChange(0x21, 0x0D, {0x00: [0x1AE971]}, 0x12),
    SPCInstrumentChange(0x21, 0x0D, {0x01: [0x1AE983]}, 0x09),
    SPCInstrumentChange(0x21, 0x0D, {0x01: [0x1AE9A6],
                                     0x03: [0x1AE9F3]}, 0x0B),
    SPCInstrumentChange(0x21, 0x0D, {0x02: [0x1AE9B6]}, 0x0A),
    SPCInstrumentChange(0x21, 0x0E, {0x00: [0x1AEA4F]}, 0x12),
    SPCInstrumentChange(0x21, 0x0E, {0x01: [0x1AEA84],
                                     0x03: [0x1AEAE7],
                                     0x05: [0x1AEB94]}, 0x0B),
    SPCInstrumentChange(0x21, 0x0E, {0x02: [0x1AEAAF]}, 0x09),
    SPCInstrumentChange(0x21, 0x0E, {0x04: [0x1AEB69]}, 0x0A),
    SPCInstrumentChange(0x21, 0x0E, {0x06: [0x1AEC03]}, 0x02),
    SPCInstrumentChange(0x21, 0x0E, {0x07: [0x1AEC3C]}, 0x0C),

    SPCInstrumentChange(0x22, 0x00, {0x00: [0x1ADA2D],
                                     0x01: [0x1ADA41],
                                     0x02: [0x1ADA51],
                                     0x03: [0x1ADA6C],
                                     0x04: [0x1ADA83]}, 0x0A),
    SPCInstrumentChange(0x22, 0x01, {0x05: [0x1ADAC9]}, 0x0A),
    SPCInstrumentChange(0x22, 0x02, {0x07: [0x1ADB60]}, 0x0A),
    SPCInstrumentChange(0x22, 0x03, {0x06: [0x1ADC10]}, 0x16),
    SPCInstrumentChange(0x22, 0x05, {0x05: [0x1AD7CC]}, 0x09),
    SPCInstrumentChange(0x22, 0x05, {0x06: [0x1AD803]}, 0x11),
    SPCInstrumentChange(0x22, 0x05, {0x07: [0x1AD81B]}, 0x0A),
    SPCInstrumentChange(0x22, 0x06, {0x02: [0x1AD8A6]}, 0x11),
    SPCInstrumentChange(0x22, 0x06, {0x03: [0x1AD959]}, 0x13),
    SPCInstrumentChange(0x22, 0x06, {0x06: [0x1AD9B7]}, 0x16),
    SPCInstrumentChange(0x22, 0x07, {0x00: [0x1ADCCD]}, 0x0B),
    SPCInstrumentChange(0x22, 0x07, {0x02: [0x1ADD73]}, 0x11),
    SPCInstrumentChange(0x22, 0x07, {0x04: [0x1ADDE0],
                                     0x07: [0x1ADE9E]}, 0x0A),
    SPCInstrumentChange(0x22, 0x07, {0x05: [0x1ADE14]}, 0x12),
    SPCInstrumentChange(0x22, 0x07, {0x06: [0x1ADE62]}, 0x16),
    SPCInstrumentChange(0x22, 0x08, {0x00: [0x1ADF02],
                                     0x04: [0x1ADFA8]}, 0x0B),
    SPCInstrumentChange(0x22, 0x08, {0x01: [0x1ADF2A]}, 0x11),
    SPCInstrumentChange(0x22, 0x08, {0x02: [0x1ADF83],
                                     0x05: [0x1ADFED]}, 0x09),
    SPCInstrumentChange(0x22, 0x08, {0x06: [0x1AE02D],
                                     0x07: [0x1AE047]}, 0x0A)
]