落雨宸的时光机
647 字
3 分钟
Reaper ReaScript to translate Program Change signals to CC signals

1. Preface#

Just today I decided to try out Reaper, since it’s fully customizable and generous.

From time to time, I go on live performance. As a keyboarder, it’s a must to switch between patches (in layman’s terms, instruments), and it’s a surprise to see that there’s no integrated Program Change option in Reaper.

I came across the SWS / S&M Extension, which has a Live Config option, allowing you to switch between different tracks. That makes sense. However, it requires a keybind to work, which only accepts a single MIDI signal with a Controller value as the patch number. Unfortunately, my keyboard (or most keyboards) sends only a PC X (Program Change) signal, with a controller value of 0. That means whichever button I press, it always jumps to patch 0. Unacceptable.

So I turned to AI and various docs, and eventually figured out a solution.

Reaper allows you to create your own scripts to automate everything in Reaper, which is called the ReaScript. You can always create a ReaScript by the following steps:

  • Press ? (Shift + /) on your keyboard, and the Actions window will pop up.
  • Click New action..., and select New ReaScript....
  • You will be prompted to save the script. After saving it, an editing window, called the ReaScript Development Environment, will pop up, you can either edit it or run it. Note: the API help is in this window. Read it if you haven’t.

You can also find the script here: https://www.reaper.fm/sdk/reascript/reascripthelp.html

In the API document, there is an API below:

reaper.StuffMIDIMessage(integer mode, integer msg1, integer msg2, integer msg3)

Stuffs a 3 byte MIDI message into either the Virtual MIDI Keyboard queue, or the MIDI-as-control input queue, or sends to a MIDI hardware output. mode=0 for VKB, 1 for control (actions map etc), 2 for VKB-on-current-channel; 16 for external MIDI device 0, 17 for external MIDI device 1, etc; see GetNumMIDIOutputs, GetMIDIOutputName.

which is critical for this script.

3. Setup process#

First make sure you have SWS installed. Select Extensions -> SWS/S&M -> Live Config in the menu bar, and configure the Tracks you like.

Then, create a ReaScript and paste in the script below.

The script does one thing: catch all MIDI signals, filter out PC signals, and send a CC 119 instead.

local last_pc = -1
local last_seq = -1
local frame_count = 0

-- Toast 提示函数
function toast(message)
    local x, y = reaper.GetMousePosition()
    reaper.TrackCtl_SetToolTip(message, x + 20, y + 20, true)
    local start = reaper.time_precise()
    local function clear()
        if reaper.time_precise() - start > 2.0 then 
            reaper.TrackCtl_SetToolTip("", 0, 0, false)
        else reaper.defer(clear) end
    end
    clear()
end

function main()
    frame_count = frame_count + 1

    if frame_count >= 4 then
        frame_count = 0
        
        -- idx=0 锁存最新 MIDI 事件
        local retval, msg = reaper.MIDI_GetRecentInputEvent(0)
        
        -- 核心:只有当序列号发生变化时,才执行解析逻辑
        if retval > 0 and retval ~= last_seq then
            last_seq = retval
            
            if #msg >= 2 then
                local b1, b2 = msg:byte(1), msg:byte(2)
                -- 判断是否为 Program Change (0xC0)
                if (b1 & 0xF0) == 0xC0 then
                    local pc_val = b2
                    local chan = b1 & 0x0F
                    
                    if pc_val ~= last_pc then
                        last_pc = pc_val
                        -- 模式 1:注入到 SWS 监听的控制路径
                        reaper.StuffMIDIMessage(1, 0xB0 | chan, 119, pc_val)
                    end
                end
            end
        end
    end
    
    reaper.defer(main)
end

-- reaper.ClearConsole()
toast("PC -> CC on.")
main()

Then navigate to SWS/S&M -> Live Config, press the Learn button, and press any Program Change Key on your keyboard. It will show up as MIDI CC 119.

And it’s done. You can now use your Program Change Keys.

4. Tips#

It’s recommended to set the following settings in the “Options”:

  • Mute all but active track (CPU savings)
  • Disarm all but active track Critical. Must be set to switch between tracks. If not set, SWS will only “click” on the track, instead of arming the record.
  • Ignore switches to empty configs
Reaper ReaScript to translate Program Change signals to CC signals
https://blog.lzc256.com/posts/reaper-translate-pc-to-cc-to-allow-the-use-of-sws-liveconfig/
作者
落雨宸
发布于
2026-02-26
许可协议
CC BY-NC-SA 4.0


Loading Comment Component...