Rust bindings for the REAPER C++ API.
Important note: If you want to use reaper-rs for your own project, please use the master branch for the time being, not the crates on crates.io! I push changes here pretty often, but I don't publish to crates.io at the moment, so my crates there are very outdated. Rationale: As long as I'm the only consumer of this library, this process is easier for me.
Here's the snippet:
reaper-medium = { git = "https://github.com/helgoboss/reaper-rs.git", branch = "master" }
reaper-low = { git = "https://github.com/helgoboss/reaper-rs.git", branch = "master" }
reaper-macros = { git = "https://github.com/helgoboss/reaper-rs.git", branch = "master" }
reaper-rs allows programmers to write plug-ins for the REAPER DAW (digital audio workstation) in the Rust programming language. It does so by providing raw Rust bindings for the REAPER C++ API and more convenient APIs on top of that. It also exposes the SWELL C++ API, which is provided by REAPER on Linux and macOS in order to enable developers to create cross-platform user interfaces with a subset of the Win32 API.
A minimal working extension project can be found here. Make sure you also read the basics though! It's important to understand the differences between the provided APIs and be aware that the high-level API shouldn't be used for anything else than bootstrapping the extension (because I don't keep it stable).
reaper-rs consists of the following production crates:
reaper-high
(not yet published)reaper-rx
(not yet published)reaper-macros
provides a simple attribute macro to simplify bootstrapping REAPER extension plug-ins.
reaper-low
, reaper-medium
and reaper-high
represent the 3 different APIs of reaper-rs
The remaining crates are add-ons for the high-level API.
This API contains the raw bindings, nothing more. It's unsafe to a large extent and not intended to be used
directly. However, it serves as foundation for all the other APIs and is easy to keep up-to-date because it's
mostly auto-generated from reaper_plugin_functions.h
. It also can serve as last resort if a function has not
yet been implemented in the medium-level API (although I rather want to encourage to contribute to the medium-level API
in such a case).
Status:
Example:
unsafe {
reaper.ShowConsoleMsg(c_str!("Hello world from reaper-rs low-level API!").as_ptr());
let track = reaper.GetTrack(null_mut(), 0);
reaper.DeleteTrack(track);
}
This API builds on top of the low-level API. It exposes the original REAPER C++ API functions almost one to one, but in an idiomatic and type-safe way. It's a big step forward from the raw bindings and far more convenient to use. Its focus is on stability rather than exploring new paradigms. Since the high-level API is still very unstable, this is the recommended API.
Status:
Basics:
reaper.show_console_msg("Hello world from reaper-rs medium-level API!");
let track = reaper.get_track(CurrentProject, 0).ok_or("no tracks")?;
unsafe { reaper.delete_track(track); }
Control surface:
#[derive(Debug)]
struct MyControlSurface;
impl ControlSurface for MyControlSurface {
fn set_track_list_change(&self) {
println!("Tracks changed");
}
}
session.plugin_register_add_csurf_inst(MyControlSurface);
Audio hook:
struct MyOnAudioBuffer {
counter: u64
}
impl OnAudioBuffer for MyOnAudioBuffer {
fn call(&mut self, args: OnAudioBufferArgs) {
if self.counter % 100 == 0 {
println!("Audio hook callback counter: {}\n", self.counter);
}
self.counter += 1;
}
}
session.audio_reg_hardware_hook_add(MyOnAudioBuffer { counter: 0 });
This API builds on top of the medium-level API. It makes a break with the "flat functions" nature of the original
REAPER C++ API and replaces it with an API that uses object-oriented paradigms. This break makes it possible to provide
an intuitive API which can be used completely without unsafe
.
Status:
Example:
reaper.show_console_msg("Hello world from reaper-rs high-level API!");
let project = reaper.current_project();
let track = project.track_by_index(0).ok_or("no tracks")?;
project.remove_track(&track);
reaper-rx
adds reactive programming via rxRust to the mix.
Example:
rx.track_removed().subscribe(|t| println!("Track {:?} removed", t));
The procedure depends on the desired type of plug-in. In addition to writing REAPER extension plug-ins, reaper-rs can be used for developing VST plug-ins that use REAPER functions. No matter what you choose, the possibilities of interacting with REAPER are essentially the same. The difference between the two is the context in which your plug-in will run.
An extension plug-in is loaded when REAPER starts and remains active until REAPER quits, so it's perfectly suited to add some functions to REAPER which should be available globally. Popular examples are SWS and ReaPack (both written in C++).
A REAPER VST plug-in is loaded as track, take or monitoring FX as part of a particular REAPER project, just like any instrument or effect plug-in out there. That also means it can be instantiated multiple times. Examples are Playtime (written in C++) and ReaLearn (successfully ported to Rust).
In both cases you need to make a library crate of type cdylib
.
Using the reaper_extension_plugin
macro is the fastest way to get going.
Add this to your Cargo.toml
:
[dependencies]
reaper-low = "0.1.0"
reaper-medium = "0.1.0"
reaper-macros = "0.1.0"
[lib]
name = "reaper_my_extension"
crate-type = ["cdylib"]
Then in your lib.rs
:
use std::error::Error;
use reaper_macros::reaper_extension_plugin;
use reaper_low::PluginContext;
use reaper_medium::ReaperSession;
#[reaper_extension_plugin]
fn plugin_main(context: PluginContext) -> Result<(), Box<dyn Error>> {
let session = ReaperSession::load(context);
session.reaper().show_console_msg("Hello world from reaper-rs medium-level API!");
Ok(())
}
Important: Compiled REAPER extension plug-ins (i.e.
.dll
files) must be prefixed withreaper_
in order for REAPER to load them during startup - even on Linux and macOS, where library file names usually start withlib
. On Windows, it's enough to name the libraryreaper_my_extension
inCargo.toml
and it will result in the compiled file being namedreaper_my_extension
, thus obeying this rule. On Linux and macOS, you still need to remove thelib
prefix. In any case, make sure that the compiled file placed inREAPER_RESOURCE_PATH/UserPlugins
is prefixed withreaper_
before attempting to test it!
The macro primarily exposes an extern "C" ReaperPluginEntry()
function which calls
reaper_low::bootstrap_extension_plugin()
. So if for some reason you don't want to use that
macro, have a look at the macro implementation. No magic there.
The following instructions should result in a functional extension, loaded into REAPER on start:
cargo new reaper-my-extension --lib
to initialize the projectcargo build
from within reaper-my-extension
to generate the compiled plugin extension inside of the target/debug
directoryREAPER/UserPlugins
directory
target/debug
file, to REAPER/UserPlugins
so that they were synced
Note: Here it's explicitly necessary to give the link a name that starts with
reaper_
(by default it will start withlib
)
ln -s ./target/debug/<name-of-the-compiled-extension-file> <path to REAPER/UserPlugins>
mklink \D target\debug\<name-of-the-compiled-extension-file> %AppData%\REAPER\UserPlugins
A REAPER VST plug-in is nothing else than a normal VST plug-in which gets access to functions from the REAPER C++ API.
Luckily, there is a Rust crate for creating VST plug-ins already: vst-rs.
So all you need to do is write a VST plug-in via vst-rs and gain access to the REAPER functions by letting
reaper-rs access the HostCallback
function.
Add this to your Cargo.toml
:
[dependencies]
reaper-low = "0.1.0"
reaper-medium = "0.1.0"
vst = "0.2.0"
[lib]
name = "my_reaper_vst_plugin"
crate-type = ["cdylib"]
Then in your lib.rs
:
use vst::plugin::{Info, Plugin, HostCallback};
use reaper_low::{PluginContext, reaper_vst_plugin, static_plugin_context};
use reaper_medium::ReaperSession;
reaper_vst_plugin!();
#[derive(Default)]
struct MyReaperVstPlugin {
host: HostCallback,
};
impl Plugin for MyReaperVstPlugin {
fn new(host: HostCallback) -> Self {
Self { host }
}
fn get_info(&self) -> Info {
Info {
name: "My REAPER VST plug-in".to_string(),
unique_id: 6830,
..Default::default()
}
}
fn init(&mut self) {
if let Ok(context) = PluginContext::from_vst_plugin(&self.host, static_plugin_context()) {
let session = ReaperSession::load(context);
session
.reaper()
.show_console_msg("Hello world from reaper-rs medium-level API!");
}
}
}
vst::plugin_main!(MyReaperVstPlugin);
Contributions are very welcome! Especially to the medium-level API.
Directory entry | Content |
---|---|
/ |
Workspace root |
/main |
Production code |
/main/common-types |
Types that are used in reaper-medium but generally useful (factored out for interoperability) |
/main/fluent |
Fluent API (reaper-fluent , potential/partial work-in-progress replacement of reaper-high ) |
/main/high |
High-level API (reaper-high ) |
/main/low |
Low-level API (reaper-low ) |
/main/macros |
Macros (reaper-macros ) |
/main/medium |
Medium-level API (reaper-medium ) |
/main/rx |
rxRust integration for high-level API (reaper-rx ) |
/test |
Integration test code |
/test/test |
Integration test logic (reaper-test ) |
/test/test-extension-plugin |
Test extension plug-in (reaper-test-extension-plugin ) |
/test/test-vst-plugin |
Test VST plug-in (reaper-test-vst-plugin ) |
reaper-low
has several generated files, namely bindings.rs
, reaper.rs
and swell.rs
.
These files are not generated with each build though. In order to decrease build time and improve
IDE/debugging support, they are included in the Git repository like any other Rust source.
You can generate these files on demand (see build section), e.g. after you have adjusted
reaper_plugin_functions.h
. Right now this is enabled for Linux only. If we generated the files on
Windows, bindings.rs
would look very different (whereas reaper.rs
should end up the
same). The reason is that reaper_plugin.h
includes windows.h
on Windows, whereas on Linux and macOS, it uses
swell.h
(Simple Windows Emulation Layer) as a replacement. But even on macOS,
the result looks different than on Linux.
Most parts of bindings.rs
are used to generate reaper.rs
and otherwise ignored, but a few
structs, types and constants are published as part of the raw
module. In order to have
deterministic builds, for now the convention is to only commit files generated on Linux.
Rationale: swell.h
is a sort of subset of windows.h
, so if things work
with the subset, they also should work for the superset. The inverse isn't true.
Thanks to Cargo, building reaper-rs is not a big deal.
In the following you will find the complete instructions for Windows 10, including Rust setup. Points where you have to consider the target architecture (REAPER 32-bit vs. 64-bit) are marked with :star:.
rustup-init.exe
reaper-low
, reaper-medium
and reaper-high
) :star:
rustup default stable-x86_64-pc-windows-msvc
git clone --recurse-submodules https://github.com/helgoboss/reaper-rs.git`
cd reaper-rs
cargo build
Regenerate the low-level API:
Complete instructions to build reaper-rs from a fresh Ubuntu 18.04.3 LTS installation, including Rust setup:
# Install basic stuff
sudo apt update
sudo apt install curl git build-essential pkg-config libssl-dev liblzma-dev llvm-dev libclang-dev clang -y
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # choose 1 (default)
source $HOME/.cargo/env
# Using nightly is not necessary if you want to build just the low-level, medium-level or high-level API!
rustup default stable-x86_64-unknown-linux-gnu
# Clone reaper-rs
cd Downloads
git clone --recurse-submodules https://github.com/helgoboss/reaper-rs.git
cd reaper-rs
# Build reaper-rs
cargo build
Make the test plug-ins available in REAPER:
mkdir -p $HOME/.config/REAPER/UserPlugins/FX
ln -s $HOME/Downloads/reaper-rs/target/debug/libreaper_test_extension_plugin.so $HOME/.config/REAPER/UserPlugins/reaper_test_extension_plugin.so
ln -s $HOME/Downloads/reaper-rs/target/debug/libreaper_test_vst_plugin.so $HOME/.config/REAPER/UserPlugins/FX/reaper_test_vst_plugin.so
Regenerate the low-level API:
cd main/low
cargo build --features generate
cargo fmt
In order to get the most deterministic results, use Docker (see section "Docker").
The following instructions include Rust setup. However, it's very well possible that some native toolchain setup instructions are missing, because I don't have a bare macOS installation at my disposal. The Rust installation script should provide you with the necessary instructions if something is missing.
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # choose 1 (default)
source $HOME/.cargo/env
# Using nightly is not necessary if you want to build just the low-level, medium-level or high-level API!
rustup default stable-x86_64-apple-darwin
# Clone reaper-rs
cd Downloads
git clone --recurse-submodules https://github.com/helgoboss/reaper-rs.git
cd reaper-rs
# Build reaper-rs
cargo build
Regenerate the low-level API:
Regenerating the low-level API should always be done on Linux. And for getting really deterministic results, it's best to always use the same distro and same environment, which is easy to achieve with Docker:
# Build image (official Rust image + LLVM and Clang)
docker build --tag rust-for-reaper-rs .
# Build reaper-low with feature "generate"
docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD":/usr/src/myapp -w /usr/src/myapp rust-for-reaper-rs cargo build --package reaper-low --features generate
# Format code
cargo fmt
When building the complete reaper-rs workspace, 3 test crates are produced:
reaper-test
reaper-test-extension-plugin
reaper-test-vst-plugin
reaper-test
provides an integration test that is supposed to be run in REAPER itself. This is the main testing
mechanism for reaper-rs. reaper-test-extension-plugin
and reaper-test-vst-plugin
are both test plug-ins
which register the integration test as REAPER action.
Running the integration test is not only a good way to find reaper-rs regression bugs, but can also help to expose subtle changes in the REAPER C++ API itself. Currently, the test assertions are very strict in order to reveal even the slightest deviations.
Attention: The test should be executed using a fresh reaper.ini
. Some assertions assume that REAPER
preferences are set to their defaults. Executing the test with modified preferences can lead to wrong test results!
On Linux and macOS, the REAPER integration test will be run automatically as Cargo integration test
run_reaper_integration_test
when invoking cargo test
(downloads, unpacks and executes REAPER). This test is part of
reaper-test-extension-plugin
. It can be disabled by building that crate with --no-default-features
.
reaper-rs has been born as part of the effort of porting the REAPER VST plug-in ReaLearn to Rust and publish it as open-source project. The high-level API is heavily inspired by ReaPlus, a C++ facade for the native REAPER C++ API, which is a basic building block of the original ReaLearn.