cabbage3docs

Cabbage JavaScript API Reference

The Cabbage JavaScript API provides the communication layer between webview UIs and the Cabbage backend. This API enables bidirectional communication for plugin development.

Architecture Overview

UI → Backend (Outgoing Communication)

Use Cabbage.sendControlData() to send widget value changes to the backend. The backend automatically determines routing based on channel automatable status.

Backend → UI (Incoming Communication)

The backend sends messages via window.hostMessageCallback(). Implement this global function to handle incoming messages.

API Reference

Core Functions

Cabbage.sendControlData(data, vscode)

Send widget value changes to the Cabbage backend. This is the primary API for user interactions.

Parameters:

Behavior:

Examples:

// Basic usage
Cabbage.sendControlData({ channel: "frequency", value: 1000 }, this.vscode);

// With gesture for DAW automation
Cabbage.sendControlData({ channel: "volume", value: 0.8, gesture: "begin" }, this.vscode);
Cabbage.sendControlData({ channel: "volume", value: 0.9, gesture: "value" }, this.vscode);
Cabbage.sendControlData({ channel: "volume", value: 0.9, gesture: "end" }, this.vscode);

Cabbage.sendChannelData(channel, data, vscode)

Send channel data directly to Csound without DAW automation - bypasses all widget routing and validation.

Parameters:

Notes:

Cabbage.sendMidiMessageFromUI(statusByte, dataByte1, dataByte2, vscode)

Send MIDI messages from the UI to the backend.

Parameters:

Example:

Cabbage.sendMidiMessageFromUI(144, 60, 100, this.vscode); // Note on

Cabbage.MidiMessageFromHost(statusByte, dataByte1, dataByte2)

Handle incoming MIDI messages from the backend (callback function).

Parameters:

Cabbage.isReadyToLoad(vscode, additionalData)

Signal that the UI is ready to load and initialize.

Parameters:

Utility Functions

Cabbage.triggerFileOpenDialog(vscode, channel, options)

Trigger a file open dialog for file selection widgets.

Parameters:

Example:

Cabbage.triggerFileOpenDialog(this.vscode, "audioFile", {
    filters: "*.wav;*.aiff",
    openAtLastKnownLocation: true
});

Cabbage.openUrl(vscode, url, file)

Open a URL or file in the system default application.

Parameters:

Cabbage.requestResize(width, height, vscode)

Request a resize of the plugin GUI window (plugin mode only).

Parameters:

Notes:

Private Functions

These functions are used internally and are not for general use.

Cabbage.sendCustomCommand(command, vscode, additionalData)

Send custom commands to the backend for specialized operations.

Parameters:

Example:

Cabbage.sendCustomCommand('cabbageIsReadyToLoad', null);

Cabbage.sendWidgetUpdate(widget, vscode)

Update widget state in the backend (used by property panel).

Parameters:

Incoming Messages (Backend -> UI)

Implement window.hostMessageCallback to handle messages from the backend:

window.hostMessageCallback = function(data) {
    switch(data.command) {
        case "parameterChange":
            // DAW automation or backend parameter update
            // Update display only - NEVER send back to backend
            // updateWidgetDisplay(data.channel, data.value);
            break;

        case "updateWidget":
            // Widget property update from Csound (cabbageSetValue, cabbageSet)
            // updateWidget(data.widgetJson);
            break;

        case "csoundOutputUpdate":
            // Csound console output for debugging
            console.log("Csound output:", data.text);
            break;

        case "resizeResponse":
            // Response to GUI resize request
            if (data.accepted) {
            //     updateGuiDimensions(data.width, data.height);
            } else {
            //     handleResizeRejected(data.width, data.height);
            }
            break;
    }
};

Message Types

parameterChange

updateWidget

csoundOutputUpdate

resizeResponse

Gesture Types

Use appropriate gestures for proper DAW automation recording:

Best Practices

Avoiding Feedback Loops

When receiving parameterChange messages, only update the visual display. Never send sendControlData() in response, as this creates feedback loops that interfere with DAW automation.

Thread Safety

All outgoing functions are asynchronous and thread-safe. They queue messages without blocking the audio thread.

Value Ranges

Send values in their natural/meaningful ranges (e.g., 20-20000 Hz for frequency). The backend handles all DAW normalization automatically.

Environment Detection