Skip to content

Commit

Permalink
Context menu for selecting which parameters to randomize
Browse files Browse the repository at this point in the history
  • Loading branch information
RareBreeds committed Nov 21, 2023
1 parent 7759d55 commit ff73f9f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 17 deletions.
50 changes: 39 additions & 11 deletions src/PolygeneModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,24 +223,47 @@ void RareBreeds_Orbits_Polygene::Channel::dataFromJson(json_t *root)
}
}

void RareBreeds_Orbits_Polygene::Channel::onRandomizeWithHistory()
void RareBreeds_Orbits_Polygene::Channel::onRandomizeWithHistory(int randomization_mask)
{
RandomizeChannelAction* action = new RandomizeChannelAction;
action->moduleId = m_module->id;
action->old_state = m_state;
onRandomize();
onRandomize(randomization_mask);
action->new_state = m_state;
APP->history->push(action);
}

void RareBreeds_Orbits_Polygene::Channel::onRandomize()
void RareBreeds_Orbits_Polygene::Channel::onRandomize(int randomization_mask)
{
m_state.length = random::uniform() * rhythm::max_length;
m_state.hits = random::uniform();
m_state.shift = random::uniform() * (rhythm::max_length - 1);
m_state.variation = random::uniform();
m_state.reverse = (random::uniform() < 0.5f);
m_state.invert = (random::uniform() < 0.5f);
if (randomization_mask & (1 << RANDOMIZE_LENGTH))
{
m_state.length = random::uniform() * rhythm::max_length;
}

if (randomization_mask & (1 << RANDOMIZE_HITS))
{
m_state.hits = random::uniform();
}

if (randomization_mask & (1 << RANDOMIZE_SHIFT))
{
m_state.shift = random::uniform() * (rhythm::max_length - 1);
}

if (randomization_mask & (1 << RANDOMIZE_VARIATION))
{
m_state.variation = random::uniform();
}

if (randomization_mask & (1 << RANDOMIZE_REVERSE))
{
m_state.reverse = (random::uniform() < 0.5f);
}

if (randomization_mask & (1 << RANDOMIZE_INVERT))
{
m_state.invert = (random::uniform() < 0.5f);
}
}

PolygeneDisplayData RareBreeds_Orbits_Polygene::getDisplayData(void)
Expand Down Expand Up @@ -397,7 +420,7 @@ void RareBreeds_Orbits_Polygene::process(const ProcessArgs &args)
bool rnd = params[RANDOM_KNOB_PARAM].getValue() > 0.5f;
if(m_random_trigger.process(rnd, args.sampleTime))
{
m_active_channel->onRandomizeWithHistory();
m_active_channel->onRandomizeWithHistory(m_randomization_mask);
syncParamsToActiveChannel();
}

Expand Down Expand Up @@ -438,6 +461,8 @@ json_t *RareBreeds_Orbits_Polygene::dataToJson()
json_object_set_new(root, "shift_cv", json_integer(m_input_mode[SHIFT_CV_INPUT]));
json_object_set_new(root, "variation_cv", json_integer(m_input_mode[VARIATION_CV_INPUT]));

json_object_set_new(root, "randomization_mask", json_integer(m_randomization_mask));

json_object_set_new(root, "active_channel_id", json_integer(m_active_channel_id));

json_t *channels = json_array();
Expand Down Expand Up @@ -495,6 +520,9 @@ void RareBreeds_Orbits_Polygene::dataFromJson(json_t *root)
json_load_integer(root, "variation_cv", &mode);
m_input_mode[VARIATION_CV_INPUT] = (InputMode) mode;

m_randomization_mask = RANDOMIZE_ALL;
json_load_integer(root, "randomization_mask", &m_randomization_mask);

json_load_integer(root, "active_channel_id", &m_active_channel_id);
json_t *channels = json_object_get(root, "channels");
if(channels)
Expand Down Expand Up @@ -531,7 +559,7 @@ void RareBreeds_Orbits_Polygene::onRandomize(const RandomizeEvent& e)

for(auto &chan : m_channels)
{
chan.onRandomize();
chan.onRandomize(RANDOMIZE_ALL);
}

