bridge: fix Qin VST2 silent playback via per-child SHM sandbox

- out-of-process VST2 sandbox: each Qin track spawns its own plugin_host.exe child
  with unique SHM (SonicForge_PluginHost2_<bridgePid>_<ch>_<seq>), watchdog <=3 respawns
- preset round-trip bridge<->child over SHM (presetIn/presetOut 64KB), stub rejection MIN_PRESET=100
- seed qin_default.b64 default preset into every fresh child; saved patch wins on restore
- forward MIDI on channel 0 (Qin_RV only listens on ch0); one instrument per child
- controls 4/5/6 = OPEN_GUI/CLOSE_GUI/QUIT
- gitignore runtime carla_projects
This commit is contained in:
2026-08-20 19:16:43 +07:00
parent d6649e4474
commit b91e6d3875
10 changed files with 589 additions and 34 deletions
+165 -30
View File
@@ -1,11 +1,19 @@
// native_bridge/src/plugin_host_main.cpp - G4.1 sandbox VST3 host (child process).
// Proof-of-concept (NOT shipped): runs ONE VST3 out-of-process. The plugin's
// own crash can only kill this process, never the DAW/bridge. Audio + MIDI +
// heartbeat travel over a child-owned SHM mapping reusing the SharedAudioBufferIPC
// layout, so the bridge can later adopt the same struct without layout churn.
// native_bridge/src/plugin_host_main.cpp - G4.1 sandbox VST host (child process).
// Runs ONE VST out-of-process. The plugin's own crash can only kill this
// process, never the DAW/bridge. Audio + MIDI + heartbeat travel over a
// child-owned SHM mapping reusing the SharedAudioBufferIPC layout, so the
// bridge can later adopt the same struct without layout churn.
//
// argv: --shm <name> --path <vst3> [--sr <rate>] [--block <n>] [--parent <pid>]
// VST3 mode (default): opens the plugin GUI at startup on its own window.
// VST2 mode (--vst2, jBridge stubs like Qin_RV): the GUI is deferred - the
// bridge sends control 4 (OPEN_GUI) / 5 (CLOSE_GUI) / 6 (QUIT) through the
// SHM control queue; the child polls it in its main loop. Deferral matters:
// loading a Qin must not pop a window for every track.
//
// argv: --shm <name> --path <vst> [--vst2] [--channel <n>] [--sr <rate>] [--block <n>] [--parent <pid>] [--open]
#include "Vst3Instrument.h"
#define MIN_PRESET 100 // b64 chars; real chunks are KBs
#include "Vst2Instrument.h"
#include "SharedMemoryIPC.h"
#ifdef _WIN32
@@ -30,12 +38,25 @@ static bool parent_alive(uint32_t pid) {
#endif
}
static HWND make_host_window() {
WNDCLASSEX wc = {};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = DefWindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = "PluginHostClass";
RegisterClassEx(&wc);
return CreateWindowEx(0, "PluginHostClass", "PluginHost", WS_OVERLAPPEDWINDOW,
0, 0, 800, 600, nullptr, nullptr, wc.hInstance, nullptr);
}
int main(int argc, char* argv[]) {
std::string shmName, path;
std::string shmName, path, presetFile;
double sr = 44100.0;
uint32_t block = AUDIO_BLOCK_SIZE;
uint32_t parentPid = 0;
bool isOpen = false;
bool vst2 = false;
uint32_t channel = 0;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--shm") == 0 && i + 1 < argc) shmName = argv[++i];
else if (strcmp(argv[i], "--path") == 0 && i + 1 < argc) path = argv[++i];
@@ -43,9 +64,12 @@ int main(int argc, char* argv[]) {
else if (strcmp(argv[i], "--block") == 0 && i + 1 < argc) block = (uint32_t)atoi(argv[++i]);
else if (strcmp(argv[i], "--parent") == 0 && i + 1 < argc) parentPid = (uint32_t)strtoul(argv[++i], nullptr, 0);
else if (strcmp(argv[i], "--open") == 0) isOpen = true;
else if (strcmp(argv[i], "--vst2") == 0) vst2 = true;
else if (strcmp(argv[i], "--channel") == 0 && i + 1 < argc) channel = (uint32_t)atoi(argv[++i]);
else if (strcmp(argv[i], "--presetfile") == 0 && i + 1 < argc) presetFile = argv[++i];
}
if (shmName.empty() || path.empty()) {
printf("usage: plugin_host --shm <name> --path <vst3> [--sr rate] [--block n] [--parent pid]\n");
printf("usage: plugin_host --shm <name> --path <vst> [--vst2] [--channel n] [--sr rate] [--block n] [--parent pid] [--open]\n");
return 2;
}
@@ -53,8 +77,8 @@ int main(int argc, char* argv[]) {
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
timeBeginPeriod(1); // Sleep(1) really sleeps ~1ms, not the 15.6ms timer tick
#endif
printf("[plugin_host] pid=%lu shm=%s path=%s sr=%.0f block=%u\n",
(unsigned long)GetCurrentProcessId(), shmName.c_str(), path.c_str(), sr, block);
printf("[plugin_host] pid=%lu shm=%s path=%s sr=%.0f block=%u vst2=%d ch=%u\n",
(unsigned long)GetCurrentProcessId(), shmName.c_str(), path.c_str(), sr, block, vst2 ? 1 : 0, channel);
fflush(stdout);
// --open: bridge already created the mapping (G4.2); default PoC: child owns.
@@ -65,30 +89,72 @@ int main(int argc, char* argv[]) {
}
SandboxHostIPC* ipc = shm_sandbox_ptr(shm);
Vst3Instrument inst;
inst.init(sr, block); // sets sampleRate_/maxBlockSize_ used by setupProcessing
if (!inst.loadPlugin(path, sr)) {
printf("[plugin_host] loadPlugin failed\n");
return 4;
Vst3Instrument instV3;
Vst2Instrument instV2;
if (vst2) {
instV2.setChannel(channel);
if (!instV2.loadPlugin(path, sr)) {
printf("[plugin_host] loadPlugin failed\n");
return 4;
}
instV2.init(sr, block); // loadPlugin FIRST: init() opens effect (kEffectOpen+resume); before load it is a silent no-op
} else {
instV3.init(sr, block); // sets sampleRate_/maxBlockSize_ used by setupProcessing
if (!instV3.loadPlugin(path, sr)) {
printf("[plugin_host] loadPlugin failed\n");
return 4;
}
}
INativeInstrument& inst = vst2 ? (INativeInstrument&)instV2 : (INativeInstrument&)instV3;
// Optional preset restore: Qin_RV and other jBridge romplers render SILENT
// from their factory state - they need their program/bank chunk. The bridge
// forwards presetBase64 (base64 text, one line) via a temp file.
if (!presetFile.empty()) {
FILE* f = fopen(presetFile.c_str(), "rb");
if (f) {
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
std::string b64;
b64.resize(sz > 0 ? (size_t)sz : 0);
if (sz > 0 && fread(&b64[0], 1, (size_t)sz, f) == (size_t)sz) {
inst.loadSerializedState(b64);
printf("[plugin_host] preset restored from %s (%ldB)\n", presetFile.c_str(), sz);
}
fclose(f);
} else {
printf("[plugin_host] preset file open failed: %s\n", presetFile.c_str());
}
fflush(stdout);
}
// GUI on the main thread (editorhost pattern: attach then pump below).
// VST3: GUI on the main thread at startup (editorhost pattern: attach then
// pump below). VST2: deferred until control 4 (OPEN_GUI) so loading a Qin
// does not pop a window for every track.
HWND hwnd = nullptr;
{
WNDCLASSEX wc = {};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = DefWindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = "PluginHostClass";
RegisterClassEx(&wc);
hwnd = CreateWindowEx(0, "PluginHostClass", "PluginHost", WS_OVERLAPPEDWINDOW,
0, 0, 800, 600, nullptr, nullptr, wc.hInstance, nullptr);
if (!vst2) {
hwnd = make_host_window();
printf("[plugin_host] ownHwnd=%p openGUI...\n", (void*)hwnd);
fflush(stdout);
bool gui = inst.openGUI(hwnd);
printf("[plugin_host] openGUI=%d\n", gui ? 1 : 0);
fflush(stdout);
}
// G4.5: publish the initial chunk so the bridge can persist it even
// without a preset round-trip yet (factory state = the silent chunk;
// the GUI or a presetIn apply replaces it later).
inst.captureState();
{
std::string out = inst.serializeState();
if (!out.empty() && out.size() < PRESET_BLOB_SIZE) {
memcpy((void*)ipc->presetOut, out.data(), out.size());
ipc->presetOutLen = (uint32_t)out.size();
printf("[plugin_host] initial preset published ch=%u (%uB)\n", channel, ipc->presetOutLen);
fflush(stdout);
}
}
printf("[plugin_host] ownHwnd=%p openGUI...\n", (void*)hwnd);
fflush(stdout);
bool gui = inst.openGUI(hwnd);
printf("[plugin_host] openGUI=%d\n", gui ? 1 : 0);
fflush(stdout);
// Heartbeat ~10Hz, independent of the audio loop (G3.1 pattern).
std::thread([ipc]() {
@@ -100,12 +166,81 @@ int main(int argc, char* argv[]) {
int activeNotes = 0;
bool idle = false;
bool quit = false;
uint32_t lastCaptureTick = 0;
std::string lastOut;
for (;;) {
MSG msg;
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
// VST2 controls: 4=OPEN_GUI, 5=CLOSE_GUI, 6=QUIT. This SHM is
// per-child - only the bridge-side SandboxVst2Host writes it, so no
// frontend saveStateJob spam like the bridge's own control queue.
if (vst2) {
uint32_t nctl = ipc->base.controlQueueCount > 8 ? 8 : ipc->base.controlQueueCount;
for (uint32_t i = 0; i < nctl; ++i) {
const auto& c = ipc->base.controlQueue[i];
if (c.type == 4) {
if (!hwnd) hwnd = make_host_window();
if (hwnd && !inst.hasAttachedView()) {
printf("[plugin_host] OPEN_GUI ch=%u\n", channel);
fflush(stdout);
inst.openGUI(hwnd);
ShowWindow(hwnd, SW_SHOW);
}
} else if (c.type == 5) {
inst.closeGUI();
if (hwnd) ShowWindow(hwnd, SW_HIDE);
} else if (c.type == 6) {
quit = true;
}
}
ipc->base.controlQueueCount = 0;
if (quit) break;
}
// G4.5: preset round-trip. Apply bridge->child presetIn (restore path
// writes it right after spawn; the seed default too). Republish
// child->bridge presetOut when the chunk changed.
if (ipc->presetInLen > 0 && ipc->presetInLen < PRESET_BLOB_SIZE) {
// Reject tiny b64 (Qin's empty stub "KAM..." is 16 chars): the
// saved-state file can hold a stub captured before the plugin
// loaded an instrument; applying it would wipe the seeded
// default and leave the channel silent.
if (ipc->presetInLen < MIN_PRESET) {
printf("[plugin_host] preset rejected ch=%u (stub %uB)\n", channel, (unsigned)ipc->presetInLen);
fflush(stdout);
ipc->presetInLen = 0;
} else {
std::string b64(ipc->presetIn, ipc->presetInLen);
inst.loadSerializedState(b64);
printf("[plugin_host] preset applied from SHM ch=%u (%uB)\n", channel, ipc->presetInLen);
fflush(stdout);
ipc->presetInLen = 0;
// republish immediately so a just-after restore save has it
inst.captureState();
std::string out = inst.serializeState();
if (!out.empty() && out.size() < PRESET_BLOB_SIZE) {
memcpy((void*)ipc->presetOut, out.data(), out.size());
ipc->presetOutLen = (uint32_t)out.size();
lastOut = out;
}
}
}
if (++lastCaptureTick >= 200) { // ~2s of loop passes
lastCaptureTick = 0;
inst.captureState();
std::string out = inst.serializeState();
if (!out.empty() && out != lastOut && out.size() < PRESET_BLOB_SIZE) {
memcpy((void*)ipc->presetOut, out.data(), out.size());
ipc->presetOutLen = (uint32_t)out.size();
lastOut = out;
printf("[plugin_host] preset republished ch=%u (%uB)\n", channel, ipc->presetOutLen);
fflush(stdout);
}
}
inst.guiIdle(); // VST2 editors repaint via effEditIdle; no-op for VST3
uint32_t n = ipc->base.midiQueueCount > 64 ? 64 : ipc->base.midiQueueCount;
bool midiActivity = n > 0;
for (uint32_t i = 0; i < n; ++i) {