Also add a second payload value to event responses

This will only be used for `effGetSpeakerArrangement`.
This commit is contained in:
Robbert van der Helm
2020-05-07 17:15:47 +02:00
parent 044b7fa9a2
commit a3aad51e41
5 changed files with 144 additions and 109 deletions
+50 -34
View File
@@ -233,8 +233,7 @@ auto passthrough_event(AEffect* plugin, F callback) {
std::array<char, max_string_length> string_buffer; std::array<char, max_string_length> string_buffer;
string_buffer[0] = 0; string_buffer[0] = 0;
void* data = std::visit( auto read_payload_fn = overload{
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());
@@ -243,13 +242,12 @@ auto passthrough_event(AEffect* plugin, F callback) {
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 // This is the X11 window handle that the editor should reparent
// reparent itself to. We have a special wrapper around the // itself to. We have a special wrapper around the dispatch
// dispatch function that intercepts `effEditOpen` events // function that intercepts `effEditOpen` events and creates a
// and creates a Win32 window and then finally embeds the // Win32 window and then finally embeds the X11 window Wine
// X11 window Wine created into this wnidow handle. Make // created into this wnidow handle. Make sure to convert the
// sure to convert the window ID first to `size_t` in case // window ID first to `size_t` in case this is the 32-bit host.
// 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));
}, },
@@ -260,35 +258,40 @@ auto passthrough_event(AEffect* plugin, F callback) {
[&](DynamicSpeakerArrangement& speaker_arrangement) -> void* { [&](DynamicSpeakerArrangement& speaker_arrangement) -> void* {
return &speaker_arrangement.as_c_speaker_arrangement(); return &speaker_arrangement.as_c_speaker_arrangement();
}, },
[&](WantsChunkBuffer&) -> void* { [&](WantsChunkBuffer&) -> void* { return string_buffer.data(); },
return string_buffer.data();
},
[&](VstIOProperties& props) -> void* { return &props; }, [&](VstIOProperties& props) -> void* { return &props; },
[&](VstMidiKeyName& key_name) -> void* { return &key_name; }, [&](VstMidiKeyName& key_name) -> void* { return &key_name; },
[&](VstParameterProperties& props) -> void* { return &props; }, [&](VstParameterProperties& props) -> void* { return &props; },
[&](WantsVstRect&) -> void* { return string_buffer.data(); }, [&](WantsVstRect&) -> void* { return string_buffer.data(); },
[&](const WantsVstTimeInfo&) -> void* { return nullptr; }, [&](const WantsVstTimeInfo&) -> void* { return nullptr; },
[&](WantsString&) -> void* { return string_buffer.data(); }}, [&](WantsString&) -> void* { return string_buffer.data(); }};
event.payload);
// 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.has_value()) {
value = reinterpret_cast<intptr_t>(
std::visit(read_payload_fn, event.value_payload.value()));
}
const intptr_t return_value = callback( const intptr_t return_value = callback(
plugin, event.opcode, event.index, event.value, data, event.option); plugin, event.opcode, event.index, value, data, event.option);
// Only write back data when needed, this depends on the event payload // Only write back data when needed, this depends on the event payload
// type // type
const auto response_data = std::visit( auto write_payload_fn = overload{
overload{
[&](auto) -> EventResposnePayload { return nullptr; }, [&](auto) -> EventResposnePayload { return nullptr; },
[&](const AEffect& updated_plugin) -> EventResposnePayload { [&](const AEffect& updated_plugin) -> EventResposnePayload {
// This is a bit of a special case! Instead of writing some // This is a bit of a special case! Instead of writing some
// return value, we will update values on the native VST // return value, we will update values on the native VST
// plugin's `AEffect` object. This is triggered by the // plugin's `AEffect` object. This is triggered by the
// `audioMasterIOChanged` callback from the hsoted VST // `audioMasterIOChanged` callback from the hsoted VST plugin.
// plugin.
// These are the same fields written by bitsery in the // These are the same fields written by bitsery in the
// initialization of `PluginBridge`. I can't think of a way // initialization of `PluginBridge`. I can't think of a way to
// to reuse the serializer without first having to serialize // reuse the serializer without first having to serialize
// `updated_plugin` first though. // `updated_plugin` first though.
plugin->magic = updated_plugin.magic; plugin->magic = updated_plugin.magic;
plugin->numPrograms = updated_plugin.numPrograms; plugin->numPrograms = updated_plugin.numPrograms;
@@ -306,10 +309,10 @@ auto passthrough_event(AEffect* plugin, F callback) {
return nullptr; return nullptr;
}, },
[&](WantsChunkBuffer&) -> EventResposnePayload { [&](WantsChunkBuffer&) -> EventResposnePayload {
// In this case the plugin will have written its data // In this case the plugin will have written its data stored in
// stored in an array to which a pointer is stored in // an array to which a pointer is stored in `data`, with the
// `data`, with the return value from the event determines // return value from the event determines how much data the
// how much data the plugin has written // plugin has written
const uint8_t* chunk_data = *static_cast<uint8_t**>(data); const uint8_t* chunk_data = *static_cast<uint8_t**>(data);
return std::vector<uint8_t>(chunk_data, return std::vector<uint8_t>(chunk_data,
chunk_data + return_value); chunk_data + return_value);
@@ -324,16 +327,16 @@ auto passthrough_event(AEffect* plugin, F callback) {
return props; return props;
}, },
[&](WantsVstRect&) -> EventResposnePayload { [&](WantsVstRect&) -> EventResposnePayload {
// The plugin has written a pointer to a VstRect struct // The plugin has written a pointer to a VstRect struct into the
// into the data poitner // data poitner
return **static_cast<VstRect**>(data); return **static_cast<VstRect**>(data);
}, },
[&](WantsVstTimeInfo&) -> EventResposnePayload { [&](WantsVstTimeInfo&) -> EventResposnePayload {
// Not sure why the VST API has twenty different ways of // Not sure why the VST API has twenty different ways of
// returning structs, but in this case the value returned // returning structs, but in this case the value returned from
// from the callback function is actually a pointer to a // the callback function is actually a pointer to a
// `VstTimeInfo` struct! It can also be a null pointer if // `VstTimeInfo` struct! It can also be a null pointer if the
// the host doesn't support this. // host doesn't support this.
const auto time_info = const auto time_info =
reinterpret_cast<const VstTimeInfo*>(return_value); reinterpret_cast<const VstTimeInfo*>(return_value);
if (time_info == nullptr) { if (time_info == nullptr) {
@@ -344,10 +347,23 @@ auto passthrough_event(AEffect* plugin, F callback) {
}, },
[&](WantsString&) -> EventResposnePayload { [&](WantsString&) -> EventResposnePayload {
return std::string(static_cast<char*>(data)); return std::string(static_cast<char*>(data));
}}, }};
event.payload);
EventResult response{return_value, response_data}; // As mentioned about, the `effSetSpeakerArrangement` and
// `effGetSpeakerArrangement` events are the only two events that use
// the value argument as a pointer to write data to. Additionally, the
// `effGetSpeakerArrangement` expects the plugin to write its own data
// to this value. Hence why we need to encode the response here
// separately.
const EventResposnePayload response_data =
std::visit(write_payload_fn, event.payload);
std::optional<EventResposnePayload> value_response_data = std::nullopt;
if (event.value_payload.has_value()) {
value_response_data =
std::visit(write_payload_fn, event.value_payload.value());
}
EventResult response{return_value, response_data, value_response_data};
return response; return response;
}; };
+7 -1
View File
@@ -161,6 +161,7 @@ void Logger::log_event(bool is_dispatch,
message << "(index = " << index << ", value = " << value message << "(index = " << index << ", value = " << value
<< ", option = " << option << ", data = "; << ", option = " << option << ", data = ";
// TODO: Print value payload
std::visit( std::visit(
overload{ overload{
[&](const std::nullptr_t&) { message << "<nullptr>"; }, [&](const std::nullptr_t&) { message << "<nullptr>"; },
@@ -185,7 +186,7 @@ void Logger::log_event(bool is_dispatch,
}, },
[&](const DynamicSpeakerArrangement& speaker_arrangement) { [&](const DynamicSpeakerArrangement& speaker_arrangement) {
message << "<" << speaker_arrangement.speakers.size() message << "<" << speaker_arrangement.speakers.size()
<< " speakers>"; << " output_speakers>";
}, },
[&](const WantsChunkBuffer&) { [&](const WantsChunkBuffer&) {
message << "<writable_buffer>"; message << "<writable_buffer>";
@@ -224,6 +225,7 @@ void Logger::log_event_response(bool is_dispatch,
message << return_value; message << return_value;
// TODO: Print value payload
std::visit( std::visit(
overload{ overload{
[&](const std::nullptr_t&) {}, [&](const std::nullptr_t&) {},
@@ -240,6 +242,10 @@ void Logger::log_event_response(bool is_dispatch,
message << ", <" << buffer.size() << " byte chunk>"; message << ", <" << buffer.size() << " byte chunk>";
}, },
[&](const AEffect&) { message << ", <AEffect_object>"; }, [&](const AEffect&) { message << ", <AEffect_object>"; },
[&](const DynamicSpeakerArrangement& speaker_arrangement) {
message << ", <" << speaker_arrangement.speakers.size()
<< " output_speakers>";
},
[&](const VstIOProperties&) { message << ", <io_properties>"; }, [&](const VstIOProperties&) { message << ", <io_properties>"; },
[&](const VstMidiKeyName&) { message << ", <key_name>"; }, [&](const VstMidiKeyName&) { message << ", <key_name>"; },
[&](const VstParameterProperties& props) { [&](const VstParameterProperties& props) {
+13
View File
@@ -426,6 +426,7 @@ using EventResposnePayload = std::variant<std::nullptr_t,
std::string, std::string,
std::vector<uint8_t>, std::vector<uint8_t>,
AEffect, AEffect,
DynamicSpeakerArrangement,
VstIOProperties, VstIOProperties,
VstMidiKeyName, VstMidiKeyName,
VstParameterProperties, VstParameterProperties,
@@ -444,6 +445,9 @@ void serialize(S& s, EventResposnePayload& payload) {
s.container1b(buffer, binary_buffer_size); s.container1b(buffer, binary_buffer_size);
}, },
[](S& s, AEffect& effect) { s.object(effect); }, [](S& s, AEffect& effect) { s.object(effect); },
[&](DynamicSpeakerArrangement& speaker_arrangement) -> void* {
return &speaker_arrangement.as_c_speaker_arrangement();
},
[](S& s, VstIOProperties& props) { s.object(props); }, [](S& s, VstIOProperties& props) { s.object(props); },
[](S& s, VstMidiKeyName& key_name) { s.object(key_name); }, [](S& s, VstMidiKeyName& key_name) { s.object(key_name); },
[](S& s, VstParameterProperties& props) { s.object(props); }, [](S& s, VstParameterProperties& props) { s.object(props); },
@@ -464,13 +468,22 @@ struct EventResult {
* into the void pointer, but sometimes an event response should forward * into the void pointer, but sometimes an event response should forward
* some kind of special struct. * some kind of special struct.
*/ */
// TODO: Fix typo and rename to `EventResultPayload` for consistency
EventResposnePayload payload; EventResposnePayload payload;
/**
* The same as the above value, but for returning values written to the
* `intptr_t` value parameter. This is only used during
* `effGetSpeakerArrangement`.
*/
std::optional<EventResposnePayload> value_payload;
template <typename S> template <typename S>
void serialize(S& s) { void serialize(S& s) {
s.value8b(return_value); s.value8b(return_value);
s.object(payload); s.object(payload);
s.ext(value_payload, bitsery::ext::StdOptional(),
[](S& s, auto& v) { s.object(v); });
} }
}; };
+1 -1
View File
@@ -289,7 +289,7 @@ PluginBridge::PluginBridge(audioMasterCallback host_callback)
incoming_midi_events.push_back( incoming_midi_events.push_back(
std::get<DynamicVstEvents>(event.payload)); std::get<DynamicVstEvents>(event.payload));
EventResult response{1, nullptr}; EventResult response{1, nullptr, std::nullopt};
return response; return response;
} else { } else {
+1 -1
View File
@@ -206,7 +206,7 @@ void WineBridge::handle_dispatch() {
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);
EventResult response{return_value, nullptr}; EventResult response{return_value, nullptr, std::nullopt};
return response; return response;
} else { } else {