// Update the parameters so they reflect the active channels randomized parameters
Expand Down
16 changes: 14 additions & 2 deletions src/PolygeneModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ struct RareBreeds_Orbits_Polygene : Module
{
NUM_LIGHTS
};
enum RandomizationMaskBits
{
RANDOMIZE_LENGTH,
RANDOMIZE_HITS,
RANDOMIZE_SHIFT,
RANDOMIZE_VARIATION,
RANDOMIZE_REVERSE,
RANDOMIZE_INVERT,
RANDOMIZE_COUNT
};
const unsigned int RANDOMIZE_ALL = (1 << RANDOMIZE_COUNT) - 1;

// The channel currently being displayed and controlled by the knobs
int m_active_channel_id = 0;
Expand Down Expand Up @@ -105,8 +116,8 @@ struct RareBreeds_Orbits_Polygene : Module
void process(const ProcessArgs &args);
json_t *dataToJson();
void dataFromJson(json_t *root);
void onRandomizeWithHistory();
void onRandomize();
void onRandomizeWithHistory(int randomization_mask);
void onRandomize(int randomization_mask);
};

int m_active_channels = 1;
Expand All @@ -121,6 +132,7 @@ struct RareBreeds_Orbits_Polygene : Module
BeatMode m_beat;
EOCMode m_eoc;
InputMode m_input_mode[NUM_INPUTS];
int m_randomization_mask = RANDOMIZE_ALL;

RareBreeds_Orbits_Polygene();
virtual ~RareBreeds_Orbits_Polygene();
Expand Down
32 changes: 28 additions & 4 deletions src/PolygeneWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,12 @@ void RareBreeds_Orbits_PolygeneWidget::appendModuleContextMenu(Menu *menu)
beat_widget.appendContextMenu(menu);
eoc_widget.appendContextMenu(menu);

menu->addChild(createSubmenuItem("Input Behavior", "",
RareBreeds_Orbits_Polygene *polygene = static_cast<RareBreeds_Orbits_Polygene *>(module);
menu->addChild(createSubmenuItem("CV Input Behavior", "",
[=](Menu* menu) {
InputMode *input_mode_array = static_cast<RareBreeds_Orbits_Polygene *>(module)->m_input_mode;
menu->addChild(createMenuLabel("Disable mono input channel copying"));

InputMode *input_mode_array = polygene->m_input_mode;
std::vector<std::pair<std::string, size_t>> items = {
std::make_pair("Sync", RareBreeds_Orbits_Polygene::SYNC_INPUT),
std::make_pair("Length", RareBreeds_Orbits_Polygene::LENGTH_CV_INPUT),
Expand All @@ -229,8 +232,6 @@ void RareBreeds_Orbits_PolygeneWidget::appendModuleContextMenu(Menu *menu)
std::make_pair("Variation", RareBreeds_Orbits_Polygene::VARIATION_CV_INPUT)
};

menu->addChild(createMenuLabel("Disable mono input channel copying"));

for(auto i : items)
{
menu->addChild(createCheckMenuItem(i.first, "",
Expand All @@ -240,6 +241,29 @@ void RareBreeds_Orbits_PolygeneWidget::appendModuleContextMenu(Menu *menu)
}
}
));

menu->addChild(createSubmenuItem("Randomize Button", "",
[=](Menu* menu) {
menu->addChild(createMenuLabel("Active channel parameters to randomize"));

std::vector<std::pair<std::string, size_t>> items = {
std::make_pair("Length", RareBreeds_Orbits_Polygene::RANDOMIZE_LENGTH),
std::make_pair("Hits", RareBreeds_Orbits_Polygene::RANDOMIZE_HITS),
std::make_pair("Shift", RareBreeds_Orbits_Polygene::RANDOMIZE_SHIFT),
std::make_pair("Variation", RareBreeds_Orbits_Polygene::RANDOMIZE_VARIATION),
std::make_pair("Reverse", RareBreeds_Orbits_Polygene::RANDOMIZE_REVERSE),
std::make_pair("Invert", RareBreeds_Orbits_Polygene::RANDOMIZE_INVERT)
};

for(auto i : items)
{
menu->addChild(createCheckMenuItem(i.first, "",
[=]() {return polygene->m_randomization_mask & (1 << i.second);},
[=]() {polygene->m_randomization_mask ^= (1 << i.second);}
));
}
}
));
}

void RareBreeds_Orbits_PolygeneWidget::draw(const DrawArgs& args)
Expand Down

0 comments on commit ff73f9f

Please sign in to comment.