back to main reference page


JS Programming Reference




top  Introduction

This is a reference guide to programming audio-oriented effects for REAPER using JS. JS is a scripting language which is compiled on the fly and allows you to modify and/or generate audio and MIDI, as well as draw custom vector based UI and analysis displays.

JS effects are simple text files, which when loaded in REAPER become full featured plug-ins. You can try loading existing JS effects and since they are distributed in source form, you can also edit existing effects to suit your needs (we recommend if editing an existing effect you save it as something with a new name--if you do not you may lose your changes when upgrading REAPER).

This guide will offer an outline of the structure of the text file used by JS, the syntax for writing code, as well as a list of all functions and special variables available for use.


top  JS File Structure

JS Effects are text files that are composed of some description lines followed by one or more code sections.

The description lines that can be specified are:

Following the description lines, there should be code sections. All of the code sections are optional (though an effect without any would likely have limited use). Code sections are declared by a single line, then followed by as much code as needed until the end of the file, or until the next code section. Each code section can only be defined once. The following code sections are currently used:
top  Basic Code Reference

The code in the code sections is a simple language that has similarties to C. Some basic features of this language are:

Basic operators:

Some key notes about the above:
  • ( and ) are used to override default precedence, or to enclose multiple statements. For example:
    • x = (y+1)*5;
    • (a = 5; b = 3; );
  • Branching is done using the ? operator, rather than if()/else. For example:
    • a < 5 ? b=1; // if a is less than 5 then set b to 1.
    • a < 5 ? b=1 : c=1; // if a is less than 5 set b to 1, otherwise set C to 1.
    • a < 5 ? ( b=1; c=1; ); // if a is less than 5 set b and c to 1.


Basic math functions:



top  Advanced Functions

In addition to the basic functions available, REAPER/JS also provides numerous advanced functions, organized into the following categories:

Looping, FFT/MDCT, Memory Utility, Slider, File, MIDI, and Graphics.


Accessing Samples

Looping
FFT/MDCT Functions Memory Utility Slider Functions File Functions

The following functions can be used in the
@serialize section or in other sections (for reading of files specified with file: or from an extended file slider.

In a @serialize section, they can be for read or for write, but on all others they are for reading only.

For @serialize, file_open() and file_close() should not be used, and the file handle should be 0.

These functions should NOT under any circumstances be used in the @gfx section.

MIDI Functions
The following functions can be used in @block or @sample sections to send and receive MIDI.

Note that if a JS effect calls midirecv(), MIDI will not be passed through unless you also midisend() the received messages.

Offset is offset from current block, in samples. msg1 is status byte, msg23 is second (and third if available) data bytes, second byte is low 8 bits (msg23&$xff), third byte is next 8 bits (msg23/256)&$xff.
Graphics Functions
Effects can specify a
@gfx code section, from which the effect can draw its own custom UI and/or analysis display.

These functions must only be called from the @gfx section.

There are numerous Graphics State Variables that should be used as well.


top  Special Variables

Basic Functionality: Audio and transport state: Extended Functionality: Delay Compensation (PDC):
  • pdc_delay
    Context:
    @block, @slider
    Usage: read-write
    The current delay added by the plug-in, in samples. Note that you shouldnt change this too often. This specifies the amount of the delay that should be compensated, however you need to set the pdc_bot_ch and pdc_top_ch below to tell JS which channels should be compensated.

  • pdc_bot_ch, pdc_top_ch
    Context:
    @block, @slider
    Usage: read-write
    The channels that are delayed by pdc_delay. For example:
     
        pdc_bot_ch=0; pdc_top_ch=2; // delays the first two channels (spl0/spl1).
        pdc_bot_ch=2; pdc_top_ch=5; // delays channels spl2,spl3, and spl4.
        
    (this is provided so that channels you dont delay can be properly synchronized by the host).

  • pdc_midi
    Context:
    @block, @slider
    Usage: read-write
    If set to 1.0, this will delay compensate MIDI as well as any specified audio channels.

Graphics and Mouse:
Note: these should all only be accessed from within the
@gfx code section: