-- =====================================================
-- Load ReaBank
-- =====================================================

-- >>>  <<<
local resource_path = reaper.GetResourcePath()
local reabank_dir = resource_path .. "/Data"
local selected_bank = reabank_dir .. "/" .. "EEXL B Jazz Trumpet.reabank"
local bank_msb = 0      -- MSB der Bank
local bank_lsb = 0      -- LSB der Bank
local program  = 0      -- Program Change Nummer (0–127)

-- -----------------------------------------------------

-- get active track
local track = reaper.GetSelectedTrack(0, 0)

if not track then
    reaper.ShowMessageBox("No track selected!", "error", 0)
    return
end

-- import ReaBank
reaper.MIDI_LoadBank(selected_bank)

-- find first MIDI item
local item = reaper.GetTrackMediaItem(track, 0)

if not item then
    reaper.ShowMessageBox("Track has no MIDI item!", "error", 0)
    return
end

local take = reaper.GetActiveTake(item)

if not take or not reaper.TakeIsMIDI(take) then
    reaper.ShowMessageBox("Found no MIDI take!", "error", 0)
    return
end

-- Bank Select + Program Change
local ppq = 0

-- Bank MSB (CC0)
reaper.MIDI_InsertCC(take, false, false, ppq,
    0xB0, 0, 0, bank_msb)

-- Bank LSB (CC32)
reaper.MIDI_InsertCC(take, false, false, ppq,
    0xB0, 0, 32, bank_lsb)

-- Program Change
reaper.MIDI_InsertEvt(take, false, false, ppq,
    string.char(0xC0, program))

reaper.MIDI_Sort(take)

reaper.ShowMessageBox("ReaBank loaded!", "ready", 0)
