From 0cce91d4609db226b66fb4a56bd50a7b10bb0542 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 19 Apr 2020 15:38:31 +0200 Subject: [PATCH] Reuse output buffers --- src/wine-host/plugin-bridge.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/wine-host/plugin-bridge.cpp b/src/wine-host/plugin-bridge.cpp index 6cd1585b..36e9a195 100644 --- a/src/wine-host/plugin-bridge.cpp +++ b/src/wine-host/plugin-bridge.cpp @@ -154,6 +154,8 @@ PluginBridge::PluginBridge(std::string plugin_dll_path, }); process_replacing_handler = std::thread([&]() { + std::vector> output_buffers(plugin->numOutputs); + while (true) { AudioBuffers request; request = read_object(host_vst_process_replacing, request, @@ -161,25 +163,26 @@ PluginBridge::PluginBridge(std::string plugin_dll_path, // TODO: Check if the plugin doesn't support `processReplacing` and // call the legacy `process` function instead - // TODO: Reuse the output_buffers and resize if necessary to avoid - // unnecessary allocations - std::vector> output_buffers( - plugin->numOutputs, std::vector(request.sample_frames)); - // The process functions expect a `float**` for their inputs and // their outputs std::vector inputs; for (auto& buffer : request.buffers) { inputs.push_back(buffer.data()); } + + // We reuse the buffers to avoid some unnecessary heap allocations, + // so we need to make sure the buffers are large enough since + // plugins can change their output configuration std::vector outputs; + output_buffers.resize(plugin->numOutputs); for (auto& buffer : output_buffers) { + buffer.resize(request.sample_frames); outputs.push_back(buffer.data()); } + // Some plugins (e.g. Serum) don't allow audio processing while the + // GUI is being updated { - // Some plugins (e.g. Serum) don't allow audio processing while - // the GUI is being updated std::lock_guard lock(processing_mutex); plugin->processReplacing(plugin, inputs.data(), outputs.data(), request.sample_frames);