Last active
December 27, 2023 19:38
-
-
Save cfillion/d4d3e16b65c90bb7609a97edadf4bff9 to your computer and use it in GitHub Desktop.
Make REAPER scripts go brrr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set REAPER misc timer interval to 60 Hz | |
// | |
// 1. Grab reaper_plugin.h from https://github.com/justinfrankel/reaper-sdk/raw/main/sdk/reaper_plugin.h | |
// 2. Grab reaper_plugin_functions.h by running the REAPER action "[developer] Write C++ API functions header" | |
// 3. Grab WDL: git clone https://github.com/justinfrankel/WDL.git | |
// 4. Build then copy or link the binary file into <REAPER resource directory>/UserPlugins | |
// | |
// Linux | |
// ===== | |
// | |
// c++ -fPIC -O2 -std=c++14 -DSWELL_PROVIDED_BY_APP -IWDL/WDL -shared reaper_60fps.cpp WDL/WDL/swell/swell-modstub-generic.cpp -o reaper_60fps.so | |
// | |
// macOS | |
// ===== | |
// | |
// c++ -fPIC -O2 -std=c++14 -DSWELL_PROVIDED_BY_APP -IWDL/WDL -dynamiclib reaper_60fps.cpp WDL/WDL/swell/swell-modstub.mm -framework AppKit -o reaper_60fps.dylib | |
// | |
// Windows | |
// ======= | |
// | |
// (Use the VS Command Prompt matching your REAPER architecture, eg. x64 to use the 64-bit compiler) | |
// cl /nologo /O2 /Z7 /Zo /DUNICODE reaper_60fps.cpp User32.lib /link /DEBUG /OPT:REF /PDBALTPATH:%_PDB% /DLL /OUT:reaper_60fps.dll | |
// cl /nologo /O2 /Z7 /Zo /DUNICODE /DHIRES reaper_60fps.cpp User32.lib Winmm.lib /link /DEBUG /OPT:REF /PDBALTPATH:%_PDB% /DLL /OUT:reaper_60fps_hires.dll | |
#define REAPERAPI_IMPLEMENT | |
#include "reaper_plugin_functions.h" | |
constexpr unsigned int TIMER_RATE { 1000 / 60 }; | |
constexpr unsigned int MISC_TIMER { 666 }; | |
#ifdef HIRES | |
void CALLBACK timerCallback(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR) | |
{ | |
SendMessage(GetMainHwnd(), WM_TIMER, MISC_TIMER, 0); | |
} | |
#endif | |
static void activate() | |
{ | |
plugin_register("-timer", reinterpret_cast<void *>(&activate)); | |
#ifdef HIRES | |
// REAPER already does timeBeginPeriod(1) | |
if(timeSetEvent(TIMER_RATE, 1, &timerCallback, 0, TIME_PERIODIC)) | |
KillTimer(GetMainHwnd(), MISC_TIMER); | |
#else | |
SetTimer(GetMainHwnd(), MISC_TIMER, TIMER_RATE, nullptr); | |
#endif | |
} | |
extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT( | |
REAPER_PLUGIN_HINSTANCE instance, reaper_plugin_info_t *rec) | |
{ | |
if(!rec || rec->caller_version != REAPER_PLUGIN_VERSION) | |
return 0; | |
// change the timer interval after it's been initialized by REAPER | |
plugin_register = reinterpret_cast<decltype(plugin_register)>(rec->GetFunc("plugin_register")); | |
GetMainHwnd = reinterpret_cast<decltype(GetMainHwnd )>(rec->GetFunc("GetMainHwnd")); | |
plugin_register("timer", reinterpret_cast<void *>(&activate)); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment