Skip to content

Commit

Permalink
- fix global cables not working in compiled plugin
Browse files Browse the repository at this point in the history
- added Tools -> Create C++ code for global cables function
  • Loading branch information
christoph-hart committed Oct 5, 2024
1 parent 0d747c2 commit 8a0d61f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 9 deletions.
58 changes: 58 additions & 0 deletions hi_backend/backend/BackendApplicationCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ void BackendCommandTarget::getAllCommands(Array<CommandID>& commands)
MenuToolsSimulateChangingBufferSize,
MenuToolsShowDspNetworkDllInfo,
MenuToolsCreateRnboTemplate,
MenuToolsCreateGlobalCableCppCode,
MenuViewResetLookAndFeel,
MenuViewReset,
MenuViewRotate,
Expand Down Expand Up @@ -553,6 +554,10 @@ void BackendCommandTarget::getCommandInfo(CommandID commandID, ApplicationComman
setCommandTarget(result, "Create RSA Key pair", true, false, 'X', false);
result.categoryName = "Tools";
break;
case MenuToolsCreateGlobalCableCppCode:
setCommandTarget(result, "Create C++ code for global cables", true, false, 'X', false);
result.categoryName = "Tools";
break;
case MenuToolsConvertSVGToPathData:
setCommandTarget(result, "Show SVG to Path Converter", true, false, 'X', false);
result.categoryName = "Tools";
Expand Down Expand Up @@ -739,6 +744,7 @@ bool BackendCommandTarget::perform(const InvocationInfo &info)
case MenuExportProjectAsExpansion: Actions::exportHiseProject(bpe); return true;
case MenuExportSampleDataForInstaller: Actions::exportSampleDataForInstaller(bpe); return true;
case MenuToolsWavetablesToMonolith: Actions::exportWavetablesToMonolith(bpe); return true;
case MenuToolsCreateGlobalCableCppCode: Actions::createGlobalCableCppCode(bpe); return true;
case MenuExportCompileFilesInPool: Actions::exportCompileFilesInPool(bpe); return true;
case MenuViewResetLookAndFeel: Actions::resetLookAndFeel(bpe); return true;
case MenuViewClearConsole: owner->getConsoleHandler().clearConsole(); return true;
Expand Down Expand Up @@ -1052,6 +1058,7 @@ PopupMenu BackendCommandTarget::getMenuForIndex(int topLevelMenuIndex, const Str
ADD_MENU_ITEM(MenuToolsSimulateChangingBufferSize);
ADD_MENU_ITEM(MenuToolsCreateRnboTemplate);
ADD_MENU_ITEM(MenuToolsCreateThirdPartyNode);
ADD_MENU_ITEM(MenuToolsCreateGlobalCableCppCode);
p.addSeparator();
p.addSectionHeader("License Management");
ADD_MENU_ITEM(MenuToolsCreateRSAKeys);
Expand Down Expand Up @@ -3281,6 +3288,57 @@ void BackendCommandTarget::Actions::cleanDspNetworkFiles(BackendRootWindow* bpe)
np->setModalBaseWindowComponent(bpe);
}

void BackendCommandTarget::Actions::createGlobalCableCppCode(BackendRootWindow* bpe)
{
auto rm = dynamic_cast<scriptnode::routing::GlobalRoutingManager*>(bpe->getBackendProcessor()->getGlobalRoutingManager());

auto cableList = rm->getIdList(scriptnode::routing::GlobalRoutingManager::SlotBase::SlotType::Cable);

if(cableList.isEmpty())
{
PresetHandler::showYesNoWindow("No global cables present", "You need to add global cables before using this method", PresetHandler::IconType::Error);
return;
}

String code;

code << "// Use this enum to refer to the cables, eg. this->setGlobalCableValue<GlobalCables::" << cppgen::Helpers::getValidCppVariableName(cableList[0]) << ">(0.4)\n";
code << "enum class GlobalCables\n{\n";

for(int i = 0; i < cableList.size(); i++)
{
code << "\t" << cppgen::Helpers::getValidCppVariableName(cableList[i]) << " = " << String(i);

if(i != cableList.size()-1)
code << ',';

code << "\n";
}

code <<"};\n";

code << "// Subclass your node from this\n";
code << "using cable_manager_t = routing::global_cable_cpp_manager<";
String nl(",\n ");

for(int i = 0; i < cableList.size(); i++)
{
code << "SN_GLOBAL_CABLE(" << String(cableList[i].hashCode()) << ")";

if(i != cableList.size()-1)
code << nl;
}

code << ">;\n";

SystemClipboard::copyTextToClipboard(code);

auto chain = bpe->getBackendProcessor()->getMainSynthChain();

debugToConsole(chain, "Copied code to clipboard:");
debugToConsole(chain, code);
}

#undef REPLACE_WILDCARD
#undef REPLACE_WILDCARD_WITH_STRING

Expand Down
3 changes: 3 additions & 0 deletions hi_backend/backend/BackendApplicationCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class BackendCommandTarget: public ApplicationCommandTarget,
MenuToolsSimulateChangingBufferSize,
MenuToolsCreateRnboTemplate,
MenuToolsCreateThirdPartyNode,
MenuToolsCreateGlobalCableCppCode,
// ----------------------------------
// License Management
MenuToolsCreateRSAKeys,
Expand Down Expand Up @@ -403,6 +404,8 @@ class BackendCommandTarget: public ApplicationCommandTarget,
static void exportProject(BackendRootWindow* bpe, int buildOption);

static void cleanDspNetworkFiles(BackendRootWindow* bpe);

static void createGlobalCableCppCode(BackendRootWindow* bpe);
};

private:
Expand Down
3 changes: 0 additions & 3 deletions hi_core/hi_core/MainController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,7 @@ void MainController::loadPresetInternal(const ValueTree& valueTreeToLoad)

getUserPresetHandler().initDefaultPresetManager({});

Processor::Iterator<HardcodedSwappableEffect> rti(synthChain, false);

while(auto m = rti.getNextProcessor())
m->connectRuntimeTargets();
}
catch (String& errorMessage)
{
Expand Down
5 changes: 5 additions & 0 deletions hi_core/hi_dsp/modules/ModulatorSynthChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ void ModulatorSynthChain::compileAllScripts()
sp->getContent()->resetContentProperties();
sp->compileScript();
}

Processor::Iterator<HardcodedSwappableEffect> rti(this, false);

while(auto m = rti.getNextProcessor())
m->connectRuntimeTargets();
}
}

Expand Down
50 changes: 44 additions & 6 deletions hi_dsp_library/dsp_nodes/RoutingNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,12 +728,6 @@ template <typename CableType> struct send: public base
CableType cable;
};

}


namespace routing
{

struct ms_encode: public HiseDspBase
{
SN_NODE_ID("ms_encode");
Expand Down Expand Up @@ -1207,6 +1201,50 @@ template <int N, typename SubType, bool HasSendChannels> struct static_matrix
static constexpr int getNumChannels() { return N; }
};

#ifndef SN_GLOBAL_CABLE
#define SN_GLOBAL_CABLE(hash) routing::global_cable<runtime_target::indexers::fix_hash<hash>, parameter::empty>
#endif

/** A interface class that can be used to send values back to HISE through a global cable.
In order to use this class
- create a type definition from this template class with the hash codes of the
global cable IDs using the SN_GLOBAL_CABLE() macro.
- subclass your node from this the type definition
- call setGlobalCableValue(0.33) from anywhere in your node
Note: it's recommended to use the function in HISE that will create the code snippet
(Tools -> Create C++ code for global cables) which will autogenerate
the relevant code bits from all available global cables
*/
template <typename... Ts> struct global_cable_cpp_manager: private advanced_tuple<Ts...>
{
virtual ~global_cable_cpp_manager()
{
connectToRuntimeTarget(false, {});
}

void connectToRuntimeTarget(bool addConnection, const runtime_target::connection& c)
{
static constexpr int size = sizeof...(Ts);
reset_each(addConnection, c, this->getIndexSequence());
}

template <auto CableIndex> void setGlobalCableValue(double value)
{
this->get<(int)CableIndex>().setValue(value);
}

private:

template <std::size_t ...Ns> void reset_each(bool addConnection, const runtime_target::connection& c, std::index_sequence<Ns...>)
{
using swallow = int[]; (void)swallow { 1, ( std::get<Ns>(this->elements).connectToRuntimeTarget(addConnection, c) , void(), int{})... };
};
};



}


Expand Down

0 comments on commit 8a0d61f

Please sign in to comment.