mirror of
https://github.com/robbert-vdh/yabridge.git
synced 2026-05-10 04:30:12 +02:00
No longer return a lambda in passthrough_event()
Now that it's no longer used directly this is no longer needed, and it's much clearer without the lambda.
This commit is contained in:
+147
-161
@@ -565,10 +565,10 @@ class EventHandler {
|
|||||||
* then a blocking loop that handles events from the primary `socket`.
|
* then a blocking loop that handles events from the primary `socket`.
|
||||||
*
|
*
|
||||||
* The specified function will be used to create an `EventResult` from an
|
* The specified function will be used to create an `EventResult` from an
|
||||||
* `Event`. This is almost always a wrapper around `passthrough_event()`,
|
* `Event`. This is almost uses `passthrough_event()`, which converts a
|
||||||
* which converts the `EventPayload` into a format used by VST2, calls
|
* `EventPayload` into the format used by VST2, calls either `dispatch()` or
|
||||||
* either `dispatch()` or `audioMaster()` depending on the socket, and then
|
* `audioMaster()` depending on the context, and then serializes the result
|
||||||
* serializes the result back into an `EventResultPayload`.
|
* back into an `EventResultPayload`.
|
||||||
*
|
*
|
||||||
* This function will also be used separately for receiving MIDI data, as
|
* This function will also be used separately for receiving MIDI data, as
|
||||||
* some plugins will need pointers to received MIDI data to stay alive until
|
* some plugins will need pointers to received MIDI data to stay alive until
|
||||||
@@ -920,180 +920,166 @@ class Sockets {
|
|||||||
boost::filesystem::path generate_endpoint_base(const std::string& plugin_name);
|
boost::filesystem::path generate_endpoint_base(const std::string& plugin_name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a callback function that takes an `Event` object, decodes the data
|
* Unmarshall an `EventPayload` back to the representation used by VST2, pass
|
||||||
* into the expected format for VST2 function calls, calls the given function
|
* that value to a callback function (either `AEffect::dispatcher()` for host ->
|
||||||
* (either `AEffect::dispatcher()` for host -> plugin events or `audioMaster()`
|
* plugin events or `audioMaster()` for plugin -> host events), and then
|
||||||
* for plugin -> host events), and serializes the results back into an
|
* serialize the results back into an `EventResult`.
|
||||||
* `EventResult` object. I'd rather not get too Haskell-y in my C++, but this is
|
|
||||||
* the cleanest solution for this problem.
|
|
||||||
*
|
*
|
||||||
* This is the receiving analogue of the `*DataCovnerter` objects.
|
* This is the receiving analogue of the `*DataCovnerter` objects.
|
||||||
*
|
*
|
||||||
* TODO: Now that `EventHandler::receive_events` replaced `receive_event()`,
|
|
||||||
* refactor this to just handle the event directly rather than returning a
|
|
||||||
* lambda
|
|
||||||
*
|
|
||||||
* @param plugin The `AEffect` instance that should be passed to the callback
|
* @param plugin The `AEffect` instance that should be passed to the callback
|
||||||
* function.
|
* function. During `WantsAEffect` we'll send back a copy of this, and when we
|
||||||
|
* get sent an `AEffect` instance (e.g. during `audioMasterIOChanged()`) we'll
|
||||||
|
* write the updated values to this isntance.
|
||||||
* @param callback The function to call with the arguments received from the
|
* @param callback The function to call with the arguments received from the
|
||||||
* socket.
|
* socket, either `AEffect::dispatcher()` or `audioMasterCallback()`.
|
||||||
*
|
*
|
||||||
* @tparam A function with the same signature as `AEffect::dispatcher` or
|
* @tparam F A function with the same signature as `AEffect::dispatcher` or
|
||||||
* `audioMasterCallback`.
|
* `audioMasterCallback`.
|
||||||
*
|
*
|
||||||
* @return A `EventResult(Event)` callback function that can be passed to
|
* @return The result of the operation. If necessary the `DataConverter` will
|
||||||
* `EditorHandler::receive_events()`.
|
* unmarshall the payload again and write it back.
|
||||||
*
|
*
|
||||||
* @relates EventHandler::receive_events
|
* @relates EventHandler::receive_events
|
||||||
*/
|
*/
|
||||||
template <typename F>
|
template <typename F>
|
||||||
auto passthrough_event(AEffect* plugin, F callback) {
|
EventResult passthrough_event(AEffect* plugin, F callback, Event& event) {
|
||||||
return [=](Event& event) -> EventResult {
|
// This buffer is used to write strings and small objects to. We'll
|
||||||
// This buffer is used to write strings and small objects to. We'll
|
// initialize the beginning with null values to both prevent it from being
|
||||||
// initialize the beginning with null values to both prevent it from
|
// read as some arbitrary C-style string, and to make sure that
|
||||||
// being read as some arbitrary C-style string, and to make sure that
|
// `*static_cast<void**>(string_buffer.data)` will be a null pointer if the
|
||||||
// `*static_cast<void**>(string_buffer.data)` will be a null pointer if
|
// plugin is supposed to write a pointer there but doesn't (such as with
|
||||||
// the plugin is supposed to write a pointer there but doesn't (such as
|
// `effEditGetRect`/`WantsVstRect`).
|
||||||
// with `effEditGetRect`/`WantsVstRect`).
|
std::array<char, max_string_length> string_buffer;
|
||||||
std::array<char, max_string_length> string_buffer;
|
std::fill(string_buffer.begin(), string_buffer.begin() + sizeof(size_t), 0);
|
||||||
std::fill(string_buffer.begin(), string_buffer.begin() + sizeof(size_t),
|
|
||||||
0);
|
|
||||||
|
|
||||||
auto read_payload_fn = overload{
|
auto read_payload_fn = overload{
|
||||||
[&](const std::nullptr_t&) -> void* { return nullptr; },
|
[&](const std::nullptr_t&) -> void* { return nullptr; },
|
||||||
[&](const std::string& s) -> void* {
|
[&](const std::string& s) -> void* {
|
||||||
return const_cast<char*>(s.c_str());
|
return const_cast<char*>(s.c_str());
|
||||||
},
|
},
|
||||||
[&](const std::vector<uint8_t>& buffer) -> void* {
|
[&](const std::vector<uint8_t>& buffer) -> void* {
|
||||||
return const_cast<uint8_t*>(buffer.data());
|
return const_cast<uint8_t*>(buffer.data());
|
||||||
},
|
},
|
||||||
[&](native_size_t& window_handle) -> void* {
|
[&](native_size_t& window_handle) -> void* {
|
||||||
// This is the X11 window handle that the editor should reparent
|
// This is the X11 window handle that the editor should reparent
|
||||||
// itself to. We have a special wrapper around the dispatch
|
// itself to. We have a special wrapper around the dispatch function
|
||||||
// function that intercepts `effEditOpen` events and creates a
|
// that intercepts `effEditOpen` events and creates a Win32 window
|
||||||
// Win32 window and then finally embeds the X11 window Wine
|
// and then finally embeds the X11 window Wine created into this
|
||||||
// created into this wnidow handle. Make sure to convert the
|
// wnidow handle. Make sure to convert the window ID first to
|
||||||
// window ID first to `size_t` in case this is the 32-bit host.
|
// `size_t` in case this is the 32-bit host.
|
||||||
return reinterpret_cast<void*>(
|
return reinterpret_cast<void*>(static_cast<size_t>(window_handle));
|
||||||
static_cast<size_t>(window_handle));
|
},
|
||||||
},
|
[&](const AEffect&) -> void* { return nullptr; },
|
||||||
[&](const AEffect&) -> void* { return nullptr; },
|
[&](DynamicVstEvents& events) -> void* {
|
||||||
[&](DynamicVstEvents& events) -> void* {
|
return &events.as_c_events();
|
||||||
return &events.as_c_events();
|
},
|
||||||
},
|
[&](DynamicSpeakerArrangement& speaker_arrangement) -> void* {
|
||||||
[&](DynamicSpeakerArrangement& speaker_arrangement) -> void* {
|
return &speaker_arrangement.as_c_speaker_arrangement();
|
||||||
return &speaker_arrangement.as_c_speaker_arrangement();
|
},
|
||||||
},
|
[&](WantsAEffectUpdate&) -> void* {
|
||||||
[&](WantsAEffectUpdate&) -> void* {
|
// The host will never actually ask for an updated `AEffect` object
|
||||||
// The host will never actually ask for an updated `AEffect`
|
// since that should not be a thing. This is purely a meant as a
|
||||||
// object since that should not be a thing. This is purely a
|
// workaround for plugins that initialize their `AEffect` object
|
||||||
// meant as a workaround for plugins that initialize their
|
// after the plugin has already finished initializing.
|
||||||
// `AEffect` object after the plugin has already finished
|
return nullptr;
|
||||||
// initializing.
|
},
|
||||||
|
[&](WantsChunkBuffer&) -> void* { return string_buffer.data(); },
|
||||||
|
[&](VstIOProperties& props) -> void* { return &props; },
|
||||||
|
[&](VstMidiKeyName& key_name) -> void* { return &key_name; },
|
||||||
|
[&](VstParameterProperties& props) -> void* { return &props; },
|
||||||
|
[&](WantsVstRect&) -> void* { return string_buffer.data(); },
|
||||||
|
[&](const WantsVstTimeInfo&) -> void* { return nullptr; },
|
||||||
|
[&](WantsString&) -> void* { return string_buffer.data(); }};
|
||||||
|
|
||||||
|
// Almost all events pass data through the `data` argument. There are two
|
||||||
|
// events, `effSetParameter` and `effGetParameter` that also pass data
|
||||||
|
// through the value argument.
|
||||||
|
void* data = std::visit(read_payload_fn, event.payload);
|
||||||
|
intptr_t value = event.value;
|
||||||
|
if (event.value_payload) {
|
||||||
|
value = reinterpret_cast<intptr_t>(
|
||||||
|
std::visit(read_payload_fn, *event.value_payload));
|
||||||
|
}
|
||||||
|
|
||||||
|
const intptr_t return_value =
|
||||||
|
callback(plugin, event.opcode, event.index, value, data, event.option);
|
||||||
|
|
||||||
|
// Only write back data when needed, this depends on the event payload type
|
||||||
|
auto write_payload_fn = overload{
|
||||||
|
[&](auto) -> EventResultPayload { return nullptr; },
|
||||||
|
[&](const AEffect& updated_plugin) -> EventResultPayload {
|
||||||
|
// This is a bit of a special case! Instead of writing some
|
||||||
|
// return value, we will update values on the native VST
|
||||||
|
// plugin's `AEffect` object. This is triggered by the
|
||||||
|
// `audioMasterIOChanged` callback from the hosted VST plugin.
|
||||||
|
update_aeffect(*plugin, updated_plugin);
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
},
|
||||||
|
[&](DynamicSpeakerArrangement& speaker_arrangement)
|
||||||
|
-> EventResultPayload { return speaker_arrangement; },
|
||||||
|
[&](WantsChunkBuffer&) -> EventResultPayload {
|
||||||
|
// In this case the plugin will have written its data stored in
|
||||||
|
// an array to which a pointer is stored in `data`, with the
|
||||||
|
// return value from the event determines how much data the
|
||||||
|
// plugin has written
|
||||||
|
const uint8_t* chunk_data = *static_cast<uint8_t**>(data);
|
||||||
|
return std::vector<uint8_t>(chunk_data, chunk_data + return_value);
|
||||||
|
},
|
||||||
|
[&](VstIOProperties& props) -> EventResultPayload { return props; },
|
||||||
|
[&](VstMidiKeyName& key_name) -> EventResultPayload {
|
||||||
|
return key_name;
|
||||||
|
},
|
||||||
|
[&](VstParameterProperties& props) -> EventResultPayload {
|
||||||
|
return props;
|
||||||
|
},
|
||||||
|
[&](WantsAEffectUpdate&) -> EventResultPayload { return *plugin; },
|
||||||
|
[&](WantsVstRect&) -> EventResultPayload {
|
||||||
|
// The plugin should have written a pointer to a VstRect struct into
|
||||||
|
// the data pointer. I haven't seen this fail yet, but since some
|
||||||
|
// hosts will call `effEditGetRect()` before `effEditOpen()` I can
|
||||||
|
// assume there are plugins that don't handle this correctly.
|
||||||
|
VstRect* editor_rect = *static_cast<VstRect**>(data);
|
||||||
|
if (!editor_rect) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
},
|
}
|
||||||
[&](WantsChunkBuffer&) -> void* { return string_buffer.data(); },
|
|
||||||
[&](VstIOProperties& props) -> void* { return &props; },
|
|
||||||
[&](VstMidiKeyName& key_name) -> void* { return &key_name; },
|
|
||||||
[&](VstParameterProperties& props) -> void* { return &props; },
|
|
||||||
[&](WantsVstRect&) -> void* { return string_buffer.data(); },
|
|
||||||
[&](const WantsVstTimeInfo&) -> void* { return nullptr; },
|
|
||||||
[&](WantsString&) -> void* { return string_buffer.data(); }};
|
|
||||||
|
|
||||||
// Almost all events pass data through the `data` argument. There are
|
|
||||||
// two events, `effSetParameter` and `effGetParameter` that also pass
|
|
||||||
// data through the value argument.
|
|
||||||
void* data = std::visit(read_payload_fn, event.payload);
|
|
||||||
intptr_t value = event.value;
|
|
||||||
if (event.value_payload) {
|
|
||||||
value = reinterpret_cast<intptr_t>(
|
|
||||||
std::visit(read_payload_fn, *event.value_payload));
|
|
||||||
}
|
|
||||||
|
|
||||||
const intptr_t return_value = callback(
|
|
||||||
plugin, event.opcode, event.index, value, data, event.option);
|
|
||||||
|
|
||||||
// Only write back data when needed, this depends on the event payload
|
|
||||||
// type
|
|
||||||
auto write_payload_fn = overload{
|
|
||||||
[&](auto) -> EventResultPayload { return nullptr; },
|
|
||||||
[&](const AEffect& updated_plugin) -> EventResultPayload {
|
|
||||||
// This is a bit of a special case! Instead of writing some
|
|
||||||
// return value, we will update values on the native VST
|
|
||||||
// plugin's `AEffect` object. This is triggered by the
|
|
||||||
// `audioMasterIOChanged` callback from the hosted VST plugin.
|
|
||||||
update_aeffect(*plugin, updated_plugin);
|
|
||||||
|
|
||||||
|
return *editor_rect;
|
||||||
|
},
|
||||||
|
[&](WantsVstTimeInfo&) -> EventResultPayload {
|
||||||
|
// Not sure why the VST API has twenty different ways of returning
|
||||||
|
// structs, but in this case the value returned from the callback
|
||||||
|
// function is actually a pointer to a `VstTimeInfo` struct! It can
|
||||||
|
// also be a null pointer if the host doesn't support this.
|
||||||
|
const auto time_info =
|
||||||
|
reinterpret_cast<const VstTimeInfo*>(return_value);
|
||||||
|
if (!time_info) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
},
|
} else {
|
||||||
[&](DynamicSpeakerArrangement& speaker_arrangement)
|
return *time_info;
|
||||||
-> EventResultPayload { return speaker_arrangement; },
|
}
|
||||||
[&](WantsChunkBuffer&) -> EventResultPayload {
|
},
|
||||||
// In this case the plugin will have written its data stored in
|
[&](WantsString&) -> EventResultPayload {
|
||||||
// an array to which a pointer is stored in `data`, with the
|
return std::string(static_cast<char*>(data));
|
||||||
// return value from the event determines how much data the
|
}};
|
||||||
// plugin has written
|
|
||||||
const uint8_t* chunk_data = *static_cast<uint8_t**>(data);
|
|
||||||
return std::vector<uint8_t>(chunk_data,
|
|
||||||
chunk_data + return_value);
|
|
||||||
},
|
|
||||||
[&](VstIOProperties& props) -> EventResultPayload { return props; },
|
|
||||||
[&](VstMidiKeyName& key_name) -> EventResultPayload {
|
|
||||||
return key_name;
|
|
||||||
},
|
|
||||||
[&](VstParameterProperties& props) -> EventResultPayload {
|
|
||||||
return props;
|
|
||||||
},
|
|
||||||
[&](WantsAEffectUpdate&) -> EventResultPayload { return *plugin; },
|
|
||||||
[&](WantsVstRect&) -> EventResultPayload {
|
|
||||||
// The plugin should have written a pointer to a VstRect struct
|
|
||||||
// into the data pointer. I haven't seen this fail yet, but
|
|
||||||
// since some hosts will call `effEditGetRect()` before
|
|
||||||
// `effEditOpen()` I can assume there are plugins that don't
|
|
||||||
// handle this correctly.
|
|
||||||
VstRect* editor_rect = *static_cast<VstRect**>(data);
|
|
||||||
if (!editor_rect) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
return *editor_rect;
|
// As mentioned about, the `effSetSpeakerArrangement` and
|
||||||
},
|
// `effGetSpeakerArrangement` events are the only two events that use the
|
||||||
[&](WantsVstTimeInfo&) -> EventResultPayload {
|
// value argument as a pointer to write data to. Additionally, the
|
||||||
// Not sure why the VST API has twenty different ways of
|
// `effGetSpeakerArrangement` expects the plugin to write its own data to
|
||||||
// returning structs, but in this case the value returned from
|
// this value. Hence why we need to encode the response here separately.
|
||||||
// the callback function is actually a pointer to a
|
const EventResultPayload response_data =
|
||||||
// `VstTimeInfo` struct! It can also be a null pointer if the
|
std::visit(write_payload_fn, event.payload);
|
||||||
// host doesn't support this.
|
std::optional<EventResultPayload> value_response_data = std::nullopt;
|
||||||
const auto time_info =
|
if (event.value_payload) {
|
||||||
reinterpret_cast<const VstTimeInfo*>(return_value);
|
value_response_data =
|
||||||
if (!time_info) {
|
std::visit(write_payload_fn, *event.value_payload);
|
||||||
return nullptr;
|
}
|
||||||
} else {
|
|
||||||
return *time_info;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[&](WantsString&) -> EventResultPayload {
|
|
||||||
return std::string(static_cast<char*>(data));
|
|
||||||
}};
|
|
||||||
|
|
||||||
// As mentioned about, the `effSetSpeakerArrangement` and
|
EventResult response{.return_value = return_value,
|
||||||
// `effGetSpeakerArrangement` events are the only two events that use
|
.payload = response_data,
|
||||||
// the value argument as a pointer to write data to. Additionally, the
|
.value_payload = value_response_data};
|
||||||
// `effGetSpeakerArrangement` expects the plugin to write its own data
|
|
||||||
// to this value. Hence why we need to encode the response here
|
|
||||||
// separately.
|
|
||||||
const EventResultPayload response_data =
|
|
||||||
std::visit(write_payload_fn, event.payload);
|
|
||||||
std::optional<EventResultPayload> value_response_data = std::nullopt;
|
|
||||||
if (event.value_payload) {
|
|
||||||
value_response_data =
|
|
||||||
std::visit(write_payload_fn, *event.value_payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
EventResult response{.return_value = return_value,
|
return response;
|
||||||
.payload = response_data,
|
|
||||||
.value_payload = value_response_data};
|
|
||||||
|
|
||||||
return response;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,8 +141,8 @@ PluginBridge::PluginBridge(audioMasterCallback host_callback)
|
|||||||
|
|
||||||
return response;
|
return response;
|
||||||
} else {
|
} else {
|
||||||
return passthrough_event(&plugin,
|
return passthrough_event(&plugin, host_callback_function,
|
||||||
host_callback_function)(event);
|
event);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -152,8 +152,8 @@ Vst2Bridge::Vst2Bridge(MainContext& main_context,
|
|||||||
DynamicVstEvents& events =
|
DynamicVstEvents& events =
|
||||||
next_audio_buffer_midi_events.back();
|
next_audio_buffer_midi_events.back();
|
||||||
|
|
||||||
// Exact same handling as in `passthrough_event`, apart from
|
// Exact same handling as in `passthrough_event()`, apart
|
||||||
// making a copy of the events first
|
// from making a copy of the events first
|
||||||
const intptr_t return_value = plugin->dispatcher(
|
const intptr_t return_value = plugin->dispatcher(
|
||||||
plugin, event.opcode, event.index, event.value,
|
plugin, event.opcode, event.index, event.value,
|
||||||
&events.as_c_events(), event.option);
|
&events.as_c_events(), event.option);
|
||||||
@@ -173,8 +173,10 @@ Vst2Bridge::Vst2Bridge(MainContext& main_context,
|
|||||||
// Maybe this should just be a hard error instead, since it
|
// Maybe this should just be a hard error instead, since it
|
||||||
// should never happen
|
// should never happen
|
||||||
return passthrough_event(
|
return passthrough_event(
|
||||||
plugin, std::bind(&Vst2Bridge::dispatch_wrapper, this,
|
plugin,
|
||||||
_1, _2, _3, _4, _5, _6))(event);
|
std::bind(&Vst2Bridge::dispatch_wrapper, this, _1, _2,
|
||||||
|
_3, _4, _5, _6),
|
||||||
|
event);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -320,9 +322,6 @@ bool Vst2Bridge::should_skip_message_loop() const {
|
|||||||
void Vst2Bridge::handle_dispatch() {
|
void Vst2Bridge::handle_dispatch() {
|
||||||
sockets.host_vst_dispatch.receive_events(
|
sockets.host_vst_dispatch.receive_events(
|
||||||
std::nullopt, [&](Event& event, bool /*on_main_thread*/) {
|
std::nullopt, [&](Event& event, bool /*on_main_thread*/) {
|
||||||
// TODO: As per the TODO in `passthrough_event`, this can use a
|
|
||||||
// round of refactoring now that we never use its returned
|
|
||||||
// lambda directly anymore
|
|
||||||
return passthrough_event(
|
return passthrough_event(
|
||||||
plugin,
|
plugin,
|
||||||
[&](AEffect* plugin, int opcode, int index, intptr_t value,
|
[&](AEffect* plugin, int opcode, int index, intptr_t value,
|
||||||
@@ -346,7 +345,8 @@ void Vst2Bridge::handle_dispatch() {
|
|||||||
return dispatch_wrapper(plugin, opcode, index, value,
|
return dispatch_wrapper(plugin, opcode, index, value,
|
||||||
data, option);
|
data, option);
|
||||||
}
|
}
|
||||||
})(event);
|
},
|
||||||
|
event);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -449,7 +449,7 @@ class HostCallbackDataConverter : DefaultDataConverter {
|
|||||||
case audioMasterIOChanged:
|
case audioMasterIOChanged:
|
||||||
// This is a helpful event that indicates that the VST
|
// This is a helpful event that indicates that the VST
|
||||||
// plugin's `AEffect` struct has changed. Writing these
|
// plugin's `AEffect` struct has changed. Writing these
|
||||||
// results back is done inside of `passthrough_event`.
|
// results back is done inside of `passthrough_event()`.
|
||||||
return AEffect(*plugin);
|
return AEffect(*plugin);
|
||||||
break;
|
break;
|
||||||
case audioMasterProcessEvents:
|
case audioMasterProcessEvents:
|
||||||
|
|||||||
Reference in New Issue
Block a user