Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
wclap_adapter.hpp
Go to the documentation of this file.
1#pragma once
2
3// WebCLAP (WCLAP) adapter for Pulp
4// Exports a Pulp plugin as a WCLAP — a CLAP plugin compiled to WebAssembly.
5//
6// WebCLAP spec (https://github.com/WebCLAP):
7// A WCLAP is a .wasm module that exports clap_entry and conforms to the
8// CLAP plugin API. It can run in:
9// - Native DAWs via wclap-bridge (Wasmtime runtime)
10// - Browsers via wclap-host-js (AudioWorklet + WebAssembly)
11//
12// Requirements for a valid WCLAP:
13// 1. Export `clap_entry` (the standard CLAP entry point)
14// 2. Support shared or exported memory
15// 3. Export a growable function table
16// 4. Provide malloc() or cabi_realloc() for host memory allocation
17// 5. Build with WASI SDK (wasi_snapshot_preview1)
18//
19// This adapter provides:
20// - WCLAP entry point generation via PULP_WCLAP_PLUGIN() macro
21// - Memory allocation exports for the WCLAP host sandbox
22// - WASI-compatible build configuration
23//
24// Build:
25// Uses wasi-sdk (not Emscripten) to target wasm32-wasi.
26// The output is a .wasm file that exports clap_entry.
27
29#include <cstddef>
30
31namespace pulp::format::wclap {
32
33// Memory allocator export required by WebCLAP hosts.
34// The host calls this to allocate memory inside the WASM sandbox
35// for passing strings, events, and other data to the plugin.
36extern "C" void* wclap_malloc(size_t size);
37extern "C" void wclap_free(void* ptr);
38
39// Compatibility alias — WebCLAP hosts may call either name
40extern "C" void* cabi_realloc(void* ptr, size_t old_size,
41 size_t align, size_t new_size);
42
43} // namespace pulp::format::wclap
44
45// Generate a WCLAP entry point.
46// This reuses the existing CLAP adapter and entry point,
47// adding the memory allocation exports required by WebCLAP.
48//
49// Usage (in one .cpp file per WCLAP plugin):
50// #include "my_processor.hpp"
51// #include <pulp/format/web/wclap_adapter.hpp>
52// #include <pulp/format/clap_entry.hpp>
53//
54// PULP_WCLAP_PLUGIN("com.mycompany.myplugin", "My Plugin",
55// "My Company", "1.0.0",
56// my_namespace::create_my_processor)
57//
58// Build with wasi-sdk:
59// /opt/wasi-sdk/bin/clang++ --target=wasm32-wasi \
60// -mexported-symbols=clap_entry,malloc,free \
61// -o MyPlugin.wasm wclap_entry.cpp ...
62
63// id/name/vendor/version come from the Processor's descriptor() (see
64// PULP_CLAP_PLUGIN); the WCLAP macro only needs the factory plus the WebCLAP
65// memory-allocation exports the host sandbox requires.
66#define PULP_WCLAP_PLUGIN(factory_fn) \
67 PULP_CLAP_PLUGIN(factory_fn) \
68 \
69 /* WebCLAP memory allocation exports */ \
70 extern "C" { \
71 __attribute__((export_name("malloc"))) \
72 void* wclap_malloc(size_t size) { \
73 return ::malloc(size); \
74 } \
75 __attribute__((export_name("free"))) \
76 void wclap_free(void* ptr) { \
77 ::free(ptr); \
78 } \
79 __attribute__((export_name("cabi_realloc"))) \
80 void* cabi_realloc(void* ptr, size_t old_size, \
81 size_t, size_t new_size) { \
82 (void)old_size; \
83 if (new_size == 0) { ::free(ptr); return nullptr; } \
84 return ::realloc(ptr, new_size); \
85 } \
86 }
Definition clap_webview.hpp:21
void * wclap_malloc(size_t size)
void * cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size)
void wclap_free(void *ptr)